@webview-bridge/react-native 1.6.1 → 1.7.0-rc.0

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.
Files changed (39) hide show
  1. package/dist/index.cjs +922 -0
  2. package/dist/index.d.cts +155 -0
  3. package/dist/index.d.ts +155 -0
  4. package/dist/index.js +821 -0
  5. package/package.json +19 -25
  6. package/dist/packages/react-native/src/createWebView.d.ts +0 -50
  7. package/dist/packages/react-native/src/createWebView.js +0 -180
  8. package/dist/packages/react-native/src/error.d.ts +0 -3
  9. package/dist/packages/react-native/src/error.js +0 -10
  10. package/dist/packages/react-native/src/index.d.ts +0 -6
  11. package/dist/packages/react-native/src/index.js +0 -22
  12. package/dist/packages/react-native/src/integrations/bridge.d.ts +0 -20
  13. package/dist/packages/react-native/src/integrations/bridge.js +0 -95
  14. package/dist/packages/react-native/src/integrations/console.d.ts +0 -3
  15. package/dist/packages/react-native/src/integrations/console.js +0 -56
  16. package/dist/packages/react-native/src/integrations/handleRegisterWebMethod.d.ts +0 -4
  17. package/dist/packages/react-native/src/integrations/handleRegisterWebMethod.js +0 -30
  18. package/dist/packages/react-native/src/integrations/postMessageSchema.d.ts +0 -2
  19. package/dist/packages/react-native/src/integrations/postMessageSchema.js +0 -56
  20. package/dist/packages/react-native/src/types/webview.d.ts +0 -3
  21. package/dist/packages/react-native/src/types/webview.js +0 -2
  22. package/dist/packages/react-native/src/useBridge.d.ts +0 -3
  23. package/dist/packages/react-native/src/useBridge.js +0 -9
  24. package/dist/shared/util/src/createEvents.d.ts +0 -25
  25. package/dist/shared/util/src/createEvents.js +0 -40
  26. package/dist/shared/util/src/createRandomId.d.ts +0 -1
  27. package/dist/shared/util/src/createRandomId.js +0 -10
  28. package/dist/shared/util/src/equals.d.ts +0 -1
  29. package/dist/shared/util/src/equals.js +0 -49
  30. package/dist/shared/util/src/index.d.ts +0 -6
  31. package/dist/shared/util/src/index.js +0 -22
  32. package/dist/shared/util/src/noop.d.ts +0 -1
  33. package/dist/shared/util/src/noop.js +0 -5
  34. package/dist/shared/util/src/removeUndefinedKeys.d.ts +0 -1
  35. package/dist/shared/util/src/removeUndefinedKeys.js +0 -12
  36. package/dist/shared/util/src/timeout.d.ts +0 -1
  37. package/dist/shared/util/src/timeout.js +0 -16
  38. package/dist/shared/util/src/types.d.ts +0 -36
  39. package/dist/shared/util/src/types.js +0 -2
@@ -0,0 +1,155 @@
1
+ import * as react_native_webview_lib_WebViewTypes from 'react-native-webview/lib/WebViewTypes';
2
+ import { Bridge, ParserSchema, BridgeStore, KeyOfOrString, Parser, OnlyJSON, Primitive, PostMessageSchemaObject, ExtractStore } from '@webview-bridge/types';
3
+ export * from '@webview-bridge/types';
4
+ import React, { Component } from 'react';
5
+ import WebView, { WebViewProps } from 'react-native-webview';
6
+
7
+ type BridgeWebView = Pick<WebView, Exclude<keyof WebView, keyof Component<WebViewProps>>>;
8
+
9
+ type CreateWebViewArgs<BridgeObject extends Bridge, PostMessageSchema extends ParserSchema<any>> = {
10
+ /**
11
+ * The bridge object to be used in the WebView.
12
+ * @example
13
+ import { createWebView, bridge } from "@webview-bridge/react-native";
14
+ import InAppBrowser from "react-native-inappbrowser-reborn";
15
+
16
+ // Register functions in the bridge object in your React Native code
17
+ export const appBridge = bridge({
18
+ async getMessage() {
19
+ return "Hello, I'm native";
20
+ },
21
+ async sum(a: number, b: number) {
22
+ return a + b;
23
+ },
24
+ async openInAppBrowser(url: string) {
25
+ if (await InAppBrowser.isAvailable()) {
26
+ await InAppBrowser.open(url);
27
+ }
28
+ },
29
+ // ... Add more functions as needed
30
+ });
31
+
32
+ export const { WebView } = createWebView({
33
+ bridge: appBridge,
34
+ debug: true, // Enable console.log visibility in the native WebView
35
+ });
36
+ */
37
+ bridge: BridgeStore<BridgeObject>;
38
+ /**
39
+ * If `true`, the console.log visibility in the WebView is enabled.
40
+ * @default false
41
+ */
42
+ debug?: boolean;
43
+ /**
44
+ * Set the timeout in milliseconds for the response from the web method.
45
+ * @default 2000
46
+ */
47
+ responseTimeout?: number;
48
+ /**
49
+ * The schema for the postMessage method.
50
+ * @link https://gronxb.github.io/webview-bridge/using-a-post-message.html
51
+ */
52
+ postMessageSchema?: PostMessageSchema;
53
+ /**
54
+ * Callback function when a method that is not defined in the bridge is called.
55
+ * @link https://gronxb.github.io/webview-bridge/backward-compatibility/new-method.html#react-native-part
56
+ */
57
+ fallback?: (method: keyof BridgeObject) => void;
58
+ };
59
+ type WebMethod<T> = T & {
60
+ isReady: boolean;
61
+ };
62
+ /**
63
+ * Create a WebView component that can communicate with the bridge.
64
+ * @link https://gronxb.github.io/webview-bridge/getting-started.html
65
+ * @example
66
+ import { createWebView, bridge } from "@webview-bridge/react-native";
67
+ import InAppBrowser from "react-native-inappbrowser-reborn";
68
+
69
+ // Register functions in the bridge object in your React Native code
70
+ export const appBridge = bridge({
71
+ async getMessage() {
72
+ return "Hello, I'm native";
73
+ },
74
+ async sum(a: number, b: number) {
75
+ return a + b;
76
+ },
77
+ async openInAppBrowser(url: string) {
78
+ if (await InAppBrowser.isAvailable()) {
79
+ await InAppBrowser.open(url);
80
+ }
81
+ },
82
+ // ... Add more functions as needed
83
+ });
84
+
85
+ export const { WebView } = createWebView({
86
+ bridge: appBridge,
87
+ debug: true, // Enable console.log visibility in the native WebView
88
+ });
89
+ */
90
+ declare const createWebView: <BridgeObject extends Bridge, PostMessageSchema extends ParserSchema<any>>({ bridge, debug, responseTimeout, postMessageSchema, fallback, }: CreateWebViewArgs<BridgeObject, PostMessageSchema>) => {
91
+ /**
92
+ * Sends an event from React Native to the Web.
93
+ * @link https://gronxb.github.io/webview-bridge/using-a-post-message.html
94
+ */
95
+ postMessage: <EventName extends KeyOfOrString<PostMessageSchema>, Args extends Parser<PostMessageSchema, EventName>>(eventName: EventName, args: Args) => void;
96
+ WebView: React.ForwardRefExoticComponent<react_native_webview_lib_WebViewTypes.IOSWebViewProps & react_native_webview_lib_WebViewTypes.AndroidWebViewProps & react_native_webview_lib_WebViewTypes.WindowsWebViewProps & React.RefAttributes<BridgeWebView>>;
97
+ /**
98
+ * @deprecated Use `postMessage` instead. And complete the type through the `postMessageSchema` option.
99
+ * @see https://gronxb.github.io/webview-bridge/using-a-post-message.html
100
+ * @example
101
+ import { createWebView, postMessageSchema } from "@webview-bridge/react-native";
102
+ import { z } from "zod";
103
+
104
+ const appPostMessageSchema = postMessageSchema({
105
+ eventName1: z.object({
106
+ message: z.string(),
107
+ }),
108
+ eventName2: z.string(),
109
+ });
110
+
111
+
112
+ // Export the event schema to be used in the web application
113
+ export type AppPostMessageSchema = typeof appPostMessageSchema;
114
+
115
+ // When you bridge a webview, a postMessage is extracted.
116
+ export const { postMessage } = createWebView({
117
+ 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.
118
+ // ..
119
+ });
120
+
121
+ // usage
122
+ postMessage("eventName1", {
123
+ message: "test",
124
+ });
125
+ postMessage("eventName2", "test");
126
+ */
127
+ linkWebMethod<T>(): {
128
+ current: WebMethod<T>;
129
+ };
130
+ };
131
+
132
+ type StoreCallback<T> = ({ get, set, }: {
133
+ get: () => T;
134
+ set: (newState: Partial<OnlyJSON<T>>) => void;
135
+ }) => T;
136
+ declare const bridge: <T extends Bridge>(procedures: T | StoreCallback<T>) => BridgeStore<T>;
137
+ type HandleBridgeArgs<ArgType = unknown> = {
138
+ bridge: BridgeStore<Bridge>;
139
+ method: string;
140
+ args?: ArgType[];
141
+ webview: WebView;
142
+ eventId: string;
143
+ };
144
+ declare const handleBridge: ({ bridge, method, args, webview, eventId, }: HandleBridgeArgs) => Promise<void>;
145
+ declare const INJECT_BRIDGE_METHODS: (bridgeNames: string[]) => string;
146
+ declare const INJECT_BRIDGE_STATE: (initialState: Record<string, Primitive>) => string;
147
+ declare const SAFE_NATIVE_EMITTER_EMIT: (eventName: string, data: unknown) => string;
148
+ declare const SAFE_NATIVE_EMITTER_THROW: (eventName: string) => string;
149
+
150
+ declare const postMessageSchema: <T extends PostMessageSchemaObject>(schema: T) => T;
151
+
152
+ declare function useBridge<T extends Bridge>(store: BridgeStore<T>): ExtractStore<BridgeStore<T>>;
153
+ declare function useBridge<T extends Bridge, U extends ExtractStore<BridgeStore<T>>, V>(store: BridgeStore<T>, selector?: (state: U) => V): V;
154
+
155
+ export { type BridgeWebView, type CreateWebViewArgs, INJECT_BRIDGE_METHODS, INJECT_BRIDGE_STATE, SAFE_NATIVE_EMITTER_EMIT, SAFE_NATIVE_EMITTER_THROW, type StoreCallback, type WebMethod, bridge, createWebView, handleBridge, postMessageSchema, useBridge };
@@ -0,0 +1,155 @@
1
+ import * as react_native_webview_lib_WebViewTypes from 'react-native-webview/lib/WebViewTypes';
2
+ import { Bridge, ParserSchema, BridgeStore, KeyOfOrString, Parser, OnlyJSON, Primitive, PostMessageSchemaObject, ExtractStore } from '@webview-bridge/types';
3
+ export * from '@webview-bridge/types';
4
+ import React, { Component } from 'react';
5
+ import WebView, { WebViewProps } from 'react-native-webview';
6
+
7
+ type BridgeWebView = Pick<WebView, Exclude<keyof WebView, keyof Component<WebViewProps>>>;
8
+
9
+ type CreateWebViewArgs<BridgeObject extends Bridge, PostMessageSchema extends ParserSchema<any>> = {
10
+ /**
11
+ * The bridge object to be used in the WebView.
12
+ * @example
13
+ import { createWebView, bridge } from "@webview-bridge/react-native";
14
+ import InAppBrowser from "react-native-inappbrowser-reborn";
15
+
16
+ // Register functions in the bridge object in your React Native code
17
+ export const appBridge = bridge({
18
+ async getMessage() {
19
+ return "Hello, I'm native";
20
+ },
21
+ async sum(a: number, b: number) {
22
+ return a + b;
23
+ },
24
+ async openInAppBrowser(url: string) {
25
+ if (await InAppBrowser.isAvailable()) {
26
+ await InAppBrowser.open(url);
27
+ }
28
+ },
29
+ // ... Add more functions as needed
30
+ });
31
+
32
+ export const { WebView } = createWebView({
33
+ bridge: appBridge,
34
+ debug: true, // Enable console.log visibility in the native WebView
35
+ });
36
+ */
37
+ bridge: BridgeStore<BridgeObject>;
38
+ /**
39
+ * If `true`, the console.log visibility in the WebView is enabled.
40
+ * @default false
41
+ */
42
+ debug?: boolean;
43
+ /**
44
+ * Set the timeout in milliseconds for the response from the web method.
45
+ * @default 2000
46
+ */
47
+ responseTimeout?: number;
48
+ /**
49
+ * The schema for the postMessage method.
50
+ * @link https://gronxb.github.io/webview-bridge/using-a-post-message.html
51
+ */
52
+ postMessageSchema?: PostMessageSchema;
53
+ /**
54
+ * Callback function when a method that is not defined in the bridge is called.
55
+ * @link https://gronxb.github.io/webview-bridge/backward-compatibility/new-method.html#react-native-part
56
+ */
57
+ fallback?: (method: keyof BridgeObject) => void;
58
+ };
59
+ type WebMethod<T> = T & {
60
+ isReady: boolean;
61
+ };
62
+ /**
63
+ * Create a WebView component that can communicate with the bridge.
64
+ * @link https://gronxb.github.io/webview-bridge/getting-started.html
65
+ * @example
66
+ import { createWebView, bridge } from "@webview-bridge/react-native";
67
+ import InAppBrowser from "react-native-inappbrowser-reborn";
68
+
69
+ // Register functions in the bridge object in your React Native code
70
+ export const appBridge = bridge({
71
+ async getMessage() {
72
+ return "Hello, I'm native";
73
+ },
74
+ async sum(a: number, b: number) {
75
+ return a + b;
76
+ },
77
+ async openInAppBrowser(url: string) {
78
+ if (await InAppBrowser.isAvailable()) {
79
+ await InAppBrowser.open(url);
80
+ }
81
+ },
82
+ // ... Add more functions as needed
83
+ });
84
+
85
+ export const { WebView } = createWebView({
86
+ bridge: appBridge,
87
+ debug: true, // Enable console.log visibility in the native WebView
88
+ });
89
+ */
90
+ declare const createWebView: <BridgeObject extends Bridge, PostMessageSchema extends ParserSchema<any>>({ bridge, debug, responseTimeout, postMessageSchema, fallback, }: CreateWebViewArgs<BridgeObject, PostMessageSchema>) => {
91
+ /**
92
+ * Sends an event from React Native to the Web.
93
+ * @link https://gronxb.github.io/webview-bridge/using-a-post-message.html
94
+ */
95
+ postMessage: <EventName extends KeyOfOrString<PostMessageSchema>, Args extends Parser<PostMessageSchema, EventName>>(eventName: EventName, args: Args) => void;
96
+ WebView: React.ForwardRefExoticComponent<react_native_webview_lib_WebViewTypes.IOSWebViewProps & react_native_webview_lib_WebViewTypes.AndroidWebViewProps & react_native_webview_lib_WebViewTypes.WindowsWebViewProps & React.RefAttributes<BridgeWebView>>;
97
+ /**
98
+ * @deprecated Use `postMessage` instead. And complete the type through the `postMessageSchema` option.
99
+ * @see https://gronxb.github.io/webview-bridge/using-a-post-message.html
100
+ * @example
101
+ import { createWebView, postMessageSchema } from "@webview-bridge/react-native";
102
+ import { z } from "zod";
103
+
104
+ const appPostMessageSchema = postMessageSchema({
105
+ eventName1: z.object({
106
+ message: z.string(),
107
+ }),
108
+ eventName2: z.string(),
109
+ });
110
+
111
+
112
+ // Export the event schema to be used in the web application
113
+ export type AppPostMessageSchema = typeof appPostMessageSchema;
114
+
115
+ // When you bridge a webview, a postMessage is extracted.
116
+ export const { postMessage } = createWebView({
117
+ 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.
118
+ // ..
119
+ });
120
+
121
+ // usage
122
+ postMessage("eventName1", {
123
+ message: "test",
124
+ });
125
+ postMessage("eventName2", "test");
126
+ */
127
+ linkWebMethod<T>(): {
128
+ current: WebMethod<T>;
129
+ };
130
+ };
131
+
132
+ type StoreCallback<T> = ({ get, set, }: {
133
+ get: () => T;
134
+ set: (newState: Partial<OnlyJSON<T>>) => void;
135
+ }) => T;
136
+ declare const bridge: <T extends Bridge>(procedures: T | StoreCallback<T>) => BridgeStore<T>;
137
+ type HandleBridgeArgs<ArgType = unknown> = {
138
+ bridge: BridgeStore<Bridge>;
139
+ method: string;
140
+ args?: ArgType[];
141
+ webview: WebView;
142
+ eventId: string;
143
+ };
144
+ declare const handleBridge: ({ bridge, method, args, webview, eventId, }: HandleBridgeArgs) => Promise<void>;
145
+ declare const INJECT_BRIDGE_METHODS: (bridgeNames: string[]) => string;
146
+ declare const INJECT_BRIDGE_STATE: (initialState: Record<string, Primitive>) => string;
147
+ declare const SAFE_NATIVE_EMITTER_EMIT: (eventName: string, data: unknown) => string;
148
+ declare const SAFE_NATIVE_EMITTER_THROW: (eventName: string) => string;
149
+
150
+ declare const postMessageSchema: <T extends PostMessageSchemaObject>(schema: T) => T;
151
+
152
+ declare function useBridge<T extends Bridge>(store: BridgeStore<T>): ExtractStore<BridgeStore<T>>;
153
+ declare function useBridge<T extends Bridge, U extends ExtractStore<BridgeStore<T>>, V>(store: BridgeStore<T>, selector?: (state: U) => V): V;
154
+
155
+ export { type BridgeWebView, type CreateWebViewArgs, INJECT_BRIDGE_METHODS, INJECT_BRIDGE_STATE, SAFE_NATIVE_EMITTER_EMIT, SAFE_NATIVE_EMITTER_THROW, type StoreCallback, type WebMethod, bridge, createWebView, handleBridge, postMessageSchema, useBridge };