@idolchat/miniapp-sdk 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +76 -2
- package/dist/host.mjs +225 -0
- package/dist/index.mjs +1295 -0
- package/dist/miniapp-sdk.js +16 -0
- package/dist/types/client.d.ts +174 -0
- package/dist/types/core.d.ts +46 -0
- package/dist/types/host/postmessage-host.d.ts +56 -0
- package/dist/types/index.d.ts +18 -0
- package/dist/types/methods.d.ts +34 -0
- package/dist/types/modules/app.d.ts +18 -0
- package/dist/types/modules/device.d.ts +37 -0
- package/dist/types/modules/links.d.ts +42 -0
- package/dist/types/modules/platform.d.ts +52 -0
- package/dist/types/modules/storage.d.ts +13 -0
- package/dist/types/modules/ui.d.ts +49 -0
- package/dist/types/protocol.d.ts +87 -0
- package/dist/types/share-image.d.ts +17 -0
- package/dist/types/transport/android.d.ts +10 -0
- package/dist/types/transport/detect.d.ts +28 -0
- package/dist/types/transport/ios.d.ts +16 -0
- package/dist/types/transport/mock.d.ts +75 -0
- package/dist/types/transport/native.d.ts +12 -0
- package/dist/types/transport/postmessage.d.ts +4 -0
- package/dist/types/transport/sha256.d.ts +2 -0
- package/dist/types/transport/types.d.ts +8 -0
- package/dist/types/validate.d.ts +29 -0
- package/global.d.ts +16 -0
- package/package.json +34 -3
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/** Wire protocol shared by the app-side SDK and every host. See the protocol. */
|
|
2
|
+
export declare const PROTOCOL_VERSION = 1;
|
|
3
|
+
export declare const ERROR_CODES: readonly ["METHOD_NOT_SUPPORTED", "INVALID_PARAMS", "TIMEOUT", "QUOTA_EXCEEDED", "PERMISSION_DENIED", "CANCELLED", "INTERNAL_ERROR"];
|
|
4
|
+
export type ErrorCode = (typeof ERROR_CODES)[number];
|
|
5
|
+
export interface WireError {
|
|
6
|
+
code: ErrorCode | (string & {});
|
|
7
|
+
message: string;
|
|
8
|
+
}
|
|
9
|
+
/** App → host. */
|
|
10
|
+
export interface CallMessage {
|
|
11
|
+
v: 1;
|
|
12
|
+
id: string;
|
|
13
|
+
method: string;
|
|
14
|
+
params?: unknown;
|
|
15
|
+
gesture: boolean;
|
|
16
|
+
}
|
|
17
|
+
/** Host → app. */
|
|
18
|
+
export type ResultMessage = {
|
|
19
|
+
v: 1;
|
|
20
|
+
type: "result";
|
|
21
|
+
id: string;
|
|
22
|
+
ok: true;
|
|
23
|
+
data?: unknown;
|
|
24
|
+
} | {
|
|
25
|
+
v: 1;
|
|
26
|
+
type: "result";
|
|
27
|
+
id: string;
|
|
28
|
+
ok: false;
|
|
29
|
+
error: WireError;
|
|
30
|
+
};
|
|
31
|
+
export interface EventMessage {
|
|
32
|
+
v: 1;
|
|
33
|
+
type: "event";
|
|
34
|
+
name: string;
|
|
35
|
+
data?: unknown;
|
|
36
|
+
}
|
|
37
|
+
export interface RequestMessage {
|
|
38
|
+
v: 1;
|
|
39
|
+
type: "request";
|
|
40
|
+
id: string;
|
|
41
|
+
name: string;
|
|
42
|
+
data?: unknown;
|
|
43
|
+
timeoutMs: number;
|
|
44
|
+
}
|
|
45
|
+
export type HostMessage = ResultMessage | EventMessage | RequestMessage;
|
|
46
|
+
/** Params of the `_reply` call that answers a host request. */
|
|
47
|
+
export type ReplyParams = {
|
|
48
|
+
id: string;
|
|
49
|
+
ok: true;
|
|
50
|
+
data?: unknown;
|
|
51
|
+
} | {
|
|
52
|
+
id: string;
|
|
53
|
+
ok: false;
|
|
54
|
+
error: WireError;
|
|
55
|
+
};
|
|
56
|
+
export interface MiniAppUser {
|
|
57
|
+
id: string;
|
|
58
|
+
displayName?: string;
|
|
59
|
+
/** A `data:image/…` URL (protocol §9). */
|
|
60
|
+
avatarUrl?: string;
|
|
61
|
+
locale?: string;
|
|
62
|
+
}
|
|
63
|
+
export interface InitResult {
|
|
64
|
+
protocol: number;
|
|
65
|
+
methods: string[];
|
|
66
|
+
/** e.g. `storageBytes`. */
|
|
67
|
+
limits?: Record<string, unknown>;
|
|
68
|
+
/** `id` is the app-scoped, pseudonymous userId, never the IdolChat account id or username (protocol §9). */
|
|
69
|
+
user?: MiniAppUser | null;
|
|
70
|
+
startParam?: string | null;
|
|
71
|
+
launchToken?: string | null;
|
|
72
|
+
}
|
|
73
|
+
export declare class MiniAppError extends Error {
|
|
74
|
+
readonly code: WireError["code"];
|
|
75
|
+
constructor(code: WireError["code"], message: string);
|
|
76
|
+
toWire(): WireError;
|
|
77
|
+
}
|
|
78
|
+
export declare const isRecord: (x: unknown) => x is Record<string, unknown>;
|
|
79
|
+
/** Accepts a JSON string or object from an untrusted channel; returns null when it is not a host message. */
|
|
80
|
+
export declare function parseHostMessage(raw: unknown): HostMessage | null;
|
|
81
|
+
export declare function parseError(e: Record<string, unknown>): WireError;
|
|
82
|
+
/** Accepts an app → host call from an untrusted channel; returns null when malformed. */
|
|
83
|
+
export declare function parseCallMessage(raw: unknown): CallMessage | null;
|
|
84
|
+
/** Normalizes a host's `user` (§9); anything without a string `id` is `null`. */
|
|
85
|
+
export declare function parseUser(u: unknown): MiniAppUser | null;
|
|
86
|
+
/** Normalizes an `app.init` result from the host. */
|
|
87
|
+
export declare function parseInitResult(r: unknown): InitResult;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/** The `image` of `share.*` (protocol §7). Hosts re-validate everything, including dimensions. */
|
|
2
|
+
export declare const SHARE_IMAGE_LIMITS: {
|
|
3
|
+
readonly maxBytes: number;
|
|
4
|
+
readonly maxSide: 2048;
|
|
5
|
+
readonly types: readonly ["image/png", "image/jpeg", "image/webp"];
|
|
6
|
+
};
|
|
7
|
+
export type ShareImageType = (typeof SHARE_IMAGE_LIMITS.types)[number];
|
|
8
|
+
export type ShareImageCheck = {
|
|
9
|
+
ok: true;
|
|
10
|
+
type: ShareImageType;
|
|
11
|
+
bytes: number;
|
|
12
|
+
} | {
|
|
13
|
+
ok: false;
|
|
14
|
+
reason: string;
|
|
15
|
+
};
|
|
16
|
+
/** Type and decoded size of a base64 `data:` URL, without decoding it. */
|
|
17
|
+
export declare function checkShareImage(image: unknown): ShareImageCheck;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Transport } from "./types.js";
|
|
2
|
+
declare global {
|
|
3
|
+
interface Window {
|
|
4
|
+
MiniAppAndroid?: {
|
|
5
|
+
call(json: string): void;
|
|
6
|
+
};
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
export declare const isAndroid: (win: Window) => boolean;
|
|
10
|
+
export declare function createAndroidTransport(win: Window): Transport;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { type LaunchTokenMinter, type MockOptions } from "./mock.js";
|
|
2
|
+
import type { Transport } from "./types.js";
|
|
3
|
+
/** ios → android → iframe → mock (a browser tab on its own). */
|
|
4
|
+
export declare function detectTransport(win: Window): Transport;
|
|
5
|
+
type Manifest = {
|
|
6
|
+
url: URL;
|
|
7
|
+
body: Record<string, unknown>;
|
|
8
|
+
};
|
|
9
|
+
/** A manifest without `scopes` declares none, as the bundle check reads it; no manifest allows all. */
|
|
10
|
+
export declare function manifestScopes(manifest: Manifest | null): readonly string[] | null;
|
|
11
|
+
/** The manifest's `permissionReasons` strings, for the mock's consent dialogs. */
|
|
12
|
+
export declare function manifestReasons(manifest: Manifest | null): Record<string, string>;
|
|
13
|
+
type DevWindow = {
|
|
14
|
+
fetch: Window["fetch"];
|
|
15
|
+
location: {
|
|
16
|
+
readonly href: string;
|
|
17
|
+
};
|
|
18
|
+
};
|
|
19
|
+
/** A launch-token minter when `idolchat-miniapp dev` serves the page; null anywhere else. */
|
|
20
|
+
export declare const devLaunchTokens: (win: DevWindow) => Promise<LaunchTokenMinter | null>;
|
|
21
|
+
type Switches = Pick<MockOptions, "startParam" | "profile" | "colorScheme" | "notifications" | "userName">;
|
|
22
|
+
/**
|
|
23
|
+
* What a developer can vary in a plain tab from the page URL: `?startapp=level_3`, `?profile=0`
|
|
24
|
+
* (profile refused), `?theme=dark`, `?notifications=granted|denied|prompt`, `?user=alice` (another
|
|
25
|
+
* test user).
|
|
26
|
+
*/
|
|
27
|
+
export declare function localSwitches(search: string): Switches;
|
|
28
|
+
export {};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { Transport } from "./types.js";
|
|
2
|
+
interface WebkitBridge {
|
|
3
|
+
messageHandlers?: {
|
|
4
|
+
miniapp?: {
|
|
5
|
+
postMessage(body: string): void;
|
|
6
|
+
};
|
|
7
|
+
};
|
|
8
|
+
}
|
|
9
|
+
declare global {
|
|
10
|
+
interface Window {
|
|
11
|
+
webkit?: WebkitBridge;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
export declare const isIos: (win: Window) => boolean;
|
|
15
|
+
export declare function createIosTransport(win: Window): Transport;
|
|
16
|
+
export {};
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { type MiniAppUser } from "../protocol.js";
|
|
2
|
+
import type { Transport } from "./types.js";
|
|
3
|
+
export type ConsentState = "prompt" | "granted" | "denied";
|
|
4
|
+
/**
|
|
5
|
+
* Mints a launch token as the API does (`{ nonce?, profile? }`, protocol §7), plus the dev-only
|
|
6
|
+
* `user` (a `?user=` name); detect.ts: `idolchat-miniapp dev`.
|
|
7
|
+
*/
|
|
8
|
+
export type LaunchTokenMinter = (body: {
|
|
9
|
+
nonce?: string;
|
|
10
|
+
profile: boolean;
|
|
11
|
+
user?: string;
|
|
12
|
+
}) => Promise<string>;
|
|
13
|
+
/** Drives the mock as IdolChat would; detect.ts exposes it as `window.__idolchatMock`, never in a real host. */
|
|
14
|
+
export interface MockControls {
|
|
15
|
+
/** Fires a host event, e.g. `emit("ui.themeChanged", { colorScheme: "dark" })`. */
|
|
16
|
+
emit(name: string, data?: unknown): void;
|
|
17
|
+
/**
|
|
18
|
+
* IdolChat's close sequence (protocol §4.5): `ui.backButtonPressed` if the app shows the back
|
|
19
|
+
* button; otherwise `app.beforeClose`, awaited up to 3 s, then `lifecycle.closing`.
|
|
20
|
+
*/
|
|
21
|
+
close(): Promise<"backButtonPressed" | "closing">;
|
|
22
|
+
}
|
|
23
|
+
export type MockTransport = Transport & {
|
|
24
|
+
readonly controls: MockControls;
|
|
25
|
+
};
|
|
26
|
+
/** Where the mock watches for taps and page visibility; normally `document`. */
|
|
27
|
+
export interface MockDocument {
|
|
28
|
+
addEventListener(type: string, listener: (e: {
|
|
29
|
+
readonly isTrusted: boolean;
|
|
30
|
+
}) => void, options?: boolean | AddEventListenerOptions): void;
|
|
31
|
+
readonly visibilityState?: DocumentVisibilityState;
|
|
32
|
+
}
|
|
33
|
+
/** The browser's own dialogs, so a developer can decline; normally `window`. */
|
|
34
|
+
export type MockDialogs = Pick<Window, "alert" | "confirm" | "prompt" | "open">;
|
|
35
|
+
export interface MockOptions {
|
|
36
|
+
storage?: Pick<Storage, "getItem" | "setItem" | "removeItem" | "key" | "length"> | undefined;
|
|
37
|
+
doc?: MockDocument | undefined;
|
|
38
|
+
/** Without them, dialogs answer as before: confirmed, the first popup button, consent granted. */
|
|
39
|
+
dialogs?: MockDialogs | undefined;
|
|
40
|
+
now?: () => number;
|
|
41
|
+
limitBytes?: number;
|
|
42
|
+
user?: MiniAppUser | null;
|
|
43
|
+
/** A distinct test user (`?user=<name>`): its own stable id and `Mock User <name>`; see `mockUser`. */
|
|
44
|
+
userName?: string;
|
|
45
|
+
/** What a share link would carry, e.g. from the page's `?startapp=`. */
|
|
46
|
+
startParam?: string | null;
|
|
47
|
+
colorScheme?: "light" | "dark";
|
|
48
|
+
notifications?: ConsentState;
|
|
49
|
+
/** Told the notifications consent at start and on each change; detect.ts reports it to `dev`. */
|
|
50
|
+
onNotificationsConsent?: (state: ConsentState, userName?: string) => void;
|
|
51
|
+
/** `profile` consent; `user: null` (a host with no user at all) forces `denied`. */
|
|
52
|
+
profile?: ConsentState;
|
|
53
|
+
/** The default user's avatar once it resolves (detect.ts: the app's own icon); null keeps the placeholder. */
|
|
54
|
+
avatar?: Promise<string | null>;
|
|
55
|
+
/**
|
|
56
|
+
* The scopes the app's manifest declares; a 🔒 call without its scope is left out of `methods`, so
|
|
57
|
+
* the SDK rejects it with METHOD_NOT_SUPPORTED. Omitted or null (no manifest) allows every scope.
|
|
58
|
+
*/
|
|
59
|
+
scopes?: readonly string[] | Promise<readonly string[] | null> | null;
|
|
60
|
+
/** The manifest's `permissionReasons`, shown in the consent dialogs as a host shows them. */
|
|
61
|
+
permissionReasons?: Readonly<Record<string, string>> | Promise<Readonly<Record<string, string>>>;
|
|
62
|
+
/** Without one (the default), `launchToken` is null and `auth.getLaunchToken` is not offered. */
|
|
63
|
+
launchTokens?: LaunchTokenMinter | Promise<LaunchTokenMinter | null> | null;
|
|
64
|
+
/** Called with each accepted `share.*` call, i.e. what a host would send. */
|
|
65
|
+
onShare?: (method: "share.toChat" | "share.system" | "share.toPost" | "share.toStory", params: Record<string, unknown>) => void;
|
|
66
|
+
}
|
|
67
|
+
/** A `?user=` name; the dev server's `user` field takes the same. */
|
|
68
|
+
export declare const MOCK_USER_NAME_PATTERN: RegExp;
|
|
69
|
+
/** The test user for a `?user=` name: an id stable per name (the dev server derives the same). */
|
|
70
|
+
export declare function mockUser(name?: string): MiniAppUser;
|
|
71
|
+
/**
|
|
72
|
+
* A browser-tab host as strict as a real one: 👆 calls need a real tap, params are validated, and
|
|
73
|
+
* `methods` lists only what a tab can honestly simulate — the rest answers METHOD_NOT_SUPPORTED.
|
|
74
|
+
*/
|
|
75
|
+
export declare function createMockTransport(opts?: MockOptions): MockTransport;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/** Native hosts call `window.__idcMiniApp._dispatch(json)` to deliver host → app messages. */
|
|
2
|
+
type Dispatch = (raw: unknown) => void;
|
|
3
|
+
interface NativeBridge {
|
|
4
|
+
_dispatch(raw: unknown): void;
|
|
5
|
+
}
|
|
6
|
+
declare global {
|
|
7
|
+
interface Window {
|
|
8
|
+
__idcMiniApp?: NativeBridge;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
export declare function listenNative(win: Window, onMessage: Dispatch): () => void;
|
|
12
|
+
export {};
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { Transport } from "./types.js";
|
|
2
|
+
export declare const isFramed: (win: Window) => boolean;
|
|
3
|
+
/** App inside an iframe. The embedder is untrusted to us; we only accept messages whose source is our parent. */
|
|
4
|
+
export declare function createPostMessageTransport(win: Window): Transport;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { CallMessage } from "../protocol.js";
|
|
2
|
+
export type TransportName = "ios" | "android" | "postmessage" | "mock";
|
|
3
|
+
export interface Transport {
|
|
4
|
+
readonly name: TransportName;
|
|
5
|
+
send(msg: CallMessage): void;
|
|
6
|
+
/** Delivers raw inbound host payloads (untrusted; the core parses them). Returns an unsubscribe. */
|
|
7
|
+
listen(onMessage: (raw: unknown) => void): () => void;
|
|
8
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/** Host-side param checks of protocol §7; each throws `INVALID_PARAMS` naming the reason. */
|
|
2
|
+
export type Params = Record<string, unknown>;
|
|
3
|
+
export declare function invalid(reason: string): never;
|
|
4
|
+
export declare function requiredString(p: Params, name: string): string;
|
|
5
|
+
export declare const optionalString: (p: Params, name: string) => string | undefined;
|
|
6
|
+
export declare const isNonBlank: (v: unknown) => v is string;
|
|
7
|
+
/** `share.toChat`'s required `text`, `share.toPost`'s optional one: never blank after trimming. */
|
|
8
|
+
export declare function shareText(p: Params, required: boolean): void;
|
|
9
|
+
export declare function oneOf<const T extends readonly string[]>(p: Params, name: string, values: T): T[number];
|
|
10
|
+
export declare function hexColor(p: Params): string;
|
|
11
|
+
export declare function progress(p: Params): number;
|
|
12
|
+
/** Only web links leave the app; `javascript:` and friends are refused. */
|
|
13
|
+
export declare function webUrl(p: Params, name?: string): string;
|
|
14
|
+
export declare function startParam(p: Params): string | undefined;
|
|
15
|
+
/** A story is its image or its text: one of them is required, text 1–`STORY_TEXT_MAX` trimmed. */
|
|
16
|
+
export declare function storyContent(p: Params): void;
|
|
17
|
+
export declare const permissionName: (p: Params) => "camera" | "location" | "microphone" | "notifications" | "photos" | "profile";
|
|
18
|
+
export interface PopupButton {
|
|
19
|
+
id: string;
|
|
20
|
+
text: string;
|
|
21
|
+
type: "default" | "cancel" | "destructive";
|
|
22
|
+
}
|
|
23
|
+
/** 1–3 buttons with string `id` and `text`; an unknown `type` reads as `default`, as in Play. */
|
|
24
|
+
export declare function popupButtons(p: Params): PopupButton[];
|
|
25
|
+
/** `name` non-empty; `props` a flat object of string, number or boolean values. */
|
|
26
|
+
export declare function analyticsEvent(p: Params): {
|
|
27
|
+
name: string;
|
|
28
|
+
props: Params;
|
|
29
|
+
};
|
package/global.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// For apps that load miniapp-sdk.js with a <script> tag: `import "@idolchat/miniapp-sdk/global"` once
|
|
2
|
+
// (or add it to tsconfig "types") and use `MiniApp` / `window.MiniApp` directly.
|
|
3
|
+
import type { MiniApp as MiniAppClient } from "./dist/types/index.js";
|
|
4
|
+
|
|
5
|
+
declare global {
|
|
6
|
+
// Set by miniapp-sdk.js before the app's own scripts run.
|
|
7
|
+
var MiniApp: MiniAppClient;
|
|
8
|
+
// Local mock only (a plain browser tab or `idolchat-miniapp dev`); absent inside IdolChat.
|
|
9
|
+
// oxlint-disable-next-line no-underscore-dangle -- the documented dev hook's public name
|
|
10
|
+
var __idolchatMock:
|
|
11
|
+
| {
|
|
12
|
+
emit(name: string, data?: unknown): void;
|
|
13
|
+
close(): Promise<"backButtonPressed" | "closing">;
|
|
14
|
+
}
|
|
15
|
+
| undefined;
|
|
16
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,37 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@idolchat/miniapp-sdk",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "The JavaScript SDK an IdolChat mini app uses to talk to its host.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./dist/types/index.d.ts",
|
|
10
|
+
"default": "./dist/index.mjs"
|
|
11
|
+
},
|
|
12
|
+
"./host": {
|
|
13
|
+
"types": "./dist/types/host/postmessage-host.d.ts",
|
|
14
|
+
"default": "./dist/host.mjs"
|
|
15
|
+
},
|
|
16
|
+
"./dist/miniapp-sdk.js": "./dist/miniapp-sdk.js",
|
|
17
|
+
"./package.json": "./package.json",
|
|
18
|
+
"./global": {
|
|
19
|
+
"types": "./global.d.ts"
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
"publishConfig": {
|
|
23
|
+
"access": "public"
|
|
24
|
+
},
|
|
25
|
+
"files": [
|
|
26
|
+
"dist",
|
|
27
|
+
"global.d.ts"
|
|
28
|
+
],
|
|
29
|
+
"engines": {
|
|
30
|
+
"node": ">=20"
|
|
31
|
+
},
|
|
32
|
+
"sideEffects": [
|
|
33
|
+
"./dist/index.mjs",
|
|
34
|
+
"./dist/miniapp-sdk.js"
|
|
35
|
+
],
|
|
36
|
+
"scripts": {}
|
|
6
37
|
}
|