@maplibre/maplibre-react-native 11.0.0-beta.19 → 11.0.0-beta.20
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/android/src/main/java/org/maplibre/reactnative/MLRNPackage.kt +13 -0
- package/android/src/main/java/org/maplibre/reactnative/modules/MLRNLogModule.kt +2 -0
- package/android/src/main/java/org/maplibre/reactnative/modules/MLRNNetworkModule.kt +0 -42
- package/android/src/main/java/org/maplibre/reactnative/modules/MLRNTransformRequestModule.kt +144 -0
- package/android/src/main/java/org/maplibre/reactnative/modules/TransformRequestInterceptor.kt +184 -0
- package/ios/modules/logging/MLRNLogging.h +4 -0
- package/ios/modules/logging/MLRNLogging.m +8 -0
- package/ios/modules/network/MLRNNetworkModule.mm +0 -10
- package/ios/modules/transform-request/MLRNTransformRequest.h +38 -0
- package/ios/modules/transform-request/MLRNTransformRequest.m +413 -0
- package/ios/modules/transform-request/MLRNTransformRequestModule.h +9 -0
- package/ios/modules/transform-request/MLRNTransformRequestModule.mm +72 -0
- package/lib/commonjs/index.js +7 -0
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/modules/network/NativeNetworkModule.js.map +1 -1
- package/lib/commonjs/modules/network/NetworkManager.js +1 -39
- package/lib/commonjs/modules/network/NetworkManager.js.map +1 -1
- package/lib/commonjs/modules/transform-request/NativeTransformRequestModule.js +9 -0
- package/lib/commonjs/modules/transform-request/NativeTransformRequestModule.js.map +1 -0
- package/lib/commonjs/modules/transform-request/TransformRequestManager.js +223 -0
- package/lib/commonjs/modules/transform-request/TransformRequestManager.js.map +1 -0
- package/lib/module/index.js +1 -0
- package/lib/module/index.js.map +1 -1
- package/lib/module/modules/network/NativeNetworkModule.js.map +1 -1
- package/lib/module/modules/network/NetworkManager.js +1 -39
- package/lib/module/modules/network/NetworkManager.js.map +1 -1
- package/lib/module/modules/transform-request/NativeTransformRequestModule.js +5 -0
- package/lib/module/modules/transform-request/NativeTransformRequestModule.js.map +1 -0
- package/lib/module/modules/transform-request/TransformRequestManager.js +220 -0
- package/lib/module/modules/transform-request/TransformRequestManager.js.map +1 -0
- package/lib/typescript/commonjs/index.d.ts +1 -0
- package/lib/typescript/commonjs/index.d.ts.map +1 -1
- package/lib/typescript/commonjs/modules/network/NativeNetworkModule.d.ts +0 -2
- package/lib/typescript/commonjs/modules/network/NativeNetworkModule.d.ts.map +1 -1
- package/lib/typescript/commonjs/modules/network/NetworkManager.d.ts +1 -33
- package/lib/typescript/commonjs/modules/network/NetworkManager.d.ts.map +1 -1
- package/lib/typescript/commonjs/modules/transform-request/NativeTransformRequestModule.d.ts +15 -0
- package/lib/typescript/commonjs/modules/transform-request/NativeTransformRequestModule.d.ts.map +1 -0
- package/lib/typescript/commonjs/modules/transform-request/TransformRequestManager.d.ts +201 -0
- package/lib/typescript/commonjs/modules/transform-request/TransformRequestManager.d.ts.map +1 -0
- package/lib/typescript/module/index.d.ts +1 -0
- package/lib/typescript/module/index.d.ts.map +1 -1
- package/lib/typescript/module/modules/network/NativeNetworkModule.d.ts +0 -2
- package/lib/typescript/module/modules/network/NativeNetworkModule.d.ts.map +1 -1
- package/lib/typescript/module/modules/network/NetworkManager.d.ts +1 -33
- package/lib/typescript/module/modules/network/NetworkManager.d.ts.map +1 -1
- package/lib/typescript/module/modules/transform-request/NativeTransformRequestModule.d.ts +15 -0
- package/lib/typescript/module/modules/transform-request/NativeTransformRequestModule.d.ts.map +1 -0
- package/lib/typescript/module/modules/transform-request/TransformRequestManager.d.ts +201 -0
- package/lib/typescript/module/modules/transform-request/TransformRequestManager.d.ts.map +1 -0
- package/package.json +1 -12
- package/src/index.ts +8 -0
- package/src/modules/network/NativeNetworkModule.ts +0 -4
- package/src/modules/network/NetworkManager.ts +1 -47
- package/src/modules/transform-request/NativeTransformRequestModule.ts +41 -0
- package/src/modules/transform-request/TransformRequestManager.ts +292 -0
- package/android/src/main/java/org/maplibre/reactnative/http/RequestHeadersInterceptor.kt +0 -65
- package/ios/modules/network/MLRNNetworkHTTPHeaders.h +0 -14
- package/ios/modules/network/MLRNNetworkHTTPHeaders.m +0 -97
|
@@ -28,6 +28,7 @@ import org.maplibre.reactnative.modules.MLRNLogModule
|
|
|
28
28
|
import org.maplibre.reactnative.modules.MLRNNetworkModule
|
|
29
29
|
import org.maplibre.reactnative.modules.MLRNOfflineModule
|
|
30
30
|
import org.maplibre.reactnative.modules.MLRNStaticMapModule
|
|
31
|
+
import org.maplibre.reactnative.modules.MLRNTransformRequestModule
|
|
31
32
|
import org.maplibre.reactnative.utils.ReactTagResolver
|
|
32
33
|
|
|
33
34
|
class MLRNPackage : BaseReactPackage() {
|
|
@@ -65,6 +66,8 @@ class MLRNPackage : BaseReactPackage() {
|
|
|
65
66
|
MLRNLogModule.NAME -> return MLRNLogModule(reactContext)
|
|
66
67
|
|
|
67
68
|
MLRNNetworkModule.NAME -> return MLRNNetworkModule(reactContext)
|
|
69
|
+
|
|
70
|
+
MLRNTransformRequestModule.NAME -> return MLRNTransformRequestModule(reactContext)
|
|
68
71
|
}
|
|
69
72
|
|
|
70
73
|
return null
|
|
@@ -164,6 +167,16 @@ class MLRNPackage : BaseReactPackage() {
|
|
|
164
167
|
isTurboModule = true,
|
|
165
168
|
)
|
|
166
169
|
|
|
170
|
+
moduleInfos[MLRNTransformRequestModule.NAME] =
|
|
171
|
+
ReactModuleInfo(
|
|
172
|
+
MLRNTransformRequestModule.NAME,
|
|
173
|
+
MLRNTransformRequestModule.NAME,
|
|
174
|
+
canOverrideExistingModule = false,
|
|
175
|
+
needsEagerInit = false,
|
|
176
|
+
isCxxModule = false,
|
|
177
|
+
isTurboModule = true,
|
|
178
|
+
)
|
|
179
|
+
|
|
167
180
|
moduleInfos
|
|
168
181
|
}
|
|
169
182
|
|
|
@@ -3,10 +3,12 @@ package org.maplibre.reactnative.modules
|
|
|
3
3
|
import android.util.Log
|
|
4
4
|
import com.facebook.react.bridge.Arguments
|
|
5
5
|
import com.facebook.react.bridge.ReactApplicationContext
|
|
6
|
+
import com.facebook.react.module.annotations.ReactModule
|
|
6
7
|
import org.maplibre.android.log.Logger
|
|
7
8
|
import org.maplibre.android.log.LoggerDefinition
|
|
8
9
|
import org.maplibre.reactnative.NativeLogModuleSpec
|
|
9
10
|
|
|
11
|
+
@ReactModule(name = MLRNLogModule.NAME)
|
|
10
12
|
class MLRNLogModule(
|
|
11
13
|
reactContext: ReactApplicationContext,
|
|
12
14
|
) : NativeLogModuleSpec(
|
|
@@ -1,14 +1,9 @@
|
|
|
1
1
|
package org.maplibre.reactnative.modules
|
|
2
2
|
|
|
3
|
-
import android.util.Log
|
|
4
3
|
import com.facebook.react.bridge.ReactApplicationContext
|
|
5
4
|
import com.facebook.react.module.annotations.ReactModule
|
|
6
|
-
import okhttp3.Dispatcher
|
|
7
|
-
import okhttp3.OkHttpClient
|
|
8
5
|
import org.maplibre.android.MapLibre
|
|
9
|
-
import org.maplibre.android.module.http.HttpRequestUtil
|
|
10
6
|
import org.maplibre.reactnative.NativeNetworkModuleSpec
|
|
11
|
-
import org.maplibre.reactnative.http.RequestHeadersInterceptor
|
|
12
7
|
|
|
13
8
|
@ReactModule(name = MLRNNetworkModule.NAME)
|
|
14
9
|
class MLRNNetworkModule(
|
|
@@ -16,52 +11,15 @@ class MLRNNetworkModule(
|
|
|
16
11
|
) : NativeNetworkModuleSpec(reactContext) {
|
|
17
12
|
companion object {
|
|
18
13
|
const val NAME = "MLRNNetworkModule"
|
|
19
|
-
private var requestHeadersInterceptorAdded = false
|
|
20
14
|
}
|
|
21
15
|
|
|
22
16
|
override fun getName() = NAME
|
|
23
17
|
|
|
24
18
|
private val context: ReactApplicationContext = reactContext
|
|
25
19
|
|
|
26
|
-
override fun addRequestHeader(
|
|
27
|
-
name: String,
|
|
28
|
-
value: String,
|
|
29
|
-
match: String?,
|
|
30
|
-
) {
|
|
31
|
-
context.runOnUiQueueThread {
|
|
32
|
-
if (!requestHeadersInterceptorAdded) {
|
|
33
|
-
Log.i("MLRNNetworkModule", "Add interceptor")
|
|
34
|
-
val httpClient =
|
|
35
|
-
OkHttpClient
|
|
36
|
-
.Builder()
|
|
37
|
-
.addInterceptor(RequestHeadersInterceptor.INSTANCE)
|
|
38
|
-
.dispatcher(getDispatcher())
|
|
39
|
-
.build()
|
|
40
|
-
HttpRequestUtil.setOkHttpClient(httpClient)
|
|
41
|
-
requestHeadersInterceptorAdded = true
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
RequestHeadersInterceptor.INSTANCE.addHeader(name, value, match)
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
override fun removeRequestHeader(name: String) {
|
|
49
|
-
context.runOnUiQueueThread {
|
|
50
|
-
RequestHeadersInterceptor.INSTANCE.removeHeader(name)
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
|
|
54
20
|
override fun setConnected(connected: Boolean) {
|
|
55
21
|
context.runOnUiQueueThread {
|
|
56
22
|
MapLibre.setConnected(connected)
|
|
57
23
|
}
|
|
58
24
|
}
|
|
59
|
-
|
|
60
|
-
private fun getDispatcher(): Dispatcher {
|
|
61
|
-
val dispatcher = Dispatcher()
|
|
62
|
-
// Matches core limit set on
|
|
63
|
-
// https://github.com/mapbox/mapbox-gl-native/blob/master/platform/android/src/http_file_source.cpp#L192
|
|
64
|
-
dispatcher.maxRequestsPerHost = 20
|
|
65
|
-
return dispatcher
|
|
66
|
-
}
|
|
67
25
|
}
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
package org.maplibre.reactnative.modules
|
|
2
|
+
|
|
3
|
+
import android.util.Log
|
|
4
|
+
import com.facebook.react.bridge.ReactApplicationContext
|
|
5
|
+
import com.facebook.react.module.annotations.ReactModule
|
|
6
|
+
import okhttp3.Dispatcher
|
|
7
|
+
import okhttp3.OkHttpClient
|
|
8
|
+
import org.maplibre.android.module.http.HttpRequestUtil
|
|
9
|
+
import org.maplibre.reactnative.NativeTransformRequestModuleSpec
|
|
10
|
+
|
|
11
|
+
@ReactModule(name = MLRNTransformRequestModule.NAME)
|
|
12
|
+
class MLRNTransformRequestModule(
|
|
13
|
+
reactContext: ReactApplicationContext,
|
|
14
|
+
) : NativeTransformRequestModuleSpec(reactContext) {
|
|
15
|
+
companion object {
|
|
16
|
+
const val NAME = "MLRNTransformRequestModule"
|
|
17
|
+
|
|
18
|
+
private var transformRequestInterceptorAdded = false
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
override fun getName() = NAME
|
|
22
|
+
|
|
23
|
+
override fun initialize() {
|
|
24
|
+
reactApplicationContext.getNativeModule(MLRNLogModule::class.java)?.let { logModule ->
|
|
25
|
+
TransformRequestInterceptor.INSTANCE.jsLogger = { level, tag, message ->
|
|
26
|
+
logModule.onLog(level, tag, message)
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
private fun ensureInterceptorAdded() {
|
|
32
|
+
if (!transformRequestInterceptorAdded) {
|
|
33
|
+
Log.i(NAME, "Add interceptor")
|
|
34
|
+
val httpClient =
|
|
35
|
+
OkHttpClient
|
|
36
|
+
.Builder()
|
|
37
|
+
.addInterceptor(TransformRequestInterceptor.INSTANCE)
|
|
38
|
+
.dispatcher(getDispatcher())
|
|
39
|
+
.build()
|
|
40
|
+
HttpRequestUtil.setOkHttpClient(httpClient)
|
|
41
|
+
transformRequestInterceptorAdded = true
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
override fun addUrlTransform(
|
|
46
|
+
id: String,
|
|
47
|
+
match: String?,
|
|
48
|
+
find: String,
|
|
49
|
+
replace: String,
|
|
50
|
+
) {
|
|
51
|
+
requireValidRegex(match, "addUrlTransform", "match")
|
|
52
|
+
requireValidRegex(find, "addUrlTransform", "find")
|
|
53
|
+
reactApplicationContext.runOnUiQueueThread {
|
|
54
|
+
ensureInterceptorAdded()
|
|
55
|
+
TransformRequestInterceptor.INSTANCE.addUrlTransform(id, match, find, replace)
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
override fun removeUrlTransform(id: String) {
|
|
60
|
+
reactApplicationContext.runOnUiQueueThread {
|
|
61
|
+
TransformRequestInterceptor.INSTANCE.removeUrlTransform(id)
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
override fun clearUrlTransforms() {
|
|
66
|
+
reactApplicationContext.runOnUiQueueThread {
|
|
67
|
+
TransformRequestInterceptor.INSTANCE.clearUrlTransforms()
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
override fun addUrlSearchParam(
|
|
72
|
+
id: String,
|
|
73
|
+
match: String?,
|
|
74
|
+
key: String,
|
|
75
|
+
value: String,
|
|
76
|
+
) {
|
|
77
|
+
requireValidRegex(match, "addUrlSearchParam", "match")
|
|
78
|
+
reactApplicationContext.runOnUiQueueThread {
|
|
79
|
+
ensureInterceptorAdded()
|
|
80
|
+
TransformRequestInterceptor.INSTANCE.addUrlSearchParam(id, match, key, value)
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
override fun removeUrlSearchParam(id: String) {
|
|
85
|
+
reactApplicationContext.runOnUiQueueThread {
|
|
86
|
+
TransformRequestInterceptor.INSTANCE.removeUrlSearchParam(id)
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
override fun clearUrlSearchParams() {
|
|
91
|
+
reactApplicationContext.runOnUiQueueThread {
|
|
92
|
+
TransformRequestInterceptor.INSTANCE.clearUrlSearchParams()
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
override fun addHeader(
|
|
97
|
+
id: String,
|
|
98
|
+
match: String?,
|
|
99
|
+
name: String,
|
|
100
|
+
value: String,
|
|
101
|
+
) {
|
|
102
|
+
requireValidRegex(match, "addHeader", "match")
|
|
103
|
+
reactApplicationContext.runOnUiQueueThread {
|
|
104
|
+
ensureInterceptorAdded()
|
|
105
|
+
TransformRequestInterceptor.INSTANCE.addHeader(id, match, name, value)
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
override fun removeHeader(id: String) {
|
|
110
|
+
reactApplicationContext.runOnUiQueueThread {
|
|
111
|
+
TransformRequestInterceptor.INSTANCE.removeHeader(id)
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
override fun clearHeaders() {
|
|
116
|
+
reactApplicationContext.runOnUiQueueThread {
|
|
117
|
+
TransformRequestInterceptor.INSTANCE.clearHeaders()
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
private fun requireValidRegex(
|
|
122
|
+
pattern: String?,
|
|
123
|
+
methodName: String,
|
|
124
|
+
fieldName: String,
|
|
125
|
+
) {
|
|
126
|
+
if (pattern != null) {
|
|
127
|
+
try {
|
|
128
|
+
Regex(pattern)
|
|
129
|
+
} catch (e: Exception) {
|
|
130
|
+
throw IllegalArgumentException(
|
|
131
|
+
"[$NAME] $methodName: invalid $fieldName regex '$pattern': ${e.message}",
|
|
132
|
+
)
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
private fun getDispatcher(): Dispatcher {
|
|
138
|
+
val dispatcher = Dispatcher()
|
|
139
|
+
// Matches core limit set on
|
|
140
|
+
// https://github.com/mapbox/mapbox-gl-native/blob/master/platform/android/src/http_file_source.cpp#L192
|
|
141
|
+
dispatcher.maxRequestsPerHost = 20
|
|
142
|
+
return dispatcher
|
|
143
|
+
}
|
|
144
|
+
}
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
package org.maplibre.reactnative.modules
|
|
2
|
+
|
|
3
|
+
import android.util.Log
|
|
4
|
+
import okhttp3.HttpUrl
|
|
5
|
+
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
|
|
6
|
+
import okhttp3.Interceptor
|
|
7
|
+
import okhttp3.Response
|
|
8
|
+
import java.io.IOException
|
|
9
|
+
import kotlin.collections.iterator
|
|
10
|
+
|
|
11
|
+
data class UrlTransformConfig(
|
|
12
|
+
val matchRegex: Regex?,
|
|
13
|
+
val findRegex: Regex,
|
|
14
|
+
val replace: String,
|
|
15
|
+
)
|
|
16
|
+
|
|
17
|
+
data class UrlSearchParamConfig(
|
|
18
|
+
val matchRegex: Regex?,
|
|
19
|
+
val name: String,
|
|
20
|
+
val value: String,
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
data class HeaderConfig(
|
|
24
|
+
val matchRegex: Regex?,
|
|
25
|
+
val name: String,
|
|
26
|
+
val value: String,
|
|
27
|
+
)
|
|
28
|
+
|
|
29
|
+
class TransformRequestInterceptor : Interceptor {
|
|
30
|
+
private val urlTransforms: LinkedHashMap<String, UrlTransformConfig> = LinkedHashMap()
|
|
31
|
+
private val urlSearchParams: LinkedHashMap<String, UrlSearchParamConfig> = LinkedHashMap()
|
|
32
|
+
private val headers: LinkedHashMap<String, HeaderConfig> = LinkedHashMap()
|
|
33
|
+
|
|
34
|
+
@Volatile
|
|
35
|
+
var jsLogger: ((level: String, tag: String, message: String) -> Unit)? = null
|
|
36
|
+
|
|
37
|
+
private fun debugLog(message: String) {
|
|
38
|
+
if (Log.isLoggable(TAG, Log.DEBUG)) {
|
|
39
|
+
Log.d(TAG, message)
|
|
40
|
+
}
|
|
41
|
+
jsLogger?.invoke("debug", TAG, message)
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
private fun errorLog(message: String) {
|
|
45
|
+
Log.e(TAG, message)
|
|
46
|
+
jsLogger?.invoke("error", TAG, message)
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
fun addUrlTransform(
|
|
50
|
+
id: String,
|
|
51
|
+
match: String?,
|
|
52
|
+
find: String,
|
|
53
|
+
replace: String,
|
|
54
|
+
) {
|
|
55
|
+
urlTransforms[id] = UrlTransformConfig(match?.let { Regex(it) }, Regex(find), replace)
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
fun removeUrlTransform(id: String) {
|
|
59
|
+
urlTransforms.remove(id)
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
fun clearUrlTransforms() {
|
|
63
|
+
urlTransforms.clear()
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
fun addUrlSearchParam(
|
|
67
|
+
id: String,
|
|
68
|
+
match: String?,
|
|
69
|
+
name: String,
|
|
70
|
+
value: String,
|
|
71
|
+
) {
|
|
72
|
+
urlSearchParams[id] = UrlSearchParamConfig(match?.let { Regex(it) }, name, value)
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
fun removeUrlSearchParam(id: String) {
|
|
76
|
+
urlSearchParams.remove(id)
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
fun clearUrlSearchParams() {
|
|
80
|
+
urlSearchParams.clear()
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
fun addHeader(
|
|
84
|
+
id: String,
|
|
85
|
+
match: String?,
|
|
86
|
+
name: String,
|
|
87
|
+
value: String,
|
|
88
|
+
) {
|
|
89
|
+
headers[id] = HeaderConfig(match?.let { Regex(it) }, name, value)
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
fun removeHeader(id: String) {
|
|
93
|
+
headers.remove(id)
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
fun clearHeaders() {
|
|
97
|
+
headers.clear()
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
@Throws(IOException::class)
|
|
101
|
+
override fun intercept(chain: Interceptor.Chain): Response {
|
|
102
|
+
var request = chain.request()
|
|
103
|
+
|
|
104
|
+
val hasConfig = urlTransforms.isNotEmpty() || urlSearchParams.isNotEmpty() || headers.isNotEmpty()
|
|
105
|
+
if (hasConfig) {
|
|
106
|
+
debugLog("Request: ${request.url}")
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// 1. URL transforms
|
|
110
|
+
var currentUrl = request.url.toString()
|
|
111
|
+
for ((transformId, config) in urlTransforms) {
|
|
112
|
+
val urlBeforeTransform = currentUrl
|
|
113
|
+
val shouldApply = config.matchRegex == null || config.matchRegex.containsMatchIn(currentUrl)
|
|
114
|
+
|
|
115
|
+
if (!shouldApply) {
|
|
116
|
+
debugLog(" URL Transform [$transformId]${matchDescription(config.matchRegex)}: SKIPPED (no match)")
|
|
117
|
+
continue
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
val result = config.findRegex.replace(currentUrl, config.replace)
|
|
121
|
+
if (result.toHttpUrlOrNull() == null) {
|
|
122
|
+
errorLog(
|
|
123
|
+
"URL Transform [$transformId]${matchDescription(config.matchRegex)}: produced invalid URL '$result', " +
|
|
124
|
+
"using pre-transform URL",
|
|
125
|
+
)
|
|
126
|
+
} else {
|
|
127
|
+
debugLog(
|
|
128
|
+
" URL Transform [$transformId]${matchDescription(config.matchRegex)}: APPLIED '$urlBeforeTransform' → '$result'",
|
|
129
|
+
)
|
|
130
|
+
currentUrl = result
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// 2. URL search params
|
|
135
|
+
var modifiedUrl: HttpUrl = currentUrl.toHttpUrlOrNull() ?: request.url
|
|
136
|
+
for (entry in urlSearchParams.entries) {
|
|
137
|
+
val config = entry.value
|
|
138
|
+
val shouldApply =
|
|
139
|
+
config.matchRegex == null || config.matchRegex.containsMatchIn(modifiedUrl.toString())
|
|
140
|
+
|
|
141
|
+
if (shouldApply) {
|
|
142
|
+
debugLog(
|
|
143
|
+
" URL Search Param [${entry.key}]${matchDescription(config.matchRegex)}: APPLIED '${config.name}=${config.value}'",
|
|
144
|
+
)
|
|
145
|
+
modifiedUrl =
|
|
146
|
+
modifiedUrl
|
|
147
|
+
.newBuilder()
|
|
148
|
+
.setQueryParameter(config.name, config.value)
|
|
149
|
+
.build()
|
|
150
|
+
} else {
|
|
151
|
+
debugLog(" URL Search Param [${entry.key}]${matchDescription(config.matchRegex)}: SKIPPED (no match)")
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// 3. Headers
|
|
156
|
+
val requestBuilder = request.newBuilder().url(modifiedUrl)
|
|
157
|
+
for (entry in headers.entries) {
|
|
158
|
+
val config = entry.value
|
|
159
|
+
val shouldApply =
|
|
160
|
+
config.matchRegex == null || config.matchRegex.containsMatchIn(modifiedUrl.toString())
|
|
161
|
+
|
|
162
|
+
if (shouldApply) {
|
|
163
|
+
debugLog(" Header [${entry.key}]${matchDescription(config.matchRegex)}: APPLIED '${config.name}: ${config.value}'")
|
|
164
|
+
requestBuilder.header(config.name, config.value)
|
|
165
|
+
} else {
|
|
166
|
+
debugLog(" Header [${entry.key}]${matchDescription(config.matchRegex)}: SKIPPED (no match)")
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
if (hasConfig) {
|
|
171
|
+
debugLog("Final URL: $modifiedUrl")
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
request = requestBuilder.build()
|
|
175
|
+
return chain.proceed(request)
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
companion object {
|
|
179
|
+
val INSTANCE: TransformRequestInterceptor = TransformRequestInterceptor()
|
|
180
|
+
private const val TAG = "TransformRequestInterceptor"
|
|
181
|
+
|
|
182
|
+
private fun matchDescription(matchRegex: Regex?) = if (matchRegex != null) " (match='${matchRegex.pattern}')" else ""
|
|
183
|
+
}
|
|
184
|
+
}
|
|
@@ -66,6 +66,14 @@
|
|
|
66
66
|
}
|
|
67
67
|
}
|
|
68
68
|
|
|
69
|
+
- (void)forwardLog:(nonnull NSString *)level
|
|
70
|
+
tag:(nonnull NSString *)tag
|
|
71
|
+
message:(nonnull NSString *)message {
|
|
72
|
+
if (self.delegate) {
|
|
73
|
+
[self.delegate logging:self didReceiveLogWithLevel:level filePath:tag line:0 message:message];
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
69
77
|
- (void)setLogLevel:(nonnull NSString *)logLevel {
|
|
70
78
|
MLNLoggingLevel mlnLogLevel = MLNLoggingLevelNone;
|
|
71
79
|
if ([logLevel isEqualToString:@"none"]) {
|
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
#import "MLRNNetworkModule.h"
|
|
2
2
|
|
|
3
|
-
#import "MLRNNetworkHTTPHeaders.h"
|
|
4
|
-
|
|
5
3
|
@implementation MLRNNetworkModule
|
|
6
4
|
|
|
7
5
|
+ (NSString *)moduleName {
|
|
@@ -13,14 +11,6 @@
|
|
|
13
11
|
return std::make_shared<facebook::react::NativeNetworkModuleSpecJSI>(params);
|
|
14
12
|
}
|
|
15
13
|
|
|
16
|
-
- (void)addRequestHeader:(NSString *)name value:(NSString *)value match:(NSString *_Nullable)match {
|
|
17
|
-
[MLRNNetworkHTTPHeaders.sharedInstance addRequestHeader:name value:value match:match];
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
- (void)removeRequestHeader:(NSString *)name {
|
|
21
|
-
[MLRNNetworkHTTPHeaders.sharedInstance removeRequestHeader:name];
|
|
22
|
-
}
|
|
23
|
-
|
|
24
14
|
- (void)setConnected:(BOOL)connected {
|
|
25
15
|
// Android only
|
|
26
16
|
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
#import <Foundation/Foundation.h>
|
|
2
|
+
#import <MapLibre/MLNNetworkConfiguration.h>
|
|
3
|
+
|
|
4
|
+
@interface MLRNTransformRequest : NSObject <MLNNetworkConfigurationDelegate>
|
|
5
|
+
|
|
6
|
+
+ (MLRNTransformRequest *_Nonnull)sharedInstance;
|
|
7
|
+
|
|
8
|
+
@property (nonatomic, copy, nullable) void (^logCallback)
|
|
9
|
+
(NSString *_Nonnull level, NSString *_Nonnull tag, NSString *_Nonnull message);
|
|
10
|
+
|
|
11
|
+
- (void)addUrlTransform:(nonnull NSString *)transformId
|
|
12
|
+
match:(nullable NSString *)match
|
|
13
|
+
find:(nonnull NSString *)find
|
|
14
|
+
replace:(nonnull NSString *)replace;
|
|
15
|
+
|
|
16
|
+
- (void)removeUrlTransform:(nonnull NSString *)transformId;
|
|
17
|
+
|
|
18
|
+
- (void)clearUrlTransforms;
|
|
19
|
+
|
|
20
|
+
- (void)addUrlSearchParam:(nonnull NSString *)transformId
|
|
21
|
+
match:(nullable NSString *)match
|
|
22
|
+
name:(nonnull NSString *)name
|
|
23
|
+
value:(nonnull NSString *)value;
|
|
24
|
+
|
|
25
|
+
- (void)removeUrlSearchParam:(nonnull NSString *)transformId;
|
|
26
|
+
|
|
27
|
+
- (void)clearUrlSearchParams;
|
|
28
|
+
|
|
29
|
+
- (void)addHeader:(nonnull NSString *)transformId
|
|
30
|
+
match:(nullable NSString *)match
|
|
31
|
+
name:(nonnull NSString *)name
|
|
32
|
+
value:(nonnull NSString *)value;
|
|
33
|
+
|
|
34
|
+
- (void)removeHeader:(nonnull NSString *)transformId;
|
|
35
|
+
|
|
36
|
+
- (void)clearHeaders;
|
|
37
|
+
|
|
38
|
+
@end
|