Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] fix transformRequest #17

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions example/examples.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,9 @@
{
"title": "Display a popup on hover",
"folder": "popup_on_hover"
},
{
"title": "Use transform request",
"folder": "transform_request"
}
]
27 changes: 27 additions & 0 deletions example/transform_request/index.dart
Original file line number Diff line number Diff line change
@@ -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;
},
),
);
}
36 changes: 36 additions & 0 deletions example/transform_request/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<!doctype html>
<html>

<head>
<title>Use transform request</title>
<script src='https://api.tiles.mapbox.com/mapbox-gl-js/v2.7.1/mapbox-gl.js'></script>
<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v2.7.1/mapbox-gl.css' rel='stylesheet' />
<script defer type="application/javascript" src="index.dart.js"></script>
<style type="text/css">
body {
margin: 20px;
}

#map {
height: 450px;
}

div.wrapper {
margin: 0 auto;
max-width: 800px;
text-align: center;
font-style: italic;
}
</style>
</head>

<body>
<div>
<div class="wrapper">
<div id='map'></div>
<p>Example of the <a href="https://github.com/andrea689/mapbox-gl-dart">mapbox-gl-dart</a></p>
</div>
</div>
</body>

</html>
21 changes: 9 additions & 12 deletions lib/src/interop/ui/map_interop.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -1198,7 +1197,7 @@ class MapOptionsJsImpl {
bool? renderWorldCopies,
num? maxTileCacheSize,
String? localIdeographFontFamily,
RequestTransformFunctionJsImpl? transformRequest,
dynamic transformRequest,
bool? collectResourceTiming,
num? fadeDuration,
bool? crossSourceCollisions,
Expand All @@ -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,
Expand Down
26 changes: 18 additions & 8 deletions lib/src/ui/map.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -1220,8 +1221,10 @@ class MapOptions extends JsObjectWrapper<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.
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;
Expand Down Expand Up @@ -1275,7 +1278,7 @@ class MapOptions extends JsObjectWrapper<MapOptionsJsImpl> {
bool? renderWorldCopies,
num? maxTileCacheSize,
String? localIdeographFontFamily,
RequestTransformFunctionJsImpl? transformRequest, //TODO: Remove JsImpl
RequestTransformFunction? transformRequest,
bool? collectResourceTiming,
num? fadeDuration,
bool? crossSourceCollisions,
Expand Down Expand Up @@ -1319,7 +1322,8 @@ class MapOptions extends JsObjectWrapper<MapOptionsJsImpl> {
renderWorldCopies: renderWorldCopies,
maxTileCacheSize: maxTileCacheSize,
localIdeographFontFamily: localIdeographFontFamily,
transformRequest: transformRequest,
transformRequest:
transformRequest != null ? allowInterop(transformRequest) : null,
collectResourceTiming: collectResourceTiming,
fadeDuration: fadeDuration,
crossSourceCollisions: crossSourceCollisions,
Expand All @@ -1332,17 +1336,23 @@ class MapOptions extends JsObjectWrapper<MapOptionsJsImpl> {
: super.fromJsObject(jsObject);
}

typedef RequestTransformFunction = RequestParameters? Function(
String url, String resourceType);

class RequestParameters extends JsObjectWrapper<RequestParametersJsImpl> {
String? get url => jsObject.url;
String? get credentials => jsObject.credentials;
dynamic get headers => jsObject.headers;
String get credentials => jsObject.credentials;
Map<String, dynamic>? 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<String, dynamic>? headers,
String? method,
bool? collectResourceTiming,
}) =>
Expand Down