@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
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { Miniapp } from '@nebula-rn/sdk';
|
|
2
|
+
import {
|
|
3
|
+
createScanCodeError,
|
|
4
|
+
SCAN_CODE_CAPABILITY_NAME,
|
|
5
|
+
SCAN_CODE_CAPABILITY_VERSION,
|
|
6
|
+
} from './contract';
|
|
7
|
+
import type {
|
|
8
|
+
ScanCodeData,
|
|
9
|
+
ScanCodeErrorCode,
|
|
10
|
+
ScanCodeOptions,
|
|
11
|
+
ScanCodeResult,
|
|
12
|
+
} from './types';
|
|
13
|
+
|
|
14
|
+
export type {
|
|
15
|
+
ScanCodeCapabilityName,
|
|
16
|
+
ScanCodeCapabilityVersion,
|
|
17
|
+
ScanCodeData,
|
|
18
|
+
ScanCodeErrorCode,
|
|
19
|
+
ScanCodeFailureResult,
|
|
20
|
+
ScanCodeOptions,
|
|
21
|
+
ScanCodeResult,
|
|
22
|
+
ScanCodeSuccessResult,
|
|
23
|
+
} from './types';
|
|
24
|
+
export { ScanCodeError } from './types';
|
|
25
|
+
export {
|
|
26
|
+
SCAN_CODE_CAPABILITY_NAME,
|
|
27
|
+
SCAN_CODE_CAPABILITY_VERSION,
|
|
28
|
+
SCAN_CODE_DEFAULT_TYPES,
|
|
29
|
+
SCAN_CODE_MINIMUM_HOST_BRIDGE_VERSION,
|
|
30
|
+
} from './contract';
|
|
31
|
+
|
|
32
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
33
|
+
return !!value && typeof value === 'object' && !Array.isArray(value);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function toScanCodeErrorCode(code: unknown): ScanCodeErrorCode {
|
|
37
|
+
switch (code) {
|
|
38
|
+
case 'PERMISSION_DENIED':
|
|
39
|
+
case 'USER_CANCELLED':
|
|
40
|
+
case 'UNSUPPORTED_SOURCE':
|
|
41
|
+
case 'SCAN_FAILED':
|
|
42
|
+
return code;
|
|
43
|
+
default:
|
|
44
|
+
return 'SCAN_FAILED';
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function toScanCodeFailureResult(
|
|
49
|
+
error: unknown,
|
|
50
|
+
): Extract<ScanCodeResult, { ok: false }> {
|
|
51
|
+
if (isRecord(error)) {
|
|
52
|
+
return {
|
|
53
|
+
ok: false,
|
|
54
|
+
error: createScanCodeError(
|
|
55
|
+
toScanCodeErrorCode(error.code),
|
|
56
|
+
typeof error.message === 'string'
|
|
57
|
+
? error.message
|
|
58
|
+
: 'scanCode:fail host request failed',
|
|
59
|
+
),
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
if (error instanceof Error) {
|
|
64
|
+
return {
|
|
65
|
+
ok: false,
|
|
66
|
+
error: createScanCodeError('SCAN_FAILED', error.message),
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return {
|
|
71
|
+
ok: false,
|
|
72
|
+
error: createScanCodeError(
|
|
73
|
+
'SCAN_FAILED',
|
|
74
|
+
'scanCode:fail unknown host error',
|
|
75
|
+
),
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function toScanCodeSuccessResult(data: unknown): ScanCodeResult {
|
|
80
|
+
if (!isRecord(data)) {
|
|
81
|
+
return {
|
|
82
|
+
ok: false,
|
|
83
|
+
error: createScanCodeError(
|
|
84
|
+
'SCAN_FAILED',
|
|
85
|
+
'scanCode:fail invalid host response',
|
|
86
|
+
),
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
return {
|
|
91
|
+
ok: true,
|
|
92
|
+
data: {
|
|
93
|
+
result: typeof data.result === 'string' ? data.result : undefined,
|
|
94
|
+
scanType: typeof data.scanType === 'string' ? data.scanType : 'unknown',
|
|
95
|
+
rawData: data.rawData,
|
|
96
|
+
},
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export async function scanCode(
|
|
101
|
+
options: ScanCodeOptions = {},
|
|
102
|
+
): Promise<ScanCodeData> {
|
|
103
|
+
const result = await scanCodeSafe(options);
|
|
104
|
+
if (!result.ok) {
|
|
105
|
+
throw result.error;
|
|
106
|
+
}
|
|
107
|
+
return result.data;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export async function scanCodeSafe(
|
|
111
|
+
options: ScanCodeOptions = {},
|
|
112
|
+
): Promise<ScanCodeResult> {
|
|
113
|
+
try {
|
|
114
|
+
const result = await Miniapp.invokeHostApi<ScanCodeData>(
|
|
115
|
+
SCAN_CODE_CAPABILITY_NAME,
|
|
116
|
+
options as Record<string, unknown>,
|
|
117
|
+
SCAN_CODE_CAPABILITY_VERSION,
|
|
118
|
+
120000,
|
|
119
|
+
);
|
|
120
|
+
|
|
121
|
+
return result.ok
|
|
122
|
+
? toScanCodeSuccessResult(result.data)
|
|
123
|
+
: toScanCodeFailureResult(result.error);
|
|
124
|
+
} catch (error) {
|
|
125
|
+
return toScanCodeFailureResult(error);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
export type ScanCodeCapabilityName = 'scanCode';
|
|
2
|
+
export type ScanCodeCapabilityVersion = '1.0';
|
|
3
|
+
|
|
4
|
+
export type ScanCodeErrorCode =
|
|
5
|
+
'PERMISSION_DENIED' | 'USER_CANCELLED' | 'UNSUPPORTED_SOURCE' | 'SCAN_FAILED';
|
|
6
|
+
|
|
7
|
+
export interface ScanCodeOptions {
|
|
8
|
+
scanType?: string[];
|
|
9
|
+
onlyFromCamera?: boolean;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface ScanCodeData {
|
|
13
|
+
result: string | undefined;
|
|
14
|
+
scanType: string | 'unknown';
|
|
15
|
+
rawData?: unknown;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface ScanCodeSuccessResult {
|
|
19
|
+
ok: true;
|
|
20
|
+
data: ScanCodeData;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface ScanCodeFailureResult {
|
|
24
|
+
ok: false;
|
|
25
|
+
error: ScanCodeError;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export type ScanCodeResult = ScanCodeSuccessResult | ScanCodeFailureResult;
|
|
29
|
+
|
|
30
|
+
export class ScanCodeError extends Error {
|
|
31
|
+
readonly code: ScanCodeErrorCode;
|
|
32
|
+
|
|
33
|
+
constructor(code: ScanCodeErrorCode, message: string) {
|
|
34
|
+
super(message);
|
|
35
|
+
this.name = 'ScanCodeError';
|
|
36
|
+
this.code = code;
|
|
37
|
+
}
|
|
38
|
+
}
|
package/src/sensors.ts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { subscribeToHostEvent } from './runtime/host';
|
|
2
|
+
|
|
3
|
+
export interface SensorData {
|
|
4
|
+
x: number;
|
|
5
|
+
y: number;
|
|
6
|
+
z: number;
|
|
7
|
+
timestamp?: number;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export interface BarometerData {
|
|
11
|
+
pressure: number;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function createSensorSubscription<TPayload>(
|
|
15
|
+
apiName: string,
|
|
16
|
+
callback: (data: TPayload) => void,
|
|
17
|
+
payload: Record<string, unknown>,
|
|
18
|
+
) {
|
|
19
|
+
let unsubscribePromise: Promise<void> | null = null;
|
|
20
|
+
const handle = subscribeToHostEvent<TPayload>(apiName, payload, callback);
|
|
21
|
+
|
|
22
|
+
return () => {
|
|
23
|
+
if (!unsubscribePromise) {
|
|
24
|
+
unsubscribePromise = handle.then(subscription =>
|
|
25
|
+
subscription.unsubscribe(),
|
|
26
|
+
);
|
|
27
|
+
}
|
|
28
|
+
return unsubscribePromise;
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export const onAccelerometerChange = (
|
|
33
|
+
callback: (data: SensorData) => void,
|
|
34
|
+
interval: number = 100,
|
|
35
|
+
) => createSensorSubscription('accelerometerChange', callback, { interval });
|
|
36
|
+
|
|
37
|
+
export const onGyroscopeChange = (
|
|
38
|
+
callback: (data: SensorData) => void,
|
|
39
|
+
interval: number = 100,
|
|
40
|
+
) => createSensorSubscription('gyroscopeChange', callback, { interval });
|
|
41
|
+
|
|
42
|
+
export const onMagnetometerChange = (
|
|
43
|
+
callback: (data: SensorData) => void,
|
|
44
|
+
interval: number = 100,
|
|
45
|
+
) => createSensorSubscription('magnetometerChange', callback, { interval });
|
|
46
|
+
|
|
47
|
+
export const onBarometerChange = (callback: (data: BarometerData) => void) =>
|
|
48
|
+
createSensorSubscription('barometerChange', callback, {});
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { setStorageItem } from '../storage';
|
|
2
|
+
|
|
3
|
+
export interface SetStorageOption {
|
|
4
|
+
key: string;
|
|
5
|
+
data: unknown;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export const setStorage = (options: SetStorageOption) => {
|
|
9
|
+
const { key, data } = options;
|
|
10
|
+
return setStorageItem(key, data);
|
|
11
|
+
};
|
package/src/storage.ts
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { invokeHostApi } from './runtime/host';
|
|
2
|
+
|
|
3
|
+
export const storage = null;
|
|
4
|
+
|
|
5
|
+
export function serializeStorageValue(data: unknown): string {
|
|
6
|
+
return JSON.stringify(data);
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function deserializeStorageValue<T = unknown>(data: string): T {
|
|
10
|
+
return JSON.parse(data) as T;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function setStorageItem(key: string, data: unknown): Promise<void> {
|
|
14
|
+
return invokeHostApi<void>('storage.setItem', {
|
|
15
|
+
key,
|
|
16
|
+
data: serializeStorageValue(data),
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export async function getStorageItem(key: string): Promise<string | undefined> {
|
|
21
|
+
const result = await invokeHostApi<{ value?: string | null }>(
|
|
22
|
+
'storage.getItem',
|
|
23
|
+
{
|
|
24
|
+
key,
|
|
25
|
+
},
|
|
26
|
+
);
|
|
27
|
+
|
|
28
|
+
return result.value ?? undefined;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function removeStorageItem(key: string): Promise<void> {
|
|
32
|
+
return invokeHostApi<void>('storage.removeItem', {
|
|
33
|
+
key,
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function clearStorageItems(): Promise<void> {
|
|
38
|
+
return invokeHostApi<void>('storage.clearItems');
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export async function getStorageKeys(): Promise<string[]> {
|
|
42
|
+
const result = await invokeHostApi<{ keys: string[] }>('storage.getKeys');
|
|
43
|
+
return result.keys;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export async function getStorageCurrentSize(): Promise<number> {
|
|
47
|
+
const result = await invokeHostApi<{ size: number }>(
|
|
48
|
+
'storage.getCurrentSize',
|
|
49
|
+
);
|
|
50
|
+
return result.size;
|
|
51
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { createHostTask } from '../runtime/host';
|
|
2
|
+
|
|
3
|
+
export interface UploadFileOption {
|
|
4
|
+
url: string;
|
|
5
|
+
filePath: string;
|
|
6
|
+
name: string;
|
|
7
|
+
header?: Record<string, string>;
|
|
8
|
+
formData?: Record<string, string>;
|
|
9
|
+
timeout?: number;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface UploadFileResult {
|
|
13
|
+
data: string;
|
|
14
|
+
statusCode: number;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface UploadProgressUpdateRes {
|
|
18
|
+
progress: number;
|
|
19
|
+
totalBytesSent: number;
|
|
20
|
+
totalBytesExpectedToSend: number;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface UploadHeadersReceivedRes {
|
|
24
|
+
header: Record<string, string>;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface UploadTask {
|
|
28
|
+
abort(): Promise<void>;
|
|
29
|
+
onProgressUpdate(listener: (res: UploadProgressUpdateRes) => void): void;
|
|
30
|
+
offProgressUpdate(listener: (res: UploadProgressUpdateRes) => void): void;
|
|
31
|
+
onHeadersReceived(listener: (res: UploadHeadersReceivedRes) => void): void;
|
|
32
|
+
offHeadersReceived(listener: (res: UploadHeadersReceivedRes) => void): void;
|
|
33
|
+
then<TResult1 = UploadFileResult, TResult2 = never>(
|
|
34
|
+
onfulfilled?:
|
|
35
|
+
| ((value: UploadFileResult) => TResult1 | PromiseLike<TResult1>)
|
|
36
|
+
| undefined
|
|
37
|
+
| null,
|
|
38
|
+
onrejected?:
|
|
39
|
+
| ((reason: unknown) => TResult2 | PromiseLike<TResult2>)
|
|
40
|
+
| undefined
|
|
41
|
+
| null,
|
|
42
|
+
): Promise<TResult1 | TResult2>;
|
|
43
|
+
catch<TResult = never>(
|
|
44
|
+
onrejected?:
|
|
45
|
+
((reason: unknown) => TResult | PromiseLike<TResult>) | undefined | null,
|
|
46
|
+
): Promise<UploadFileResult | TResult>;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export const uploadFile = (options: UploadFileOption): UploadTask => {
|
|
50
|
+
return createHostTask<
|
|
51
|
+
UploadFileResult,
|
|
52
|
+
UploadProgressUpdateRes,
|
|
53
|
+
UploadHeadersReceivedRes
|
|
54
|
+
>('uploadFile', options, {
|
|
55
|
+
timeoutMs: options.timeout ?? 60000,
|
|
56
|
+
});
|
|
57
|
+
};
|