@weapp-core/api 0.2.0 → 0.3.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/README.md +48 -1
- package/dist/apiCatalog-aYGell1q.mjs +1046 -0
- package/dist/index-Bt6fzcnM.d.mts +8492 -0
- package/dist/index.d.mts +1 -8499
- package/dist/index.mjs +38 -1052
- package/dist/vitest/setup.d.mts +1 -0
- package/dist/vitest/setup.mjs +5 -0
- package/dist/vitest.d.mts +55 -0
- package/dist/vitest.mjs +107 -0
- package/package.json +34 -2
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { D as WeapiInstance, K as WeapiMiniProgramCrossPlatformRawAdapter, W as WeapiCrossPlatformRawAdapter, z as WeapiAdapter } from "./index-Bt6fzcnM.mjs";
|
|
2
|
+
import { Mock } from "vitest";
|
|
3
|
+
//#region src/vitest/index.d.ts
|
|
4
|
+
type Procedure = (...args: any[]) => any;
|
|
5
|
+
type MockImplementation<T extends Procedure> = (...args: Parameters<T>) => ReturnType<T>;
|
|
6
|
+
type ApiMockFunction<TPublic extends Procedure, TRaw extends Procedure = TPublic> = Omit<Mock<TPublic>, 'mockImplementation' | 'mockImplementationOnce' | 'withImplementation'> & TPublic & {
|
|
7
|
+
mockImplementation: {
|
|
8
|
+
(implementation: MockImplementation<TRaw>): ApiMockFunction<TPublic, TRaw>;
|
|
9
|
+
(implementation: MockImplementation<TPublic>): ApiMockFunction<TPublic, TRaw>;
|
|
10
|
+
};
|
|
11
|
+
mockImplementationOnce: {
|
|
12
|
+
(implementation: MockImplementation<TRaw>): ApiMockFunction<TPublic, TRaw>;
|
|
13
|
+
(implementation: MockImplementation<TPublic>): ApiMockFunction<TPublic, TRaw>;
|
|
14
|
+
};
|
|
15
|
+
withImplementation: {
|
|
16
|
+
(implementation: MockImplementation<TRaw>, callback: () => Promise<unknown>): Promise<ApiMockFunction<TPublic, TRaw>>;
|
|
17
|
+
(implementation: MockImplementation<TRaw>, callback: () => unknown): ApiMockFunction<TPublic, TRaw>;
|
|
18
|
+
(implementation: MockImplementation<TPublic>, callback: () => Promise<unknown>): Promise<ApiMockFunction<TPublic, TRaw>>;
|
|
19
|
+
(implementation: MockImplementation<TPublic>, callback: () => unknown): ApiMockFunction<TPublic, TRaw>;
|
|
20
|
+
};
|
|
21
|
+
};
|
|
22
|
+
type RawMethod<TAdapter extends WeapiAdapter, TKey extends PropertyKey, TFallback extends Procedure> = TKey extends keyof TAdapter ? TAdapter[TKey] extends Procedure ? TAdapter[TKey] : TFallback : TFallback;
|
|
23
|
+
/**
|
|
24
|
+
* @description 与 API 实例方法签名对齐的 Vitest mock 对象。
|
|
25
|
+
*/
|
|
26
|
+
export type ApiMock<TAdapter extends WeapiAdapter = WeapiCrossPlatformRawAdapter> = { [TKey in keyof WeapiInstance<TAdapter>]: WeapiInstance<TAdapter>[TKey] extends Procedure ? ApiMockFunction<WeapiInstance<TAdapter>[TKey], RawMethod<TAdapter, TKey, WeapiInstance<TAdapter>[TKey]>> : WeapiInstance<TAdapter>[TKey]; };
|
|
27
|
+
/**
|
|
28
|
+
* @description 创建 API mock 时预设的属性或方法实现。
|
|
29
|
+
*/
|
|
30
|
+
export type ApiMockOverrides<TAdapter extends WeapiAdapter = WeapiCrossPlatformRawAdapter> = Partial<WeapiInstance<TAdapter>>;
|
|
31
|
+
/**
|
|
32
|
+
* @description 创建与 API 类型对齐、按访问惰性生成方法的 Vitest mock。
|
|
33
|
+
*/
|
|
34
|
+
export declare function createApiMock<TAdapter extends WeapiAdapter = WeapiCrossPlatformRawAdapter>(overrides?: ApiMockOverrides<NoInfer<TAdapter>>): ApiMock<TAdapter>;
|
|
35
|
+
/**
|
|
36
|
+
* @description 默认 API Vitest mock 单例。
|
|
37
|
+
*/
|
|
38
|
+
export declare const apiMock: ApiMock<WeapiMiniProgramCrossPlatformRawAdapter>;
|
|
39
|
+
/**
|
|
40
|
+
* @description 默认 API Vitest mock 单例的兼容名称。
|
|
41
|
+
*/
|
|
42
|
+
export declare const wpiMock: ApiMock<WeapiMiniProgramCrossPlatformRawAdapter>;
|
|
43
|
+
/**
|
|
44
|
+
* @description 创建 API Vitest mock 的兼容名称。
|
|
45
|
+
*/
|
|
46
|
+
export declare const createWpiMock: typeof createApiMock;
|
|
47
|
+
/**
|
|
48
|
+
* @description 重置指定 API mock 自身已创建的方法,不影响其他 Vitest mocks。
|
|
49
|
+
*/
|
|
50
|
+
export declare function resetApiMock(mock?: ApiMock<any>): void;
|
|
51
|
+
/**
|
|
52
|
+
* @description 在 Vitest setup 文件中注册需要替换 API 实例的模块入口。
|
|
53
|
+
*/
|
|
54
|
+
export declare function setupApiMock(moduleIds: readonly string[]): void;
|
|
55
|
+
//#endregion
|
package/dist/vitest.mjs
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { i as WEAPI_WECHAT_METHODS, n as WEAPI_DOUYIN_METHODS, t as WEAPI_ALIPAY_METHODS } from "./apiCatalog-aYGell1q.mjs";
|
|
2
|
+
import { beforeEach, vi } from "vitest";
|
|
3
|
+
//#region src/vitest/index.ts
|
|
4
|
+
const INTERNAL_METHOD_NAMES = [
|
|
5
|
+
"getAdapter",
|
|
6
|
+
"resolveTarget",
|
|
7
|
+
"setAdapter",
|
|
8
|
+
"supports"
|
|
9
|
+
];
|
|
10
|
+
const API_METHOD_NAMES = /* @__PURE__ */ new Set([
|
|
11
|
+
...WEAPI_WECHAT_METHODS,
|
|
12
|
+
...WEAPI_ALIPAY_METHODS,
|
|
13
|
+
...WEAPI_DOUYIN_METHODS,
|
|
14
|
+
...INTERNAL_METHOD_NAMES
|
|
15
|
+
]);
|
|
16
|
+
const API_VALUE_NAMES = /* @__PURE__ */ new Set(["platform", "raw"]);
|
|
17
|
+
const API_MOCK_STATES = /* @__PURE__ */ new WeakMap();
|
|
18
|
+
function createNamedMock(name, implementation) {
|
|
19
|
+
return (implementation ? vi.fn(implementation) : vi.fn()).mockName(`api.${String(name)}`);
|
|
20
|
+
}
|
|
21
|
+
function materializeOverride(state, key, value) {
|
|
22
|
+
if (typeof value === "function") {
|
|
23
|
+
const mock = vi.isMockFunction(value) ? value : createNamedMock(key, value);
|
|
24
|
+
state.mocks.add(mock);
|
|
25
|
+
state.values.set(key, mock);
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
state.values.set(key, value);
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* @description 创建与 API 类型对齐、按访问惰性生成方法的 Vitest mock。
|
|
32
|
+
*/
|
|
33
|
+
function createApiMock(overrides = {}) {
|
|
34
|
+
const state = {
|
|
35
|
+
mocks: /* @__PURE__ */ new Set(),
|
|
36
|
+
values: /* @__PURE__ */ new Map()
|
|
37
|
+
};
|
|
38
|
+
for (const key of Reflect.ownKeys(overrides)) materializeOverride(state, key, Reflect.get(overrides, key));
|
|
39
|
+
const proxy = new Proxy(Object.create(null), {
|
|
40
|
+
get(_target, key) {
|
|
41
|
+
if (key === Symbol.toStringTag) return "ApiMock";
|
|
42
|
+
if (key === "then") return;
|
|
43
|
+
if (state.values.has(key)) return state.values.get(key);
|
|
44
|
+
if (API_VALUE_NAMES.has(key) || typeof key !== "string" && !API_METHOD_NAMES.has(key)) return;
|
|
45
|
+
const mock = createNamedMock(key);
|
|
46
|
+
state.mocks.add(mock);
|
|
47
|
+
state.values.set(key, mock);
|
|
48
|
+
return mock;
|
|
49
|
+
},
|
|
50
|
+
getOwnPropertyDescriptor(_target, key) {
|
|
51
|
+
if (!state.values.has(key)) return;
|
|
52
|
+
return {
|
|
53
|
+
configurable: true,
|
|
54
|
+
enumerable: true,
|
|
55
|
+
value: state.values.get(key),
|
|
56
|
+
writable: true
|
|
57
|
+
};
|
|
58
|
+
},
|
|
59
|
+
has(_target, key) {
|
|
60
|
+
return state.values.has(key) || API_METHOD_NAMES.has(key);
|
|
61
|
+
},
|
|
62
|
+
ownKeys() {
|
|
63
|
+
return [...state.values.keys()];
|
|
64
|
+
},
|
|
65
|
+
set(_target, key, value) {
|
|
66
|
+
materializeOverride(state, key, value);
|
|
67
|
+
return true;
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
API_MOCK_STATES.set(proxy, state);
|
|
71
|
+
return proxy;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* @description 默认 API Vitest mock 单例。
|
|
75
|
+
*/
|
|
76
|
+
const apiMock = createApiMock();
|
|
77
|
+
/**
|
|
78
|
+
* @description 默认 API Vitest mock 单例的兼容名称。
|
|
79
|
+
*/
|
|
80
|
+
const wpiMock = apiMock;
|
|
81
|
+
/**
|
|
82
|
+
* @description 创建 API Vitest mock 的兼容名称。
|
|
83
|
+
*/
|
|
84
|
+
const createWpiMock = createApiMock;
|
|
85
|
+
/**
|
|
86
|
+
* @description 重置指定 API mock 自身已创建的方法,不影响其他 Vitest mocks。
|
|
87
|
+
*/
|
|
88
|
+
function resetApiMock(mock = apiMock) {
|
|
89
|
+
API_MOCK_STATES.get(mock)?.mocks.forEach((item) => item.mockReset());
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* @description 在 Vitest setup 文件中注册需要替换 API 实例的模块入口。
|
|
93
|
+
*/
|
|
94
|
+
function setupApiMock(moduleIds) {
|
|
95
|
+
for (const moduleId of moduleIds) vi.doMock(moduleId, async () => {
|
|
96
|
+
return {
|
|
97
|
+
...await vi.importActual(moduleId),
|
|
98
|
+
api: apiMock,
|
|
99
|
+
wpi: apiMock
|
|
100
|
+
};
|
|
101
|
+
});
|
|
102
|
+
beforeEach(() => {
|
|
103
|
+
resetApiMock(apiMock);
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
//#endregion
|
|
107
|
+
export { apiMock, createApiMock, createWpiMock, resetApiMock, setupApiMock, wpiMock };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@weapp-core/api",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.3.1",
|
|
5
5
|
"description": "Framework-agnostic cross-platform mini-program API proxy",
|
|
6
6
|
"author": "ice breaker <1324318532@qq.com>",
|
|
7
7
|
"license": "MIT",
|
|
@@ -22,12 +22,22 @@
|
|
|
22
22
|
"adapter",
|
|
23
23
|
"weapp"
|
|
24
24
|
],
|
|
25
|
-
"sideEffects":
|
|
25
|
+
"sideEffects": [
|
|
26
|
+
"./dist/vitest/setup.mjs"
|
|
27
|
+
],
|
|
26
28
|
"weapp-vite-dev": {
|
|
27
29
|
"exports": {
|
|
28
30
|
".": {
|
|
29
31
|
"types": "./src/index.ts",
|
|
30
32
|
"default": "./src/index.ts"
|
|
33
|
+
},
|
|
34
|
+
"./vitest": {
|
|
35
|
+
"types": "./src/vitest/index.ts",
|
|
36
|
+
"default": "./src/vitest/index.ts"
|
|
37
|
+
},
|
|
38
|
+
"./vitest/setup": {
|
|
39
|
+
"types": "./src/vitest/setup.ts",
|
|
40
|
+
"default": "./src/vitest/setup.ts"
|
|
31
41
|
}
|
|
32
42
|
},
|
|
33
43
|
"main": "./src/index.ts",
|
|
@@ -41,6 +51,14 @@
|
|
|
41
51
|
"types": "./types/index.d.ts",
|
|
42
52
|
"default": "./dist/index.mjs"
|
|
43
53
|
}
|
|
54
|
+
},
|
|
55
|
+
"./vitest": {
|
|
56
|
+
"types": "./dist/vitest.d.mts",
|
|
57
|
+
"import": "./dist/vitest.mjs"
|
|
58
|
+
},
|
|
59
|
+
"./vitest/setup": {
|
|
60
|
+
"types": "./dist/vitest/setup.d.mts",
|
|
61
|
+
"import": "./dist/vitest/setup.mjs"
|
|
44
62
|
}
|
|
45
63
|
},
|
|
46
64
|
"main": "./dist/index.mjs",
|
|
@@ -51,11 +69,25 @@
|
|
|
51
69
|
"dist",
|
|
52
70
|
"types"
|
|
53
71
|
],
|
|
72
|
+
"peerDependencies": {
|
|
73
|
+
"vitest": ">=4.0.0"
|
|
74
|
+
},
|
|
75
|
+
"peerDependenciesMeta": {
|
|
76
|
+
"vitest": {
|
|
77
|
+
"optional": true
|
|
78
|
+
}
|
|
79
|
+
},
|
|
54
80
|
"dependencies": {
|
|
55
81
|
"@douyin-microapp/typings": "^1.3.1",
|
|
56
82
|
"@mini-types/alipay": "^3.0.14",
|
|
57
83
|
"miniprogram-api-typings": "^5.2.3"
|
|
58
84
|
},
|
|
85
|
+
"tsd": {
|
|
86
|
+
"compilerOptions": {
|
|
87
|
+
"module": "NodeNext",
|
|
88
|
+
"moduleResolution": "NodeNext"
|
|
89
|
+
}
|
|
90
|
+
},
|
|
59
91
|
"publishConfig": {
|
|
60
92
|
"access": "public"
|
|
61
93
|
},
|