@lo-ink/miniapp-sdk 0.19.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 +38 -0
- package/dist/adapter.d.ts +15 -0
- package/dist/adapter.js +1 -0
- package/dist/appearance.d.ts +17 -0
- package/dist/appearance.js +59 -0
- package/dist/async.d.ts +5 -0
- package/dist/async.js +87 -0
- package/dist/client.d.ts +17 -0
- package/dist/client.js +230 -0
- package/dist/errors.d.ts +6 -0
- package/dist/errors.js +18 -0
- package/dist/features.d.ts +6 -0
- package/dist/features.js +41 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +9 -0
- package/dist/protocol.d.ts +436 -0
- package/dist/protocol.js +43 -0
- package/dist/request-options.d.ts +6 -0
- package/dist/request-options.js +16 -0
- package/dist/session.d.ts +16 -0
- package/dist/session.js +56 -0
- package/dist/storage.d.ts +25 -0
- package/dist/storage.js +27 -0
- package/dist-cjs/adapter.d.ts +15 -0
- package/dist-cjs/adapter.js +2 -0
- package/dist-cjs/appearance.d.ts +17 -0
- package/dist-cjs/appearance.js +62 -0
- package/dist-cjs/async.d.ts +5 -0
- package/dist-cjs/async.js +90 -0
- package/dist-cjs/client.d.ts +17 -0
- package/dist-cjs/client.js +235 -0
- package/dist-cjs/errors.d.ts +6 -0
- package/dist-cjs/errors.js +23 -0
- package/dist-cjs/features.d.ts +6 -0
- package/dist-cjs/features.js +48 -0
- package/dist-cjs/index.d.ts +9 -0
- package/dist-cjs/index.js +25 -0
- package/dist-cjs/package.json +1 -0
- package/dist-cjs/protocol.d.ts +436 -0
- package/dist-cjs/protocol.js +46 -0
- package/dist-cjs/request-options.d.ts +6 -0
- package/dist-cjs/request-options.js +19 -0
- package/dist-cjs/session.d.ts +16 -0
- package/dist-cjs/session.js +59 -0
- package/dist-cjs/storage.d.ts +25 -0
- package/dist-cjs/storage.js +32 -0
- package/package.json +65 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 LO contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# LO Mini App SDK
|
|
2
|
+
|
|
3
|
+
Platform-neutral TypeScript contract and imperative client for LO Mini Apps. It has no runtime dependencies, performs no host discovery, installs no globals, and is safe to import during server rendering.
|
|
4
|
+
|
|
5
|
+
```ts
|
|
6
|
+
import { createMiniAppClient, requestWriteAccess } from "@lo-ink/miniapp-sdk";
|
|
7
|
+
import { createAdapter } from "@lo-ink/adapter-lo";
|
|
8
|
+
|
|
9
|
+
const adapter = createAdapter();
|
|
10
|
+
if (adapter) {
|
|
11
|
+
const client = createMiniAppClient(adapter);
|
|
12
|
+
if (client.supports("requestWriteAccess")) {
|
|
13
|
+
const allowed = await requestWriteAccess(client, { signal });
|
|
14
|
+
}
|
|
15
|
+
client.dispose();
|
|
16
|
+
}
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
An adapter supplies verified capabilities, normalized events, a snapshot, launch data, and typed operations. The client enforces capability checks, positive per-call timeouts, abort handling, one-shot settlement, request cleanup, listener cleanup, and deterministic disposal.
|
|
20
|
+
|
|
21
|
+
`adapter.snapshot().theme` uses semantic `ThemeColors` fields such as `background`, `text`, `action`, and `secondaryBackground`; host wire keys never enter application code. Calling `client.on` throws `MiniAppError("unsupported")` when an adapter cannot provide event subscription.
|
|
22
|
+
|
|
23
|
+
The portable operation set includes invoice presentation with a normalized `paid | cancelled | failed | pending` result. Hosts that cannot present invoices omit the `invoice` capability, so the client rejects before invoking the adapter.
|
|
24
|
+
|
|
25
|
+
Launch data is untrusted client input. Send it to an application backend that validates its signature, age, and audience. Never put server secrets in a Mini App.
|
|
26
|
+
|
|
27
|
+
`authenticate` uses session caching only when the application injects a `Storage` object. It never reads global session storage implicitly.
|
|
28
|
+
|
|
29
|
+
## Development
|
|
30
|
+
|
|
31
|
+
Requires Node 20 or newer.
|
|
32
|
+
|
|
33
|
+
```sh
|
|
34
|
+
npm ci
|
|
35
|
+
npm test
|
|
36
|
+
npm run check
|
|
37
|
+
npm pack --dry-run
|
|
38
|
+
```
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { Capability, HostSnapshot, MiniAppEvent, MiniAppEventMap, MiniAppOperation, OperationInput, OperationOutput, RequestContext } from "./protocol.js";
|
|
2
|
+
export type AdapterRequest<T> = {
|
|
3
|
+
promise: PromiseLike<T>;
|
|
4
|
+
/** Releases callback/event resources. Safe to call more than once. */
|
|
5
|
+
cleanup?: () => void;
|
|
6
|
+
};
|
|
7
|
+
/** Platform boundary implemented by host-specific packages. */
|
|
8
|
+
export interface MiniAppAdapter {
|
|
9
|
+
readonly id: string;
|
|
10
|
+
readonly launchData: string;
|
|
11
|
+
readonly capabilities: ReadonlySet<Capability>;
|
|
12
|
+
snapshot(): HostSnapshot;
|
|
13
|
+
subscribe<K extends MiniAppEvent>(event: K, listener: (payload: MiniAppEventMap[K]) => void): () => void;
|
|
14
|
+
execute<K extends MiniAppOperation>(operation: K, input: OperationInput<K>, context: RequestContext): AdapterRequest<OperationOutput<K>> | PromiseLike<OperationOutput<K>>;
|
|
15
|
+
}
|
package/dist/adapter.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { MiniAppClient } from "./client.js";
|
|
2
|
+
type AppearanceEnvironment = {
|
|
3
|
+
root: {
|
|
4
|
+
dataset: Record<string, string | undefined>;
|
|
5
|
+
style: {
|
|
6
|
+
setProperty(name: string, value: string): void;
|
|
7
|
+
};
|
|
8
|
+
};
|
|
9
|
+
prefersDark(): boolean;
|
|
10
|
+
background(variable: string): string;
|
|
11
|
+
onPreferenceChange(listener: () => void): () => void;
|
|
12
|
+
};
|
|
13
|
+
/** Binds normalized host appearance without reading a platform global. */
|
|
14
|
+
export declare function bindAppearance(client: MiniAppClient | null, environment: AppearanceEnvironment, options?: {
|
|
15
|
+
backgroundVariable?: string;
|
|
16
|
+
}): () => void;
|
|
17
|
+
export {};
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/** Binds normalized host appearance without reading a platform global. */
|
|
2
|
+
export function bindAppearance(client, environment, options = {}) {
|
|
3
|
+
if (!client)
|
|
4
|
+
return () => { };
|
|
5
|
+
const update = () => {
|
|
6
|
+
const snapshot = client.adapter.snapshot();
|
|
7
|
+
const preference = environment.root.dataset.preference;
|
|
8
|
+
const dark = preference === "dark" ||
|
|
9
|
+
(preference !== "light" &&
|
|
10
|
+
(snapshot.colorScheme === "dark" ||
|
|
11
|
+
(!snapshot.colorScheme && environment.prefersDark())));
|
|
12
|
+
environment.root.dataset.theme = dark ? "dark" : "light";
|
|
13
|
+
environment.root.style.setProperty("--host-top", `${Math.max(snapshot.safeArea?.top ?? 0, snapshot.contentSafeArea?.top ?? 0)}px`);
|
|
14
|
+
environment.root.style.setProperty("--host-bottom", `${Math.max(snapshot.safeArea?.bottom ?? 0, snapshot.contentSafeArea?.bottom ?? 0)}px`);
|
|
15
|
+
const background = environment
|
|
16
|
+
.background(options.backgroundVariable ?? "--page-background")
|
|
17
|
+
.trim();
|
|
18
|
+
if (/^#[0-9a-f]{6}$/i.test(background)) {
|
|
19
|
+
if (client.supports("headerColor"))
|
|
20
|
+
void client
|
|
21
|
+
.call("setHeaderColor", { color: background })
|
|
22
|
+
.catch(() => { });
|
|
23
|
+
if (client.supports("backgroundColor"))
|
|
24
|
+
void client
|
|
25
|
+
.call("setBackgroundColor", { color: background })
|
|
26
|
+
.catch(() => { });
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
update();
|
|
30
|
+
const releases = [];
|
|
31
|
+
try {
|
|
32
|
+
releases.push(environment.onPreferenceChange(update));
|
|
33
|
+
releases.push(client.on("themeChanged", update));
|
|
34
|
+
releases.push(client.on("viewportChanged", update));
|
|
35
|
+
releases.push(client.on("safeAreaChanged", update));
|
|
36
|
+
releases.push(client.on("contentSafeAreaChanged", update));
|
|
37
|
+
}
|
|
38
|
+
catch (error) {
|
|
39
|
+
for (const release of releases) {
|
|
40
|
+
try {
|
|
41
|
+
release();
|
|
42
|
+
}
|
|
43
|
+
catch {
|
|
44
|
+
/* Continue releasing previously bound listeners. */
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
throw error;
|
|
48
|
+
}
|
|
49
|
+
return () => {
|
|
50
|
+
for (const release of releases) {
|
|
51
|
+
try {
|
|
52
|
+
release();
|
|
53
|
+
}
|
|
54
|
+
catch {
|
|
55
|
+
/* Continue releasing remaining listeners. */
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
}
|
package/dist/async.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
/** Adapts a callback API and ignores late callbacks after timeout or abort. */
|
|
2
|
+
export declare function withHostCallback<T>(start: (finish: (error: unknown, value?: T) => void) => void | (() => void), options?: {
|
|
3
|
+
signal?: AbortSignal;
|
|
4
|
+
timeoutMs?: number;
|
|
5
|
+
}): Promise<T>;
|
package/dist/async.js
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { MiniAppError, normalizeMiniAppError } from "./errors.js";
|
|
2
|
+
import { readRequestOptions } from "./request-options.js";
|
|
3
|
+
const MAX_TIMEOUT_MS = 2_147_483_647;
|
|
4
|
+
/** Adapts a callback API and ignores late callbacks after timeout or abort. */
|
|
5
|
+
export async function withHostCallback(start, options = {}) {
|
|
6
|
+
options = readRequestOptions(options);
|
|
7
|
+
const timeoutMs = options.timeoutMs ?? 30_000;
|
|
8
|
+
if (!Number.isInteger(timeoutMs) ||
|
|
9
|
+
timeoutMs < 1 ||
|
|
10
|
+
timeoutMs > MAX_TIMEOUT_MS) {
|
|
11
|
+
return Promise.reject(new RangeError(`timeoutMs must be an integer from 1 to ${MAX_TIMEOUT_MS}`));
|
|
12
|
+
}
|
|
13
|
+
return new Promise((resolve, reject) => {
|
|
14
|
+
let settled = false;
|
|
15
|
+
let cleanup;
|
|
16
|
+
let cleanupRan = false;
|
|
17
|
+
let timer;
|
|
18
|
+
const runCleanup = () => {
|
|
19
|
+
if (cleanupRan || !cleanup)
|
|
20
|
+
return;
|
|
21
|
+
cleanupRan = true;
|
|
22
|
+
try {
|
|
23
|
+
cleanup();
|
|
24
|
+
}
|
|
25
|
+
catch {
|
|
26
|
+
/* Cleanup must not replace the request result. */
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
const settleSuccess = (value) => {
|
|
30
|
+
if (settled)
|
|
31
|
+
return;
|
|
32
|
+
settled = true;
|
|
33
|
+
if (timer !== undefined)
|
|
34
|
+
clearTimeout(timer);
|
|
35
|
+
try {
|
|
36
|
+
options.signal?.removeEventListener("abort", abort);
|
|
37
|
+
}
|
|
38
|
+
catch {
|
|
39
|
+
/* Preserve request settlement. */
|
|
40
|
+
}
|
|
41
|
+
runCleanup();
|
|
42
|
+
resolve(value);
|
|
43
|
+
};
|
|
44
|
+
const settleFailure = (error) => {
|
|
45
|
+
if (settled)
|
|
46
|
+
return;
|
|
47
|
+
settled = true;
|
|
48
|
+
if (timer !== undefined)
|
|
49
|
+
clearTimeout(timer);
|
|
50
|
+
try {
|
|
51
|
+
options.signal?.removeEventListener("abort", abort);
|
|
52
|
+
}
|
|
53
|
+
catch {
|
|
54
|
+
/* Preserve request settlement. */
|
|
55
|
+
}
|
|
56
|
+
runCleanup();
|
|
57
|
+
reject(normalizeMiniAppError(error));
|
|
58
|
+
};
|
|
59
|
+
const finish = (error, value) => {
|
|
60
|
+
if (error == null)
|
|
61
|
+
settleSuccess(value);
|
|
62
|
+
else
|
|
63
|
+
settleFailure(error);
|
|
64
|
+
};
|
|
65
|
+
const abort = () => settleFailure(new MiniAppError("aborted"));
|
|
66
|
+
try {
|
|
67
|
+
options.signal?.addEventListener("abort", abort, { once: true });
|
|
68
|
+
if (settled)
|
|
69
|
+
return;
|
|
70
|
+
if (options.signal?.aborted)
|
|
71
|
+
return abort();
|
|
72
|
+
}
|
|
73
|
+
catch (error) {
|
|
74
|
+
settleFailure(error);
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
timer = setTimeout(() => settleFailure(new MiniAppError("timeout")), timeoutMs);
|
|
78
|
+
try {
|
|
79
|
+
cleanup = start(finish);
|
|
80
|
+
if (settled)
|
|
81
|
+
runCleanup();
|
|
82
|
+
}
|
|
83
|
+
catch (error) {
|
|
84
|
+
settleFailure(error);
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
}
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { MiniAppAdapter } from "./adapter.js";
|
|
2
|
+
import type { Capability, MiniAppEvent, MiniAppEventMap, MiniAppOperation, OperationInput, OperationOutput } from "./protocol.js";
|
|
3
|
+
export type CallOptions = {
|
|
4
|
+
signal?: AbortSignal;
|
|
5
|
+
timeoutMs?: number;
|
|
6
|
+
};
|
|
7
|
+
export interface MiniAppClient {
|
|
8
|
+
readonly adapter: MiniAppAdapter;
|
|
9
|
+
readonly disposed: boolean;
|
|
10
|
+
supports(capability: Capability): boolean;
|
|
11
|
+
on<K extends MiniAppEvent>(event: K, listener: (payload: MiniAppEventMap[K]) => void): () => void;
|
|
12
|
+
call<K extends MiniAppOperation>(operation: K, input: OperationInput<K>, options?: CallOptions): Promise<OperationOutput<K>>;
|
|
13
|
+
dispose(): void;
|
|
14
|
+
}
|
|
15
|
+
export declare function createMiniAppClient(adapter: MiniAppAdapter): MiniAppClient;
|
|
16
|
+
export declare function supports(target: MiniAppClient | MiniAppAdapter | null, capability: Capability): boolean;
|
|
17
|
+
export declare function subscribe<K extends MiniAppEvent>(client: MiniAppClient | null, event: K, listener: (payload: MiniAppEventMap[K]) => void): () => void;
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
import { MiniAppError, normalizeMiniAppError } from "./errors.js";
|
|
2
|
+
import { readRequestOptions } from "./request-options.js";
|
|
3
|
+
const MAX_TIMEOUT_MS = 2_147_483_647;
|
|
4
|
+
const operationCapability = {
|
|
5
|
+
ready: "ready",
|
|
6
|
+
expand: "expand",
|
|
7
|
+
requestFullscreen: "fullscreen",
|
|
8
|
+
exitFullscreen: "fullscreen",
|
|
9
|
+
hideKeyboard: "hideKeyboard",
|
|
10
|
+
setOrientationLock: "orientation",
|
|
11
|
+
setButton: "mainButton",
|
|
12
|
+
setClosingConfirmation: "closingConfirmation",
|
|
13
|
+
setHeaderColor: "headerColor",
|
|
14
|
+
setBackgroundColor: "backgroundColor",
|
|
15
|
+
setBottomBarColor: "bottomBarColor",
|
|
16
|
+
haptic: "haptics",
|
|
17
|
+
showPopup: "popup",
|
|
18
|
+
openLink: "openLink",
|
|
19
|
+
sendData: "sendData",
|
|
20
|
+
switchInlineQuery: "switchInlineQuery",
|
|
21
|
+
readClipboard: "clipboard",
|
|
22
|
+
getLocation: "location",
|
|
23
|
+
openLocationSettings: "location",
|
|
24
|
+
getBiometryInfo: "biometry",
|
|
25
|
+
requestBiometryAccess: "biometry",
|
|
26
|
+
authenticateBiometry: "biometry",
|
|
27
|
+
updateBiometryToken: "biometry",
|
|
28
|
+
openBiometrySettings: "biometry",
|
|
29
|
+
startAccelerometer: "sensors",
|
|
30
|
+
stopAccelerometer: "sensors",
|
|
31
|
+
startGyroscope: "sensors",
|
|
32
|
+
stopGyroscope: "sensors",
|
|
33
|
+
startDeviceOrientation: "sensors",
|
|
34
|
+
stopDeviceOrientation: "sensors",
|
|
35
|
+
downloadFile: "downloadFile",
|
|
36
|
+
openQrScanner: "qrScanner",
|
|
37
|
+
closeQrScanner: "qrScanner",
|
|
38
|
+
requestWriteAccess: "requestWriteAccess",
|
|
39
|
+
requestContact: "requestContact",
|
|
40
|
+
shareMessage: "shareMessage",
|
|
41
|
+
shareToStory: "shareToStory",
|
|
42
|
+
openInvoice: "invoice",
|
|
43
|
+
cloudStorageSet: "cloudStorage",
|
|
44
|
+
cloudStorageGet: "cloudStorage",
|
|
45
|
+
cloudStorageGetMany: "cloudStorage",
|
|
46
|
+
cloudStorageRemove: "cloudStorage",
|
|
47
|
+
cloudStorageRemoveMany: "cloudStorage",
|
|
48
|
+
cloudStorageKeys: "cloudStorage",
|
|
49
|
+
deviceStorageSet: "deviceStorage",
|
|
50
|
+
deviceStorageGet: "deviceStorage",
|
|
51
|
+
deviceStorageRemove: "deviceStorage",
|
|
52
|
+
deviceStorageClear: "deviceStorage",
|
|
53
|
+
secureStorageSet: "secureStorage",
|
|
54
|
+
secureStorageGet: "secureStorage",
|
|
55
|
+
secureStorageRestore: "secureStorage",
|
|
56
|
+
secureStorageRemove: "secureStorage",
|
|
57
|
+
secureStorageClear: "secureStorage",
|
|
58
|
+
};
|
|
59
|
+
const defaultTimeout = {
|
|
60
|
+
requestWriteAccess: 60_000,
|
|
61
|
+
requestContact: 60_000,
|
|
62
|
+
shareMessage: 300_000,
|
|
63
|
+
openInvoice: 300_000,
|
|
64
|
+
secureStorageSet: 60_000,
|
|
65
|
+
secureStorageGet: 60_000,
|
|
66
|
+
secureStorageRestore: 60_000,
|
|
67
|
+
secureStorageRemove: 60_000,
|
|
68
|
+
secureStorageClear: 60_000,
|
|
69
|
+
};
|
|
70
|
+
export function createMiniAppClient(adapter) {
|
|
71
|
+
let disposed = false;
|
|
72
|
+
const subscriptions = new Set();
|
|
73
|
+
const pending = new Set();
|
|
74
|
+
const client = {
|
|
75
|
+
adapter,
|
|
76
|
+
get disposed() {
|
|
77
|
+
return disposed;
|
|
78
|
+
},
|
|
79
|
+
supports(capability) {
|
|
80
|
+
return !disposed && adapter.capabilities.has(capability);
|
|
81
|
+
},
|
|
82
|
+
on(event, listener) {
|
|
83
|
+
if (disposed)
|
|
84
|
+
return () => { };
|
|
85
|
+
let active = true;
|
|
86
|
+
const releaseAdapter = adapter.subscribe(event, listener);
|
|
87
|
+
const release = () => {
|
|
88
|
+
if (!active)
|
|
89
|
+
return;
|
|
90
|
+
active = false;
|
|
91
|
+
subscriptions.delete(release);
|
|
92
|
+
releaseAdapter();
|
|
93
|
+
};
|
|
94
|
+
subscriptions.add(release);
|
|
95
|
+
if (disposed) {
|
|
96
|
+
try {
|
|
97
|
+
release();
|
|
98
|
+
}
|
|
99
|
+
catch {
|
|
100
|
+
/* Disposal remains best effort. */
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
return release;
|
|
104
|
+
},
|
|
105
|
+
async call(operation, input, options = {}) {
|
|
106
|
+
if (disposed)
|
|
107
|
+
throw new MiniAppError("disposed");
|
|
108
|
+
options = readRequestOptions(options);
|
|
109
|
+
const capability = operation === "setButton"
|
|
110
|
+
? `${input.button}Button`
|
|
111
|
+
: operationCapability[operation];
|
|
112
|
+
if (capability && !adapter.capabilities.has(capability)) {
|
|
113
|
+
throw new MiniAppError("unsupported", `${operation} is not supported`);
|
|
114
|
+
}
|
|
115
|
+
const timeoutMs = options.timeoutMs ?? defaultTimeout[operation] ?? 30_000;
|
|
116
|
+
if (!Number.isInteger(timeoutMs) ||
|
|
117
|
+
timeoutMs < 1 ||
|
|
118
|
+
timeoutMs > MAX_TIMEOUT_MS) {
|
|
119
|
+
throw new RangeError(`timeoutMs must be an integer from 1 to ${MAX_TIMEOUT_MS}`);
|
|
120
|
+
}
|
|
121
|
+
if (options.signal?.aborted)
|
|
122
|
+
throw new MiniAppError("aborted");
|
|
123
|
+
return new Promise((resolve, reject) => {
|
|
124
|
+
let settled = false;
|
|
125
|
+
let cleanupAdapter;
|
|
126
|
+
let cleanupRan = false;
|
|
127
|
+
let timer;
|
|
128
|
+
const controller = new AbortController();
|
|
129
|
+
const runCleanup = () => {
|
|
130
|
+
if (cleanupRan || !cleanupAdapter)
|
|
131
|
+
return;
|
|
132
|
+
cleanupRan = true;
|
|
133
|
+
try {
|
|
134
|
+
cleanupAdapter();
|
|
135
|
+
}
|
|
136
|
+
catch {
|
|
137
|
+
/* Cleanup must not change the result. */
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
const finish = (outcome) => {
|
|
141
|
+
if (settled)
|
|
142
|
+
return;
|
|
143
|
+
settled = true;
|
|
144
|
+
if (timer !== undefined)
|
|
145
|
+
clearTimeout(timer);
|
|
146
|
+
try {
|
|
147
|
+
options.signal?.removeEventListener("abort", abort);
|
|
148
|
+
}
|
|
149
|
+
catch {
|
|
150
|
+
/* Preserve request settlement. */
|
|
151
|
+
}
|
|
152
|
+
pending.delete(disposeRequest);
|
|
153
|
+
runCleanup();
|
|
154
|
+
if (outcome.ok)
|
|
155
|
+
resolve(outcome.value);
|
|
156
|
+
else
|
|
157
|
+
reject(normalizeMiniAppError(outcome.error));
|
|
158
|
+
};
|
|
159
|
+
const cancel = (reason) => {
|
|
160
|
+
if (!controller.signal.aborted)
|
|
161
|
+
controller.abort(reason);
|
|
162
|
+
finish({ ok: false, error: reason });
|
|
163
|
+
};
|
|
164
|
+
const abort = () => cancel(new MiniAppError("aborted"));
|
|
165
|
+
const disposeRequest = (reason) => cancel(reason);
|
|
166
|
+
pending.add(disposeRequest);
|
|
167
|
+
try {
|
|
168
|
+
options.signal?.addEventListener("abort", abort, { once: true });
|
|
169
|
+
if (settled)
|
|
170
|
+
return;
|
|
171
|
+
if (options.signal?.aborted) {
|
|
172
|
+
abort();
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
catch (error) {
|
|
177
|
+
finish({ ok: false, error });
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
timer = setTimeout(() => cancel(new MiniAppError("timeout")), timeoutMs);
|
|
181
|
+
try {
|
|
182
|
+
const request = adapter.execute(operation, input, {
|
|
183
|
+
signal: controller.signal,
|
|
184
|
+
});
|
|
185
|
+
const promise = "promise" in request ? request.promise : request;
|
|
186
|
+
cleanupAdapter = "promise" in request ? request.cleanup : undefined;
|
|
187
|
+
if (settled)
|
|
188
|
+
runCleanup();
|
|
189
|
+
Promise.resolve(promise).then((value) => finish({ ok: true, value }), (error) => finish({ ok: false, error }));
|
|
190
|
+
}
|
|
191
|
+
catch (error) {
|
|
192
|
+
finish({ ok: false, error });
|
|
193
|
+
}
|
|
194
|
+
});
|
|
195
|
+
},
|
|
196
|
+
dispose() {
|
|
197
|
+
if (disposed)
|
|
198
|
+
return;
|
|
199
|
+
disposed = true;
|
|
200
|
+
for (const release of [...subscriptions]) {
|
|
201
|
+
try {
|
|
202
|
+
release();
|
|
203
|
+
}
|
|
204
|
+
catch {
|
|
205
|
+
/* Continue releasing remaining listeners. */
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
const reason = new MiniAppError("disposed");
|
|
209
|
+
for (const cancel of [...pending]) {
|
|
210
|
+
try {
|
|
211
|
+
cancel(reason);
|
|
212
|
+
}
|
|
213
|
+
catch {
|
|
214
|
+
/* Continue cancelling remaining calls. */
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
},
|
|
218
|
+
};
|
|
219
|
+
return client;
|
|
220
|
+
}
|
|
221
|
+
export function supports(target, capability) {
|
|
222
|
+
if (!target)
|
|
223
|
+
return false;
|
|
224
|
+
return "adapter" in target
|
|
225
|
+
? target.supports(capability)
|
|
226
|
+
: target.capabilities.has(capability);
|
|
227
|
+
}
|
|
228
|
+
export function subscribe(client, event, listener) {
|
|
229
|
+
return client?.on(event, listener) ?? (() => { });
|
|
230
|
+
}
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export type MiniAppErrorCode = "unsupported" | "timeout" | "aborted" | "disposed" | "failed" | "invalid-response";
|
|
2
|
+
export declare class MiniAppError extends Error {
|
|
3
|
+
readonly code: MiniAppErrorCode;
|
|
4
|
+
constructor(code: MiniAppErrorCode, message?: string, options?: ErrorOptions);
|
|
5
|
+
}
|
|
6
|
+
export declare function normalizeMiniAppError(error: unknown): MiniAppError;
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export class MiniAppError extends Error {
|
|
2
|
+
code;
|
|
3
|
+
constructor(code, message = code, options) {
|
|
4
|
+
super(message, options);
|
|
5
|
+
this.code = code;
|
|
6
|
+
this.name = "MiniAppError";
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
export function normalizeMiniAppError(error) {
|
|
10
|
+
if (error instanceof MiniAppError)
|
|
11
|
+
return error;
|
|
12
|
+
if (error instanceof Error) {
|
|
13
|
+
return new MiniAppError("failed", error.message || "Host request failed", {
|
|
14
|
+
cause: error,
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
return new MiniAppError("failed", "Host request failed", { cause: error });
|
|
18
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { CallOptions, MiniAppClient } from "./client.js";
|
|
2
|
+
import type { StoryShareParams } from "./protocol.js";
|
|
3
|
+
export declare const requestWriteAccess: (client: MiniAppClient, options?: CallOptions) => Promise<boolean>;
|
|
4
|
+
export declare const requestContact: (client: MiniAppClient, options?: CallOptions) => Promise<boolean>;
|
|
5
|
+
export declare function shareMessage(client: MiniAppClient, id: string, options?: CallOptions): Promise<boolean>;
|
|
6
|
+
export declare function shareToStory(client: MiniAppClient, mediaUrl: string, params?: StoryShareParams, options?: CallOptions): Promise<void>;
|
package/dist/features.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
export const requestWriteAccess = (client, options) => client.call("requestWriteAccess", undefined, {
|
|
2
|
+
timeoutMs: 60_000,
|
|
3
|
+
...options,
|
|
4
|
+
});
|
|
5
|
+
export const requestContact = (client, options) => client.call("requestContact", undefined, { timeoutMs: 60_000, ...options });
|
|
6
|
+
export function shareMessage(client, id, options) {
|
|
7
|
+
if (typeof id !== "string" || !id.length || id.length > 128) {
|
|
8
|
+
return Promise.reject(new TypeError("Invalid prepared message ID"));
|
|
9
|
+
}
|
|
10
|
+
return client.call("shareMessage", { id }, { timeoutMs: 300_000, ...options });
|
|
11
|
+
}
|
|
12
|
+
function validateStoryUrl(value, media) {
|
|
13
|
+
if (typeof value !== "string" ||
|
|
14
|
+
value.length > 8192 ||
|
|
15
|
+
!/^https?:\/\//i.test(value) ||
|
|
16
|
+
/[\s\\\u0000-\u001f\u007f]/.test(value)) {
|
|
17
|
+
throw new TypeError("Invalid story URL");
|
|
18
|
+
}
|
|
19
|
+
const url = new URL(value);
|
|
20
|
+
if (!url.hostname ||
|
|
21
|
+
url.username ||
|
|
22
|
+
url.password ||
|
|
23
|
+
(media && url.protocol !== "https:")) {
|
|
24
|
+
throw new TypeError("Invalid story URL");
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
export async function shareToStory(client, mediaUrl, params, options) {
|
|
28
|
+
validateStoryUrl(mediaUrl, true);
|
|
29
|
+
if (params?.text !== undefined &&
|
|
30
|
+
(typeof params.text !== "string" || params.text.length > 2048)) {
|
|
31
|
+
throw new TypeError("Invalid story caption");
|
|
32
|
+
}
|
|
33
|
+
if (params?.link) {
|
|
34
|
+
validateStoryUrl(params.link.url, false);
|
|
35
|
+
if (params.link.name !== undefined &&
|
|
36
|
+
(typeof params.link.name !== "string" || params.link.name.length > 48)) {
|
|
37
|
+
throw new TypeError("Invalid story link label");
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return client.call("shareToStory", { mediaUrl, params }, options);
|
|
41
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export * from "./adapter.js";
|
|
2
|
+
export * from "./appearance.js";
|
|
3
|
+
export * from "./async.js";
|
|
4
|
+
export * from "./client.js";
|
|
5
|
+
export * from "./errors.js";
|
|
6
|
+
export * from "./features.js";
|
|
7
|
+
export * from "./protocol.js";
|
|
8
|
+
export * from "./session.js";
|
|
9
|
+
export * from "./storage.js";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export * from "./adapter.js";
|
|
2
|
+
export * from "./appearance.js";
|
|
3
|
+
export * from "./async.js";
|
|
4
|
+
export * from "./client.js";
|
|
5
|
+
export * from "./errors.js";
|
|
6
|
+
export * from "./features.js";
|
|
7
|
+
export * from "./protocol.js";
|
|
8
|
+
export * from "./session.js";
|
|
9
|
+
export * from "./storage.js";
|