@webview-bridge/react-native 1.4.7 → 1.5.0-rc.1
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/dist/packages/react-native/src/createWebView.d.ts +9 -3
- package/dist/packages/react-native/src/createWebView.js +23 -9
- package/dist/packages/react-native/src/index.d.ts +1 -0
- package/dist/packages/react-native/src/index.js +1 -0
- package/dist/packages/react-native/src/integrations/postMessageSchema.d.ts +2 -0
- package/dist/packages/react-native/src/integrations/postMessageSchema.js +56 -0
- package/dist/shared/util/src/equals.js +0 -1
- package/dist/shared/util/src/removeUndefinedKeys.js +0 -1
- package/dist/shared/util/src/types.d.ts +12 -0
- package/package.json +20 -3
- package/dist/packages/react-native/src/integrations/index.d.ts +0 -2
- package/dist/packages/react-native/src/integrations/index.js +0 -18
|
@@ -1,17 +1,23 @@
|
|
|
1
|
-
import type { Bridge, BridgeStore } from "../../../shared/util/src/types";
|
|
1
|
+
import type { Bridge, BridgeStore, KeyOfOrString, Parser, ParserSchema } from "../../../shared/util/src/types";
|
|
2
2
|
import React from "react";
|
|
3
3
|
import type { BridgeWebView } from "./types/webview";
|
|
4
|
-
export type CreateWebViewArgs<BridgeObject extends Bridge
|
|
4
|
+
export type CreateWebViewArgs<BridgeObject extends Bridge, PostMessageSchema extends ParserSchema<any>> = {
|
|
5
5
|
bridge: BridgeStore<BridgeObject>;
|
|
6
6
|
debug?: boolean;
|
|
7
7
|
responseTimeout?: number;
|
|
8
|
+
postMessageSchema?: PostMessageSchema;
|
|
8
9
|
fallback?: (method: keyof BridgeObject) => void;
|
|
9
10
|
};
|
|
10
11
|
export type WebMethod<T> = T & {
|
|
11
12
|
isReady: boolean;
|
|
12
13
|
};
|
|
13
|
-
export declare const createWebView: <BridgeObject extends Bridge
|
|
14
|
+
export declare const createWebView: <BridgeObject extends Bridge, PostMessageSchema extends ParserSchema<any>>({ bridge, debug, responseTimeout, postMessageSchema, fallback, }: CreateWebViewArgs<BridgeObject, PostMessageSchema>) => {
|
|
15
|
+
postMessage: <EventName extends KeyOfOrString<PostMessageSchema>, Args extends Parser<PostMessageSchema, EventName>>(eventName: EventName, args: Args) => void;
|
|
14
16
|
WebView: React.ForwardRefExoticComponent<import("react-native-webview/lib/WebViewTypes").IOSWebViewProps & import("react-native-webview/lib/WebViewTypes").AndroidWebViewProps & import("react-native-webview/lib/WebViewTypes").WindowsWebViewProps & React.RefAttributes<BridgeWebView>>;
|
|
17
|
+
/**
|
|
18
|
+
* @deprecated Use `postMessage` instead. And complete the type through the `postMessageSchema` option.
|
|
19
|
+
* @see https://gronxb.github.io/webview-bridge/postMessage-native-to-web.html
|
|
20
|
+
*/
|
|
15
21
|
linkWebMethod<T>(): {
|
|
16
22
|
current: WebMethod<T>;
|
|
17
23
|
};
|
|
@@ -30,9 +30,10 @@ exports.createWebView = void 0;
|
|
|
30
30
|
const util_1 = require("../../../shared/util/src");
|
|
31
31
|
const react_1 = __importStar(require("react"));
|
|
32
32
|
const react_native_webview_1 = __importDefault(require("react-native-webview"));
|
|
33
|
-
const
|
|
33
|
+
const bridge_1 = require("./integrations/bridge");
|
|
34
|
+
const console_1 = require("./integrations/console");
|
|
34
35
|
const handleRegisterWebMethod_1 = require("./integrations/handleRegisterWebMethod");
|
|
35
|
-
const createWebView = ({ bridge, debug, responseTimeout = 2000, fallback, }) => {
|
|
36
|
+
const createWebView = ({ bridge, debug, responseTimeout = 2000, postMessageSchema, fallback, }) => {
|
|
36
37
|
const WebMethod = {
|
|
37
38
|
current: {
|
|
38
39
|
isReady: false,
|
|
@@ -42,10 +43,19 @@ const createWebView = ({ bridge, debug, responseTimeout = 2000, fallback, }) =>
|
|
|
42
43
|
const emitter = (0, util_1.createEvents)();
|
|
43
44
|
bridge.subscribe((state) => {
|
|
44
45
|
for (const ref of webviewRefList) {
|
|
45
|
-
ref?.current?.injectJavaScript((0,
|
|
46
|
+
ref?.current?.injectJavaScript((0, bridge_1.SAFE_NATIVE_EMITTER_EMIT)("bridgeStateChange", state));
|
|
46
47
|
}
|
|
47
48
|
});
|
|
48
49
|
return {
|
|
50
|
+
postMessage: (eventName, args) => {
|
|
51
|
+
let _args = args;
|
|
52
|
+
if (postMessageSchema) {
|
|
53
|
+
_args = postMessageSchema[eventName].parse(args);
|
|
54
|
+
}
|
|
55
|
+
for (const ref of webviewRefList) {
|
|
56
|
+
ref?.current?.injectJavaScript((0, bridge_1.SAFE_NATIVE_EMITTER_EMIT)(`postMessage/${String(eventName)}`, _args));
|
|
57
|
+
}
|
|
58
|
+
},
|
|
49
59
|
WebView: (0, react_1.forwardRef)((props, ref) => {
|
|
50
60
|
const webviewRef = (0, react_1.useRef)(null);
|
|
51
61
|
(0, react_1.useLayoutEffect)(() => {
|
|
@@ -70,12 +80,12 @@ const createWebView = ({ bridge, debug, responseTimeout = 2000, fallback, }) =>
|
|
|
70
80
|
switch (type) {
|
|
71
81
|
case "log": {
|
|
72
82
|
const { method, args } = body;
|
|
73
|
-
debug && (0,
|
|
83
|
+
debug && (0, console_1.handleLog)(method, args);
|
|
74
84
|
return;
|
|
75
85
|
}
|
|
76
86
|
case "bridge": {
|
|
77
87
|
const { method, args, eventId } = body;
|
|
78
|
-
(0,
|
|
88
|
+
(0, bridge_1.handleBridge)({
|
|
79
89
|
bridge,
|
|
80
90
|
method,
|
|
81
91
|
args,
|
|
@@ -86,7 +96,7 @@ const createWebView = ({ bridge, debug, responseTimeout = 2000, fallback, }) =>
|
|
|
86
96
|
}
|
|
87
97
|
case "getBridgeState": {
|
|
88
98
|
for (const ref of webviewRefList) {
|
|
89
|
-
ref?.current?.injectJavaScript((0,
|
|
99
|
+
ref?.current?.injectJavaScript((0, bridge_1.SAFE_NATIVE_EMITTER_EMIT)("bridgeStateChange", bridge.getState()));
|
|
90
100
|
}
|
|
91
101
|
return;
|
|
92
102
|
}
|
|
@@ -114,20 +124,24 @@ const createWebView = ({ bridge, debug, responseTimeout = 2000, fallback, }) =>
|
|
|
114
124
|
}
|
|
115
125
|
};
|
|
116
126
|
return (<react_native_webview_1.default {...props} ref={webviewRef} onMessage={handleMessage} injectedJavaScriptBeforeContentLoaded={[
|
|
117
|
-
(0,
|
|
118
|
-
(0,
|
|
127
|
+
(0, bridge_1.INJECT_BRIDGE_METHODS)(bridgeNames),
|
|
128
|
+
(0, bridge_1.INJECT_BRIDGE_STATE)(initialState),
|
|
119
129
|
props.injectedJavaScriptBeforeContentLoaded,
|
|
120
130
|
"true;",
|
|
121
131
|
]
|
|
122
132
|
.filter(Boolean)
|
|
123
133
|
.join("\n")} injectedJavaScript={[
|
|
124
|
-
debug &&
|
|
134
|
+
debug && console_1.INJECT_DEBUG,
|
|
125
135
|
props.injectedJavaScript,
|
|
126
136
|
"true;",
|
|
127
137
|
]
|
|
128
138
|
.filter(Boolean)
|
|
129
139
|
.join("\n")}/>);
|
|
130
140
|
}),
|
|
141
|
+
/**
|
|
142
|
+
* @deprecated Use `postMessage` instead. And complete the type through the `postMessageSchema` option.
|
|
143
|
+
* @see https://gronxb.github.io/webview-bridge/postMessage-native-to-web.html
|
|
144
|
+
*/
|
|
131
145
|
linkWebMethod() {
|
|
132
146
|
return WebMethod;
|
|
133
147
|
},
|
|
@@ -16,6 +16,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./createWebView"), exports);
|
|
18
18
|
__exportStar(require("./integrations/bridge"), exports);
|
|
19
|
+
__exportStar(require("./integrations/postMessageSchema"), exports);
|
|
19
20
|
__exportStar(require("./types/webview"), exports);
|
|
20
21
|
__exportStar(require("./useBridge"), exports);
|
|
21
22
|
__exportStar(require("../../../shared/util/src/types"), exports);
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.postMessageSchema = void 0;
|
|
4
|
+
const postMessageSchema = (schema) => {
|
|
5
|
+
const parserSchema = Object.entries(schema).map(([eventName, eventSchema]) => {
|
|
6
|
+
// zod
|
|
7
|
+
if ("parse" in eventSchema && typeof eventSchema.parse === "function") {
|
|
8
|
+
return [
|
|
9
|
+
eventName,
|
|
10
|
+
{
|
|
11
|
+
parse: eventSchema.parse,
|
|
12
|
+
schema: eventSchema,
|
|
13
|
+
},
|
|
14
|
+
];
|
|
15
|
+
}
|
|
16
|
+
// yup
|
|
17
|
+
else if ("validateSync" in eventSchema &&
|
|
18
|
+
typeof eventSchema.validateSync === "function") {
|
|
19
|
+
return [
|
|
20
|
+
eventName,
|
|
21
|
+
{
|
|
22
|
+
parse: (data) => {
|
|
23
|
+
return eventSchema.validateSync(data, { abortEarly: true });
|
|
24
|
+
},
|
|
25
|
+
schema: eventSchema,
|
|
26
|
+
},
|
|
27
|
+
];
|
|
28
|
+
}
|
|
29
|
+
// superstruct
|
|
30
|
+
else if ("assert" in eventSchema &&
|
|
31
|
+
typeof eventSchema.assert === "function" &&
|
|
32
|
+
"validate" in eventSchema &&
|
|
33
|
+
typeof eventSchema.validate === "function") {
|
|
34
|
+
return [
|
|
35
|
+
eventName,
|
|
36
|
+
{
|
|
37
|
+
parse: (data) => {
|
|
38
|
+
void eventSchema.assert(data);
|
|
39
|
+
const [_, result] = eventSchema.validate(data);
|
|
40
|
+
return result;
|
|
41
|
+
},
|
|
42
|
+
schema: eventSchema,
|
|
43
|
+
},
|
|
44
|
+
];
|
|
45
|
+
}
|
|
46
|
+
return [
|
|
47
|
+
eventName,
|
|
48
|
+
{
|
|
49
|
+
parse: (data) => data,
|
|
50
|
+
schema: void 0,
|
|
51
|
+
},
|
|
52
|
+
];
|
|
53
|
+
});
|
|
54
|
+
return Object.fromEntries(parserSchema);
|
|
55
|
+
};
|
|
56
|
+
exports.postMessageSchema = postMessageSchema;
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.removeUndefinedKeys = void 0;
|
|
4
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
5
4
|
const removeUndefinedKeys = (obj) => {
|
|
6
5
|
Object.keys(obj).forEach((key) => {
|
|
7
6
|
if (obj[key] === undefined) {
|
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import type { Infer as SupertructInfer, Struct } from "superstruct";
|
|
2
|
+
import type { AnySchema as YupTypeAny, InferType as yupInfer } from "yup";
|
|
3
|
+
import type { infer as zodInfer, ZodTypeAny } from "zod";
|
|
1
4
|
export type AsyncFunction = (...args: any[]) => Promise<any>;
|
|
2
5
|
export type Primitive = string | number | boolean | null | undefined;
|
|
3
6
|
export type RawJSON = Primitive | {
|
|
@@ -20,4 +23,13 @@ export type OnlyJSON<T> = {
|
|
|
20
23
|
export type ExcludePrimitive<T> = {
|
|
21
24
|
[P in keyof T as T[P] extends RawJSON ? never : P]: T[P];
|
|
22
25
|
};
|
|
26
|
+
export type KeyOfOrString<T> = T extends undefined ? string : keyof T;
|
|
27
|
+
export type PostMessageSchemaObject = Record<string, ZodTypeAny | YupTypeAny | Struct<any>>;
|
|
28
|
+
export type ParserSchema<T = object> = {
|
|
29
|
+
[P in keyof T]: {
|
|
30
|
+
parse: (data: any) => any;
|
|
31
|
+
schema: T[P];
|
|
32
|
+
};
|
|
33
|
+
};
|
|
34
|
+
export type Parser<Input extends ParserSchema<any>, EventName> = Input extends undefined ? Record<string, Primitive> | Primitive : EventName extends keyof Input ? Input[EventName]["schema"] extends ZodTypeAny ? zodInfer<Input[EventName]["schema"]> : Input[EventName]["schema"] extends YupTypeAny ? yupInfer<Input[EventName]["schema"]> : Input[EventName]["schema"] extends Struct<any> ? SupertructInfer<Input[EventName]["schema"]> : Record<string, Primitive> | Primitive : never;
|
|
23
35
|
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webview-bridge/react-native",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.5.0-rc.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Fully Type-Safe Integration for React Native WebView and Web",
|
|
6
6
|
"main": "dist/packages/react-native/src/index.js",
|
|
@@ -22,11 +22,28 @@
|
|
|
22
22
|
"@types/use-sync-external-store": "^0.0.6",
|
|
23
23
|
"react": "^18.2.0",
|
|
24
24
|
"react-native-webview": "^13.6.2",
|
|
25
|
-
"
|
|
25
|
+
"superstruct": "^1.0.4",
|
|
26
|
+
"typescript": "^5.2.2",
|
|
27
|
+
"yup": "^1.4.0",
|
|
28
|
+
"zod": "^3.23.8"
|
|
26
29
|
},
|
|
27
30
|
"peerDependencies": {
|
|
28
31
|
"react": "*",
|
|
29
|
-
"react-native-webview": "*"
|
|
32
|
+
"react-native-webview": "*",
|
|
33
|
+
"superstruct": "*",
|
|
34
|
+
"yup": "*",
|
|
35
|
+
"zod": "*"
|
|
36
|
+
},
|
|
37
|
+
"peerDependenciesMeta": {
|
|
38
|
+
"superstruct": {
|
|
39
|
+
"optional": true
|
|
40
|
+
},
|
|
41
|
+
"yup": {
|
|
42
|
+
"optional": true
|
|
43
|
+
},
|
|
44
|
+
"zod": {
|
|
45
|
+
"optional": true
|
|
46
|
+
}
|
|
30
47
|
},
|
|
31
48
|
"dependencies": {
|
|
32
49
|
"use-sync-external-store": "^1.2.0"
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./bridge"), exports);
|
|
18
|
-
__exportStar(require("./console"), exports);
|