@farcaster/miniapp-sdk 0.0.0-canary-20250630212339
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/LICENSE +21 -0
- package/README.md +24 -0
- package/dist/back.d.ts +15 -0
- package/dist/back.js +88 -0
- package/dist/endpoint.d.ts +2 -0
- package/dist/endpoint.js +32 -0
- package/dist/ethereumProvider.d.ts +3 -0
- package/dist/ethereumProvider.js +132 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +7 -0
- package/dist/index.min.js +190 -0
- package/dist/index.min.js.map +7 -0
- package/dist/miniAppHost.d.ts +3 -0
- package/dist/miniAppHost.js +5 -0
- package/dist/quickAuth.d.ts +42 -0
- package/dist/quickAuth.js +74 -0
- package/dist/sdk.d.ts +2 -0
- package/dist/sdk.js +182 -0
- package/dist/sdkEmitter.d.ts +15 -0
- package/dist/sdkEmitter.js +23 -0
- package/dist/solanaProvider.d.ts +3 -0
- package/dist/solanaProvider.js +19 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/dist/types.d.ts +74 -0
- package/dist/types.js +1 -0
- package/package.json +36 -0
- package/src/back.ts +143 -0
- package/src/endpoint.ts +34 -0
- package/src/ethereumProvider.ts +183 -0
- package/src/index.ts +11 -0
- package/src/miniAppHost.ts +8 -0
- package/src/navigation.d.ts +191 -0
- package/src/quickAuth.ts +148 -0
- package/src/sdk.ts +191 -0
- package/src/sdkEmitter.ts +27 -0
- package/src/solanaProvider.ts +33 -0
- package/src/types.ts +107 -0
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import type { AddMiniApp, ComposeCast, Context, GetCapabilities, GetChains, ImpactOccurred, MiniAppNotificationDetails, NotificationOccurred, Ready, SelectionChanged, SendToken, SetPrimaryButtonOptions, SignIn, SolanaWalletProvider, SwapToken, ViewCast, ViewProfile, ViewToken } from '@farcaster/miniapp-core';
|
|
2
|
+
import type { EventEmitter } from 'eventemitter3';
|
|
3
|
+
import type * as Provider from 'ox/Provider';
|
|
4
|
+
import type { Back } from './back.ts';
|
|
5
|
+
import type { QuickAuth } from './quickAuth.ts';
|
|
6
|
+
declare global {
|
|
7
|
+
interface Window {
|
|
8
|
+
ReactNativeWebView: {
|
|
9
|
+
postMessage: (message: string) => void;
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
/** Combines members of an intersection into a readable type. */
|
|
14
|
+
type Compute<type> = {
|
|
15
|
+
[key in keyof type]: type[key];
|
|
16
|
+
} & unknown;
|
|
17
|
+
export type EventMap = {
|
|
18
|
+
primaryButtonClicked: () => void;
|
|
19
|
+
miniAppAdded: ({ notificationDetails, }: {
|
|
20
|
+
notificationDetails?: MiniAppNotificationDetails;
|
|
21
|
+
}) => void;
|
|
22
|
+
miniAppAddRejected: ({ reason, }: {
|
|
23
|
+
reason: AddMiniApp.AddMiniAppRejectedReason;
|
|
24
|
+
}) => void;
|
|
25
|
+
miniAppRemoved: () => void;
|
|
26
|
+
notificationsEnabled: ({ notificationDetails, }: {
|
|
27
|
+
notificationDetails: MiniAppNotificationDetails;
|
|
28
|
+
}) => void;
|
|
29
|
+
notificationsDisabled: () => void;
|
|
30
|
+
backNavigationTriggered: () => void;
|
|
31
|
+
};
|
|
32
|
+
export type Emitter = Compute<EventEmitter<EventMap>>;
|
|
33
|
+
type SetPrimaryButton = (options: SetPrimaryButtonOptions) => Promise<void>;
|
|
34
|
+
export type MiniAppSDK = {
|
|
35
|
+
getCapabilities: GetCapabilities;
|
|
36
|
+
getChains: GetChains;
|
|
37
|
+
isInMiniApp: () => Promise<boolean>;
|
|
38
|
+
context: Promise<Context.MiniAppContext>;
|
|
39
|
+
back: Back;
|
|
40
|
+
quickAuth: QuickAuth;
|
|
41
|
+
actions: {
|
|
42
|
+
ready: (options?: Partial<Ready.ReadyOptions>) => Promise<void>;
|
|
43
|
+
openUrl: (url: string) => Promise<void>;
|
|
44
|
+
close: () => Promise<void>;
|
|
45
|
+
setPrimaryButton: SetPrimaryButton;
|
|
46
|
+
addFrame: AddMiniApp.AddMiniApp;
|
|
47
|
+
addMiniApp: AddMiniApp.AddMiniApp;
|
|
48
|
+
signIn: SignIn.SignIn;
|
|
49
|
+
viewCast: ViewCast.ViewCast;
|
|
50
|
+
viewProfile: ViewProfile.ViewProfile;
|
|
51
|
+
composeCast: <close extends boolean | undefined = undefined>(options?: ComposeCast.Options<close>) => Promise<ComposeCast.Result<close>>;
|
|
52
|
+
viewToken: ViewToken.ViewToken;
|
|
53
|
+
sendToken: SendToken.SendToken;
|
|
54
|
+
swapToken: SwapToken.SwapToken;
|
|
55
|
+
};
|
|
56
|
+
experimental: {
|
|
57
|
+
getSolanaProvider: () => Promise<SolanaWalletProvider | undefined>;
|
|
58
|
+
/**
|
|
59
|
+
* @deprecated - use `sdk.quickAuth.getToken`
|
|
60
|
+
*/
|
|
61
|
+
quickAuth: QuickAuth['getToken'];
|
|
62
|
+
};
|
|
63
|
+
wallet: {
|
|
64
|
+
ethProvider: Provider.Provider;
|
|
65
|
+
getEthereumProvider: () => Promise<Provider.Provider | undefined>;
|
|
66
|
+
getSolanaProvider: () => Promise<SolanaWalletProvider | undefined>;
|
|
67
|
+
};
|
|
68
|
+
haptics: {
|
|
69
|
+
impactOccurred: ImpactOccurred;
|
|
70
|
+
notificationOccurred: NotificationOccurred;
|
|
71
|
+
selectionChanged: SelectionChanged;
|
|
72
|
+
};
|
|
73
|
+
} & Emitter;
|
|
74
|
+
export {};
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@farcaster/miniapp-sdk",
|
|
3
|
+
"version": "0.0.0-canary-20250630212339",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "https://github.com/farcasterxyz/frames.git",
|
|
8
|
+
"directory": "packages/miniapp-sdk"
|
|
9
|
+
},
|
|
10
|
+
"main": "dist/index.js",
|
|
11
|
+
"files": [
|
|
12
|
+
"dist",
|
|
13
|
+
"src"
|
|
14
|
+
],
|
|
15
|
+
"devDependencies": {
|
|
16
|
+
"esbuild": "^0.25.0",
|
|
17
|
+
"mipd": "^0.0.7",
|
|
18
|
+
"typescript": "^5.8.3",
|
|
19
|
+
"@farcaster/tsconfig": "0.0.0-canary-20250630212339"
|
|
20
|
+
},
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"@farcaster/quick-auth": "^0.0.6",
|
|
23
|
+
"comlink": "^4.4.2",
|
|
24
|
+
"eventemitter3": "^5.0.1",
|
|
25
|
+
"ox": "^0.4.4",
|
|
26
|
+
"@farcaster/miniapp-core": "0.0.0-canary-20250630212339"
|
|
27
|
+
},
|
|
28
|
+
"scripts": {
|
|
29
|
+
"clean": "rm -rf dist",
|
|
30
|
+
"prebuild": "npm run clean",
|
|
31
|
+
"build": "pnpm build:cjs && pnpm build:umd",
|
|
32
|
+
"build:cjs": "tsc",
|
|
33
|
+
"build:umd": "node scripts/build.js",
|
|
34
|
+
"typecheck": "tsc --noEmit"
|
|
35
|
+
}
|
|
36
|
+
}
|
package/src/back.ts
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import type { WireMiniAppHost } from '@farcaster/miniapp-core'
|
|
2
|
+
import type { Remote } from 'comlink'
|
|
3
|
+
import type { Emitter } from './types.ts'
|
|
4
|
+
|
|
5
|
+
export type Back = {
|
|
6
|
+
visible: boolean
|
|
7
|
+
show: () => Promise<void>
|
|
8
|
+
hide: () => Promise<void>
|
|
9
|
+
onback: (() => unknown) | null
|
|
10
|
+
enableWebNavigation: () => Promise<void>
|
|
11
|
+
disableWebNavigation: () => Promise<void>
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export const createBack: (options: {
|
|
15
|
+
emitter: Emitter
|
|
16
|
+
miniAppHost: Remote<WireMiniAppHost>
|
|
17
|
+
}) => Back = ({ miniAppHost, emitter }) => {
|
|
18
|
+
let teardownWebNavigation: (() => void) | undefined = undefined
|
|
19
|
+
let backCb: (() => unknown) | null = null
|
|
20
|
+
|
|
21
|
+
return {
|
|
22
|
+
visible: false,
|
|
23
|
+
get onback() {
|
|
24
|
+
return backCb
|
|
25
|
+
},
|
|
26
|
+
set onback(cb) {
|
|
27
|
+
if (backCb) {
|
|
28
|
+
emitter.removeListener('backNavigationTriggered', backCb)
|
|
29
|
+
}
|
|
30
|
+
backCb = cb
|
|
31
|
+
if (cb) {
|
|
32
|
+
emitter.addListener('backNavigationTriggered', cb)
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
async show() {
|
|
36
|
+
await miniAppHost.updateBackState({
|
|
37
|
+
visible: true,
|
|
38
|
+
})
|
|
39
|
+
this.visible = true
|
|
40
|
+
},
|
|
41
|
+
async hide() {
|
|
42
|
+
await miniAppHost.updateBackState({
|
|
43
|
+
visible: false,
|
|
44
|
+
})
|
|
45
|
+
this.visible = false
|
|
46
|
+
},
|
|
47
|
+
async enableWebNavigation() {
|
|
48
|
+
teardownWebNavigation = setupWebBack({
|
|
49
|
+
back: this,
|
|
50
|
+
emitter,
|
|
51
|
+
})
|
|
52
|
+
},
|
|
53
|
+
async disableWebNavigation() {
|
|
54
|
+
teardownWebNavigation?.()
|
|
55
|
+
teardownWebNavigation = undefined
|
|
56
|
+
},
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function setupWebBack({
|
|
61
|
+
emitter,
|
|
62
|
+
back,
|
|
63
|
+
}: {
|
|
64
|
+
emitter: Emitter
|
|
65
|
+
back: Back
|
|
66
|
+
}) {
|
|
67
|
+
const navigation = getWebNavigation()
|
|
68
|
+
if (navigation) {
|
|
69
|
+
return setupNavigationApi({ emitter, back, navigation })
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if (typeof window !== 'undefined') {
|
|
73
|
+
return setupFallback({ emitter, back, window })
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function getWebNavigation(): Navigation | undefined {
|
|
78
|
+
if (typeof window !== 'undefined' && window.navigation !== undefined) {
|
|
79
|
+
return window.navigation
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function setupNavigationApi({
|
|
84
|
+
emitter,
|
|
85
|
+
back,
|
|
86
|
+
navigation,
|
|
87
|
+
}: {
|
|
88
|
+
emitter: Emitter
|
|
89
|
+
back: Back
|
|
90
|
+
navigation: Navigation
|
|
91
|
+
}) {
|
|
92
|
+
function handleNavigateSuccess() {
|
|
93
|
+
if (navigation.canGoBack) {
|
|
94
|
+
back.show()
|
|
95
|
+
} else {
|
|
96
|
+
back.hide()
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function handleBackNavigationTriggered() {
|
|
101
|
+
if (back.visible && navigation.canGoBack) {
|
|
102
|
+
navigation.back()
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
navigation.addEventListener('navigatesuccess', handleNavigateSuccess)
|
|
107
|
+
emitter.addListener('backNavigationTriggered', handleBackNavigationTriggered)
|
|
108
|
+
|
|
109
|
+
return () => {
|
|
110
|
+
navigation.removeEventListener('navigatesuccess', handleNavigateSuccess)
|
|
111
|
+
emitter.removeListener(
|
|
112
|
+
'backNavigationTriggered',
|
|
113
|
+
handleBackNavigationTriggered,
|
|
114
|
+
)
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function setupFallback({
|
|
119
|
+
emitter,
|
|
120
|
+
back,
|
|
121
|
+
window,
|
|
122
|
+
}: {
|
|
123
|
+
emitter: Emitter
|
|
124
|
+
back: Back
|
|
125
|
+
window: Window
|
|
126
|
+
}) {
|
|
127
|
+
back.show()
|
|
128
|
+
|
|
129
|
+
function handleBackNavigationTriggered() {
|
|
130
|
+
if (back.visible) {
|
|
131
|
+
window.history.back()
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
emitter.addListener('backNavigationTriggered', handleBackNavigationTriggered)
|
|
136
|
+
|
|
137
|
+
return () => {
|
|
138
|
+
emitter.removeListener(
|
|
139
|
+
'backNavigationTriggered',
|
|
140
|
+
handleBackNavigationTriggered,
|
|
141
|
+
)
|
|
142
|
+
}
|
|
143
|
+
}
|
package/src/endpoint.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { type Endpoint, windowEndpoint } from 'comlink'
|
|
2
|
+
|
|
3
|
+
const mockEndpoint: Endpoint = {
|
|
4
|
+
postMessage() {
|
|
5
|
+
// noop
|
|
6
|
+
},
|
|
7
|
+
addEventListener: () => {
|
|
8
|
+
// noop
|
|
9
|
+
},
|
|
10
|
+
removeEventListener: () => {
|
|
11
|
+
// noop
|
|
12
|
+
},
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const webViewEndpoint: Endpoint = {
|
|
16
|
+
postMessage: (data: unknown) => {
|
|
17
|
+
console.debug('[webview:req]', data)
|
|
18
|
+
window.ReactNativeWebView.postMessage(JSON.stringify(data))
|
|
19
|
+
},
|
|
20
|
+
addEventListener: (_, listener, ...args) => {
|
|
21
|
+
document.addEventListener('FarcasterFrameCallback', listener, ...args)
|
|
22
|
+
},
|
|
23
|
+
removeEventListener: (_, listener) => {
|
|
24
|
+
document.removeEventListener('FarcasterFrameCallback', listener)
|
|
25
|
+
},
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export const endpoint = (() => {
|
|
29
|
+
// No actions are actually gonna take place during SSR, thus it's safe to return mocked endpoint
|
|
30
|
+
if (typeof window === 'undefined') return mockEndpoint
|
|
31
|
+
return window?.ReactNativeWebView
|
|
32
|
+
? webViewEndpoint
|
|
33
|
+
: windowEndpoint(window?.parent ?? window)
|
|
34
|
+
})()
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
EthProviderWireEvent,
|
|
3
|
+
MiniAppClientEvent,
|
|
4
|
+
MiniAppHostCapability,
|
|
5
|
+
} from '@farcaster/miniapp-core'
|
|
6
|
+
import type {
|
|
7
|
+
AnnounceProviderParameters,
|
|
8
|
+
AnnounceProviderReturnType,
|
|
9
|
+
EIP1193Provider,
|
|
10
|
+
EIP6963ProviderDetail,
|
|
11
|
+
} from 'mipd'
|
|
12
|
+
import * as Provider from 'ox/Provider'
|
|
13
|
+
import * as RpcRequest from 'ox/RpcRequest'
|
|
14
|
+
import * as RpcResponse from 'ox/RpcResponse'
|
|
15
|
+
import { miniAppHost } from './miniAppHost.ts'
|
|
16
|
+
|
|
17
|
+
const emitter = Provider.createEmitter()
|
|
18
|
+
const store = RpcRequest.createStore()
|
|
19
|
+
|
|
20
|
+
type GenericProviderRpcError = {
|
|
21
|
+
code: number
|
|
22
|
+
details?: string
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function toProviderRpcError({
|
|
26
|
+
code,
|
|
27
|
+
details,
|
|
28
|
+
}: GenericProviderRpcError): Provider.ProviderRpcError {
|
|
29
|
+
switch (code) {
|
|
30
|
+
case 4001:
|
|
31
|
+
return new Provider.UserRejectedRequestError()
|
|
32
|
+
case 4100:
|
|
33
|
+
return new Provider.UnauthorizedError()
|
|
34
|
+
case 4200:
|
|
35
|
+
return new Provider.UnsupportedMethodError()
|
|
36
|
+
case 4900:
|
|
37
|
+
return new Provider.DisconnectedError()
|
|
38
|
+
case 4901:
|
|
39
|
+
return new Provider.ChainDisconnectedError()
|
|
40
|
+
default:
|
|
41
|
+
return new Provider.ProviderRpcError(
|
|
42
|
+
code,
|
|
43
|
+
details ?? 'Unknown provider RPC error',
|
|
44
|
+
)
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export const ethereumProvider: Provider.Provider = Provider.from({
|
|
49
|
+
...emitter,
|
|
50
|
+
async request(args) {
|
|
51
|
+
// @ts-expect-error
|
|
52
|
+
const request = store.prepare(args)
|
|
53
|
+
|
|
54
|
+
try {
|
|
55
|
+
const response = await miniAppHost
|
|
56
|
+
.ethProviderRequestV2(request)
|
|
57
|
+
.then((res) => RpcResponse.parse(res, { request, raw: true }))
|
|
58
|
+
|
|
59
|
+
if (response.error) {
|
|
60
|
+
throw toProviderRpcError(response.error)
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return response.result
|
|
64
|
+
} catch (e) {
|
|
65
|
+
// ethProviderRequestV2 not supported, fall back to v1
|
|
66
|
+
if (
|
|
67
|
+
e instanceof Error &&
|
|
68
|
+
e.message.match(/cannot read property 'apply'/i)
|
|
69
|
+
) {
|
|
70
|
+
return await miniAppHost.ethProviderRequest(request)
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if (
|
|
74
|
+
e instanceof Provider.ProviderRpcError ||
|
|
75
|
+
e instanceof RpcResponse.BaseError
|
|
76
|
+
) {
|
|
77
|
+
throw e
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
throw new RpcResponse.InternalError({
|
|
81
|
+
message: e instanceof Error ? e.message : undefined,
|
|
82
|
+
})
|
|
83
|
+
}
|
|
84
|
+
},
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
export async function getEthereumProvider(): Promise<
|
|
88
|
+
Provider.Provider | undefined
|
|
89
|
+
> {
|
|
90
|
+
try {
|
|
91
|
+
const capabilities = await miniAppHost.getCapabilities()
|
|
92
|
+
if (
|
|
93
|
+
!capabilities.includes('wallet.getEthereumProvider') &&
|
|
94
|
+
!capabilities.includes('wallet.getEvmProvider' as MiniAppHostCapability)
|
|
95
|
+
) {
|
|
96
|
+
// We used getEvmProvider for a short period before getEthereumProvider.
|
|
97
|
+
// In case we're talking to an old host, we check the old key.
|
|
98
|
+
return undefined
|
|
99
|
+
}
|
|
100
|
+
return ethereumProvider
|
|
101
|
+
} catch {
|
|
102
|
+
// If this is an old frame host that doesn't support getCapabilities,
|
|
103
|
+
// getEthereumProvider will assume that it's supported
|
|
104
|
+
return ethereumProvider
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function announceEvmProvider(
|
|
109
|
+
detail: AnnounceProviderParameters,
|
|
110
|
+
): AnnounceProviderReturnType {
|
|
111
|
+
const event: CustomEvent<EIP6963ProviderDetail> = new CustomEvent(
|
|
112
|
+
'eip6963:announceProvider',
|
|
113
|
+
{ detail: Object.freeze(detail) },
|
|
114
|
+
)
|
|
115
|
+
|
|
116
|
+
window.dispatchEvent(event)
|
|
117
|
+
|
|
118
|
+
const handler = () => window.dispatchEvent(event)
|
|
119
|
+
window.addEventListener('eip6963:requestProvider', handler)
|
|
120
|
+
return () => window.removeEventListener('eip6963:requestProvider', handler)
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// Required to pass SSR
|
|
124
|
+
if (typeof document !== 'undefined') {
|
|
125
|
+
// forward eip6963:requestProvider events to the host
|
|
126
|
+
document.addEventListener('eip6963:requestProvider', () => {
|
|
127
|
+
miniAppHost.eip6963RequestProvider()
|
|
128
|
+
})
|
|
129
|
+
|
|
130
|
+
// react native webview events
|
|
131
|
+
document.addEventListener('FarcasterFrameEthProviderEvent', (event) => {
|
|
132
|
+
if (event instanceof MessageEvent) {
|
|
133
|
+
const ethProviderEvent = event.data as EthProviderWireEvent
|
|
134
|
+
// @ts-expect-error
|
|
135
|
+
emitter.emit(ethProviderEvent.event, ...ethProviderEvent.params)
|
|
136
|
+
}
|
|
137
|
+
})
|
|
138
|
+
|
|
139
|
+
document.addEventListener('FarcasterFrameEvent', (event) => {
|
|
140
|
+
if (event instanceof MessageEvent) {
|
|
141
|
+
const miniAppEvent = event.data as MiniAppClientEvent
|
|
142
|
+
if (miniAppEvent.event === 'eip6963:announceProvider') {
|
|
143
|
+
announceEvmProvider({
|
|
144
|
+
info: miniAppEvent.info,
|
|
145
|
+
provider: ethereumProvider as EIP1193Provider,
|
|
146
|
+
})
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
})
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// Required to pass SSR
|
|
153
|
+
if (typeof window !== 'undefined') {
|
|
154
|
+
// forward eip6963:requestProvider events to the host
|
|
155
|
+
window.addEventListener('eip6963:requestProvider', () => {
|
|
156
|
+
miniAppHost.eip6963RequestProvider()
|
|
157
|
+
})
|
|
158
|
+
|
|
159
|
+
// web events
|
|
160
|
+
window.addEventListener('message', (event) => {
|
|
161
|
+
if (event instanceof MessageEvent) {
|
|
162
|
+
if (event.data.type === 'frameEthProviderEvent') {
|
|
163
|
+
const ethProviderEvent = event.data as EthProviderWireEvent
|
|
164
|
+
// @ts-expect-error
|
|
165
|
+
emitter.emit(ethProviderEvent.event, ...ethProviderEvent.params)
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
})
|
|
169
|
+
|
|
170
|
+
window.addEventListener('message', (event) => {
|
|
171
|
+
if (event instanceof MessageEvent) {
|
|
172
|
+
if (event.data.type === 'frameEvent') {
|
|
173
|
+
const miniAppEvent = event.data.event as MiniAppClientEvent
|
|
174
|
+
if (miniAppEvent.event === 'eip6963:announceProvider') {
|
|
175
|
+
announceEvmProvider({
|
|
176
|
+
info: miniAppEvent.info,
|
|
177
|
+
provider: ethereumProvider as EIP1193Provider,
|
|
178
|
+
})
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
})
|
|
183
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { WireMiniAppHost } from '@farcaster/miniapp-core'
|
|
2
|
+
import { wrap } from 'comlink'
|
|
3
|
+
import { endpoint } from './endpoint.ts'
|
|
4
|
+
|
|
5
|
+
export const miniAppHost = wrap<WireMiniAppHost>(endpoint)
|
|
6
|
+
|
|
7
|
+
// Backward compatibility
|
|
8
|
+
export const frameHost = miniAppHost
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
interface Window {
|
|
2
|
+
readonly navigation?: Navigation
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
interface NavigationEventMap {
|
|
6
|
+
navigate: NavigateEvent
|
|
7
|
+
navigatesuccess: Event
|
|
8
|
+
navigateerror: ErrorEvent
|
|
9
|
+
currententrychange: NavigationCurrentEntryChangeEvent
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
interface NavigationResult {
|
|
13
|
+
committed: Promise<NavigationHistoryEntry>
|
|
14
|
+
finished: Promise<NavigationHistoryEntry>
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
declare class Navigation extends EventTarget {
|
|
18
|
+
entries(): NavigationHistoryEntry[]
|
|
19
|
+
readonly currentEntry: NavigationHistoryEntry | null
|
|
20
|
+
updateCurrentEntry(options: NavigationUpdateCurrentEntryOptions): void
|
|
21
|
+
readonly transition: NavigationTransition | null
|
|
22
|
+
|
|
23
|
+
readonly canGoBack: boolean
|
|
24
|
+
readonly canGoForward: boolean
|
|
25
|
+
|
|
26
|
+
navigate(url: string, options?: NavigationNavigateOptions): NavigationResult
|
|
27
|
+
reload(options?: NavigationReloadOptions): NavigationResult
|
|
28
|
+
|
|
29
|
+
traverseTo(key: string, options?: NavigationOptions): NavigationResult
|
|
30
|
+
back(options?: NavigationOptions): NavigationResult
|
|
31
|
+
forward(options?: NavigationOptions): NavigationResult
|
|
32
|
+
|
|
33
|
+
onnavigate: ((this: Navigation, ev: NavigateEvent) => any) | null
|
|
34
|
+
onnavigatesuccess: ((this: Navigation, ev: Event) => any) | null
|
|
35
|
+
onnavigateerror: ((this: Navigation, ev: ErrorEvent) => any) | null
|
|
36
|
+
oncurrententrychange:
|
|
37
|
+
| ((this: Navigation, ev: NavigationCurrentEntryChangeEvent) => any)
|
|
38
|
+
| null
|
|
39
|
+
|
|
40
|
+
addEventListener<K extends keyof NavigationEventMap>(
|
|
41
|
+
type: K,
|
|
42
|
+
listener: (this: Navigation, ev: NavigationEventMap[K]) => any,
|
|
43
|
+
options?: boolean | AddEventListenerOptions,
|
|
44
|
+
): void
|
|
45
|
+
addEventListener(
|
|
46
|
+
type: string,
|
|
47
|
+
listener: EventListenerOrEventListenerObject,
|
|
48
|
+
options?: boolean | AddEventListenerOptions,
|
|
49
|
+
): void
|
|
50
|
+
removeEventListener<K extends keyof NavigationEventMap>(
|
|
51
|
+
type: K,
|
|
52
|
+
listener: (this: Navigation, ev: NavigationEventMap[K]) => any,
|
|
53
|
+
options?: boolean | EventListenerOptions,
|
|
54
|
+
): void
|
|
55
|
+
removeEventListener(
|
|
56
|
+
type: string,
|
|
57
|
+
listener: EventListenerOrEventListenerObject,
|
|
58
|
+
options?: boolean | EventListenerOptions,
|
|
59
|
+
): void
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
declare class NavigationTransition {
|
|
63
|
+
readonly navigationType: NavigationTypeString
|
|
64
|
+
readonly from: NavigationHistoryEntry
|
|
65
|
+
readonly finished: Promise<void>
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
interface NavigationHistoryEntryEventMap {
|
|
69
|
+
dispose: Event
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
interface NavigationHistoryEntry extends EventTarget {
|
|
73
|
+
readonly key: string
|
|
74
|
+
readonly id: string
|
|
75
|
+
readonly url: string | null
|
|
76
|
+
readonly index: number
|
|
77
|
+
readonly sameDocument: boolean
|
|
78
|
+
|
|
79
|
+
getState(): unknown
|
|
80
|
+
|
|
81
|
+
ondispose: ((this: NavigationHistoryEntry, ev: Event) => any) | null
|
|
82
|
+
|
|
83
|
+
addEventListener<K extends keyof NavigationHistoryEntryEventMap>(
|
|
84
|
+
type: K,
|
|
85
|
+
listener: (
|
|
86
|
+
this: NavigationHistoryEntry,
|
|
87
|
+
ev: NavigationHistoryEntryEventMap[K],
|
|
88
|
+
) => any,
|
|
89
|
+
options?: boolean | AddEventListenerOptions,
|
|
90
|
+
): void
|
|
91
|
+
addEventListener(
|
|
92
|
+
type: string,
|
|
93
|
+
listener: EventListenerOrEventListenerObject,
|
|
94
|
+
options?: boolean | AddEventListenerOptions,
|
|
95
|
+
): void
|
|
96
|
+
removeEventListener<K extends keyof NavigationHistoryEntryEventMap>(
|
|
97
|
+
type: K,
|
|
98
|
+
listener: (
|
|
99
|
+
this: NavigationHistoryEntry,
|
|
100
|
+
ev: NavigationHistoryEntryEventMap[K],
|
|
101
|
+
) => any,
|
|
102
|
+
options?: boolean | EventListenerOptions,
|
|
103
|
+
): void
|
|
104
|
+
removeEventListener(
|
|
105
|
+
type: string,
|
|
106
|
+
listener: EventListenerOrEventListenerObject,
|
|
107
|
+
options?: boolean | EventListenerOptions,
|
|
108
|
+
): void
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
declare let NavigationHistoryEntry: {
|
|
112
|
+
prototype: NavigationHistoryEntry
|
|
113
|
+
new (): NavigationHistoryEntry
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
type NavigationTypeString = 'reload' | 'push' | 'replace' | 'traverse'
|
|
117
|
+
|
|
118
|
+
interface NavigationUpdateCurrentEntryOptions {
|
|
119
|
+
state: unknown
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
interface NavigationOptions {
|
|
123
|
+
info?: unknown
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
interface NavigationNavigateOptions extends NavigationOptions {
|
|
127
|
+
state?: unknown
|
|
128
|
+
history?: 'auto' | 'push' | 'replace'
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
interface NavigationReloadOptions extends NavigationOptions {
|
|
132
|
+
state?: unknown
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
declare class NavigationCurrentEntryChangeEvent extends Event {
|
|
136
|
+
constructor(type: string, eventInit?: NavigationCurrentEntryChangeEventInit)
|
|
137
|
+
|
|
138
|
+
readonly navigationType: NavigationTypeString | null
|
|
139
|
+
readonly from: NavigationHistoryEntry
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
interface NavigationCurrentEntryChangeEventInit extends EventInit {
|
|
143
|
+
navigationType?: NavigationTypeString | null
|
|
144
|
+
from: NavigationHistoryEntry
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
declare class NavigateEvent extends Event {
|
|
148
|
+
constructor(type: string, eventInit?: NavigateEventInit)
|
|
149
|
+
|
|
150
|
+
readonly navigationType: NavigationTypeString
|
|
151
|
+
readonly canIntercept: boolean
|
|
152
|
+
readonly userInitiated: boolean
|
|
153
|
+
readonly hashChange: boolean
|
|
154
|
+
readonly hasUAVisualTransition: boolean
|
|
155
|
+
readonly destination: NavigationDestination
|
|
156
|
+
readonly signal: AbortSignal
|
|
157
|
+
readonly formData: FormData | null
|
|
158
|
+
readonly downloadRequest: string | null
|
|
159
|
+
readonly info?: unknown
|
|
160
|
+
|
|
161
|
+
intercept(options?: NavigationInterceptOptions): void
|
|
162
|
+
scroll(): void
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
interface NavigateEventInit extends EventInit {
|
|
166
|
+
navigationType?: NavigationTypeString
|
|
167
|
+
canIntercept?: boolean
|
|
168
|
+
userInitiated?: boolean
|
|
169
|
+
hashChange?: boolean
|
|
170
|
+
destination: NavigationDestination
|
|
171
|
+
signal: AbortSignal
|
|
172
|
+
formData?: FormData | null
|
|
173
|
+
downloadRequest?: string | null
|
|
174
|
+
info?: unknown
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
interface NavigationInterceptOptions {
|
|
178
|
+
handler?: () => Promise<void>
|
|
179
|
+
focusReset?: 'after-transition' | 'manual'
|
|
180
|
+
scroll?: 'after-transition' | 'manual'
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
declare class NavigationDestination {
|
|
184
|
+
readonly url: string
|
|
185
|
+
readonly key: string | null
|
|
186
|
+
readonly id: string | null
|
|
187
|
+
readonly index: number
|
|
188
|
+
readonly sameDocument: boolean
|
|
189
|
+
|
|
190
|
+
getState(): unknown
|
|
191
|
+
}
|