@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
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.bindAppearance = bindAppearance;
|
|
4
|
+
/** Binds normalized host appearance without reading a platform global. */
|
|
5
|
+
function bindAppearance(client, environment, options = {}) {
|
|
6
|
+
if (!client)
|
|
7
|
+
return () => { };
|
|
8
|
+
const update = () => {
|
|
9
|
+
const snapshot = client.adapter.snapshot();
|
|
10
|
+
const preference = environment.root.dataset.preference;
|
|
11
|
+
const dark = preference === "dark" ||
|
|
12
|
+
(preference !== "light" &&
|
|
13
|
+
(snapshot.colorScheme === "dark" ||
|
|
14
|
+
(!snapshot.colorScheme && environment.prefersDark())));
|
|
15
|
+
environment.root.dataset.theme = dark ? "dark" : "light";
|
|
16
|
+
environment.root.style.setProperty("--host-top", `${Math.max(snapshot.safeArea?.top ?? 0, snapshot.contentSafeArea?.top ?? 0)}px`);
|
|
17
|
+
environment.root.style.setProperty("--host-bottom", `${Math.max(snapshot.safeArea?.bottom ?? 0, snapshot.contentSafeArea?.bottom ?? 0)}px`);
|
|
18
|
+
const background = environment
|
|
19
|
+
.background(options.backgroundVariable ?? "--page-background")
|
|
20
|
+
.trim();
|
|
21
|
+
if (/^#[0-9a-f]{6}$/i.test(background)) {
|
|
22
|
+
if (client.supports("headerColor"))
|
|
23
|
+
void client
|
|
24
|
+
.call("setHeaderColor", { color: background })
|
|
25
|
+
.catch(() => { });
|
|
26
|
+
if (client.supports("backgroundColor"))
|
|
27
|
+
void client
|
|
28
|
+
.call("setBackgroundColor", { color: background })
|
|
29
|
+
.catch(() => { });
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
update();
|
|
33
|
+
const releases = [];
|
|
34
|
+
try {
|
|
35
|
+
releases.push(environment.onPreferenceChange(update));
|
|
36
|
+
releases.push(client.on("themeChanged", update));
|
|
37
|
+
releases.push(client.on("viewportChanged", update));
|
|
38
|
+
releases.push(client.on("safeAreaChanged", update));
|
|
39
|
+
releases.push(client.on("contentSafeAreaChanged", update));
|
|
40
|
+
}
|
|
41
|
+
catch (error) {
|
|
42
|
+
for (const release of releases) {
|
|
43
|
+
try {
|
|
44
|
+
release();
|
|
45
|
+
}
|
|
46
|
+
catch {
|
|
47
|
+
/* Continue releasing previously bound listeners. */
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
throw error;
|
|
51
|
+
}
|
|
52
|
+
return () => {
|
|
53
|
+
for (const release of releases) {
|
|
54
|
+
try {
|
|
55
|
+
release();
|
|
56
|
+
}
|
|
57
|
+
catch {
|
|
58
|
+
/* Continue releasing remaining listeners. */
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
}
|
|
@@ -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>;
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.withHostCallback = withHostCallback;
|
|
4
|
+
const errors_js_1 = require("./errors.js");
|
|
5
|
+
const request_options_js_1 = require("./request-options.js");
|
|
6
|
+
const MAX_TIMEOUT_MS = 2_147_483_647;
|
|
7
|
+
/** Adapts a callback API and ignores late callbacks after timeout or abort. */
|
|
8
|
+
async function withHostCallback(start, options = {}) {
|
|
9
|
+
options = (0, request_options_js_1.readRequestOptions)(options);
|
|
10
|
+
const timeoutMs = options.timeoutMs ?? 30_000;
|
|
11
|
+
if (!Number.isInteger(timeoutMs) ||
|
|
12
|
+
timeoutMs < 1 ||
|
|
13
|
+
timeoutMs > MAX_TIMEOUT_MS) {
|
|
14
|
+
return Promise.reject(new RangeError(`timeoutMs must be an integer from 1 to ${MAX_TIMEOUT_MS}`));
|
|
15
|
+
}
|
|
16
|
+
return new Promise((resolve, reject) => {
|
|
17
|
+
let settled = false;
|
|
18
|
+
let cleanup;
|
|
19
|
+
let cleanupRan = false;
|
|
20
|
+
let timer;
|
|
21
|
+
const runCleanup = () => {
|
|
22
|
+
if (cleanupRan || !cleanup)
|
|
23
|
+
return;
|
|
24
|
+
cleanupRan = true;
|
|
25
|
+
try {
|
|
26
|
+
cleanup();
|
|
27
|
+
}
|
|
28
|
+
catch {
|
|
29
|
+
/* Cleanup must not replace the request result. */
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
const settleSuccess = (value) => {
|
|
33
|
+
if (settled)
|
|
34
|
+
return;
|
|
35
|
+
settled = true;
|
|
36
|
+
if (timer !== undefined)
|
|
37
|
+
clearTimeout(timer);
|
|
38
|
+
try {
|
|
39
|
+
options.signal?.removeEventListener("abort", abort);
|
|
40
|
+
}
|
|
41
|
+
catch {
|
|
42
|
+
/* Preserve request settlement. */
|
|
43
|
+
}
|
|
44
|
+
runCleanup();
|
|
45
|
+
resolve(value);
|
|
46
|
+
};
|
|
47
|
+
const settleFailure = (error) => {
|
|
48
|
+
if (settled)
|
|
49
|
+
return;
|
|
50
|
+
settled = true;
|
|
51
|
+
if (timer !== undefined)
|
|
52
|
+
clearTimeout(timer);
|
|
53
|
+
try {
|
|
54
|
+
options.signal?.removeEventListener("abort", abort);
|
|
55
|
+
}
|
|
56
|
+
catch {
|
|
57
|
+
/* Preserve request settlement. */
|
|
58
|
+
}
|
|
59
|
+
runCleanup();
|
|
60
|
+
reject((0, errors_js_1.normalizeMiniAppError)(error));
|
|
61
|
+
};
|
|
62
|
+
const finish = (error, value) => {
|
|
63
|
+
if (error == null)
|
|
64
|
+
settleSuccess(value);
|
|
65
|
+
else
|
|
66
|
+
settleFailure(error);
|
|
67
|
+
};
|
|
68
|
+
const abort = () => settleFailure(new errors_js_1.MiniAppError("aborted"));
|
|
69
|
+
try {
|
|
70
|
+
options.signal?.addEventListener("abort", abort, { once: true });
|
|
71
|
+
if (settled)
|
|
72
|
+
return;
|
|
73
|
+
if (options.signal?.aborted)
|
|
74
|
+
return abort();
|
|
75
|
+
}
|
|
76
|
+
catch (error) {
|
|
77
|
+
settleFailure(error);
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
timer = setTimeout(() => settleFailure(new errors_js_1.MiniAppError("timeout")), timeoutMs);
|
|
81
|
+
try {
|
|
82
|
+
cleanup = start(finish);
|
|
83
|
+
if (settled)
|
|
84
|
+
runCleanup();
|
|
85
|
+
}
|
|
86
|
+
catch (error) {
|
|
87
|
+
settleFailure(error);
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
}
|
|
@@ -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;
|
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createMiniAppClient = createMiniAppClient;
|
|
4
|
+
exports.supports = supports;
|
|
5
|
+
exports.subscribe = subscribe;
|
|
6
|
+
const errors_js_1 = require("./errors.js");
|
|
7
|
+
const request_options_js_1 = require("./request-options.js");
|
|
8
|
+
const MAX_TIMEOUT_MS = 2_147_483_647;
|
|
9
|
+
const operationCapability = {
|
|
10
|
+
ready: "ready",
|
|
11
|
+
expand: "expand",
|
|
12
|
+
requestFullscreen: "fullscreen",
|
|
13
|
+
exitFullscreen: "fullscreen",
|
|
14
|
+
hideKeyboard: "hideKeyboard",
|
|
15
|
+
setOrientationLock: "orientation",
|
|
16
|
+
setButton: "mainButton",
|
|
17
|
+
setClosingConfirmation: "closingConfirmation",
|
|
18
|
+
setHeaderColor: "headerColor",
|
|
19
|
+
setBackgroundColor: "backgroundColor",
|
|
20
|
+
setBottomBarColor: "bottomBarColor",
|
|
21
|
+
haptic: "haptics",
|
|
22
|
+
showPopup: "popup",
|
|
23
|
+
openLink: "openLink",
|
|
24
|
+
sendData: "sendData",
|
|
25
|
+
switchInlineQuery: "switchInlineQuery",
|
|
26
|
+
readClipboard: "clipboard",
|
|
27
|
+
getLocation: "location",
|
|
28
|
+
openLocationSettings: "location",
|
|
29
|
+
getBiometryInfo: "biometry",
|
|
30
|
+
requestBiometryAccess: "biometry",
|
|
31
|
+
authenticateBiometry: "biometry",
|
|
32
|
+
updateBiometryToken: "biometry",
|
|
33
|
+
openBiometrySettings: "biometry",
|
|
34
|
+
startAccelerometer: "sensors",
|
|
35
|
+
stopAccelerometer: "sensors",
|
|
36
|
+
startGyroscope: "sensors",
|
|
37
|
+
stopGyroscope: "sensors",
|
|
38
|
+
startDeviceOrientation: "sensors",
|
|
39
|
+
stopDeviceOrientation: "sensors",
|
|
40
|
+
downloadFile: "downloadFile",
|
|
41
|
+
openQrScanner: "qrScanner",
|
|
42
|
+
closeQrScanner: "qrScanner",
|
|
43
|
+
requestWriteAccess: "requestWriteAccess",
|
|
44
|
+
requestContact: "requestContact",
|
|
45
|
+
shareMessage: "shareMessage",
|
|
46
|
+
shareToStory: "shareToStory",
|
|
47
|
+
openInvoice: "invoice",
|
|
48
|
+
cloudStorageSet: "cloudStorage",
|
|
49
|
+
cloudStorageGet: "cloudStorage",
|
|
50
|
+
cloudStorageGetMany: "cloudStorage",
|
|
51
|
+
cloudStorageRemove: "cloudStorage",
|
|
52
|
+
cloudStorageRemoveMany: "cloudStorage",
|
|
53
|
+
cloudStorageKeys: "cloudStorage",
|
|
54
|
+
deviceStorageSet: "deviceStorage",
|
|
55
|
+
deviceStorageGet: "deviceStorage",
|
|
56
|
+
deviceStorageRemove: "deviceStorage",
|
|
57
|
+
deviceStorageClear: "deviceStorage",
|
|
58
|
+
secureStorageSet: "secureStorage",
|
|
59
|
+
secureStorageGet: "secureStorage",
|
|
60
|
+
secureStorageRestore: "secureStorage",
|
|
61
|
+
secureStorageRemove: "secureStorage",
|
|
62
|
+
secureStorageClear: "secureStorage",
|
|
63
|
+
};
|
|
64
|
+
const defaultTimeout = {
|
|
65
|
+
requestWriteAccess: 60_000,
|
|
66
|
+
requestContact: 60_000,
|
|
67
|
+
shareMessage: 300_000,
|
|
68
|
+
openInvoice: 300_000,
|
|
69
|
+
secureStorageSet: 60_000,
|
|
70
|
+
secureStorageGet: 60_000,
|
|
71
|
+
secureStorageRestore: 60_000,
|
|
72
|
+
secureStorageRemove: 60_000,
|
|
73
|
+
secureStorageClear: 60_000,
|
|
74
|
+
};
|
|
75
|
+
function createMiniAppClient(adapter) {
|
|
76
|
+
let disposed = false;
|
|
77
|
+
const subscriptions = new Set();
|
|
78
|
+
const pending = new Set();
|
|
79
|
+
const client = {
|
|
80
|
+
adapter,
|
|
81
|
+
get disposed() {
|
|
82
|
+
return disposed;
|
|
83
|
+
},
|
|
84
|
+
supports(capability) {
|
|
85
|
+
return !disposed && adapter.capabilities.has(capability);
|
|
86
|
+
},
|
|
87
|
+
on(event, listener) {
|
|
88
|
+
if (disposed)
|
|
89
|
+
return () => { };
|
|
90
|
+
let active = true;
|
|
91
|
+
const releaseAdapter = adapter.subscribe(event, listener);
|
|
92
|
+
const release = () => {
|
|
93
|
+
if (!active)
|
|
94
|
+
return;
|
|
95
|
+
active = false;
|
|
96
|
+
subscriptions.delete(release);
|
|
97
|
+
releaseAdapter();
|
|
98
|
+
};
|
|
99
|
+
subscriptions.add(release);
|
|
100
|
+
if (disposed) {
|
|
101
|
+
try {
|
|
102
|
+
release();
|
|
103
|
+
}
|
|
104
|
+
catch {
|
|
105
|
+
/* Disposal remains best effort. */
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
return release;
|
|
109
|
+
},
|
|
110
|
+
async call(operation, input, options = {}) {
|
|
111
|
+
if (disposed)
|
|
112
|
+
throw new errors_js_1.MiniAppError("disposed");
|
|
113
|
+
options = (0, request_options_js_1.readRequestOptions)(options);
|
|
114
|
+
const capability = operation === "setButton"
|
|
115
|
+
? `${input.button}Button`
|
|
116
|
+
: operationCapability[operation];
|
|
117
|
+
if (capability && !adapter.capabilities.has(capability)) {
|
|
118
|
+
throw new errors_js_1.MiniAppError("unsupported", `${operation} is not supported`);
|
|
119
|
+
}
|
|
120
|
+
const timeoutMs = options.timeoutMs ?? defaultTimeout[operation] ?? 30_000;
|
|
121
|
+
if (!Number.isInteger(timeoutMs) ||
|
|
122
|
+
timeoutMs < 1 ||
|
|
123
|
+
timeoutMs > MAX_TIMEOUT_MS) {
|
|
124
|
+
throw new RangeError(`timeoutMs must be an integer from 1 to ${MAX_TIMEOUT_MS}`);
|
|
125
|
+
}
|
|
126
|
+
if (options.signal?.aborted)
|
|
127
|
+
throw new errors_js_1.MiniAppError("aborted");
|
|
128
|
+
return new Promise((resolve, reject) => {
|
|
129
|
+
let settled = false;
|
|
130
|
+
let cleanupAdapter;
|
|
131
|
+
let cleanupRan = false;
|
|
132
|
+
let timer;
|
|
133
|
+
const controller = new AbortController();
|
|
134
|
+
const runCleanup = () => {
|
|
135
|
+
if (cleanupRan || !cleanupAdapter)
|
|
136
|
+
return;
|
|
137
|
+
cleanupRan = true;
|
|
138
|
+
try {
|
|
139
|
+
cleanupAdapter();
|
|
140
|
+
}
|
|
141
|
+
catch {
|
|
142
|
+
/* Cleanup must not change the result. */
|
|
143
|
+
}
|
|
144
|
+
};
|
|
145
|
+
const finish = (outcome) => {
|
|
146
|
+
if (settled)
|
|
147
|
+
return;
|
|
148
|
+
settled = true;
|
|
149
|
+
if (timer !== undefined)
|
|
150
|
+
clearTimeout(timer);
|
|
151
|
+
try {
|
|
152
|
+
options.signal?.removeEventListener("abort", abort);
|
|
153
|
+
}
|
|
154
|
+
catch {
|
|
155
|
+
/* Preserve request settlement. */
|
|
156
|
+
}
|
|
157
|
+
pending.delete(disposeRequest);
|
|
158
|
+
runCleanup();
|
|
159
|
+
if (outcome.ok)
|
|
160
|
+
resolve(outcome.value);
|
|
161
|
+
else
|
|
162
|
+
reject((0, errors_js_1.normalizeMiniAppError)(outcome.error));
|
|
163
|
+
};
|
|
164
|
+
const cancel = (reason) => {
|
|
165
|
+
if (!controller.signal.aborted)
|
|
166
|
+
controller.abort(reason);
|
|
167
|
+
finish({ ok: false, error: reason });
|
|
168
|
+
};
|
|
169
|
+
const abort = () => cancel(new errors_js_1.MiniAppError("aborted"));
|
|
170
|
+
const disposeRequest = (reason) => cancel(reason);
|
|
171
|
+
pending.add(disposeRequest);
|
|
172
|
+
try {
|
|
173
|
+
options.signal?.addEventListener("abort", abort, { once: true });
|
|
174
|
+
if (settled)
|
|
175
|
+
return;
|
|
176
|
+
if (options.signal?.aborted) {
|
|
177
|
+
abort();
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
catch (error) {
|
|
182
|
+
finish({ ok: false, error });
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
timer = setTimeout(() => cancel(new errors_js_1.MiniAppError("timeout")), timeoutMs);
|
|
186
|
+
try {
|
|
187
|
+
const request = adapter.execute(operation, input, {
|
|
188
|
+
signal: controller.signal,
|
|
189
|
+
});
|
|
190
|
+
const promise = "promise" in request ? request.promise : request;
|
|
191
|
+
cleanupAdapter = "promise" in request ? request.cleanup : undefined;
|
|
192
|
+
if (settled)
|
|
193
|
+
runCleanup();
|
|
194
|
+
Promise.resolve(promise).then((value) => finish({ ok: true, value }), (error) => finish({ ok: false, error }));
|
|
195
|
+
}
|
|
196
|
+
catch (error) {
|
|
197
|
+
finish({ ok: false, error });
|
|
198
|
+
}
|
|
199
|
+
});
|
|
200
|
+
},
|
|
201
|
+
dispose() {
|
|
202
|
+
if (disposed)
|
|
203
|
+
return;
|
|
204
|
+
disposed = true;
|
|
205
|
+
for (const release of [...subscriptions]) {
|
|
206
|
+
try {
|
|
207
|
+
release();
|
|
208
|
+
}
|
|
209
|
+
catch {
|
|
210
|
+
/* Continue releasing remaining listeners. */
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
const reason = new errors_js_1.MiniAppError("disposed");
|
|
214
|
+
for (const cancel of [...pending]) {
|
|
215
|
+
try {
|
|
216
|
+
cancel(reason);
|
|
217
|
+
}
|
|
218
|
+
catch {
|
|
219
|
+
/* Continue cancelling remaining calls. */
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
},
|
|
223
|
+
};
|
|
224
|
+
return client;
|
|
225
|
+
}
|
|
226
|
+
function supports(target, capability) {
|
|
227
|
+
if (!target)
|
|
228
|
+
return false;
|
|
229
|
+
return "adapter" in target
|
|
230
|
+
? target.supports(capability)
|
|
231
|
+
: target.capabilities.has(capability);
|
|
232
|
+
}
|
|
233
|
+
function subscribe(client, event, listener) {
|
|
234
|
+
return client?.on(event, listener) ?? (() => { });
|
|
235
|
+
}
|
|
@@ -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;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MiniAppError = void 0;
|
|
4
|
+
exports.normalizeMiniAppError = normalizeMiniAppError;
|
|
5
|
+
class MiniAppError extends Error {
|
|
6
|
+
code;
|
|
7
|
+
constructor(code, message = code, options) {
|
|
8
|
+
super(message, options);
|
|
9
|
+
this.code = code;
|
|
10
|
+
this.name = "MiniAppError";
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
exports.MiniAppError = MiniAppError;
|
|
14
|
+
function normalizeMiniAppError(error) {
|
|
15
|
+
if (error instanceof MiniAppError)
|
|
16
|
+
return error;
|
|
17
|
+
if (error instanceof Error) {
|
|
18
|
+
return new MiniAppError("failed", error.message || "Host request failed", {
|
|
19
|
+
cause: error,
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
return new MiniAppError("failed", "Host request failed", { cause: error });
|
|
23
|
+
}
|
|
@@ -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>;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.requestContact = exports.requestWriteAccess = void 0;
|
|
4
|
+
exports.shareMessage = shareMessage;
|
|
5
|
+
exports.shareToStory = shareToStory;
|
|
6
|
+
const requestWriteAccess = (client, options) => client.call("requestWriteAccess", undefined, {
|
|
7
|
+
timeoutMs: 60_000,
|
|
8
|
+
...options,
|
|
9
|
+
});
|
|
10
|
+
exports.requestWriteAccess = requestWriteAccess;
|
|
11
|
+
const requestContact = (client, options) => client.call("requestContact", undefined, { timeoutMs: 60_000, ...options });
|
|
12
|
+
exports.requestContact = requestContact;
|
|
13
|
+
function shareMessage(client, id, options) {
|
|
14
|
+
if (typeof id !== "string" || !id.length || id.length > 128) {
|
|
15
|
+
return Promise.reject(new TypeError("Invalid prepared message ID"));
|
|
16
|
+
}
|
|
17
|
+
return client.call("shareMessage", { id }, { timeoutMs: 300_000, ...options });
|
|
18
|
+
}
|
|
19
|
+
function validateStoryUrl(value, media) {
|
|
20
|
+
if (typeof value !== "string" ||
|
|
21
|
+
value.length > 8192 ||
|
|
22
|
+
!/^https?:\/\//i.test(value) ||
|
|
23
|
+
/[\s\\\u0000-\u001f\u007f]/.test(value)) {
|
|
24
|
+
throw new TypeError("Invalid story URL");
|
|
25
|
+
}
|
|
26
|
+
const url = new URL(value);
|
|
27
|
+
if (!url.hostname ||
|
|
28
|
+
url.username ||
|
|
29
|
+
url.password ||
|
|
30
|
+
(media && url.protocol !== "https:")) {
|
|
31
|
+
throw new TypeError("Invalid story URL");
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
async function shareToStory(client, mediaUrl, params, options) {
|
|
35
|
+
validateStoryUrl(mediaUrl, true);
|
|
36
|
+
if (params?.text !== undefined &&
|
|
37
|
+
(typeof params.text !== "string" || params.text.length > 2048)) {
|
|
38
|
+
throw new TypeError("Invalid story caption");
|
|
39
|
+
}
|
|
40
|
+
if (params?.link) {
|
|
41
|
+
validateStoryUrl(params.link.url, false);
|
|
42
|
+
if (params.link.name !== undefined &&
|
|
43
|
+
(typeof params.link.name !== "string" || params.link.name.length > 48)) {
|
|
44
|
+
throw new TypeError("Invalid story link label");
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return client.call("shareToStory", { mediaUrl, params }, options);
|
|
48
|
+
}
|
|
@@ -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";
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./adapter.js"), exports);
|
|
18
|
+
__exportStar(require("./appearance.js"), exports);
|
|
19
|
+
__exportStar(require("./async.js"), exports);
|
|
20
|
+
__exportStar(require("./client.js"), exports);
|
|
21
|
+
__exportStar(require("./errors.js"), exports);
|
|
22
|
+
__exportStar(require("./features.js"), exports);
|
|
23
|
+
__exportStar(require("./protocol.js"), exports);
|
|
24
|
+
__exportStar(require("./session.js"), exports);
|
|
25
|
+
__exportStar(require("./storage.js"), exports);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type":"commonjs"}
|