@farcaster/frame-sdk 0.0.37 → 0.0.38

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/types.d.ts CHANGED
@@ -29,6 +29,7 @@ export type EventMap = {
29
29
  export type Emitter = Compute<EventEmitter<EventMap>>;
30
30
  type SetPrimaryButton = (options: SetPrimaryButtonOptions) => Promise<void>;
31
31
  export type FrameSDK = {
32
+ isInMiniApp: () => Promise<boolean>;
32
33
  context: Promise<Context.FrameContext>;
33
34
  actions: {
34
35
  ready: (options?: Partial<Ready.ReadyOptions>) => Promise<void>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@farcaster/frame-sdk",
3
- "version": "0.0.37",
3
+ "version": "0.0.38",
4
4
  "license": "MIT",
5
5
  "repository": {
6
6
  "type": "git",
package/src/sdk.ts CHANGED
@@ -28,9 +28,52 @@ export function createEmitter(): Emitter {
28
28
  }
29
29
 
30
30
  const emitter = createEmitter()
31
+ let cachedIsInMiniAppResult: boolean | null = null
32
+
33
+ /**
34
+ * Determines if the current environment is a MiniApp context.
35
+ *
36
+ * @param timeoutMs - Optional timeout in milliseconds (default: 50)
37
+ * @returns Promise resolving to boolean indicating if in MiniApp context
38
+ */
39
+ async function isInMiniApp(timeoutMs = 50): Promise<boolean> {
40
+ // Return cached result if we've already determined we are in a MiniApp
41
+ if (cachedIsInMiniAppResult === true) {
42
+ return true
43
+ }
44
+
45
+ // Check for SSR environment - definitely not a MiniApp
46
+ if (typeof window === 'undefined') {
47
+ return false
48
+ }
49
+
50
+ // Short-circuit: definitely NOT a MiniApp
51
+ if (!window.ReactNativeWebView && window === window.parent) {
52
+ return false
53
+ }
54
+
55
+ // At this point, we MIGHT be in a MiniApp (iframe or RN WebView)
56
+ // but need to verify by checking for context communication.
57
+ const isInMiniApp = await Promise.race([
58
+ frameHost.context.then((context) => !!context), // Check if context resolves to truthy
59
+ new Promise<boolean>((resolve) => {
60
+ setTimeout(() => resolve(false), timeoutMs) // Timeout resolves to false
61
+ }),
62
+ ]).catch(() => {
63
+ return false
64
+ })
65
+
66
+ // Cache the result ONLY if true (we are confirmed to be in a MiniApp)
67
+ if (isInMiniApp) {
68
+ cachedIsInMiniAppResult = true
69
+ }
70
+
71
+ return isInMiniApp
72
+ }
31
73
 
32
74
  export const sdk: FrameSDK = {
33
75
  ...emitter,
76
+ isInMiniApp,
34
77
  context: frameHost.context,
35
78
  actions: {
36
79
  setPrimaryButton: frameHost.setPrimaryButton.bind(frameHost),
package/src/types.ts CHANGED
@@ -51,6 +51,7 @@ export type Emitter = Compute<EventEmitter<EventMap>>
51
51
  type SetPrimaryButton = (options: SetPrimaryButtonOptions) => Promise<void>
52
52
 
53
53
  export type FrameSDK = {
54
+ isInMiniApp: () => Promise<boolean>
54
55
  context: Promise<Context.FrameContext>
55
56
  actions: {
56
57
  ready: (options?: Partial<Ready.ReadyOptions>) => Promise<void>