@nebula-rn/client 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/chooseMedia/index.d.ts +22 -0
- package/dist/chooseMedia/index.js +4 -0
- package/dist/compressImage/index.d.ts +7 -0
- package/dist/compressImage/index.js +4 -0
- package/dist/descriptions.d.ts +9 -0
- package/dist/descriptions.js +259 -0
- package/dist/file.d.ts +51 -0
- package/dist/file.js +14 -0
- package/dist/getAppBaseInfo/index.d.ts +6 -0
- package/dist/getAppBaseInfo/index.js +4 -0
- package/dist/getClipboardData/index.d.ts +1 -0
- package/dist/getClipboardData/index.js +4 -0
- package/dist/getFileInfo/index.d.ts +9 -0
- package/dist/getFileInfo/index.js +4 -0
- package/dist/getFileSystemManager/index.d.ts +70 -0
- package/dist/getFileSystemManager/index.js +43 -0
- package/dist/getImageInfo/index.d.ts +16 -0
- package/dist/getImageInfo/index.js +20 -0
- package/dist/getLocation/index.d.ts +15 -0
- package/dist/getLocation/index.js +4 -0
- package/dist/getScreenBrightness/index.d.ts +1 -0
- package/dist/getScreenBrightness/index.js +4 -0
- package/dist/getStorage/index.d.ts +1 -0
- package/dist/getStorage/index.js +8 -0
- package/dist/getStorageInfo/index.d.ts +6 -0
- package/dist/getStorageInfo/index.js +12 -0
- package/dist/getSystemInfo/index.d.ts +1 -0
- package/dist/getSystemInfo/index.js +4 -0
- package/dist/index.d.ts +26 -0
- package/dist/index.js +26 -0
- package/dist/location.d.ts +10 -0
- package/dist/location.js +18 -0
- package/dist/makePhoneCall/index.d.ts +1 -0
- package/dist/makePhoneCall/index.js +4 -0
- package/dist/media.d.ts +5 -0
- package/dist/media.js +8 -0
- package/dist/miniAppUpdate/index.d.ts +3 -0
- package/dist/miniAppUpdate/index.js +7 -0
- package/dist/network.d.ts +9 -0
- package/dist/network.js +15 -0
- package/dist/onUserCaptureScreen/index.d.ts +1 -0
- package/dist/onUserCaptureScreen/index.js +11 -0
- package/dist/previewImage/index.d.ts +8 -0
- package/dist/previewImage/index.js +4 -0
- package/dist/runtime/host.d.ts +41 -0
- package/dist/runtime/host.js +152 -0
- package/dist/scanCode/contract.d.ts +7 -0
- package/dist/scanCode/contract.js +8 -0
- package/dist/scanCode/icon_close.png +0 -0
- package/dist/scanCode/icon_pic.png +0 -0
- package/dist/scanCode/index.d.ts +6 -0
- package/dist/scanCode/index.js +72 -0
- package/dist/scanCode/types.d.ts +25 -0
- package/dist/scanCode/types.js +8 -0
- package/dist/sensors.d.ts +13 -0
- package/dist/sensors.js +15 -0
- package/dist/setClipboardData/index.d.ts +1 -0
- package/dist/setClipboardData/index.js +2 -0
- package/dist/setStorage/index.d.ts +5 -0
- package/dist/setStorage/index.js +5 -0
- package/dist/storage.d.ts +9 -0
- package/dist/storage.js +36 -0
- package/dist/uploadFile/index.d.ts +30 -0
- package/dist/uploadFile/index.js +6 -0
- package/package.json +77 -0
- package/src/chooseMedia/index.ts +36 -0
- package/src/compressImage/index.ts +14 -0
- package/src/descriptions.ts +294 -0
- package/src/file.ts +89 -0
- package/src/getAppBaseInfo/index.ts +10 -0
- package/src/getClipboardData/index.ts +5 -0
- package/src/getFileInfo/index.ts +17 -0
- package/src/getFileSystemManager/index.ts +141 -0
- package/src/getImageInfo/index.ts +40 -0
- package/src/getLocation/index.ts +23 -0
- package/src/getScreenBrightness/index.ts +5 -0
- package/src/getStorage/index.ts +10 -0
- package/src/getStorageInfo/index.ts +20 -0
- package/src/getSystemInfo/index.ts +5 -0
- package/src/index.ts +26 -0
- package/src/location.ts +40 -0
- package/src/makePhoneCall/index.ts +5 -0
- package/src/media.ts +19 -0
- package/src/miniAppUpdate/index.ts +20 -0
- package/src/network.ts +36 -0
- package/src/onUserCaptureScreen/index.ts +17 -0
- package/src/previewImage/index.tsx +13 -0
- package/src/runtime/host.ts +356 -0
- package/src/scanCode/contract.ts +20 -0
- package/src/scanCode/icon_close.png +0 -0
- package/src/scanCode/icon_pic.png +0 -0
- package/src/scanCode/index.tsx +127 -0
- package/src/scanCode/types.ts +38 -0
- package/src/sensors.ts +48 -0
- package/src/setClipboardData/index.ts +4 -0
- package/src/setStorage/index.ts +11 -0
- package/src/storage.ts +51 -0
- package/src/uploadFile/index.ts +57 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export * from './chooseMedia';
|
|
2
|
+
export * from './compressImage';
|
|
3
|
+
export * from './descriptions';
|
|
4
|
+
export * from './file';
|
|
5
|
+
export * from './getAppBaseInfo';
|
|
6
|
+
export * from './getClipboardData';
|
|
7
|
+
export * from './getFileInfo';
|
|
8
|
+
export * from './getFileSystemManager';
|
|
9
|
+
export * from './getImageInfo';
|
|
10
|
+
export * from './getLocation';
|
|
11
|
+
export * from './getScreenBrightness';
|
|
12
|
+
export * from './getStorage';
|
|
13
|
+
export * from './getStorageInfo';
|
|
14
|
+
export * from './getSystemInfo';
|
|
15
|
+
export * from './location';
|
|
16
|
+
export * from './makePhoneCall';
|
|
17
|
+
export * from './media';
|
|
18
|
+
export * from './miniAppUpdate';
|
|
19
|
+
export * from './network';
|
|
20
|
+
export * from './onUserCaptureScreen';
|
|
21
|
+
export * from './previewImage';
|
|
22
|
+
export * from './scanCode';
|
|
23
|
+
export * from './sensors';
|
|
24
|
+
export * from './setClipboardData';
|
|
25
|
+
export * from './setStorage';
|
|
26
|
+
export * from './storage';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export * from './chooseMedia';
|
|
2
|
+
export * from './compressImage';
|
|
3
|
+
export * from './descriptions';
|
|
4
|
+
export * from './file';
|
|
5
|
+
export * from './getAppBaseInfo';
|
|
6
|
+
export * from './getClipboardData';
|
|
7
|
+
export * from './getFileInfo';
|
|
8
|
+
export * from './getFileSystemManager';
|
|
9
|
+
export * from './getImageInfo';
|
|
10
|
+
export * from './getLocation';
|
|
11
|
+
export * from './getScreenBrightness';
|
|
12
|
+
export * from './getStorage';
|
|
13
|
+
export * from './getStorageInfo';
|
|
14
|
+
export * from './getSystemInfo';
|
|
15
|
+
export * from './location';
|
|
16
|
+
export * from './makePhoneCall';
|
|
17
|
+
export * from './media';
|
|
18
|
+
export * from './miniAppUpdate';
|
|
19
|
+
export * from './network';
|
|
20
|
+
export * from './onUserCaptureScreen';
|
|
21
|
+
export * from './previewImage';
|
|
22
|
+
export * from './scanCode';
|
|
23
|
+
export * from './sensors';
|
|
24
|
+
export * from './setClipboardData';
|
|
25
|
+
export * from './setStorage';
|
|
26
|
+
export * from './storage';
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export interface LocationData {
|
|
2
|
+
accuracy: number;
|
|
3
|
+
altitude: number | null;
|
|
4
|
+
latitude: number;
|
|
5
|
+
longitude: number;
|
|
6
|
+
speed: number | null;
|
|
7
|
+
timestamp: number;
|
|
8
|
+
}
|
|
9
|
+
export type UnsubscribeFn = () => void | Promise<void>;
|
|
10
|
+
export declare const onLocationChange: (enableHighAccuracy: boolean | undefined, onSuccess: (data: LocationData) => void, onError?: (error: unknown) => void) => UnsubscribeFn;
|
package/dist/location.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { subscribeToHostEvent } from './runtime/host';
|
|
2
|
+
export const onLocationChange = (enableHighAccuracy = true, onSuccess, onError) => {
|
|
3
|
+
let unsubscribePromise = null;
|
|
4
|
+
const handle = subscribeToHostEvent('locationChange', {
|
|
5
|
+
enableHighAccuracy,
|
|
6
|
+
}, onSuccess).catch(error => {
|
|
7
|
+
onError?.(error);
|
|
8
|
+
throw error;
|
|
9
|
+
});
|
|
10
|
+
return () => {
|
|
11
|
+
if (!unsubscribePromise) {
|
|
12
|
+
unsubscribePromise = handle
|
|
13
|
+
.then(subscription => subscription.unsubscribe())
|
|
14
|
+
.catch(() => { });
|
|
15
|
+
}
|
|
16
|
+
return unsubscribePromise;
|
|
17
|
+
};
|
|
18
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function makePhoneCall(phoneNumber: string): Promise<boolean>;
|
package/dist/media.d.ts
ADDED
package/dist/media.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { invokeHostApi } from '../runtime/host';
|
|
2
|
+
export function getMiniAppUpdateInfo() {
|
|
3
|
+
return invokeHostApi('getMiniAppUpdateInfo', {}, '1.0', 15000);
|
|
4
|
+
}
|
|
5
|
+
export function applyMiniAppUpdate() {
|
|
6
|
+
return invokeHostApi('applyMiniAppUpdate', {}, '1.0', 120000);
|
|
7
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export interface GetNetworkTypeResult {
|
|
2
|
+
networkType: string;
|
|
3
|
+
}
|
|
4
|
+
export interface NetworkStatusChangeResult {
|
|
5
|
+
isConnected: boolean;
|
|
6
|
+
networkType: string;
|
|
7
|
+
}
|
|
8
|
+
export declare const getNetworkType: () => Promise<string>;
|
|
9
|
+
export declare const onNetworkStatusChange: (callback: (res: NetworkStatusChangeResult) => void) => () => Promise<void>;
|
package/dist/network.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { invokeHostApi, subscribeToHostEvent } from './runtime/host';
|
|
2
|
+
export const getNetworkType = async () => {
|
|
3
|
+
const result = await invokeHostApi('getNetworkType');
|
|
4
|
+
return result.networkType;
|
|
5
|
+
};
|
|
6
|
+
export const onNetworkStatusChange = (callback) => {
|
|
7
|
+
let unsubscribePromise = null;
|
|
8
|
+
const handle = subscribeToHostEvent('networkStatusChange', {}, callback);
|
|
9
|
+
return () => {
|
|
10
|
+
if (!unsubscribePromise) {
|
|
11
|
+
unsubscribePromise = handle.then(subscription => subscription.unsubscribe());
|
|
12
|
+
}
|
|
13
|
+
return unsubscribePromise;
|
|
14
|
+
};
|
|
15
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const onUserCaptureScreen: (callback: () => void) => () => Promise<void>;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { subscribeToHostEvent } from '../runtime/host';
|
|
2
|
+
export const onUserCaptureScreen = (callback) => {
|
|
3
|
+
let unsubscribePromise = null;
|
|
4
|
+
const handle = subscribeToHostEvent('userCaptureScreen', {}, () => callback());
|
|
5
|
+
return () => {
|
|
6
|
+
if (!unsubscribePromise) {
|
|
7
|
+
unsubscribePromise = handle.then(subscription => subscription.unsubscribe());
|
|
8
|
+
}
|
|
9
|
+
return unsubscribePromise;
|
|
10
|
+
};
|
|
11
|
+
};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { NebulaApiInvokeResult } from '@nebula-rn/sdk';
|
|
2
|
+
export type HostApiEventMessage<TPayload = unknown> = {
|
|
3
|
+
__nebulaApiEvent: 'v1';
|
|
4
|
+
apiName: string;
|
|
5
|
+
channel: string;
|
|
6
|
+
subscriptionId?: string;
|
|
7
|
+
taskId?: string;
|
|
8
|
+
payload?: TPayload;
|
|
9
|
+
};
|
|
10
|
+
type HostEventListener<TPayload> = (payload: TPayload) => void;
|
|
11
|
+
type HostTaskProgressListener<TProgress> = (payload: TProgress) => void;
|
|
12
|
+
type HostTaskHeadersListener<THeaders> = (payload: THeaders) => void;
|
|
13
|
+
export type HostTask<TData, TProgress, THeaders> = {
|
|
14
|
+
abort(): Promise<void>;
|
|
15
|
+
onProgressUpdate(listener: HostTaskProgressListener<TProgress>): void;
|
|
16
|
+
offProgressUpdate(listener: HostTaskProgressListener<TProgress>): void;
|
|
17
|
+
onHeadersReceived(listener: HostTaskHeadersListener<THeaders>): void;
|
|
18
|
+
offHeadersReceived(listener: HostTaskHeadersListener<THeaders>): void;
|
|
19
|
+
then<TResult1 = TData, TResult2 = never>(onfulfilled?: ((value: TData) => TResult1 | PromiseLike<TResult1>) | undefined | null, onrejected?: ((reason: unknown) => TResult2 | PromiseLike<TResult2>) | undefined | null): Promise<TResult1 | TResult2>;
|
|
20
|
+
catch<TResult = never>(onrejected?: ((reason: unknown) => TResult | PromiseLike<TResult>) | undefined | null): Promise<TData | TResult>;
|
|
21
|
+
};
|
|
22
|
+
export type HostSubscriptionHandle = {
|
|
23
|
+
subscriptionId: string;
|
|
24
|
+
unsubscribe: () => Promise<void>;
|
|
25
|
+
};
|
|
26
|
+
export declare function createHostApiError(apiName: string, error: unknown, fallbackCode?: string): Error & {
|
|
27
|
+
code: string;
|
|
28
|
+
};
|
|
29
|
+
export declare function invokeHostApi<TData = unknown>(apiName: string, payload?: object, version?: string, timeoutMs?: number): Promise<TData>;
|
|
30
|
+
export declare function invokeHostApiResult<TData = unknown>(apiName: string, payload?: object, version?: string, timeoutMs?: number): Promise<NebulaApiInvokeResult<TData>>;
|
|
31
|
+
export declare function addHostApiEventListener<TPayload = unknown>(apiName: string, channel: string, listener: HostEventListener<TPayload>, matcher?: (message: HostApiEventMessage<TPayload>) => boolean): () => void;
|
|
32
|
+
export declare function subscribeToHostEvent<TPayload = unknown>(apiName: string, payload: object, onEvent: HostEventListener<TPayload>, options?: {
|
|
33
|
+
version?: string;
|
|
34
|
+
timeoutMs?: number;
|
|
35
|
+
unsubscribePayload?: object;
|
|
36
|
+
}): Promise<HostSubscriptionHandle>;
|
|
37
|
+
export declare function createHostTask<TData, TProgress, THeaders>(apiName: string, payload: object, options?: {
|
|
38
|
+
version?: string;
|
|
39
|
+
timeoutMs?: number;
|
|
40
|
+
}): HostTask<TData, TProgress, THeaders>;
|
|
41
|
+
export {};
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import { Miniapp } from '@nebula-rn/sdk';
|
|
2
|
+
function isRecord(value) {
|
|
3
|
+
return !!value && typeof value === 'object' && !Array.isArray(value);
|
|
4
|
+
}
|
|
5
|
+
function isHostApiEventMessage(value) {
|
|
6
|
+
return (value.__nebulaApiEvent === 'v1' &&
|
|
7
|
+
typeof value.apiName === 'string' &&
|
|
8
|
+
typeof value.channel === 'string');
|
|
9
|
+
}
|
|
10
|
+
export function createHostApiError(apiName, error, fallbackCode = 'HOST_API_FAILED') {
|
|
11
|
+
const details = isRecord(error) ? error : undefined;
|
|
12
|
+
const code = typeof details?.code === 'string' ? details.code : fallbackCode;
|
|
13
|
+
const message = typeof details?.message === 'string'
|
|
14
|
+
? details.message
|
|
15
|
+
: error instanceof Error
|
|
16
|
+
? error.message
|
|
17
|
+
: `${apiName}:fail host request failed`;
|
|
18
|
+
const hostError = new Error(message);
|
|
19
|
+
hostError.code = code;
|
|
20
|
+
return hostError;
|
|
21
|
+
}
|
|
22
|
+
export async function invokeHostApi(apiName, payload = {}, version = '1.0', timeoutMs = 15000) {
|
|
23
|
+
const result = await Miniapp.invokeHostApi(apiName, payload, version, timeoutMs);
|
|
24
|
+
if (!result.ok) {
|
|
25
|
+
throw createHostApiError(apiName, result.error);
|
|
26
|
+
}
|
|
27
|
+
return result.data;
|
|
28
|
+
}
|
|
29
|
+
export async function invokeHostApiResult(apiName, payload = {}, version = '1.0', timeoutMs = 15000) {
|
|
30
|
+
return Miniapp.invokeHostApi(apiName, payload, version, timeoutMs);
|
|
31
|
+
}
|
|
32
|
+
export function addHostApiEventListener(apiName, channel, listener, matcher) {
|
|
33
|
+
return Miniapp.onHostMessage(event => {
|
|
34
|
+
const { message } = event;
|
|
35
|
+
if (!isRecord(message) || !isHostApiEventMessage(message)) {
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
if (message.apiName !== apiName || message.channel !== channel) {
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
const typedMessage = message;
|
|
42
|
+
if (matcher && !matcher(typedMessage)) {
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
listener((typedMessage.payload ?? null));
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
export async function subscribeToHostEvent(apiName, payload, onEvent, options) {
|
|
49
|
+
const version = options?.version ?? '1.0';
|
|
50
|
+
const timeoutMs = options?.timeoutMs ?? 15000;
|
|
51
|
+
const result = await invokeHostApi(`${apiName}.subscribe`, payload, version, timeoutMs);
|
|
52
|
+
const stopListening = addHostApiEventListener(apiName, 'subscription', onEvent, message => message.subscriptionId === result.subscriptionId);
|
|
53
|
+
return {
|
|
54
|
+
subscriptionId: result.subscriptionId,
|
|
55
|
+
unsubscribe: async () => {
|
|
56
|
+
stopListening();
|
|
57
|
+
await invokeHostApi(`${apiName}.unsubscribe`, {
|
|
58
|
+
subscriptionId: result.subscriptionId,
|
|
59
|
+
...(options?.unsubscribePayload ?? {}),
|
|
60
|
+
}, version, timeoutMs);
|
|
61
|
+
},
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
class HostTaskImpl {
|
|
65
|
+
promise;
|
|
66
|
+
aborter;
|
|
67
|
+
progressListeners = new Set();
|
|
68
|
+
headersListeners = new Set();
|
|
69
|
+
cleanup;
|
|
70
|
+
constructor(promise, aborter, cleanup) {
|
|
71
|
+
this.promise = promise;
|
|
72
|
+
this.aborter = aborter;
|
|
73
|
+
this.cleanup = cleanup;
|
|
74
|
+
}
|
|
75
|
+
abort() {
|
|
76
|
+
this.cleanup();
|
|
77
|
+
return this.aborter();
|
|
78
|
+
}
|
|
79
|
+
onProgressUpdate(listener) {
|
|
80
|
+
this.progressListeners.add(listener);
|
|
81
|
+
}
|
|
82
|
+
offProgressUpdate(listener) {
|
|
83
|
+
this.progressListeners.delete(listener);
|
|
84
|
+
}
|
|
85
|
+
onHeadersReceived(listener) {
|
|
86
|
+
this.headersListeners.add(listener);
|
|
87
|
+
}
|
|
88
|
+
offHeadersReceived(listener) {
|
|
89
|
+
this.headersListeners.delete(listener);
|
|
90
|
+
}
|
|
91
|
+
emitProgress(payload) {
|
|
92
|
+
this.progressListeners.forEach(listener => listener(payload));
|
|
93
|
+
}
|
|
94
|
+
emitHeaders(payload) {
|
|
95
|
+
this.headersListeners.forEach(listener => listener(payload));
|
|
96
|
+
}
|
|
97
|
+
then(onfulfilled, onrejected) {
|
|
98
|
+
return this.promise.then(onfulfilled, onrejected);
|
|
99
|
+
}
|
|
100
|
+
catch(onrejected) {
|
|
101
|
+
return this.promise.catch(onrejected);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
export function createHostTask(apiName, payload, options) {
|
|
105
|
+
const version = options?.version ?? '1.0';
|
|
106
|
+
const timeoutMs = options?.timeoutMs ?? 15000;
|
|
107
|
+
let stopResultListener = () => { };
|
|
108
|
+
let stopProgressListener = () => { };
|
|
109
|
+
let stopHeadersListener = () => { };
|
|
110
|
+
let startedTaskId = null;
|
|
111
|
+
function cleanup() {
|
|
112
|
+
stopResultListener();
|
|
113
|
+
stopProgressListener();
|
|
114
|
+
stopHeadersListener();
|
|
115
|
+
}
|
|
116
|
+
const startTask = invokeHostApi(`${apiName}.start`, payload, version, timeoutMs);
|
|
117
|
+
const promise = startTask.then(({ taskId }) => new Promise((resolve, reject) => {
|
|
118
|
+
startedTaskId = taskId;
|
|
119
|
+
stopResultListener = addHostApiEventListener(apiName, 'task.result', resultPayload => {
|
|
120
|
+
cleanup();
|
|
121
|
+
if (isRecord(resultPayload) &&
|
|
122
|
+
typeof resultPayload.ok === 'boolean') {
|
|
123
|
+
const result = resultPayload;
|
|
124
|
+
if (result.ok) {
|
|
125
|
+
resolve(result.data);
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
reject(createHostApiError(apiName, result.error));
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
resolve(resultPayload);
|
|
132
|
+
}, message => message.taskId === taskId);
|
|
133
|
+
stopProgressListener = addHostApiEventListener(apiName, 'task.progress', progressPayload => {
|
|
134
|
+
task.emitProgress(progressPayload);
|
|
135
|
+
}, message => message.taskId === taskId);
|
|
136
|
+
stopHeadersListener = addHostApiEventListener(apiName, 'task.headers', headersPayload => {
|
|
137
|
+
task.emitHeaders(headersPayload);
|
|
138
|
+
}, message => message.taskId === taskId);
|
|
139
|
+
}));
|
|
140
|
+
const task = new HostTaskImpl(promise, async () => {
|
|
141
|
+
cleanup();
|
|
142
|
+
const taskId = startedTaskId ??
|
|
143
|
+
(await startTask.then(result => result.taskId, () => null));
|
|
144
|
+
if (!taskId) {
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
await invokeHostApi(`${apiName}.abort`, {
|
|
148
|
+
taskId,
|
|
149
|
+
}, version, timeoutMs);
|
|
150
|
+
}, cleanup);
|
|
151
|
+
return task;
|
|
152
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { ScanCodeError } from './types';
|
|
2
|
+
import type { ScanCodeCapabilityName, ScanCodeCapabilityVersion, ScanCodeErrorCode } from './types';
|
|
3
|
+
export declare const SCAN_CODE_CAPABILITY_NAME: ScanCodeCapabilityName;
|
|
4
|
+
export declare const SCAN_CODE_CAPABILITY_VERSION: ScanCodeCapabilityVersion;
|
|
5
|
+
export declare const SCAN_CODE_DEFAULT_TYPES: readonly ["qr", "ean-13", "code-128"];
|
|
6
|
+
export declare const SCAN_CODE_MINIMUM_HOST_BRIDGE_VERSION = "1.0";
|
|
7
|
+
export declare function createScanCodeError(code: ScanCodeErrorCode, message: string): ScanCodeError;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { ScanCodeError } from './types';
|
|
2
|
+
export const SCAN_CODE_CAPABILITY_NAME = 'scanCode';
|
|
3
|
+
export const SCAN_CODE_CAPABILITY_VERSION = '1.0';
|
|
4
|
+
export const SCAN_CODE_DEFAULT_TYPES = ['qr', 'ean-13', 'code-128'];
|
|
5
|
+
export const SCAN_CODE_MINIMUM_HOST_BRIDGE_VERSION = '1.0';
|
|
6
|
+
export function createScanCodeError(code, message) {
|
|
7
|
+
return new ScanCodeError(code, message);
|
|
8
|
+
}
|
|
Binary file
|
|
Binary file
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { ScanCodeData, ScanCodeOptions, ScanCodeResult } from './types';
|
|
2
|
+
export type { ScanCodeCapabilityName, ScanCodeCapabilityVersion, ScanCodeData, ScanCodeErrorCode, ScanCodeFailureResult, ScanCodeOptions, ScanCodeResult, ScanCodeSuccessResult, } from './types';
|
|
3
|
+
export { ScanCodeError } from './types';
|
|
4
|
+
export { SCAN_CODE_CAPABILITY_NAME, SCAN_CODE_CAPABILITY_VERSION, SCAN_CODE_DEFAULT_TYPES, SCAN_CODE_MINIMUM_HOST_BRIDGE_VERSION, } from './contract';
|
|
5
|
+
export declare function scanCode(options?: ScanCodeOptions): Promise<ScanCodeData>;
|
|
6
|
+
export declare function scanCodeSafe(options?: ScanCodeOptions): Promise<ScanCodeResult>;
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { Miniapp } from '@nebula-rn/sdk';
|
|
2
|
+
import { createScanCodeError, SCAN_CODE_CAPABILITY_NAME, SCAN_CODE_CAPABILITY_VERSION, } from './contract';
|
|
3
|
+
export { ScanCodeError } from './types';
|
|
4
|
+
export { SCAN_CODE_CAPABILITY_NAME, SCAN_CODE_CAPABILITY_VERSION, SCAN_CODE_DEFAULT_TYPES, SCAN_CODE_MINIMUM_HOST_BRIDGE_VERSION, } from './contract';
|
|
5
|
+
function isRecord(value) {
|
|
6
|
+
return !!value && typeof value === 'object' && !Array.isArray(value);
|
|
7
|
+
}
|
|
8
|
+
function toScanCodeErrorCode(code) {
|
|
9
|
+
switch (code) {
|
|
10
|
+
case 'PERMISSION_DENIED':
|
|
11
|
+
case 'USER_CANCELLED':
|
|
12
|
+
case 'UNSUPPORTED_SOURCE':
|
|
13
|
+
case 'SCAN_FAILED':
|
|
14
|
+
return code;
|
|
15
|
+
default:
|
|
16
|
+
return 'SCAN_FAILED';
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
function toScanCodeFailureResult(error) {
|
|
20
|
+
if (isRecord(error)) {
|
|
21
|
+
return {
|
|
22
|
+
ok: false,
|
|
23
|
+
error: createScanCodeError(toScanCodeErrorCode(error.code), typeof error.message === 'string'
|
|
24
|
+
? error.message
|
|
25
|
+
: 'scanCode:fail host request failed'),
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
if (error instanceof Error) {
|
|
29
|
+
return {
|
|
30
|
+
ok: false,
|
|
31
|
+
error: createScanCodeError('SCAN_FAILED', error.message),
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
return {
|
|
35
|
+
ok: false,
|
|
36
|
+
error: createScanCodeError('SCAN_FAILED', 'scanCode:fail unknown host error'),
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
function toScanCodeSuccessResult(data) {
|
|
40
|
+
if (!isRecord(data)) {
|
|
41
|
+
return {
|
|
42
|
+
ok: false,
|
|
43
|
+
error: createScanCodeError('SCAN_FAILED', 'scanCode:fail invalid host response'),
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
return {
|
|
47
|
+
ok: true,
|
|
48
|
+
data: {
|
|
49
|
+
result: typeof data.result === 'string' ? data.result : undefined,
|
|
50
|
+
scanType: typeof data.scanType === 'string' ? data.scanType : 'unknown',
|
|
51
|
+
rawData: data.rawData,
|
|
52
|
+
},
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
export async function scanCode(options = {}) {
|
|
56
|
+
const result = await scanCodeSafe(options);
|
|
57
|
+
if (!result.ok) {
|
|
58
|
+
throw result.error;
|
|
59
|
+
}
|
|
60
|
+
return result.data;
|
|
61
|
+
}
|
|
62
|
+
export async function scanCodeSafe(options = {}) {
|
|
63
|
+
try {
|
|
64
|
+
const result = await Miniapp.invokeHostApi(SCAN_CODE_CAPABILITY_NAME, options, SCAN_CODE_CAPABILITY_VERSION, 120000);
|
|
65
|
+
return result.ok
|
|
66
|
+
? toScanCodeSuccessResult(result.data)
|
|
67
|
+
: toScanCodeFailureResult(result.error);
|
|
68
|
+
}
|
|
69
|
+
catch (error) {
|
|
70
|
+
return toScanCodeFailureResult(error);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export type ScanCodeCapabilityName = 'scanCode';
|
|
2
|
+
export type ScanCodeCapabilityVersion = '1.0';
|
|
3
|
+
export type ScanCodeErrorCode = 'PERMISSION_DENIED' | 'USER_CANCELLED' | 'UNSUPPORTED_SOURCE' | 'SCAN_FAILED';
|
|
4
|
+
export interface ScanCodeOptions {
|
|
5
|
+
scanType?: string[];
|
|
6
|
+
onlyFromCamera?: boolean;
|
|
7
|
+
}
|
|
8
|
+
export interface ScanCodeData {
|
|
9
|
+
result: string | undefined;
|
|
10
|
+
scanType: string | 'unknown';
|
|
11
|
+
rawData?: unknown;
|
|
12
|
+
}
|
|
13
|
+
export interface ScanCodeSuccessResult {
|
|
14
|
+
ok: true;
|
|
15
|
+
data: ScanCodeData;
|
|
16
|
+
}
|
|
17
|
+
export interface ScanCodeFailureResult {
|
|
18
|
+
ok: false;
|
|
19
|
+
error: ScanCodeError;
|
|
20
|
+
}
|
|
21
|
+
export type ScanCodeResult = ScanCodeSuccessResult | ScanCodeFailureResult;
|
|
22
|
+
export declare class ScanCodeError extends Error {
|
|
23
|
+
readonly code: ScanCodeErrorCode;
|
|
24
|
+
constructor(code: ScanCodeErrorCode, message: string);
|
|
25
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export interface SensorData {
|
|
2
|
+
x: number;
|
|
3
|
+
y: number;
|
|
4
|
+
z: number;
|
|
5
|
+
timestamp?: number;
|
|
6
|
+
}
|
|
7
|
+
export interface BarometerData {
|
|
8
|
+
pressure: number;
|
|
9
|
+
}
|
|
10
|
+
export declare const onAccelerometerChange: (callback: (data: SensorData) => void, interval?: number) => () => Promise<void>;
|
|
11
|
+
export declare const onGyroscopeChange: (callback: (data: SensorData) => void, interval?: number) => () => Promise<void>;
|
|
12
|
+
export declare const onMagnetometerChange: (callback: (data: SensorData) => void, interval?: number) => () => Promise<void>;
|
|
13
|
+
export declare const onBarometerChange: (callback: (data: BarometerData) => void) => () => Promise<void>;
|
package/dist/sensors.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { subscribeToHostEvent } from './runtime/host';
|
|
2
|
+
function createSensorSubscription(apiName, callback, payload) {
|
|
3
|
+
let unsubscribePromise = null;
|
|
4
|
+
const handle = subscribeToHostEvent(apiName, payload, callback);
|
|
5
|
+
return () => {
|
|
6
|
+
if (!unsubscribePromise) {
|
|
7
|
+
unsubscribePromise = handle.then(subscription => subscription.unsubscribe());
|
|
8
|
+
}
|
|
9
|
+
return unsubscribePromise;
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
export const onAccelerometerChange = (callback, interval = 100) => createSensorSubscription('accelerometerChange', callback, { interval });
|
|
13
|
+
export const onGyroscopeChange = (callback, interval = 100) => createSensorSubscription('gyroscopeChange', callback, { interval });
|
|
14
|
+
export const onMagnetometerChange = (callback, interval = 100) => createSensorSubscription('magnetometerChange', callback, { interval });
|
|
15
|
+
export const onBarometerChange = (callback) => createSensorSubscription('barometerChange', callback, {});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const setClipboardData: (data: string) => Promise<void>;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export declare const storage: null;
|
|
2
|
+
export declare function serializeStorageValue(data: unknown): string;
|
|
3
|
+
export declare function deserializeStorageValue<T = unknown>(data: string): T;
|
|
4
|
+
export declare function setStorageItem(key: string, data: unknown): Promise<void>;
|
|
5
|
+
export declare function getStorageItem(key: string): Promise<string | undefined>;
|
|
6
|
+
export declare function removeStorageItem(key: string): Promise<void>;
|
|
7
|
+
export declare function clearStorageItems(): Promise<void>;
|
|
8
|
+
export declare function getStorageKeys(): Promise<string[]>;
|
|
9
|
+
export declare function getStorageCurrentSize(): Promise<number>;
|
package/dist/storage.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { invokeHostApi } from './runtime/host';
|
|
2
|
+
export const storage = null;
|
|
3
|
+
export function serializeStorageValue(data) {
|
|
4
|
+
return JSON.stringify(data);
|
|
5
|
+
}
|
|
6
|
+
export function deserializeStorageValue(data) {
|
|
7
|
+
return JSON.parse(data);
|
|
8
|
+
}
|
|
9
|
+
export function setStorageItem(key, data) {
|
|
10
|
+
return invokeHostApi('storage.setItem', {
|
|
11
|
+
key,
|
|
12
|
+
data: serializeStorageValue(data),
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
export async function getStorageItem(key) {
|
|
16
|
+
const result = await invokeHostApi('storage.getItem', {
|
|
17
|
+
key,
|
|
18
|
+
});
|
|
19
|
+
return result.value ?? undefined;
|
|
20
|
+
}
|
|
21
|
+
export function removeStorageItem(key) {
|
|
22
|
+
return invokeHostApi('storage.removeItem', {
|
|
23
|
+
key,
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
export function clearStorageItems() {
|
|
27
|
+
return invokeHostApi('storage.clearItems');
|
|
28
|
+
}
|
|
29
|
+
export async function getStorageKeys() {
|
|
30
|
+
const result = await invokeHostApi('storage.getKeys');
|
|
31
|
+
return result.keys;
|
|
32
|
+
}
|
|
33
|
+
export async function getStorageCurrentSize() {
|
|
34
|
+
const result = await invokeHostApi('storage.getCurrentSize');
|
|
35
|
+
return result.size;
|
|
36
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export interface UploadFileOption {
|
|
2
|
+
url: string;
|
|
3
|
+
filePath: string;
|
|
4
|
+
name: string;
|
|
5
|
+
header?: Record<string, string>;
|
|
6
|
+
formData?: Record<string, string>;
|
|
7
|
+
timeout?: number;
|
|
8
|
+
}
|
|
9
|
+
export interface UploadFileResult {
|
|
10
|
+
data: string;
|
|
11
|
+
statusCode: number;
|
|
12
|
+
}
|
|
13
|
+
export interface UploadProgressUpdateRes {
|
|
14
|
+
progress: number;
|
|
15
|
+
totalBytesSent: number;
|
|
16
|
+
totalBytesExpectedToSend: number;
|
|
17
|
+
}
|
|
18
|
+
export interface UploadHeadersReceivedRes {
|
|
19
|
+
header: Record<string, string>;
|
|
20
|
+
}
|
|
21
|
+
export interface UploadTask {
|
|
22
|
+
abort(): Promise<void>;
|
|
23
|
+
onProgressUpdate(listener: (res: UploadProgressUpdateRes) => void): void;
|
|
24
|
+
offProgressUpdate(listener: (res: UploadProgressUpdateRes) => void): void;
|
|
25
|
+
onHeadersReceived(listener: (res: UploadHeadersReceivedRes) => void): void;
|
|
26
|
+
offHeadersReceived(listener: (res: UploadHeadersReceivedRes) => void): void;
|
|
27
|
+
then<TResult1 = UploadFileResult, TResult2 = never>(onfulfilled?: ((value: UploadFileResult) => TResult1 | PromiseLike<TResult1>) | undefined | null, onrejected?: ((reason: unknown) => TResult2 | PromiseLike<TResult2>) | undefined | null): Promise<TResult1 | TResult2>;
|
|
28
|
+
catch<TResult = never>(onrejected?: ((reason: unknown) => TResult | PromiseLike<TResult>) | undefined | null): Promise<UploadFileResult | TResult>;
|
|
29
|
+
}
|
|
30
|
+
export declare const uploadFile: (options: UploadFileOption) => UploadTask;
|