@webview-bridge/web 1.0.4 → 1.0.6
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/LICENSE +21 -0
- package/dist/commonjs/index.cjs +51 -22
- package/dist/module/index.mjs +50 -22
- package/dist/typescript/packages/web/src/error.d.ts +3 -0
- package/dist/typescript/packages/web/src/types/index.d.ts +1 -0
- package/dist/typescript/shared/util/src/createEvents.d.ts +1 -1
- package/package.json +2 -2
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Sungyu Kang
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/commonjs/index.cjs
CHANGED
|
@@ -21,6 +21,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
21
21
|
var src_exports = {};
|
|
22
22
|
__export(src_exports, {
|
|
23
23
|
MethodNotFoundError: () => MethodNotFoundError,
|
|
24
|
+
NativeMethodError: () => NativeMethodError,
|
|
24
25
|
linkNativeMethod: () => linkNativeMethod,
|
|
25
26
|
registerWebMethod: () => registerWebMethod
|
|
26
27
|
});
|
|
@@ -33,6 +34,12 @@ var MethodNotFoundError = class extends Error {
|
|
|
33
34
|
this.name = "MethodNotFoundError";
|
|
34
35
|
}
|
|
35
36
|
};
|
|
37
|
+
var NativeMethodError = class extends Error {
|
|
38
|
+
constructor(methodName) {
|
|
39
|
+
super(`An error occurred in the native bridge: ${methodName}`);
|
|
40
|
+
this.name = "NativeMethodError";
|
|
41
|
+
}
|
|
42
|
+
};
|
|
36
43
|
|
|
37
44
|
// ../../shared/util/src/createEvents.ts
|
|
38
45
|
var createEvents = () => ({
|
|
@@ -50,12 +57,23 @@ var createEvents = () => ({
|
|
|
50
57
|
};
|
|
51
58
|
}
|
|
52
59
|
});
|
|
53
|
-
var createResolver = (emitter2, method, eventId, evaluate) => {
|
|
54
|
-
return new Promise((resolve) => {
|
|
55
|
-
const unbind = emitter2.on(
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
60
|
+
var createResolver = (emitter2, method, eventId, evaluate, failHandler = false) => {
|
|
61
|
+
return new Promise((resolve, reject) => {
|
|
62
|
+
const unbind = emitter2.on(
|
|
63
|
+
`${method}-${eventId}`,
|
|
64
|
+
(data, throwOccurred) => {
|
|
65
|
+
unbind();
|
|
66
|
+
if (throwOccurred) {
|
|
67
|
+
if (failHandler instanceof Error) {
|
|
68
|
+
reject(failHandler);
|
|
69
|
+
} else {
|
|
70
|
+
resolve(void 0);
|
|
71
|
+
}
|
|
72
|
+
} else {
|
|
73
|
+
resolve(data);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
);
|
|
59
77
|
evaluate();
|
|
60
78
|
});
|
|
61
79
|
};
|
|
@@ -98,6 +116,9 @@ var linkNativeMethod = (options = {
|
|
|
98
116
|
if (!window.nativeEmitter) {
|
|
99
117
|
window.nativeEmitter = emitter;
|
|
100
118
|
}
|
|
119
|
+
const isMethodAvailable = (methodName) => {
|
|
120
|
+
return throwOnError === true || Array.isArray(throwOnError) && throwOnError.includes(methodName);
|
|
121
|
+
};
|
|
101
122
|
const target = bridgeMethods.reduce(
|
|
102
123
|
(acc, method) => {
|
|
103
124
|
return {
|
|
@@ -105,25 +126,34 @@ var linkNativeMethod = (options = {
|
|
|
105
126
|
[method]: (...args) => {
|
|
106
127
|
const eventId = createRandomId();
|
|
107
128
|
return Promise.race([
|
|
108
|
-
createResolver(
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
129
|
+
createResolver(
|
|
130
|
+
emitter,
|
|
131
|
+
method,
|
|
132
|
+
eventId,
|
|
133
|
+
() => {
|
|
134
|
+
window.ReactNativeWebView?.postMessage(
|
|
135
|
+
JSON.stringify({
|
|
136
|
+
type: "bridge",
|
|
137
|
+
body: {
|
|
138
|
+
method,
|
|
139
|
+
eventId,
|
|
140
|
+
args
|
|
141
|
+
}
|
|
142
|
+
})
|
|
143
|
+
);
|
|
144
|
+
},
|
|
145
|
+
isMethodAvailable(method) && new NativeMethodError(method)
|
|
146
|
+
),
|
|
120
147
|
timeout(timeoutMs)
|
|
121
148
|
]);
|
|
122
149
|
}
|
|
123
150
|
};
|
|
124
151
|
},
|
|
125
152
|
{
|
|
126
|
-
isWebViewBridgeAvailable: Boolean(window.ReactNativeWebView) && bridgeMethods.length > 0
|
|
153
|
+
isWebViewBridgeAvailable: Boolean(window.ReactNativeWebView) && bridgeMethods.length > 0,
|
|
154
|
+
isNativeMethodAvailable(method) {
|
|
155
|
+
return typeof method === "string" && Boolean(window.ReactNativeWebView) && bridgeMethods.includes(method);
|
|
156
|
+
}
|
|
127
157
|
}
|
|
128
158
|
);
|
|
129
159
|
return new Proxy(target, {
|
|
@@ -140,9 +170,7 @@ var linkNativeMethod = (options = {
|
|
|
140
170
|
})
|
|
141
171
|
);
|
|
142
172
|
onFallback?.(method);
|
|
143
|
-
if (
|
|
144
|
-
return () => Promise.reject(new MethodNotFoundError(method));
|
|
145
|
-
} else if (Array.isArray(throwOnError) && throwOnError.includes(method)) {
|
|
173
|
+
if (isMethodAvailable(method)) {
|
|
146
174
|
return () => Promise.reject(new MethodNotFoundError(method));
|
|
147
175
|
} else {
|
|
148
176
|
console.warn(
|
|
@@ -195,6 +223,7 @@ var registerWebMethod = (bridge) => {
|
|
|
195
223
|
// Annotate the CommonJS export names for ESM import in node:
|
|
196
224
|
0 && (module.exports = {
|
|
197
225
|
MethodNotFoundError,
|
|
226
|
+
NativeMethodError,
|
|
198
227
|
linkNativeMethod,
|
|
199
228
|
registerWebMethod
|
|
200
229
|
});
|
package/dist/module/index.mjs
CHANGED
|
@@ -5,6 +5,12 @@ var MethodNotFoundError = class extends Error {
|
|
|
5
5
|
this.name = "MethodNotFoundError";
|
|
6
6
|
}
|
|
7
7
|
};
|
|
8
|
+
var NativeMethodError = class extends Error {
|
|
9
|
+
constructor(methodName) {
|
|
10
|
+
super(`An error occurred in the native bridge: ${methodName}`);
|
|
11
|
+
this.name = "NativeMethodError";
|
|
12
|
+
}
|
|
13
|
+
};
|
|
8
14
|
|
|
9
15
|
// ../../shared/util/src/createEvents.ts
|
|
10
16
|
var createEvents = () => ({
|
|
@@ -22,12 +28,23 @@ var createEvents = () => ({
|
|
|
22
28
|
};
|
|
23
29
|
}
|
|
24
30
|
});
|
|
25
|
-
var createResolver = (emitter2, method, eventId, evaluate) => {
|
|
26
|
-
return new Promise((resolve) => {
|
|
27
|
-
const unbind = emitter2.on(
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
+
var createResolver = (emitter2, method, eventId, evaluate, failHandler = false) => {
|
|
32
|
+
return new Promise((resolve, reject) => {
|
|
33
|
+
const unbind = emitter2.on(
|
|
34
|
+
`${method}-${eventId}`,
|
|
35
|
+
(data, throwOccurred) => {
|
|
36
|
+
unbind();
|
|
37
|
+
if (throwOccurred) {
|
|
38
|
+
if (failHandler instanceof Error) {
|
|
39
|
+
reject(failHandler);
|
|
40
|
+
} else {
|
|
41
|
+
resolve(void 0);
|
|
42
|
+
}
|
|
43
|
+
} else {
|
|
44
|
+
resolve(data);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
);
|
|
31
48
|
evaluate();
|
|
32
49
|
});
|
|
33
50
|
};
|
|
@@ -70,6 +87,9 @@ var linkNativeMethod = (options = {
|
|
|
70
87
|
if (!window.nativeEmitter) {
|
|
71
88
|
window.nativeEmitter = emitter;
|
|
72
89
|
}
|
|
90
|
+
const isMethodAvailable = (methodName) => {
|
|
91
|
+
return throwOnError === true || Array.isArray(throwOnError) && throwOnError.includes(methodName);
|
|
92
|
+
};
|
|
73
93
|
const target = bridgeMethods.reduce(
|
|
74
94
|
(acc, method) => {
|
|
75
95
|
return {
|
|
@@ -77,25 +97,34 @@ var linkNativeMethod = (options = {
|
|
|
77
97
|
[method]: (...args) => {
|
|
78
98
|
const eventId = createRandomId();
|
|
79
99
|
return Promise.race([
|
|
80
|
-
createResolver(
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
100
|
+
createResolver(
|
|
101
|
+
emitter,
|
|
102
|
+
method,
|
|
103
|
+
eventId,
|
|
104
|
+
() => {
|
|
105
|
+
window.ReactNativeWebView?.postMessage(
|
|
106
|
+
JSON.stringify({
|
|
107
|
+
type: "bridge",
|
|
108
|
+
body: {
|
|
109
|
+
method,
|
|
110
|
+
eventId,
|
|
111
|
+
args
|
|
112
|
+
}
|
|
113
|
+
})
|
|
114
|
+
);
|
|
115
|
+
},
|
|
116
|
+
isMethodAvailable(method) && new NativeMethodError(method)
|
|
117
|
+
),
|
|
92
118
|
timeout(timeoutMs)
|
|
93
119
|
]);
|
|
94
120
|
}
|
|
95
121
|
};
|
|
96
122
|
},
|
|
97
123
|
{
|
|
98
|
-
isWebViewBridgeAvailable: Boolean(window.ReactNativeWebView) && bridgeMethods.length > 0
|
|
124
|
+
isWebViewBridgeAvailable: Boolean(window.ReactNativeWebView) && bridgeMethods.length > 0,
|
|
125
|
+
isNativeMethodAvailable(method) {
|
|
126
|
+
return typeof method === "string" && Boolean(window.ReactNativeWebView) && bridgeMethods.includes(method);
|
|
127
|
+
}
|
|
99
128
|
}
|
|
100
129
|
);
|
|
101
130
|
return new Proxy(target, {
|
|
@@ -112,9 +141,7 @@ var linkNativeMethod = (options = {
|
|
|
112
141
|
})
|
|
113
142
|
);
|
|
114
143
|
onFallback?.(method);
|
|
115
|
-
if (
|
|
116
|
-
return () => Promise.reject(new MethodNotFoundError(method));
|
|
117
|
-
} else if (Array.isArray(throwOnError) && throwOnError.includes(method)) {
|
|
144
|
+
if (isMethodAvailable(method)) {
|
|
118
145
|
return () => Promise.reject(new MethodNotFoundError(method));
|
|
119
146
|
} else {
|
|
120
147
|
console.warn(
|
|
@@ -166,6 +193,7 @@ var registerWebMethod = (bridge) => {
|
|
|
166
193
|
};
|
|
167
194
|
export {
|
|
168
195
|
MethodNotFoundError,
|
|
196
|
+
NativeMethodError,
|
|
169
197
|
linkNativeMethod,
|
|
170
198
|
registerWebMethod
|
|
171
199
|
};
|
|
@@ -12,5 +12,5 @@ export interface EventEmitter<Events extends EventsMap = DefaultEvents> {
|
|
|
12
12
|
on<K extends keyof Events>(this: this, event: K, cb: Events[K]): () => void;
|
|
13
13
|
}
|
|
14
14
|
export declare const createEvents: <Events extends EventsMap = DefaultEvents>() => EventEmitter<Events>;
|
|
15
|
-
export declare const createResolver: (emitter: EventEmitter<DefaultEvents>, method: string, eventId: string, evaluate: () => void) => Promise<unknown>;
|
|
15
|
+
export declare const createResolver: (emitter: EventEmitter<DefaultEvents>, method: string, eventId: string, evaluate: () => void, failHandler?: Error | false) => Promise<unknown>;
|
|
16
16
|
export {};
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webview-bridge/web",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "1.0.
|
|
5
|
-
"description": "Integration
|
|
4
|
+
"version": "1.0.6",
|
|
5
|
+
"description": "Fully Type-Safe Integration for React Native WebView and Web",
|
|
6
6
|
"publishConfig": {
|
|
7
7
|
"access": "public"
|
|
8
8
|
},
|