From e25687924deabd32effdaeb1528464478a102316 Mon Sep 17 00:00:00 2001 From: Andrea Valenzano Date: Fri, 25 Mar 2022 18:42:59 +0100 Subject: [PATCH] try to fix transformRequest --- example/examples.json | 4 ++++ example/transform_request/index.dart | 27 +++++++++++++++++++++ example/transform_request/index.html | 36 ++++++++++++++++++++++++++++ lib/src/interop/ui/map_interop.dart | 21 +++++++--------- lib/src/ui/map.dart | 26 +++++++++++++------- 5 files changed, 94 insertions(+), 20 deletions(-) create mode 100644 example/transform_request/index.dart create mode 100644 example/transform_request/index.html diff --git a/example/examples.json b/example/examples.json index a55d70f..1a4da9b 100644 --- a/example/examples.json +++ b/example/examples.json @@ -18,5 +18,9 @@ { "title": "Display a popup on hover", "folder": "popup_on_hover" + }, + { + "title": "Use transform request", + "folder": "transform_request" } ] \ No newline at end of file diff --git a/example/transform_request/index.dart b/example/transform_request/index.dart new file mode 100644 index 0000000..fd00d3d --- /dev/null +++ b/example/transform_request/index.dart @@ -0,0 +1,27 @@ +import 'package:mapbox_gl_dart/mapbox_gl_dart.dart'; + +void main() { + Mapbox.accessToken = + 'pk.eyJ1IjoiYW5kcmVhNjg5IiwiYSI6ImNrNGlsYjhyZDBuYXoza213aWphOGNjZmoifQ.maw_5NsXejG1DoOeOi6hlQ'; + + MapboxMap( + MapOptions( + container: 'map', + style: 'mapbox://styles/mapbox/dark-v10', + center: LngLat(7.68227, 45.06755), + zoom: 12, + transformRequest: (url, resourceType) { + if (resourceType == 'Source') { + return RequestParameters( + url: url, + headers: {'my-custom-header': 'hello'}, + credentials: 'include', + method: 'GET', + collectResourceTiming: false, + ); + } + return null; + }, + ), + ); +} diff --git a/example/transform_request/index.html b/example/transform_request/index.html new file mode 100644 index 0000000..919633f --- /dev/null +++ b/example/transform_request/index.html @@ -0,0 +1,36 @@ + + + + + Use transform request + + + + + + + +
+
+
+

Example of the mapbox-gl-dart

+
+
+ + + \ No newline at end of file diff --git a/lib/src/interop/ui/map_interop.dart b/lib/src/interop/ui/map_interop.dart index 8260217..7bc9ca4 100644 --- a/lib/src/interop/ui/map_interop.dart +++ b/lib/src/interop/ui/map_interop.dart @@ -1143,8 +1143,7 @@ class MapOptionsJsImpl { /// A callback run before the MapboxMap makes a request for an external URL. The callback can be used to modify the url, set headers, or set the credentials property for cross-origin requests. /// Expected to return an object with a `url` property and optionally `headers` and `credentials` properties. - external RequestTransformFunctionJsImpl - get transformRequest; //TODO: Remove JsImpl + external dynamic get transformRequest; /// If `true`, Resource Timing API information will be collected for requests made by GeoJSON and Vector Tile web workers (this information is normally inaccessible from the main Javascript thread). Information will be returned in a `resourceTiming` property of relevant `data` events. external bool get collectResourceTiming; @@ -1198,7 +1197,7 @@ class MapOptionsJsImpl { bool? renderWorldCopies, num? maxTileCacheSize, String? localIdeographFontFamily, - RequestTransformFunctionJsImpl? transformRequest, + dynamic transformRequest, bool? collectResourceTiming, num? fadeDuration, bool? crossSourceCollisions, @@ -1207,21 +1206,19 @@ class MapOptionsJsImpl { }); } -typedef RequestTransformFunctionJsImpl = RequestParametersJsImpl Function( - String url, String resourceType); - @JS() @anonymous class RequestParametersJsImpl { - String? url; - String? credentials; - dynamic headers; - String? method; - bool? collectResourceTiming; + external String? get url; + external String get credentials; + external dynamic get headers; + external String? get method; + external bool? get collectResourceTiming; + external set collectResourceTiming(bool? collectResourceTiming); external factory RequestParametersJsImpl({ String? url, - String? credentials, + String credentials, dynamic headers, String? method, bool? collectResourceTiming, diff --git a/lib/src/ui/map.dart b/lib/src/ui/map.dart index ee9f65b..dc33344 100644 --- a/lib/src/ui/map.dart +++ b/lib/src/ui/map.dart @@ -5,6 +5,7 @@ import 'dart:js'; import 'package:js/js_util.dart'; import 'package:mapbox_gl_dart/mapbox_gl_dart.dart'; import 'package:mapbox_gl_dart/src/interop/interop.dart'; +import 'package:mapbox_gl_dart/src/utils.dart'; /// The `MapboxMap` object represents the map on your page. It exposes methods /// and properties that enable you to programmatically change the map, @@ -1220,8 +1221,10 @@ class MapOptions extends JsObjectWrapper { /// A callback run before the MapboxMap makes a request for an external URL. The callback can be used to modify the url, set headers, or set the credentials property for cross-origin requests. /// Expected to return an object with a `url` property and optionally `headers` and `credentials` properties. - RequestTransformFunctionJsImpl get transformRequest => - jsObject.transformRequest; //TODO: Remove JsImpl + RequestTransformFunction? get transformRequest => + jsObject.transformRequest != null + ? allowInterop(jsObject.transformRequest) + : null; /// If `true`, Resource Timing API information will be collected for requests made by GeoJSON and Vector Tile web workers (this information is normally inaccessible from the main Javascript thread). Information will be returned in a `resourceTiming` property of relevant `data` events. bool get collectResourceTiming => jsObject.collectResourceTiming; @@ -1275,7 +1278,7 @@ class MapOptions extends JsObjectWrapper { bool? renderWorldCopies, num? maxTileCacheSize, String? localIdeographFontFamily, - RequestTransformFunctionJsImpl? transformRequest, //TODO: Remove JsImpl + RequestTransformFunction? transformRequest, bool? collectResourceTiming, num? fadeDuration, bool? crossSourceCollisions, @@ -1319,7 +1322,8 @@ class MapOptions extends JsObjectWrapper { renderWorldCopies: renderWorldCopies, maxTileCacheSize: maxTileCacheSize, localIdeographFontFamily: localIdeographFontFamily, - transformRequest: transformRequest, + transformRequest: + transformRequest != null ? allowInterop(transformRequest) : null, collectResourceTiming: collectResourceTiming, fadeDuration: fadeDuration, crossSourceCollisions: crossSourceCollisions, @@ -1332,17 +1336,23 @@ class MapOptions extends JsObjectWrapper { : super.fromJsObject(jsObject); } +typedef RequestTransformFunction = RequestParameters? Function( + String url, String resourceType); + class RequestParameters extends JsObjectWrapper { String? get url => jsObject.url; - String? get credentials => jsObject.credentials; - dynamic get headers => jsObject.headers; + String get credentials => jsObject.credentials; + Map? get headers => dartifyMap(jsObject.headers); String? get method => jsObject.method; bool? get collectResourceTiming => jsObject.collectResourceTiming; + set collectResourceTiming(bool? collectResourceTiming) { + jsObject.collectResourceTiming = collectResourceTiming; + } factory RequestParameters({ String? url, - String? credentials, - dynamic headers, + required String credentials, + Map? headers, String? method, bool? collectResourceTiming, }) =>