@webview-bridge/web 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.
- package/dist/index.cjs +927 -0
- package/dist/index.d.cts +81 -0
- package/dist/index.d.ts +81 -0
- package/dist/index.js +861 -0
- package/package.json +11 -10
- package/dist/commonjs/index.cjs +0 -1
- package/dist/module/index.mjs +0 -1
- package/dist/typescript/packages/web/src/error.d.ts +0 -6
- package/dist/typescript/packages/web/src/index.d.ts +0 -5
- package/dist/typescript/packages/web/src/internal/bridgeInstance.d.ts +0 -24
- package/dist/typescript/packages/web/src/internal/createPromiseProxy.d.ts +0 -1
- package/dist/typescript/packages/web/src/internal/linkBridgeStore.d.ts +0 -7
- package/dist/typescript/packages/web/src/internal/mockStore.d.ts +0 -5
- package/dist/typescript/packages/web/src/linkBridge.d.ts +0 -10
- package/dist/typescript/packages/web/src/registerWebMethod.d.ts +0 -24
- package/dist/typescript/packages/web/src/types/index.d.ts +0 -14
- package/dist/typescript/shared/util/src/createEvents.d.ts +0 -25
- package/dist/typescript/shared/util/src/createRandomId.d.ts +0 -1
- package/dist/typescript/shared/util/src/equals.d.ts +0 -1
- package/dist/typescript/shared/util/src/index.d.ts +0 -6
- package/dist/typescript/shared/util/src/noop.d.ts +0 -1
- package/dist/typescript/shared/util/src/removeUndefinedKeys.d.ts +0 -1
- package/dist/typescript/shared/util/src/timeout.d.ts +0 -1
- package/dist/typescript/shared/util/src/types.d.ts +0 -36
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { AsyncFunction, ParserSchema, KeyOfOrString, Parser, BridgeStore, Bridge, ExtractStore, ExcludePrimitive } from '@webview-bridge/types';
|
|
2
|
+
export * from '@webview-bridge/types';
|
|
3
|
+
|
|
4
|
+
declare class MethodNotFoundError extends Error {
|
|
5
|
+
constructor(methodName: string);
|
|
6
|
+
}
|
|
7
|
+
declare class NativeMethodError extends Error {
|
|
8
|
+
constructor(methodName: string);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
type WebBridge = Record<string, AsyncFunction>;
|
|
12
|
+
type LinkBridge<T, U, V extends ParserSchema<any>> = {
|
|
13
|
+
isWebViewBridgeAvailable: boolean;
|
|
14
|
+
isNativeMethodAvailable(method: keyof T): boolean;
|
|
15
|
+
isNativeMethodAvailable(method: string): boolean;
|
|
16
|
+
store: U;
|
|
17
|
+
addEventListener<EventName extends KeyOfOrString<V>>(eventName: EventName, listener: (args: Parser<V, EventName>) => void): () => void;
|
|
18
|
+
loose: {
|
|
19
|
+
[K in keyof T]: (...args: any[]) => Promise<any>;
|
|
20
|
+
} & {
|
|
21
|
+
[key: string]: (...args: any[]) => Promise<any>;
|
|
22
|
+
};
|
|
23
|
+
} & T;
|
|
24
|
+
|
|
25
|
+
interface LinkBridgeOptions<T extends BridgeStore<T extends Bridge ? T : any>, V extends ParserSchema<any>> {
|
|
26
|
+
/**
|
|
27
|
+
* It is possible to configure `initialBridge` to operate in a non-React Native environment.
|
|
28
|
+
* Prioritize applying the bridge of the React Native WebView, and if it is unavailable, apply the `initialBridge`.
|
|
29
|
+
* Therefore, if `initialBridge` is configured, `bridge.isWebViewBridgeAvailable` should be true even in environments that are not React Native.
|
|
30
|
+
* @link https://gronxb.github.io/webview-bridge/non-react-native-environment.html
|
|
31
|
+
*/
|
|
32
|
+
initialBridge?: Partial<ExtractStore<T>>;
|
|
33
|
+
/**
|
|
34
|
+
* Set the timeout in milliseconds after calling the native method.
|
|
35
|
+
* @default 2000
|
|
36
|
+
*/
|
|
37
|
+
timeout?: number;
|
|
38
|
+
/**
|
|
39
|
+
* If `true`, an error will be thrown when calling a method that is not defined in the bridge.
|
|
40
|
+
*/
|
|
41
|
+
throwOnError?: boolean | (keyof ExtractStore<T>)[] | string[];
|
|
42
|
+
/**
|
|
43
|
+
* Callback function when a method that is not defined in the bridge is called.
|
|
44
|
+
*/
|
|
45
|
+
onFallback?: (methodName: string, args: unknown[]) => void;
|
|
46
|
+
/**
|
|
47
|
+
* Callback function when the bridge is ready.
|
|
48
|
+
*/
|
|
49
|
+
onReady?: (method: LinkBridge<ExcludePrimitive<ExtractStore<T>>, Omit<T, "setState">, V>) => void;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Create a link to the bridge connected to the React Native WebView.
|
|
53
|
+
* @link https://gronxb.github.io/webview-bridge/getting-started.html
|
|
54
|
+
*/
|
|
55
|
+
declare const linkBridge: <T extends BridgeStore<T extends Bridge ? T : any>, V extends ParserSchema<any> = ParserSchema<any>>(options?: LinkBridgeOptions<T, V>) => LinkBridge<ExcludePrimitive<ExtractStore<T>>, Omit<T, "setState">, V>;
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* @deprecated Use `bridge.addEventListener` instead.
|
|
59
|
+
* @see https://gronxb.github.io/webview-bridge/using-a-post-message.html
|
|
60
|
+
* @example
|
|
61
|
+
import { linkBridge } from "@webview-bridge/web";
|
|
62
|
+
import type { AppBridge, AppPostMessageSchema } from ""; // Import the type 'appBridge' and 'appPostMessageSchema' declared in native
|
|
63
|
+
|
|
64
|
+
const bridge = linkBridge<AppBridge, AppPostMessageSchema>({
|
|
65
|
+
// ..
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
const unsubscribe = bridge.addEventListener("eventName1", (data) => {
|
|
69
|
+
window.alert(data.message);
|
|
70
|
+
});
|
|
71
|
+
unsubscribe(); // Unsubscribe from the event
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
const unsubscribe2 = bridge.addEventListener("eventName2", (message) => {
|
|
75
|
+
window.alert(message);
|
|
76
|
+
});
|
|
77
|
+
unsubscribe2(); // Unsubscribe from the event
|
|
78
|
+
*/
|
|
79
|
+
declare const registerWebMethod: <BridgeObject extends WebBridge>(bridge: BridgeObject) => BridgeObject;
|
|
80
|
+
|
|
81
|
+
export { type LinkBridge, type LinkBridgeOptions, MethodNotFoundError, NativeMethodError, type WebBridge, linkBridge, registerWebMethod };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { AsyncFunction, ParserSchema, KeyOfOrString, Parser, BridgeStore, Bridge, ExtractStore, ExcludePrimitive } from '@webview-bridge/types';
|
|
2
|
+
export * from '@webview-bridge/types';
|
|
3
|
+
|
|
4
|
+
declare class MethodNotFoundError extends Error {
|
|
5
|
+
constructor(methodName: string);
|
|
6
|
+
}
|
|
7
|
+
declare class NativeMethodError extends Error {
|
|
8
|
+
constructor(methodName: string);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
type WebBridge = Record<string, AsyncFunction>;
|
|
12
|
+
type LinkBridge<T, U, V extends ParserSchema<any>> = {
|
|
13
|
+
isWebViewBridgeAvailable: boolean;
|
|
14
|
+
isNativeMethodAvailable(method: keyof T): boolean;
|
|
15
|
+
isNativeMethodAvailable(method: string): boolean;
|
|
16
|
+
store: U;
|
|
17
|
+
addEventListener<EventName extends KeyOfOrString<V>>(eventName: EventName, listener: (args: Parser<V, EventName>) => void): () => void;
|
|
18
|
+
loose: {
|
|
19
|
+
[K in keyof T]: (...args: any[]) => Promise<any>;
|
|
20
|
+
} & {
|
|
21
|
+
[key: string]: (...args: any[]) => Promise<any>;
|
|
22
|
+
};
|
|
23
|
+
} & T;
|
|
24
|
+
|
|
25
|
+
interface LinkBridgeOptions<T extends BridgeStore<T extends Bridge ? T : any>, V extends ParserSchema<any>> {
|
|
26
|
+
/**
|
|
27
|
+
* It is possible to configure `initialBridge` to operate in a non-React Native environment.
|
|
28
|
+
* Prioritize applying the bridge of the React Native WebView, and if it is unavailable, apply the `initialBridge`.
|
|
29
|
+
* Therefore, if `initialBridge` is configured, `bridge.isWebViewBridgeAvailable` should be true even in environments that are not React Native.
|
|
30
|
+
* @link https://gronxb.github.io/webview-bridge/non-react-native-environment.html
|
|
31
|
+
*/
|
|
32
|
+
initialBridge?: Partial<ExtractStore<T>>;
|
|
33
|
+
/**
|
|
34
|
+
* Set the timeout in milliseconds after calling the native method.
|
|
35
|
+
* @default 2000
|
|
36
|
+
*/
|
|
37
|
+
timeout?: number;
|
|
38
|
+
/**
|
|
39
|
+
* If `true`, an error will be thrown when calling a method that is not defined in the bridge.
|
|
40
|
+
*/
|
|
41
|
+
throwOnError?: boolean | (keyof ExtractStore<T>)[] | string[];
|
|
42
|
+
/**
|
|
43
|
+
* Callback function when a method that is not defined in the bridge is called.
|
|
44
|
+
*/
|
|
45
|
+
onFallback?: (methodName: string, args: unknown[]) => void;
|
|
46
|
+
/**
|
|
47
|
+
* Callback function when the bridge is ready.
|
|
48
|
+
*/
|
|
49
|
+
onReady?: (method: LinkBridge<ExcludePrimitive<ExtractStore<T>>, Omit<T, "setState">, V>) => void;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Create a link to the bridge connected to the React Native WebView.
|
|
53
|
+
* @link https://gronxb.github.io/webview-bridge/getting-started.html
|
|
54
|
+
*/
|
|
55
|
+
declare const linkBridge: <T extends BridgeStore<T extends Bridge ? T : any>, V extends ParserSchema<any> = ParserSchema<any>>(options?: LinkBridgeOptions<T, V>) => LinkBridge<ExcludePrimitive<ExtractStore<T>>, Omit<T, "setState">, V>;
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* @deprecated Use `bridge.addEventListener` instead.
|
|
59
|
+
* @see https://gronxb.github.io/webview-bridge/using-a-post-message.html
|
|
60
|
+
* @example
|
|
61
|
+
import { linkBridge } from "@webview-bridge/web";
|
|
62
|
+
import type { AppBridge, AppPostMessageSchema } from ""; // Import the type 'appBridge' and 'appPostMessageSchema' declared in native
|
|
63
|
+
|
|
64
|
+
const bridge = linkBridge<AppBridge, AppPostMessageSchema>({
|
|
65
|
+
// ..
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
const unsubscribe = bridge.addEventListener("eventName1", (data) => {
|
|
69
|
+
window.alert(data.message);
|
|
70
|
+
});
|
|
71
|
+
unsubscribe(); // Unsubscribe from the event
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
const unsubscribe2 = bridge.addEventListener("eventName2", (message) => {
|
|
75
|
+
window.alert(message);
|
|
76
|
+
});
|
|
77
|
+
unsubscribe2(); // Unsubscribe from the event
|
|
78
|
+
*/
|
|
79
|
+
declare const registerWebMethod: <BridgeObject extends WebBridge>(bridge: BridgeObject) => BridgeObject;
|
|
80
|
+
|
|
81
|
+
export { type LinkBridge, type LinkBridgeOptions, MethodNotFoundError, NativeMethodError, type WebBridge, linkBridge, registerWebMethod };
|