@webview-bridge/web 1.4.7 → 1.5.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.
@@ -328,6 +328,9 @@ var linkBridge = (options = {
328
328
  isWebViewBridgeAvailable: Boolean(window.ReactNativeWebView) && bridgeMethods.length > 0,
329
329
  isNativeMethodAvailable(methodName) {
330
330
  return typeof methodName === "string" && Boolean(window.ReactNativeWebView) && bridgeMethods.includes(methodName);
331
+ },
332
+ addEventListener: (eventName, listener) => {
333
+ return emitter.on(`postMessage/${String(eventName)}`, listener);
331
334
  }
332
335
  });
333
336
  const proxy = new Proxy(target, {
@@ -298,6 +298,9 @@ var linkBridge = (options = {
298
298
  isWebViewBridgeAvailable: Boolean(window.ReactNativeWebView) && bridgeMethods.length > 0,
299
299
  isNativeMethodAvailable(methodName) {
300
300
  return typeof methodName === "string" && Boolean(window.ReactNativeWebView) && bridgeMethods.includes(methodName);
301
+ },
302
+ addEventListener: (eventName, listener) => {
303
+ return emitter.on(`postMessage/${String(eventName)}`, listener);
301
304
  }
302
305
  });
303
306
  const proxy = new Proxy(target, {
@@ -1,9 +1,9 @@
1
- import type { Bridge, BridgeStore, ExcludePrimitive, ExtractStore } from "../../../shared/util/src/types";
1
+ import type { Bridge, BridgeStore, ExcludePrimitive, ExtractStore, ParserSchema } from "../../../shared/util/src/types";
2
2
  import { LinkBridge } from "./types";
3
- export interface LinkBridgeOptions<T extends BridgeStore<T extends Bridge ? T : any>> {
3
+ export interface LinkBridgeOptions<T extends BridgeStore<T extends Bridge ? T : any>, V extends ParserSchema<any>> {
4
4
  timeout?: number;
5
5
  throwOnError?: boolean | (keyof ExtractStore<T>)[] | string[];
6
6
  onFallback?: (methodName: string, args: unknown[]) => void;
7
- onReady?: (method: LinkBridge<ExcludePrimitive<ExtractStore<T>>, Omit<T, "setState">>) => void;
7
+ onReady?: (method: LinkBridge<ExcludePrimitive<ExtractStore<T>>, Omit<T, "setState">, V>) => void;
8
8
  }
9
- export declare const linkBridge: <T extends BridgeStore<T extends Bridge ? T : any>>(options?: LinkBridgeOptions<T>) => LinkBridge<ExcludePrimitive<ExtractStore<T>>, Omit<T, "setState">>;
9
+ export 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>;
@@ -1,7 +1,7 @@
1
- import type { Bridge, BridgeStore, ExcludePrimitive, ExtractStore } from "../../../shared/util/src/types";
1
+ import type { Bridge, BridgeStore, ExcludePrimitive, ExtractStore, ParserSchema } from "../../../shared/util/src/types";
2
2
  import { LinkBridgeOptions } from "./linkBridge";
3
3
  import { LinkBridge } from "./types";
4
4
  /**
5
5
  * @deprecated Use `linkBridge` instead. It's just renamed to `linkBridge`.
6
6
  */
7
- export declare const linkNativeMethod: <T extends BridgeStore<T extends Bridge ? T : any>>(options?: LinkBridgeOptions<T>) => LinkBridge<ExcludePrimitive<ExtractStore<T>>, Omit<T, "setState">>;
7
+ export declare const linkNativeMethod: <T extends BridgeStore<T extends Bridge ? T : any>, V extends ParserSchema<any>>(options?: LinkBridgeOptions<T, V>) => LinkBridge<ExcludePrimitive<ExtractStore<T>>, Omit<T, "setState">, V>;
@@ -1,2 +1,24 @@
1
1
  import type { WebBridge } from "./types";
2
+ /**
3
+ * @deprecated Use `bridge.addEventListener` instead.
4
+ * @see https://gronxb.github.io/webview-bridge/using-a-post-message.html
5
+ * @example
6
+ import { linkBridge } from "@webview-bridge/web";
7
+ import type { AppBridge, AppPostMessageSchema } from ""; // Import the type 'appBridge' and 'appPostMessageSchema' declared in native
8
+
9
+ const bridge = linkBridge<AppBridge, AppPostMessageSchema>({
10
+ // ..
11
+ });
12
+
13
+ const unsubscribe = bridge.addEventListener("eventName1", (data) => {
14
+ window.alert(data.message);
15
+ });
16
+ unsubscribe(); // Unsubscribe from the event
17
+
18
+
19
+ const unsubscribe2 = bridge.addEventListener("eventName2", (message) => {
20
+ window.alert(message);
21
+ });
22
+ unsubscribe2(); // Unsubscribe from the event
23
+ */
2
24
  export declare const registerWebMethod: <BridgeObject extends WebBridge>(bridge: BridgeObject) => BridgeObject;
@@ -1,10 +1,11 @@
1
- import { AsyncFunction } from "../../../../shared/util/src/types";
1
+ import type { AsyncFunction, KeyOfOrString, Parser, ParserSchema } from "../../../../shared/util/src/types";
2
2
  export type WebBridge = Record<string, AsyncFunction>;
3
- export type LinkBridge<T, U> = {
3
+ export type LinkBridge<T, U, V extends ParserSchema<any>> = {
4
4
  isWebViewBridgeAvailable: boolean;
5
5
  isNativeMethodAvailable(method: keyof T): boolean;
6
6
  isNativeMethodAvailable(method: string): boolean;
7
7
  store: U;
8
+ addEventListener<EventName extends KeyOfOrString<V>>(eventName: EventName, listener: (args: Parser<V, EventName>) => void): () => void;
8
9
  loose: {
9
10
  [K in keyof T]: (...args: any[]) => Promise<any>;
10
11
  } & {
@@ -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,7 +1,7 @@
1
1
  {
2
2
  "name": "@webview-bridge/web",
3
3
  "type": "module",
4
- "version": "1.4.7",
4
+ "version": "1.5.0",
5
5
  "description": "Fully Type-Safe Integration for React Native WebView and Web",
6
6
  "publishConfig": {
7
7
  "access": "public"