mp-taro-kit 0.0.1 → 0.0.4

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.
Files changed (145) hide show
  1. package/README.md +3 -3
  2. package/dist/core/configure.cjs +75 -0
  3. package/dist/core/configure.d.ts +18 -0
  4. package/dist/core/configure.js +69 -0
  5. package/dist/core/faces.cjs +2 -0
  6. package/dist/core/faces.d.ts +42 -0
  7. package/dist/core/faces.js +1 -0
  8. package/dist/core/index.cjs +32 -0
  9. package/dist/core/index.d.ts +6 -0
  10. package/dist/core/index.js +5 -0
  11. package/dist/core/request.cjs +60 -0
  12. package/dist/core/request.d.ts +27 -0
  13. package/dist/core/request.js +51 -0
  14. package/dist/core/router.cjs +71 -0
  15. package/dist/core/router.d.ts +11 -0
  16. package/dist/core/router.js +63 -0
  17. package/dist/core/storage.cjs +63 -0
  18. package/dist/core/storage.d.ts +6 -0
  19. package/dist/core/storage.js +57 -0
  20. package/dist/core/weapp.cjs +37 -0
  21. package/dist/core/weapp.d.ts +7 -0
  22. package/dist/core/weapp.js +32 -0
  23. package/dist/design/index.cjs +2 -0
  24. package/dist/design/index.d.ts +1 -0
  25. package/dist/design/index.js +1 -0
  26. package/dist/design/tokens.css +60 -0
  27. package/dist/index.cjs +21 -0
  28. package/dist/index.d.ts +5 -0
  29. package/dist/index.js +5 -0
  30. package/dist/runtime/index.cjs +15 -0
  31. package/dist/runtime/index.d.ts +5 -0
  32. package/dist/runtime/index.js +6 -0
  33. package/dist/runtime/loading-slot.cjs +44 -0
  34. package/dist/runtime/loading-slot.d.ts +15 -0
  35. package/dist/runtime/loading-slot.js +41 -0
  36. package/dist/runtime/notify.cjs +52 -0
  37. package/dist/runtime/notify.d.ts +19 -0
  38. package/dist/runtime/notify.js +49 -0
  39. package/dist/runtime/overlay-stack.cjs +49 -0
  40. package/dist/runtime/overlay-stack.d.ts +17 -0
  41. package/dist/runtime/overlay-stack.js +46 -0
  42. package/dist/runtime/singleton.cjs +17 -0
  43. package/dist/runtime/singleton.d.ts +6 -0
  44. package/dist/runtime/singleton.js +14 -0
  45. package/dist/runtime/thenable.cjs +17 -0
  46. package/dist/runtime/thenable.d.ts +7 -0
  47. package/dist/runtime/thenable.js +13 -0
  48. package/dist/styles/tokens.css +60 -0
  49. package/dist/ui/actions/Button.cjs +32 -0
  50. package/dist/ui/actions/Button.d.ts +13 -0
  51. package/dist/ui/actions/Button.js +29 -0
  52. package/dist/ui/actions/index.cjs +5 -0
  53. package/dist/ui/actions/index.d.ts +1 -0
  54. package/dist/ui/actions/index.js +1 -0
  55. package/dist/ui/actions/style/index.css +22 -0
  56. package/dist/ui/commands.cjs +86 -0
  57. package/dist/ui/commands.d.ts +21 -0
  58. package/dist/ui/commands.js +73 -0
  59. package/dist/ui/display/Badge.cjs +10 -0
  60. package/dist/ui/display/Badge.d.ts +9 -0
  61. package/dist/ui/display/Badge.js +7 -0
  62. package/dist/ui/display/Divider.cjs +9 -0
  63. package/dist/ui/display/Divider.d.ts +5 -0
  64. package/dist/ui/display/Divider.js +6 -0
  65. package/dist/ui/display/index.cjs +7 -0
  66. package/dist/ui/display/index.d.ts +2 -0
  67. package/dist/ui/display/index.js +2 -0
  68. package/dist/ui/display/style/index.css +30 -0
  69. package/dist/ui/feedback/Dialog.cjs +12 -0
  70. package/dist/ui/feedback/Dialog.d.ts +15 -0
  71. package/dist/ui/feedback/Dialog.js +9 -0
  72. package/dist/ui/feedback/Loading.cjs +12 -0
  73. package/dist/ui/feedback/Loading.d.ts +7 -0
  74. package/dist/ui/feedback/Loading.js +9 -0
  75. package/dist/ui/feedback/Toast.cjs +12 -0
  76. package/dist/ui/feedback/Toast.d.ts +9 -0
  77. package/dist/ui/feedback/Toast.js +9 -0
  78. package/dist/ui/feedback/index.cjs +9 -0
  79. package/dist/ui/feedback/index.d.ts +3 -0
  80. package/dist/ui/feedback/index.js +3 -0
  81. package/dist/ui/feedback/style/index.css +67 -0
  82. package/dist/ui/index.cjs +17 -0
  83. package/dist/ui/index.d.ts +4 -0
  84. package/dist/ui/index.js +2 -0
  85. package/dist/ui/infra/OverlayHost.cjs +33 -0
  86. package/dist/ui/infra/OverlayHost.d.ts +8 -0
  87. package/dist/ui/infra/OverlayHost.js +30 -0
  88. package/dist/ui/infra/SafeArea.cjs +10 -0
  89. package/dist/ui/infra/SafeArea.d.ts +8 -0
  90. package/dist/ui/infra/SafeArea.js +7 -0
  91. package/dist/ui/infra/index.cjs +11 -0
  92. package/dist/ui/infra/index.d.ts +3 -0
  93. package/dist/ui/infra/index.js +3 -0
  94. package/dist/ui/infra/style/index.css +83 -0
  95. package/dist/ui/infra/use-store.cjs +17 -0
  96. package/dist/ui/infra/use-store.d.ts +8 -0
  97. package/dist/ui/infra/use-store.js +12 -0
  98. package/dist/ui/layout/Mask.cjs +12 -0
  99. package/dist/ui/layout/Mask.d.ts +7 -0
  100. package/dist/ui/layout/Mask.js +9 -0
  101. package/dist/ui/layout/Overlay.cjs +11 -0
  102. package/dist/ui/layout/Overlay.d.ts +8 -0
  103. package/dist/ui/layout/Overlay.js +8 -0
  104. package/dist/ui/layout/Popup.cjs +13 -0
  105. package/dist/ui/layout/Popup.d.ts +11 -0
  106. package/dist/ui/layout/Popup.js +10 -0
  107. package/dist/ui/layout/index.cjs +9 -0
  108. package/dist/ui/layout/index.d.ts +3 -0
  109. package/dist/ui/layout/index.js +3 -0
  110. package/dist/ui/layout/style/index.css +51 -0
  111. package/dist/ui/navigation/Drawer.cjs +12 -0
  112. package/dist/ui/navigation/Drawer.d.ts +11 -0
  113. package/dist/ui/navigation/Drawer.js +9 -0
  114. package/dist/ui/navigation/Dropdown.cjs +12 -0
  115. package/dist/ui/navigation/Dropdown.d.ts +9 -0
  116. package/dist/ui/navigation/Dropdown.js +9 -0
  117. package/dist/ui/navigation/index.cjs +7 -0
  118. package/dist/ui/navigation/index.d.ts +2 -0
  119. package/dist/ui/navigation/index.js +2 -0
  120. package/dist/ui/navigation/style/index.css +42 -0
  121. package/dist/ui/state.cjs +20 -0
  122. package/dist/ui/state.d.ts +25 -0
  123. package/dist/ui/state.js +17 -0
  124. package/dist/utils/date.cjs +98 -0
  125. package/dist/utils/date.d.ts +18 -0
  126. package/dist/utils/date.js +89 -0
  127. package/dist/utils/i18n.cjs +31 -0
  128. package/dist/utils/i18n.d.ts +11 -0
  129. package/dist/utils/i18n.js +25 -0
  130. package/dist/utils/index.cjs +30 -0
  131. package/dist/utils/index.d.ts +5 -0
  132. package/dist/utils/index.js +6 -0
  133. package/dist/utils/messages.zh.cjs +13 -0
  134. package/dist/utils/messages.zh.d.ts +1 -0
  135. package/dist/utils/messages.zh.js +10 -0
  136. package/dist/utils/number.cjs +38 -0
  137. package/dist/utils/number.d.ts +6 -0
  138. package/dist/utils/number.js +33 -0
  139. package/dist/utils/path.cjs +33 -0
  140. package/dist/utils/path.d.ts +8 -0
  141. package/dist/utils/path.js +27 -0
  142. package/dist/utils/string.cjs +55 -0
  143. package/dist/utils/string.d.ts +8 -0
  144. package/dist/utils/string.js +50 -0
  145. package/package.json +68 -4
package/README.md CHANGED
@@ -1,9 +1,9 @@
1
1
  # mp-taro-kit
2
2
 
3
- 基于 Taro + React 的小程序 UI 基础库:遮罩、弹出层、面板、安全区等容器能力,业务内容通过 `children` / 插槽注入。
3
+ 基于 Taro + React 的小程序基础库(kit)。叫 kit 不叫 UI,是因为能力域不止界面:既有遮罩、弹出层、面板、安全区等 UI 容器能力,也会收纳跨项目通用的操作功能封装;具体业务内容通过 `children` / 插槽或宿主提供。
4
4
 
5
5
  - Skyline 为主路径,WebView 必须可用
6
- - 对外样式值走 CSS 变量(`var(--mp-*, 兜底值)`),可整体换肤
6
+ - 对外样式值走 CSS 变量(`var(--mptk-*)`),默认值集中在 `mp-taro-kit/styles/tokens`;宿主覆盖同名变量即可整体换肤
7
7
  - 按需 import,靠 tree-shaking 控体积
8
8
 
9
- > 基础建设阶段,API 未定,暂不提供使用文档。
9
+ > 能力源码已落码,尚未发版;使用文档待宿主 demo 双端验证通过后补齐。
@@ -0,0 +1,75 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.configure = configure;
4
+ exports.getFaces = getFaces;
5
+ exports.getConfig = getConfig;
6
+ exports.resetForTests = resetForTests;
7
+ const singleton_js_1 = require("../runtime/singleton.cjs");
8
+ const DEFAULT_CONFIG = {
9
+ baseURL: '',
10
+ timeout: 10000,
11
+ storagePrefix: 'mptk:',
12
+ debug: false,
13
+ headers: {},
14
+ };
15
+ const REQUIRED_FACES = [
16
+ 'request',
17
+ 'getStorage',
18
+ 'setStorage',
19
+ 'removeStorage',
20
+ 'clearStorage',
21
+ 'navigateTo',
22
+ 'redirectTo',
23
+ 'switchTab',
24
+ 'navigateBack',
25
+ 'reLaunch',
26
+ 'login',
27
+ 'scanCode',
28
+ 'requestPayment',
29
+ ];
30
+ const NOT_CONFIGURED = '[mp-taro-kit/core] 未调用 configure() 注入平台能力;请在 app 入口配置一次(mp-taro-kit/core 的 configure)';
31
+ /** 配置槽用 runtime 的跨副本单例,保证「注入一次」对主包 / 分包各副本都生效。 */
32
+ function slot() {
33
+ return (0, singleton_js_1.createSingleton)('core', () => ({ state: null }));
34
+ }
35
+ /**
36
+ * 宿主启动时调用一次,注入平台能力与库配置。
37
+ * 缺字段是开发者契约违例 ⇒ 当场抛错,不静默降级。
38
+ */
39
+ function configure(faces, options = {}) {
40
+ if (faces === null || typeof faces !== 'object') {
41
+ throw new Error('[mp-taro-kit/core] configure() 需要一个 faces 对象;请在 app 入口注入平台能力');
42
+ }
43
+ for (const key of REQUIRED_FACES) {
44
+ if (typeof faces[key] !== 'function') {
45
+ throw new Error(`[mp-taro-kit/core] configure() 的 faces 缺少 ${key}();请注入该方法`);
46
+ }
47
+ }
48
+ slot().state = {
49
+ faces,
50
+ config: {
51
+ baseURL: options.baseURL ?? DEFAULT_CONFIG.baseURL,
52
+ timeout: options.timeout ?? DEFAULT_CONFIG.timeout,
53
+ storagePrefix: options.storagePrefix ?? DEFAULT_CONFIG.storagePrefix,
54
+ debug: options.debug ?? DEFAULT_CONFIG.debug,
55
+ headers: options.headers ?? DEFAULT_CONFIG.headers,
56
+ },
57
+ };
58
+ }
59
+ /** 未配置时每次调用都抛,消息直接给修复动作。 */
60
+ function getFaces() {
61
+ const state = slot().state;
62
+ if (state === null)
63
+ throw new Error(NOT_CONFIGURED);
64
+ return state.faces;
65
+ }
66
+ function getConfig() {
67
+ const state = slot().state;
68
+ if (state === null)
69
+ throw new Error(NOT_CONFIGURED);
70
+ return state.config;
71
+ }
72
+ /** 仅供单测清空注入状态;产品代码不要调用。 */
73
+ function resetForTests() {
74
+ slot().state = null;
75
+ }
@@ -0,0 +1,18 @@
1
+ import type { PlatformFaces } from './faces.js';
2
+ export interface CoreConfig {
3
+ baseURL: string;
4
+ timeout: number;
5
+ storagePrefix: string;
6
+ debug: boolean;
7
+ headers: Record<string, string>;
8
+ }
9
+ /**
10
+ * 宿主启动时调用一次,注入平台能力与库配置。
11
+ * 缺字段是开发者契约违例 ⇒ 当场抛错,不静默降级。
12
+ */
13
+ export declare function configure(faces: PlatformFaces, options?: Partial<CoreConfig>): void;
14
+ /** 未配置时每次调用都抛,消息直接给修复动作。 */
15
+ export declare function getFaces(): PlatformFaces;
16
+ export declare function getConfig(): CoreConfig;
17
+ /** 仅供单测清空注入状态;产品代码不要调用。 */
18
+ export declare function resetForTests(): void;
@@ -0,0 +1,69 @@
1
+ import { createSingleton } from '../runtime/singleton.js';
2
+ const DEFAULT_CONFIG = {
3
+ baseURL: '',
4
+ timeout: 10000,
5
+ storagePrefix: 'mptk:',
6
+ debug: false,
7
+ headers: {},
8
+ };
9
+ const REQUIRED_FACES = [
10
+ 'request',
11
+ 'getStorage',
12
+ 'setStorage',
13
+ 'removeStorage',
14
+ 'clearStorage',
15
+ 'navigateTo',
16
+ 'redirectTo',
17
+ 'switchTab',
18
+ 'navigateBack',
19
+ 'reLaunch',
20
+ 'login',
21
+ 'scanCode',
22
+ 'requestPayment',
23
+ ];
24
+ const NOT_CONFIGURED = '[mp-taro-kit/core] 未调用 configure() 注入平台能力;请在 app 入口配置一次(mp-taro-kit/core 的 configure)';
25
+ /** 配置槽用 runtime 的跨副本单例,保证「注入一次」对主包 / 分包各副本都生效。 */
26
+ function slot() {
27
+ return createSingleton('core', () => ({ state: null }));
28
+ }
29
+ /**
30
+ * 宿主启动时调用一次,注入平台能力与库配置。
31
+ * 缺字段是开发者契约违例 ⇒ 当场抛错,不静默降级。
32
+ */
33
+ export function configure(faces, options = {}) {
34
+ if (faces === null || typeof faces !== 'object') {
35
+ throw new Error('[mp-taro-kit/core] configure() 需要一个 faces 对象;请在 app 入口注入平台能力');
36
+ }
37
+ for (const key of REQUIRED_FACES) {
38
+ if (typeof faces[key] !== 'function') {
39
+ throw new Error(`[mp-taro-kit/core] configure() 的 faces 缺少 ${key}();请注入该方法`);
40
+ }
41
+ }
42
+ slot().state = {
43
+ faces,
44
+ config: {
45
+ baseURL: options.baseURL ?? DEFAULT_CONFIG.baseURL,
46
+ timeout: options.timeout ?? DEFAULT_CONFIG.timeout,
47
+ storagePrefix: options.storagePrefix ?? DEFAULT_CONFIG.storagePrefix,
48
+ debug: options.debug ?? DEFAULT_CONFIG.debug,
49
+ headers: options.headers ?? DEFAULT_CONFIG.headers,
50
+ },
51
+ };
52
+ }
53
+ /** 未配置时每次调用都抛,消息直接给修复动作。 */
54
+ export function getFaces() {
55
+ const state = slot().state;
56
+ if (state === null)
57
+ throw new Error(NOT_CONFIGURED);
58
+ return state.faces;
59
+ }
60
+ export function getConfig() {
61
+ const state = slot().state;
62
+ if (state === null)
63
+ throw new Error(NOT_CONFIGURED);
64
+ return state.config;
65
+ }
66
+ /** 仅供单测清空注入状态;产品代码不要调用。 */
67
+ export function resetForTests() {
68
+ slot().state = null;
69
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,42 @@
1
+ /**
2
+ * 平台能力的最小本地接口面:只声明本库真正会调的那几个,不 import @tarojs/taro。
3
+ * 宿主在 app 入口用 configure() 注入一次;库不与宿主的 Taro 类型版本焊死,也不引入平台类型依赖。
4
+ */
5
+ export type HttpMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'HEAD';
6
+ export interface PlatformRequestOptions {
7
+ url: string;
8
+ method: HttpMethod;
9
+ data?: unknown;
10
+ header?: Record<string, string>;
11
+ timeout?: number;
12
+ }
13
+ export interface PlatformResponse<T> {
14
+ statusCode: number;
15
+ data: T;
16
+ }
17
+ export interface NavigateOptions {
18
+ url: string;
19
+ params?: Record<string, string | number | boolean | undefined>;
20
+ }
21
+ export interface PaymentOptions {
22
+ [key: string]: unknown;
23
+ }
24
+ export interface PlatformFaces {
25
+ request<T>(options: PlatformRequestOptions): Promise<PlatformResponse<T>>;
26
+ getStorage(key: string): unknown;
27
+ setStorage(key: string, value: string): void;
28
+ removeStorage(key: string): void;
29
+ clearStorage(): void;
30
+ navigateTo(options: NavigateOptions): Promise<unknown>;
31
+ redirectTo(options: NavigateOptions): Promise<unknown>;
32
+ switchTab(options: NavigateOptions): Promise<unknown>;
33
+ navigateBack(delta?: number): Promise<unknown>;
34
+ reLaunch(options: NavigateOptions): Promise<unknown>;
35
+ login(): Promise<{
36
+ code: string;
37
+ }>;
38
+ scanCode(): Promise<{
39
+ result: string;
40
+ }>;
41
+ requestPayment(options: PaymentOptions): Promise<unknown>;
42
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.requestPayment = exports.scanCode = exports.login = exports.storageClear = exports.storageRemove = exports.storageSet = exports.storageGet = exports.buildUrl = exports.navigateBack = exports.reLaunch = exports.switchTab = exports.redirectTo = exports.navigateTo = exports.joinUrl = exports.remove = exports.patch = exports.put = exports.post = exports.get = exports.request = exports.resetForTests = exports.getConfig = exports.getFaces = exports.configure = void 0;
4
+ var configure_js_1 = require("./configure.cjs");
5
+ Object.defineProperty(exports, "configure", { enumerable: true, get: function () { return configure_js_1.configure; } });
6
+ Object.defineProperty(exports, "getFaces", { enumerable: true, get: function () { return configure_js_1.getFaces; } });
7
+ Object.defineProperty(exports, "getConfig", { enumerable: true, get: function () { return configure_js_1.getConfig; } });
8
+ Object.defineProperty(exports, "resetForTests", { enumerable: true, get: function () { return configure_js_1.resetForTests; } });
9
+ var request_js_1 = require("./request.cjs");
10
+ Object.defineProperty(exports, "request", { enumerable: true, get: function () { return request_js_1.request; } });
11
+ Object.defineProperty(exports, "get", { enumerable: true, get: function () { return request_js_1.get; } });
12
+ Object.defineProperty(exports, "post", { enumerable: true, get: function () { return request_js_1.post; } });
13
+ Object.defineProperty(exports, "put", { enumerable: true, get: function () { return request_js_1.put; } });
14
+ Object.defineProperty(exports, "patch", { enumerable: true, get: function () { return request_js_1.patch; } });
15
+ Object.defineProperty(exports, "remove", { enumerable: true, get: function () { return request_js_1.remove; } });
16
+ Object.defineProperty(exports, "joinUrl", { enumerable: true, get: function () { return request_js_1.joinUrl; } });
17
+ var router_js_1 = require("./router.cjs");
18
+ Object.defineProperty(exports, "navigateTo", { enumerable: true, get: function () { return router_js_1.navigateTo; } });
19
+ Object.defineProperty(exports, "redirectTo", { enumerable: true, get: function () { return router_js_1.redirectTo; } });
20
+ Object.defineProperty(exports, "switchTab", { enumerable: true, get: function () { return router_js_1.switchTab; } });
21
+ Object.defineProperty(exports, "reLaunch", { enumerable: true, get: function () { return router_js_1.reLaunch; } });
22
+ Object.defineProperty(exports, "navigateBack", { enumerable: true, get: function () { return router_js_1.navigateBack; } });
23
+ Object.defineProperty(exports, "buildUrl", { enumerable: true, get: function () { return router_js_1.buildUrl; } });
24
+ var storage_js_1 = require("./storage.cjs");
25
+ Object.defineProperty(exports, "storageGet", { enumerable: true, get: function () { return storage_js_1.storageGet; } });
26
+ Object.defineProperty(exports, "storageSet", { enumerable: true, get: function () { return storage_js_1.storageSet; } });
27
+ Object.defineProperty(exports, "storageRemove", { enumerable: true, get: function () { return storage_js_1.storageRemove; } });
28
+ Object.defineProperty(exports, "storageClear", { enumerable: true, get: function () { return storage_js_1.storageClear; } });
29
+ var weapp_js_1 = require("./weapp.cjs");
30
+ Object.defineProperty(exports, "login", { enumerable: true, get: function () { return weapp_js_1.login; } });
31
+ Object.defineProperty(exports, "scanCode", { enumerable: true, get: function () { return weapp_js_1.scanCode; } });
32
+ Object.defineProperty(exports, "requestPayment", { enumerable: true, get: function () { return weapp_js_1.requestPayment; } });
@@ -0,0 +1,6 @@
1
+ export type { HttpMethod, NavigateOptions, PaymentOptions, PlatformFaces, PlatformRequestOptions, PlatformResponse, } from './faces.js';
2
+ export { configure, getFaces, getConfig, resetForTests, type CoreConfig, } from './configure.js';
3
+ export { request, get, post, put, patch, remove, joinUrl, type RequestOptions, type HttpResult, } from './request.js';
4
+ export { navigateTo, redirectTo, switchTab, reLaunch, navigateBack, buildUrl, type RouteOptions, } from './router.js';
5
+ export { storageGet, storageSet, storageRemove, storageClear } from './storage.js';
6
+ export { login, scanCode, requestPayment } from './weapp.js';
@@ -0,0 +1,5 @@
1
+ export { configure, getFaces, getConfig, resetForTests, } from './configure.js';
2
+ export { request, get, post, put, patch, remove, joinUrl, } from './request.js';
3
+ export { navigateTo, redirectTo, switchTab, reLaunch, navigateBack, buildUrl, } from './router.js';
4
+ export { storageGet, storageSet, storageRemove, storageClear } from './storage.js';
5
+ export { login, scanCode, requestPayment } from './weapp.js';
@@ -0,0 +1,60 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.joinUrl = joinUrl;
4
+ exports.request = request;
5
+ exports.get = get;
6
+ exports.post = post;
7
+ exports.put = put;
8
+ exports.patch = patch;
9
+ exports.remove = remove;
10
+ const configure_js_1 = require("./configure.cjs");
11
+ function joinUrl(baseURL, url) {
12
+ if (/^https?:\/\//.test(url))
13
+ return url;
14
+ if (baseURL === '')
15
+ return url;
16
+ return `${baseURL.replace(/\/+$/, '')}/${url.replace(/^\/+/, '')}`;
17
+ }
18
+ /**
19
+ * 网络失败属于「用户在场」⇒ 返回结果对象而非抛异常;
20
+ * 未 configure() 属于「开发者在场」⇒ getConfig / getFaces 直接抛。
21
+ */
22
+ async function request(options) {
23
+ const config = (0, configure_js_1.getConfig)();
24
+ const faces = (0, configure_js_1.getFaces)();
25
+ const outgoing = {
26
+ url: joinUrl(config.baseURL, options.url),
27
+ method: options.method ?? 'GET',
28
+ data: options.data,
29
+ header: { ...config.headers, ...options.header },
30
+ timeout: options.timeout ?? config.timeout,
31
+ };
32
+ try {
33
+ const response = await faces.request(outgoing);
34
+ const statusCode = Number.isFinite(response.statusCode) ? response.statusCode : 0;
35
+ return {
36
+ ok: statusCode >= 200 && statusCode < 300,
37
+ statusCode,
38
+ data: response.data,
39
+ error: undefined,
40
+ };
41
+ }
42
+ catch (error) {
43
+ return { ok: false, statusCode: 0, data: undefined, error: String(error) };
44
+ }
45
+ }
46
+ function get(url, options = {}) {
47
+ return request({ ...options, url, method: 'GET' });
48
+ }
49
+ function post(url, options = {}) {
50
+ return request({ ...options, url, method: 'POST' });
51
+ }
52
+ function put(url, options = {}) {
53
+ return request({ ...options, url, method: 'PUT' });
54
+ }
55
+ function patch(url, options = {}) {
56
+ return request({ ...options, url, method: 'PATCH' });
57
+ }
58
+ function remove(url, options = {}) {
59
+ return request({ ...options, url, method: 'DELETE' });
60
+ }
@@ -0,0 +1,27 @@
1
+ import type { HttpMethod } from './faces.js';
2
+ export interface RequestOptions {
3
+ url: string;
4
+ method?: HttpMethod;
5
+ data?: unknown;
6
+ header?: Record<string, string>;
7
+ timeout?: number;
8
+ }
9
+ export interface HttpResult<T> {
10
+ ok: boolean;
11
+ statusCode: number;
12
+ data: T | undefined;
13
+ error: string | undefined;
14
+ }
15
+ export declare function joinUrl(baseURL: string, url: string): string;
16
+ /**
17
+ * 网络失败属于「用户在场」⇒ 返回结果对象而非抛异常;
18
+ * 未 configure() 属于「开发者在场」⇒ getConfig / getFaces 直接抛。
19
+ */
20
+ export declare function request<T = unknown>(options: RequestOptions): Promise<HttpResult<T>>;
21
+ type WithoutUrlMethod = Omit<RequestOptions, 'url' | 'method'>;
22
+ export declare function get<T = unknown>(url: string, options?: WithoutUrlMethod): Promise<HttpResult<T>>;
23
+ export declare function post<T = unknown>(url: string, options?: WithoutUrlMethod): Promise<HttpResult<T>>;
24
+ export declare function put<T = unknown>(url: string, options?: WithoutUrlMethod): Promise<HttpResult<T>>;
25
+ export declare function patch<T = unknown>(url: string, options?: WithoutUrlMethod): Promise<HttpResult<T>>;
26
+ export declare function remove<T = unknown>(url: string, options?: WithoutUrlMethod): Promise<HttpResult<T>>;
27
+ export {};
@@ -0,0 +1,51 @@
1
+ import { getConfig, getFaces } from './configure.js';
2
+ export function joinUrl(baseURL, url) {
3
+ if (/^https?:\/\//.test(url))
4
+ return url;
5
+ if (baseURL === '')
6
+ return url;
7
+ return `${baseURL.replace(/\/+$/, '')}/${url.replace(/^\/+/, '')}`;
8
+ }
9
+ /**
10
+ * 网络失败属于「用户在场」⇒ 返回结果对象而非抛异常;
11
+ * 未 configure() 属于「开发者在场」⇒ getConfig / getFaces 直接抛。
12
+ */
13
+ export async function request(options) {
14
+ const config = getConfig();
15
+ const faces = getFaces();
16
+ const outgoing = {
17
+ url: joinUrl(config.baseURL, options.url),
18
+ method: options.method ?? 'GET',
19
+ data: options.data,
20
+ header: { ...config.headers, ...options.header },
21
+ timeout: options.timeout ?? config.timeout,
22
+ };
23
+ try {
24
+ const response = await faces.request(outgoing);
25
+ const statusCode = Number.isFinite(response.statusCode) ? response.statusCode : 0;
26
+ return {
27
+ ok: statusCode >= 200 && statusCode < 300,
28
+ statusCode,
29
+ data: response.data,
30
+ error: undefined,
31
+ };
32
+ }
33
+ catch (error) {
34
+ return { ok: false, statusCode: 0, data: undefined, error: String(error) };
35
+ }
36
+ }
37
+ export function get(url, options = {}) {
38
+ return request({ ...options, url, method: 'GET' });
39
+ }
40
+ export function post(url, options = {}) {
41
+ return request({ ...options, url, method: 'POST' });
42
+ }
43
+ export function put(url, options = {}) {
44
+ return request({ ...options, url, method: 'PUT' });
45
+ }
46
+ export function patch(url, options = {}) {
47
+ return request({ ...options, url, method: 'PATCH' });
48
+ }
49
+ export function remove(url, options = {}) {
50
+ return request({ ...options, url, method: 'DELETE' });
51
+ }
@@ -0,0 +1,71 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.buildUrl = buildUrl;
4
+ exports.navigateTo = navigateTo;
5
+ exports.redirectTo = redirectTo;
6
+ exports.switchTab = switchTab;
7
+ exports.reLaunch = reLaunch;
8
+ exports.navigateBack = navigateBack;
9
+ const singleton_js_1 = require("../runtime/singleton.cjs");
10
+ const configure_js_1 = require("./configure.cjs");
11
+ /** 在途锁跨副本共用一份;释放时机由真实路由事件 settle 决定,不用固定间隔定时器。 */
12
+ function routeLock() {
13
+ return (0, singleton_js_1.createSingleton)('core/route-lock', () => ({ value: false }));
14
+ }
15
+ function buildUrl(url, params) {
16
+ if (params === undefined)
17
+ return url;
18
+ const query = Object.keys(params)
19
+ .filter((key) => params[key] !== undefined)
20
+ .map((key) => `${encodeURIComponent(key)}=${encodeURIComponent(String(params[key]))}`)
21
+ .join('&');
22
+ if (query === '')
23
+ return url;
24
+ return url.includes('?') ? `${url}&${query}` : `${url}?${query}`;
25
+ }
26
+ async function go(kind, options) {
27
+ const faces = (0, configure_js_1.getFaces)();
28
+ const lock = routeLock();
29
+ if (lock.value && options.force !== true)
30
+ return false;
31
+ lock.value = true;
32
+ try {
33
+ await faces[kind]({ url: buildUrl(options.url, options.params) });
34
+ return true;
35
+ }
36
+ catch {
37
+ return false;
38
+ }
39
+ finally {
40
+ lock.value = false;
41
+ }
42
+ }
43
+ function navigateTo(options) {
44
+ return go('navigateTo', options);
45
+ }
46
+ function redirectTo(options) {
47
+ return go('redirectTo', options);
48
+ }
49
+ function switchTab(options) {
50
+ return go('switchTab', options);
51
+ }
52
+ function reLaunch(options) {
53
+ return go('reLaunch', options);
54
+ }
55
+ async function navigateBack(delta = 1) {
56
+ const faces = (0, configure_js_1.getFaces)();
57
+ const lock = routeLock();
58
+ if (lock.value)
59
+ return false;
60
+ lock.value = true;
61
+ try {
62
+ await faces.navigateBack(delta);
63
+ return true;
64
+ }
65
+ catch {
66
+ return false;
67
+ }
68
+ finally {
69
+ lock.value = false;
70
+ }
71
+ }
@@ -0,0 +1,11 @@
1
+ import type { NavigateOptions } from './faces.js';
2
+ export interface RouteOptions extends NavigateOptions {
3
+ /** 跳过在途锁,强制发起本次跳转。 */
4
+ force?: boolean;
5
+ }
6
+ export declare function buildUrl(url: string, params?: NavigateOptions['params']): string;
7
+ export declare function navigateTo(options: RouteOptions): Promise<boolean>;
8
+ export declare function redirectTo(options: RouteOptions): Promise<boolean>;
9
+ export declare function switchTab(options: RouteOptions): Promise<boolean>;
10
+ export declare function reLaunch(options: RouteOptions): Promise<boolean>;
11
+ export declare function navigateBack(delta?: number): Promise<boolean>;
@@ -0,0 +1,63 @@
1
+ import { createSingleton } from '../runtime/singleton.js';
2
+ import { getFaces } from './configure.js';
3
+ /** 在途锁跨副本共用一份;释放时机由真实路由事件 settle 决定,不用固定间隔定时器。 */
4
+ function routeLock() {
5
+ return createSingleton('core/route-lock', () => ({ value: false }));
6
+ }
7
+ export function buildUrl(url, params) {
8
+ if (params === undefined)
9
+ return url;
10
+ const query = Object.keys(params)
11
+ .filter((key) => params[key] !== undefined)
12
+ .map((key) => `${encodeURIComponent(key)}=${encodeURIComponent(String(params[key]))}`)
13
+ .join('&');
14
+ if (query === '')
15
+ return url;
16
+ return url.includes('?') ? `${url}&${query}` : `${url}?${query}`;
17
+ }
18
+ async function go(kind, options) {
19
+ const faces = getFaces();
20
+ const lock = routeLock();
21
+ if (lock.value && options.force !== true)
22
+ return false;
23
+ lock.value = true;
24
+ try {
25
+ await faces[kind]({ url: buildUrl(options.url, options.params) });
26
+ return true;
27
+ }
28
+ catch {
29
+ return false;
30
+ }
31
+ finally {
32
+ lock.value = false;
33
+ }
34
+ }
35
+ export function navigateTo(options) {
36
+ return go('navigateTo', options);
37
+ }
38
+ export function redirectTo(options) {
39
+ return go('redirectTo', options);
40
+ }
41
+ export function switchTab(options) {
42
+ return go('switchTab', options);
43
+ }
44
+ export function reLaunch(options) {
45
+ return go('reLaunch', options);
46
+ }
47
+ export async function navigateBack(delta = 1) {
48
+ const faces = getFaces();
49
+ const lock = routeLock();
50
+ if (lock.value)
51
+ return false;
52
+ lock.value = true;
53
+ try {
54
+ await faces.navigateBack(delta);
55
+ return true;
56
+ }
57
+ catch {
58
+ return false;
59
+ }
60
+ finally {
61
+ lock.value = false;
62
+ }
63
+ }
@@ -0,0 +1,63 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.storageGet = storageGet;
4
+ exports.storageSet = storageSet;
5
+ exports.storageRemove = storageRemove;
6
+ exports.storageClear = storageClear;
7
+ const configure_js_1 = require("./configure.cjs");
8
+ function keyOf(key) {
9
+ return `${(0, configure_js_1.getConfig)().storagePrefix}${key}`;
10
+ }
11
+ /** 平台异常 / 脏数据 / 过期都按「用户在场」兜底;未 configure() 由 getFaces 抛错。 */
12
+ function storageGet(key) {
13
+ const faces = (0, configure_js_1.getFaces)();
14
+ try {
15
+ const raw = faces.getStorage(keyOf(key));
16
+ if (raw === undefined || raw === null || raw === '')
17
+ return undefined;
18
+ const parsed = typeof raw === 'string' ? JSON.parse(raw) : raw;
19
+ if (parsed === null || typeof parsed !== 'object' || !('v' in parsed))
20
+ return undefined;
21
+ const record = parsed;
22
+ if (record.e !== null && record.e !== undefined && record.e <= Date.now()) {
23
+ faces.removeStorage(keyOf(key));
24
+ return undefined;
25
+ }
26
+ return record.v;
27
+ }
28
+ catch {
29
+ return undefined;
30
+ }
31
+ }
32
+ /** 返回 false 表示平台写入失败,不静默、也不抛。 */
33
+ function storageSet(key, value, ttlMs) {
34
+ const faces = (0, configure_js_1.getFaces)();
35
+ const record = { v: value, e: ttlMs === undefined ? null : Date.now() + ttlMs };
36
+ try {
37
+ faces.setStorage(keyOf(key), JSON.stringify(record));
38
+ return true;
39
+ }
40
+ catch {
41
+ return false;
42
+ }
43
+ }
44
+ function storageRemove(key) {
45
+ const faces = (0, configure_js_1.getFaces)();
46
+ try {
47
+ faces.removeStorage(keyOf(key));
48
+ return true;
49
+ }
50
+ catch {
51
+ return false;
52
+ }
53
+ }
54
+ function storageClear() {
55
+ const faces = (0, configure_js_1.getFaces)();
56
+ try {
57
+ faces.clearStorage();
58
+ return true;
59
+ }
60
+ catch {
61
+ return false;
62
+ }
63
+ }
@@ -0,0 +1,6 @@
1
+ /** 平台异常 / 脏数据 / 过期都按「用户在场」兜底;未 configure() 由 getFaces 抛错。 */
2
+ export declare function storageGet<T = unknown>(key: string): T | undefined;
3
+ /** 返回 false 表示平台写入失败,不静默、也不抛。 */
4
+ export declare function storageSet<T>(key: string, value: T, ttlMs?: number): boolean;
5
+ export declare function storageRemove(key: string): boolean;
6
+ export declare function storageClear(): boolean;