@webview-bridge/react-native 1.5.0-rc.1 → 1.5.1-rc.2
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 +28 -2
- package/dist/packages/react-native/src/createWebView.js +40 -10
- package/dist/packages/react-native/src/integrations/bridge.js +1 -1
- package/dist/shared/util/src/createEvents.d.ts +2 -1
- package/dist/shared/util/src/types.d.ts +1 -0
- package/package.json +1 -1
|
@@ -16,8 +16,34 @@ export declare const createWebView: <BridgeObject extends Bridge, PostMessageSch
|
|
|
16
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
17
|
/**
|
|
18
18
|
* @deprecated Use `postMessage` instead. And complete the type through the `postMessageSchema` option.
|
|
19
|
-
* @see https://gronxb.github.io/webview-bridge/
|
|
20
|
-
|
|
19
|
+
* @see https://gronxb.github.io/webview-bridge/using-a-post-message.html
|
|
20
|
+
* @example
|
|
21
|
+
import { createWebView, postMessageSchema } from "@webview-bridge/react-native";
|
|
22
|
+
import { z } from "zod";
|
|
23
|
+
|
|
24
|
+
const appPostMessageSchema = postMessageSchema({
|
|
25
|
+
eventName1: z.object({
|
|
26
|
+
message: z.string(),
|
|
27
|
+
}),
|
|
28
|
+
eventName2: z.string(),
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
// Export the event schema to be used in the web application
|
|
33
|
+
export type AppPostMessageSchema = typeof appPostMessageSchema;
|
|
34
|
+
|
|
35
|
+
// When you bridge a webview, a postMessage is extracted.
|
|
36
|
+
export const { postMessage } = createWebView({
|
|
37
|
+
postMessageSchema: appPostMessageSchema, // Pass in the your schema. This is optional, so if the type doesn't matter to you, you don't need to include it.
|
|
38
|
+
// ..
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
// usage
|
|
42
|
+
postMessage("eventName1", {
|
|
43
|
+
message: "test",
|
|
44
|
+
});
|
|
45
|
+
postMessage("eventName2", "test");
|
|
46
|
+
*/
|
|
21
47
|
linkWebMethod<T>(): {
|
|
22
48
|
current: WebMethod<T>;
|
|
23
49
|
};
|
|
@@ -64,12 +64,16 @@ const createWebView = ({ bridge, debug, responseTimeout = 2000, postMessageSchem
|
|
|
64
64
|
webviewRefList.pop();
|
|
65
65
|
};
|
|
66
66
|
}, []);
|
|
67
|
-
const
|
|
68
|
-
.
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
67
|
+
const initData = (0, react_1.useMemo)(() => {
|
|
68
|
+
const bridgeMethods = Object.entries(bridge.getState() ?? {})
|
|
69
|
+
.filter(([_, bridge]) => typeof bridge === "function")
|
|
70
|
+
.map(([name]) => name);
|
|
71
|
+
const initialState = Object.fromEntries(Object.entries(bridge.getState() ?? {}).filter(([_, value]) => typeof value !== "function"));
|
|
72
|
+
return { bridgeMethods, initialState };
|
|
73
|
+
}, []);
|
|
74
|
+
(0, react_1.useEffect)(() => {
|
|
75
|
+
webviewRef.current?.injectJavaScript((0, bridge_1.SAFE_NATIVE_EMITTER_EMIT)("hydrate", initData));
|
|
76
|
+
}, [initData]);
|
|
73
77
|
(0, react_1.useImperativeHandle)(ref, () => webviewRef.current, []);
|
|
74
78
|
const handleMessage = async (event) => {
|
|
75
79
|
props.onMessage?.(event);
|
|
@@ -124,8 +128,8 @@ const createWebView = ({ bridge, debug, responseTimeout = 2000, postMessageSchem
|
|
|
124
128
|
}
|
|
125
129
|
};
|
|
126
130
|
return (<react_native_webview_1.default {...props} ref={webviewRef} onMessage={handleMessage} injectedJavaScriptBeforeContentLoaded={[
|
|
127
|
-
(0, bridge_1.INJECT_BRIDGE_METHODS)(
|
|
128
|
-
(0, bridge_1.INJECT_BRIDGE_STATE)(initialState),
|
|
131
|
+
(0, bridge_1.INJECT_BRIDGE_METHODS)(initData.bridgeMethods),
|
|
132
|
+
(0, bridge_1.INJECT_BRIDGE_STATE)(initData.initialState),
|
|
129
133
|
props.injectedJavaScriptBeforeContentLoaded,
|
|
130
134
|
"true;",
|
|
131
135
|
]
|
|
@@ -140,8 +144,34 @@ const createWebView = ({ bridge, debug, responseTimeout = 2000, postMessageSchem
|
|
|
140
144
|
}),
|
|
141
145
|
/**
|
|
142
146
|
* @deprecated Use `postMessage` instead. And complete the type through the `postMessageSchema` option.
|
|
143
|
-
* @see https://gronxb.github.io/webview-bridge/
|
|
144
|
-
|
|
147
|
+
* @see https://gronxb.github.io/webview-bridge/using-a-post-message.html
|
|
148
|
+
* @example
|
|
149
|
+
import { createWebView, postMessageSchema } from "@webview-bridge/react-native";
|
|
150
|
+
import { z } from "zod";
|
|
151
|
+
|
|
152
|
+
const appPostMessageSchema = postMessageSchema({
|
|
153
|
+
eventName1: z.object({
|
|
154
|
+
message: z.string(),
|
|
155
|
+
}),
|
|
156
|
+
eventName2: z.string(),
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
// Export the event schema to be used in the web application
|
|
161
|
+
export type AppPostMessageSchema = typeof appPostMessageSchema;
|
|
162
|
+
|
|
163
|
+
// When you bridge a webview, a postMessage is extracted.
|
|
164
|
+
export const { postMessage } = createWebView({
|
|
165
|
+
postMessageSchema: appPostMessageSchema, // Pass in the your schema. This is optional, so if the type doesn't matter to you, you don't need to include it.
|
|
166
|
+
// ..
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
// usage
|
|
170
|
+
postMessage("eventName1", {
|
|
171
|
+
message: "test",
|
|
172
|
+
});
|
|
173
|
+
postMessage("eventName2", "test");
|
|
174
|
+
*/
|
|
145
175
|
linkWebMethod() {
|
|
146
176
|
return WebMethod;
|
|
147
177
|
},
|
|
@@ -63,7 +63,7 @@ const handleBridge = async ({ bridge, method, args, webview, eventId, }) => {
|
|
|
63
63
|
};
|
|
64
64
|
exports.handleBridge = handleBridge;
|
|
65
65
|
const INJECT_BRIDGE_METHODS = (bridgeNames) => `
|
|
66
|
-
window.__bridgeMethods__ =
|
|
66
|
+
window.__bridgeMethods__ = ${JSON.stringify(bridgeNames)};
|
|
67
67
|
`;
|
|
68
68
|
exports.INJECT_BRIDGE_METHODS = INJECT_BRIDGE_METHODS;
|
|
69
69
|
const INJECT_BRIDGE_STATE = (initialState) => `
|
|
@@ -11,9 +11,10 @@ export interface EventEmitter<Events extends EventsMap = DefaultEvents> {
|
|
|
11
11
|
}>;
|
|
12
12
|
on<K extends keyof Events>(this: this, event: K, cb: Events[K]): () => void;
|
|
13
13
|
}
|
|
14
|
+
export type DefaultEmitter = EventEmitter<DefaultEvents>;
|
|
14
15
|
export declare const createEvents: <Events extends EventsMap = DefaultEvents>() => EventEmitter<Events>;
|
|
15
16
|
export interface CreateResolverOptions {
|
|
16
|
-
emitter:
|
|
17
|
+
emitter: DefaultEmitter;
|
|
17
18
|
evaluate: () => void;
|
|
18
19
|
eventId: string;
|
|
19
20
|
failHandler?: Error | false;
|
|
@@ -3,6 +3,7 @@ import type { AnySchema as YupTypeAny, InferType as yupInfer } from "yup";
|
|
|
3
3
|
import type { infer as zodInfer, ZodTypeAny } from "zod";
|
|
4
4
|
export type AsyncFunction = (...args: any[]) => Promise<any>;
|
|
5
5
|
export type Primitive = string | number | boolean | null | undefined;
|
|
6
|
+
export type PrimitiveObject = Record<string, Primitive>;
|
|
6
7
|
export type RawJSON = Primitive | {
|
|
7
8
|
[key: string]: RawJSON;
|
|
8
9
|
} | RawJSONArray;
|
package/package.json
CHANGED