@farcaster/frame-host-react-native 0.0.15 → 0.0.17

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/webview.d.ts CHANGED
@@ -1,6 +1,7 @@
1
- import { RefObject } from "react";
2
- import WebView, { WebViewMessageEvent } from "react-native-webview";
3
- import { HostEndpoint } from "@farcaster/frame-host";
1
+ import type { HostEndpoint } from '@farcaster/frame-host';
2
+ import type { RefObject } from 'react';
3
+ import type WebView from 'react-native-webview';
4
+ import type { WebViewMessageEvent } from 'react-native-webview';
4
5
  export declare const SYMBOL_IGNORING_RPC_RESPONSE_ERROR: symbol;
5
6
  export type WebViewEndpoint = HostEndpoint & {
6
7
  /**
package/dist/webview.js CHANGED
@@ -6,26 +6,26 @@ export function createWebViewRpcEndpoint(ref) {
6
6
  const listeners = [];
7
7
  return {
8
8
  addEventListener: (type, listener) => {
9
- if (type !== "message") {
9
+ if (type !== 'message') {
10
10
  throw Error(`Got an unexpected event type "${type}". Expected "message".`);
11
11
  }
12
12
  listeners.push(listener);
13
13
  },
14
14
  removeEventListener: (type, listener) => {
15
- if (type !== "message") {
15
+ if (type !== 'message') {
16
16
  throw Error(`Got an unexpected event type "${type}". Expected "message".`);
17
17
  }
18
18
  listeners.splice(listeners.findIndex((l) => l === listener));
19
19
  },
20
20
  postMessage: (data) => {
21
21
  if (!ref.current) {
22
- if ("value" in data &&
22
+ if ('value' in data &&
23
23
  data.value === SYMBOL_IGNORING_RPC_RESPONSE_ERROR) {
24
24
  return;
25
25
  }
26
- throw Error("Failed to return RPC response to WebView via postMessage");
26
+ throw Error('Failed to return RPC response to WebView via postMessage');
27
27
  }
28
- console.debug("[webview:res]", data);
28
+ console.debug('[webview:res]', data);
29
29
  const dataStr = JSON.stringify(data);
30
30
  return ref.current.injectJavaScript(`
31
31
  console.debug('[webview:res]', ${dataStr});
@@ -34,10 +34,10 @@ export function createWebViewRpcEndpoint(ref) {
34
34
  },
35
35
  onMessage: (e) => {
36
36
  const data = JSON.parse(e.nativeEvent.data);
37
- console.debug("[webview:req]", data);
37
+ console.debug('[webview:req]', data);
38
38
  const messageEvent = new MessageEvent(data);
39
- listeners.forEach((l) => {
40
- if (typeof l === "function") {
39
+ for (const l of listeners) {
40
+ if (typeof l === 'function') {
41
41
  // Actually, messageEvent doesn't satisfy Event interface,
42
42
  // but it satisfies the minimum properties that Comlink's listener requires.
43
43
  l(messageEvent);
@@ -45,13 +45,13 @@ export function createWebViewRpcEndpoint(ref) {
45
45
  else {
46
46
  l.handleEvent(messageEvent);
47
47
  }
48
- });
48
+ }
49
49
  },
50
50
  emit: (data) => {
51
51
  if (!ref.current) {
52
- throw Error("Failed to send Event to WebView via postMessage");
52
+ throw Error('Failed to send Event to WebView via postMessage');
53
53
  }
54
- console.debug("[webview:emit]", data);
54
+ console.debug('[webview:emit]', data);
55
55
  const dataStr = JSON.stringify(data);
56
56
  return ref.current.injectJavaScript(`
57
57
  console.debug('[webview:emit]', ${dataStr});
@@ -60,9 +60,9 @@ export function createWebViewRpcEndpoint(ref) {
60
60
  },
61
61
  emitEthProvider: (event, params) => {
62
62
  if (!ref.current) {
63
- throw Error("Failed to send EthProvider Event to WebView via postMessage");
63
+ throw Error('Failed to send EthProvider Event to WebView via postMessage');
64
64
  }
65
- console.debug("[emit:ethProvider]", event, params);
65
+ console.debug('[emit:ethProvider]', event, params);
66
66
  const wireEvent = { event, params };
67
67
  const dataStr = JSON.stringify(wireEvent);
68
68
  return ref.current.injectJavaScript(`
@@ -78,7 +78,7 @@ export function createWebViewRpcEndpoint(ref) {
78
78
  */
79
79
  class MessageEvent {
80
80
  data;
81
- origin = "ReactNativeWebView";
81
+ origin = 'ReactNativeWebView';
82
82
  constructor(data) {
83
83
  this.data = data;
84
84
  }
@@ -1,14 +1,15 @@
1
- import { RefObject } from "react";
2
- import type { Provider } from "ox";
3
- import WebView, { WebViewMessageEvent } from "react-native-webview";
4
- import type { FrameHost } from "@farcaster/frame-core";
5
- import { WebViewEndpoint } from "./webview";
1
+ import type { FrameHost } from '@farcaster/frame-core';
2
+ import type { Provider } from 'ox';
3
+ import { type RefObject } from 'react';
4
+ import type WebView from 'react-native-webview';
5
+ import type { WebViewMessageEvent } from 'react-native-webview';
6
+ import { type WebViewEndpoint } from './webview';
6
7
  /**
7
8
  * Returns a handler of RPC message from WebView.
8
9
  */
9
10
  export declare function useWebViewRpcAdapter({ webViewRef, sdk, ethProvider, debug, }: {
10
11
  webViewRef: RefObject<WebView>;
11
- sdk: Omit<FrameHost, "ethProviderRequestV2">;
12
+ sdk: Omit<FrameHost, 'ethProviderRequestV2'>;
12
13
  ethProvider?: Provider.Provider;
13
14
  debug?: boolean;
14
15
  }): {
@@ -22,7 +23,7 @@ export declare function useWebViewRpcEndpoint(webViewRef: RefObject<WebView>): {
22
23
  };
23
24
  export declare function useExposeWebViewToEndpoint({ endpoint, sdk, ethProvider, debug, }: {
24
25
  endpoint: WebViewEndpoint | undefined;
25
- sdk: Omit<FrameHost, "ethProviderRequestV2">;
26
+ sdk: Omit<FrameHost, 'ethProviderRequestV2'>;
26
27
  ethProvider?: Provider.Provider;
27
28
  debug?: boolean;
28
29
  }): void;
@@ -1,6 +1,6 @@
1
- import { useCallback, useEffect, useRef } from "react";
2
- import { exposeToEndpoint, useExposeToEndpoint, } from "@farcaster/frame-host";
3
- import { createWebViewRpcEndpoint } from "./webview";
1
+ import { exposeToEndpoint, useExposeToEndpoint, } from '@farcaster/frame-host';
2
+ import { useCallback, useEffect, useRef } from 'react';
3
+ import { createWebViewRpcEndpoint } from './webview';
4
4
  /**
5
5
  * Returns a handler of RPC message from WebView.
6
6
  */
@@ -8,7 +8,7 @@ export function useWebViewRpcAdapter({ webViewRef, sdk, ethProvider, debug = fal
8
8
  const endpointRef = useRef();
9
9
  const onMessage = useCallback((e) => {
10
10
  endpointRef.current?.onMessage(e);
11
- }, [endpointRef]);
11
+ }, []);
12
12
  useEffect(() => {
13
13
  const endpoint = createWebViewRpcEndpoint(webViewRef);
14
14
  endpointRef.current = endpoint;
@@ -16,14 +16,14 @@ export function useWebViewRpcAdapter({ webViewRef, sdk, ethProvider, debug = fal
16
16
  endpoint,
17
17
  sdk,
18
18
  ethProvider,
19
- frameOrigin: "ReactNativeWebView",
19
+ frameOrigin: 'ReactNativeWebView',
20
20
  debug,
21
21
  });
22
22
  return () => {
23
23
  cleanup?.();
24
24
  endpointRef.current = undefined;
25
25
  };
26
- }, [webViewRef, sdk, ethProvider]);
26
+ }, [webViewRef, sdk, ethProvider, debug]);
27
27
  return {
28
28
  onMessage,
29
29
  emit: endpointRef.current?.emit,
@@ -34,7 +34,7 @@ export function useWebViewRpcEndpoint(webViewRef) {
34
34
  const endpointRef = useRef();
35
35
  const onMessage = useCallback((e) => {
36
36
  endpointRef.current?.onMessage(e);
37
- }, [endpointRef]);
37
+ }, []);
38
38
  useEffect(() => {
39
39
  const endpoint = createWebViewRpcEndpoint(webViewRef);
40
40
  endpointRef.current = endpoint;
@@ -51,7 +51,7 @@ export function useExposeWebViewToEndpoint({ endpoint, sdk, ethProvider, debug =
51
51
  useExposeToEndpoint({
52
52
  endpoint,
53
53
  sdk,
54
- frameOrigin: "ReactNativeWebView",
54
+ frameOrigin: 'ReactNativeWebView',
55
55
  ethProvider,
56
56
  debug,
57
57
  });
package/package.json CHANGED
@@ -1,17 +1,17 @@
1
1
  {
2
2
  "name": "@farcaster/frame-host-react-native",
3
- "version": "0.0.15",
3
+ "version": "0.0.17",
4
4
  "main": "dist/index.js",
5
5
  "files": [
6
6
  "dist",
7
7
  "src"
8
8
  ],
9
9
  "devDependencies": {
10
- "@types/react": "^18.3.12",
10
+ "@types/react": "^18.3.18",
11
11
  "react": "^18.3.1",
12
- "react-native": "^0.76.3",
13
- "react-native-webview": "^13.12.4",
14
- "typescript": "^5.6.3",
12
+ "react-native": "^0.76.5",
13
+ "react-native-webview": "^13.12.5",
14
+ "typescript": "^5.7.2",
15
15
  "@farcaster/tsconfig": "0.0.2"
16
16
  },
17
17
  "peerDependencies": {
@@ -19,8 +19,9 @@
19
19
  "react-native-webview": ">=13.0.0"
20
20
  },
21
21
  "dependencies": {
22
- "ox": "^0.4.0",
23
- "@farcaster/frame-host": "0.0.20"
22
+ "ox": "^0.4.4",
23
+ "@farcaster/frame-core": "0.0.22",
24
+ "@farcaster/frame-host": "0.0.22"
24
25
  },
25
26
  "scripts": {
26
27
  "clean": "rm -rf dist",
package/src/index.ts CHANGED
@@ -1,3 +1,3 @@
1
- export * from "@farcaster/frame-host";
2
- export * from "./webview";
3
- export * from "./webviewAdapter";
1
+ export * from '@farcaster/frame-host'
2
+ export * from './webview'
3
+ export * from './webviewAdapter'
package/src/webview.ts CHANGED
@@ -1,15 +1,17 @@
1
- import { RefObject } from "react";
2
- import WebView, { WebViewMessageEvent } from "react-native-webview";
3
- import { HostEndpoint } from "@farcaster/frame-host";
1
+ import type { HostEndpoint } from '@farcaster/frame-host'
4
2
 
5
- export const SYMBOL_IGNORING_RPC_RESPONSE_ERROR: symbol = Symbol();
3
+ import type { RefObject } from 'react'
4
+ import type WebView from 'react-native-webview'
5
+ import type { WebViewMessageEvent } from 'react-native-webview'
6
+
7
+ export const SYMBOL_IGNORING_RPC_RESPONSE_ERROR: symbol = Symbol()
6
8
 
7
9
  export type WebViewEndpoint = HostEndpoint & {
8
10
  /**
9
11
  * Manually distribute events to listeners as an alternative to `document.addEventHandler` which is unavailable in React Native.
10
12
  */
11
- onMessage: (e: WebViewMessageEvent) => void;
12
- };
13
+ onMessage: (e: WebViewMessageEvent) => void
14
+ }
13
15
 
14
16
  /**
15
17
  * An endpoint of communicating with WebView
@@ -17,81 +19,81 @@ export type WebViewEndpoint = HostEndpoint & {
17
19
  export function createWebViewRpcEndpoint(
18
20
  ref: RefObject<WebView>,
19
21
  ): WebViewEndpoint {
20
- const listeners: EventListenerOrEventListenerObject[] = [];
22
+ const listeners: EventListenerOrEventListenerObject[] = []
21
23
  return {
22
24
  addEventListener: (type, listener) => {
23
- if (type !== "message") {
25
+ if (type !== 'message') {
24
26
  throw Error(
25
27
  `Got an unexpected event type "${type}". Expected "message".`,
26
- );
28
+ )
27
29
  }
28
- listeners.push(listener);
30
+ listeners.push(listener)
29
31
  },
30
32
  removeEventListener: (type, listener) => {
31
- if (type !== "message") {
33
+ if (type !== 'message') {
32
34
  throw Error(
33
35
  `Got an unexpected event type "${type}". Expected "message".`,
34
- );
36
+ )
35
37
  }
36
- listeners.splice(listeners.findIndex((l) => l === listener));
38
+ listeners.splice(listeners.findIndex((l) => l === listener))
37
39
  },
38
40
  postMessage: (data) => {
39
41
  if (!ref.current) {
40
42
  if (
41
- "value" in data &&
43
+ 'value' in data &&
42
44
  data.value === SYMBOL_IGNORING_RPC_RESPONSE_ERROR
43
45
  ) {
44
- return;
46
+ return
45
47
  }
46
- throw Error("Failed to return RPC response to WebView via postMessage");
48
+ throw Error('Failed to return RPC response to WebView via postMessage')
47
49
  }
48
- console.debug("[webview:res]", data);
49
- const dataStr = JSON.stringify(data);
50
+ console.debug('[webview:res]', data)
51
+ const dataStr = JSON.stringify(data)
50
52
  return ref.current.injectJavaScript(`
51
53
  console.debug('[webview:res]', ${dataStr});
52
54
  document.dispatchEvent(new MessageEvent('FarcasterFrameCallback', { data: ${dataStr} }));
53
- `);
55
+ `)
54
56
  },
55
57
  onMessage: (e) => {
56
- const data = JSON.parse(e.nativeEvent.data);
57
- console.debug("[webview:req]", data);
58
- const messageEvent = new MessageEvent(data);
59
- listeners.forEach((l) => {
60
- if (typeof l === "function") {
58
+ const data = JSON.parse(e.nativeEvent.data)
59
+ console.debug('[webview:req]', data)
60
+ const messageEvent = new MessageEvent(data)
61
+ for (const l of listeners) {
62
+ if (typeof l === 'function') {
61
63
  // Actually, messageEvent doesn't satisfy Event interface,
62
64
  // but it satisfies the minimum properties that Comlink's listener requires.
63
- l(messageEvent as unknown as Event);
65
+ l(messageEvent as unknown as Event)
64
66
  } else {
65
- l.handleEvent(messageEvent as unknown as Event);
67
+ l.handleEvent(messageEvent as unknown as Event)
66
68
  }
67
- });
69
+ }
68
70
  },
69
71
  emit: (data) => {
70
72
  if (!ref.current) {
71
- throw Error("Failed to send Event to WebView via postMessage");
73
+ throw Error('Failed to send Event to WebView via postMessage')
72
74
  }
73
- console.debug("[webview:emit]", data);
74
- const dataStr = JSON.stringify(data);
75
+ console.debug('[webview:emit]', data)
76
+ const dataStr = JSON.stringify(data)
75
77
  return ref.current.injectJavaScript(`
76
78
  console.debug('[webview:emit]', ${dataStr});
77
79
  document.dispatchEvent(new MessageEvent('FarcasterFrameEvent', { data: ${dataStr} }));
78
- `);
80
+ `)
79
81
  },
80
82
  emitEthProvider: (event, params) => {
81
83
  if (!ref.current) {
82
84
  throw Error(
83
- "Failed to send EthProvider Event to WebView via postMessage",
84
- );
85
+ 'Failed to send EthProvider Event to WebView via postMessage',
86
+ )
85
87
  }
86
- console.debug("[emit:ethProvider]", event, params);
87
- const wireEvent = { event, params };
88
- const dataStr = JSON.stringify(wireEvent);
88
+ console.debug('[emit:ethProvider]', event, params)
89
+ const wireEvent = { event, params }
90
+ const dataStr = JSON.stringify(wireEvent)
89
91
  return ref.current.injectJavaScript(`
90
92
  console.debug('[webview:emit:ethProvider]', ${dataStr});
91
93
  document.dispatchEvent(new MessageEvent('FarcasterFrameEthProviderEvent', { data: ${dataStr} }));
92
- `);
94
+ `)
93
95
  },
94
- };
96
+ }
95
97
  }
96
98
 
97
99
  /**
@@ -99,6 +101,6 @@ export function createWebViewRpcEndpoint(
99
101
  * Instead, implement our own MessageEvent with the minimum properties required by Comlink implementation.
100
102
  */
101
103
  class MessageEvent {
102
- public origin = "ReactNativeWebView";
104
+ public origin = 'ReactNativeWebView'
103
105
  constructor(public data: unknown) {}
104
106
  }
@@ -1,16 +1,14 @@
1
- import { RefObject, useCallback, useEffect, useRef } from "react";
2
- import type { Provider } from "ox";
3
- import WebView, {
4
- WebViewMessageEvent,
5
- WebViewProps,
6
- } from "react-native-webview";
7
- import type { FrameHost } from "@farcaster/frame-core";
1
+ import type { FrameHost } from '@farcaster/frame-core'
8
2
  import {
9
- exposeToEndpoint,
10
3
  HostEndpoint,
4
+ exposeToEndpoint,
11
5
  useExposeToEndpoint,
12
- } from "@farcaster/frame-host";
13
- import { WebViewEndpoint, createWebViewRpcEndpoint } from "./webview";
6
+ } from '@farcaster/frame-host'
7
+ import type { Provider } from 'ox'
8
+ import { type RefObject, useCallback, useEffect, useRef } from 'react'
9
+ import type WebView from 'react-native-webview'
10
+ import type { WebViewMessageEvent, WebViewProps } from 'react-native-webview'
11
+ import { type WebViewEndpoint, createWebViewRpcEndpoint } from './webview'
14
12
 
15
13
  /**
16
14
  * Returns a handler of RPC message from WebView.
@@ -21,68 +19,68 @@ export function useWebViewRpcAdapter({
21
19
  ethProvider,
22
20
  debug = false,
23
21
  }: {
24
- webViewRef: RefObject<WebView>;
25
- sdk: Omit<FrameHost, "ethProviderRequestV2">;
26
- ethProvider?: Provider.Provider;
27
- debug?: boolean;
22
+ webViewRef: RefObject<WebView>
23
+ sdk: Omit<FrameHost, 'ethProviderRequestV2'>
24
+ ethProvider?: Provider.Provider
25
+ debug?: boolean
28
26
  }) {
29
- const endpointRef = useRef<WebViewEndpoint>();
27
+ const endpointRef = useRef<WebViewEndpoint>()
30
28
 
31
- const onMessage: WebViewProps["onMessage"] = useCallback(
29
+ const onMessage: WebViewProps['onMessage'] = useCallback(
32
30
  (e: WebViewMessageEvent) => {
33
- endpointRef.current?.onMessage(e);
31
+ endpointRef.current?.onMessage(e)
34
32
  },
35
- [endpointRef],
36
- );
33
+ [],
34
+ )
37
35
 
38
36
  useEffect(() => {
39
- const endpoint = createWebViewRpcEndpoint(webViewRef);
40
- endpointRef.current = endpoint;
37
+ const endpoint = createWebViewRpcEndpoint(webViewRef)
38
+ endpointRef.current = endpoint
41
39
 
42
40
  const cleanup = exposeToEndpoint({
43
41
  endpoint,
44
42
  sdk,
45
43
  ethProvider,
46
- frameOrigin: "ReactNativeWebView",
44
+ frameOrigin: 'ReactNativeWebView',
47
45
  debug,
48
- });
46
+ })
49
47
 
50
48
  return () => {
51
- cleanup?.();
52
- endpointRef.current = undefined;
53
- };
54
- }, [webViewRef, sdk, ethProvider]);
49
+ cleanup?.()
50
+ endpointRef.current = undefined
51
+ }
52
+ }, [webViewRef, sdk, ethProvider, debug])
55
53
 
56
54
  return {
57
55
  onMessage,
58
56
  emit: endpointRef.current?.emit,
59
57
  emitEthProvider: endpointRef.current?.emitEthProvider,
60
- };
58
+ }
61
59
  }
62
60
 
63
61
  export function useWebViewRpcEndpoint(webViewRef: RefObject<WebView>) {
64
- const endpointRef = useRef<WebViewEndpoint>();
62
+ const endpointRef = useRef<WebViewEndpoint>()
65
63
 
66
- const onMessage: WebViewProps["onMessage"] = useCallback(
64
+ const onMessage: WebViewProps['onMessage'] = useCallback(
67
65
  (e: WebViewMessageEvent) => {
68
- endpointRef.current?.onMessage(e);
66
+ endpointRef.current?.onMessage(e)
69
67
  },
70
- [endpointRef],
71
- );
68
+ [],
69
+ )
72
70
 
73
71
  useEffect(() => {
74
- const endpoint = createWebViewRpcEndpoint(webViewRef);
75
- endpointRef.current = endpoint;
72
+ const endpoint = createWebViewRpcEndpoint(webViewRef)
73
+ endpointRef.current = endpoint
76
74
 
77
75
  return () => {
78
- endpointRef.current = undefined;
79
- };
80
- }, [webViewRef]);
76
+ endpointRef.current = undefined
77
+ }
78
+ }, [webViewRef])
81
79
 
82
80
  return {
83
81
  endpoint: endpointRef.current,
84
82
  onMessage,
85
- };
83
+ }
86
84
  }
87
85
 
88
86
  export function useExposeWebViewToEndpoint({
@@ -91,16 +89,16 @@ export function useExposeWebViewToEndpoint({
91
89
  ethProvider,
92
90
  debug = false,
93
91
  }: {
94
- endpoint: WebViewEndpoint | undefined;
95
- sdk: Omit<FrameHost, "ethProviderRequestV2">;
96
- ethProvider?: Provider.Provider;
97
- debug?: boolean;
92
+ endpoint: WebViewEndpoint | undefined
93
+ sdk: Omit<FrameHost, 'ethProviderRequestV2'>
94
+ ethProvider?: Provider.Provider
95
+ debug?: boolean
98
96
  }) {
99
97
  useExposeToEndpoint({
100
98
  endpoint,
101
99
  sdk,
102
- frameOrigin: "ReactNativeWebView",
100
+ frameOrigin: 'ReactNativeWebView',
103
101
  ethProvider,
104
102
  debug,
105
- });
103
+ })
106
104
  }