@cloudcome/utils-uni 1.12.0 → 1.14.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/dist/_helpers.cjs +14 -2
- package/dist/_helpers.cjs.map +1 -1
- package/dist/_helpers.d.ts +21 -6
- package/dist/_helpers.mjs +14 -2
- package/dist/_helpers.mjs.map +1 -1
- package/dist/client.cjs +8 -7
- package/dist/client.cjs.map +1 -1
- package/dist/client.d.ts +43 -18
- package/dist/client.mjs +9 -8
- package/dist/client.mjs.map +1 -1
- package/dist/cloud/method.d.ts +102 -0
- package/dist/cloud/module.d.ts +27 -0
- package/dist/cloud/respond.d.ts +11 -17
- package/dist/cloud/types.d.ts +27 -12
- package/dist/cloud/uni-id.d.ts +2 -2
- package/dist/cloud.cjs +16 -16
- package/dist/cloud.cjs.map +1 -1
- package/dist/cloud.d.ts +3 -2
- package/dist/cloud.mjs +17 -17
- package/dist/cloud.mjs.map +1 -1
- package/dist/database/_command.class.d.ts +15 -0
- package/dist/database/{db.d.ts → _db.class.d.ts} +61 -83
- package/dist/database/command.d.ts +122 -0
- package/dist/database/proxy.d.ts +17 -0
- package/dist/database/transaction.d.ts +23 -0
- package/dist/database/types.d.ts +2 -143
- package/dist/database/unique.d.ts +27 -0
- package/dist/database/{fns.d.ts → upsert.d.ts} +4 -24
- package/dist/database.cjs +305 -173
- package/dist/database.cjs.map +1 -1
- package/dist/database.d.ts +6 -2
- package/dist/database.mjs +300 -168
- package/dist/database.mjs.map +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.mjs +1 -1
- package/package.json +3 -3
- package/dist/cloud/expose.d.ts +0 -124
package/dist/_helpers.cjs
CHANGED
|
@@ -1,10 +1,22 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
const error = require("@cloudcome/utils-core/error");
|
|
3
|
-
|
|
3
|
+
const object = require("@cloudcome/utils-core/object");
|
|
4
|
+
function parseCloudMethodOutput(output, fallbackErrorMessage = "") {
|
|
4
5
|
if (output.errCode) {
|
|
5
6
|
throw error.errorAssign(new Error(output.errMsg || fallbackErrorMessage), output);
|
|
6
7
|
}
|
|
7
8
|
return output.data;
|
|
8
9
|
}
|
|
9
|
-
|
|
10
|
+
function parseDatabaseOutput(res) {
|
|
11
|
+
const keys = Object.keys(res);
|
|
12
|
+
const isClient = keys.length === 1 && keys[0] === "result";
|
|
13
|
+
if (isClient) {
|
|
14
|
+
const { result } = res;
|
|
15
|
+
if (!result.errCode) return object.objectOmit(result, ["errCode", "errMsg", "code", "message"]);
|
|
16
|
+
throw error.errorAssign(new Error(result.errMsg), result);
|
|
17
|
+
}
|
|
18
|
+
return res;
|
|
19
|
+
}
|
|
20
|
+
exports.parseCloudMethodOutput = parseCloudMethodOutput;
|
|
21
|
+
exports.parseDatabaseOutput = parseDatabaseOutput;
|
|
10
22
|
//# sourceMappingURL=_helpers.cjs.map
|
package/dist/_helpers.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"_helpers.cjs","sources":["../src/_helpers.ts"],"sourcesContent":["import { errorAssign } from '@cloudcome/utils-core/error';\nimport type {
|
|
1
|
+
{"version":3,"file":"_helpers.cjs","sources":["../src/_helpers.ts"],"sourcesContent":["import { errorAssign } from '@cloudcome/utils-core/error';\nimport { objectOmit } from '@cloudcome/utils-core/object';\nimport type { CloudMethodOutput } from './cloud';\nimport type { ClientDatabaseOutput, CloudDatabaseOutput } from './database';\n\n/**\n * 解析云对象方法调用的输出结果\n *\n * @template O - 云对象方法返回数据的类型\n * @param output - 云对象方法调用的输出结果\n * @param fallbackErrorMessage - 当输出中没有错误信息时使用的默认错误消息\n * @returns 云对象方法返回成功时的数据部分\n * @throws 当云对象方法调用失败时,抛出包含错误信息的异常\n */\nexport function parseCloudMethodOutput<O>(output: CloudMethodOutput<O>, fallbackErrorMessage = ''): O {\n if (output.errCode) {\n throw errorAssign(new Error(output.errMsg || fallbackErrorMessage), output);\n }\n\n return output.data;\n}\n\n/**\n * 解析数据库执行结果\n * @param res 客户端、云端响应结果\n * @returns 处理后的结果\n */\nexport function parseDatabaseOutput<T>(res: ClientDatabaseOutput<T> | CloudDatabaseOutput<T>) {\n const keys = Object.keys(res as AnyObject);\n // 客户端 { result: {errCode: 0, errMsg: 'ok'} & 数据 }\n const isClient = keys.length === 1 && keys[0] === 'result';\n\n if (isClient) {\n const { result } = res as ClientDatabaseOutput<T>;\n if (!result.errCode) return objectOmit(result, ['errCode', 'errMsg', 'code', 'message']);\n throw errorAssign(new Error(result.errMsg), result);\n }\n\n // 云端 数据\n return res as T;\n}\n"],"names":["errorAssign","objectOmit"],"mappings":";;;AAcgB,SAAA,uBAA0B,QAA8B,uBAAuB,IAAO;AACpG,MAAI,OAAO,SAAS;AAClB,UAAMA,MAAAA,YAAY,IAAI,MAAM,OAAO,UAAU,oBAAoB,GAAG,MAAM;AAAA,EAAA;AAG5E,SAAO,OAAO;AAChB;AAOO,SAAS,oBAAuB,KAAuD;AACtF,QAAA,OAAO,OAAO,KAAK,GAAgB;AAEzC,QAAM,WAAW,KAAK,WAAW,KAAK,KAAK,CAAC,MAAM;AAElD,MAAI,UAAU;AACN,UAAA,EAAE,WAAW;AACf,QAAA,CAAC,OAAO,QAAgB,QAAAC,kBAAW,QAAQ,CAAC,WAAW,UAAU,QAAQ,SAAS,CAAC;AACvF,UAAMD,MAAAA,YAAY,IAAI,MAAM,OAAO,MAAM,GAAG,MAAM;AAAA,EAAA;AAI7C,SAAA;AACT;;;"}
|
package/dist/_helpers.d.ts
CHANGED
|
@@ -1,8 +1,23 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { CloudMethodOutput } from './cloud';
|
|
2
|
+
import { ClientDatabaseOutput, CloudDatabaseOutput } from './database';
|
|
2
3
|
/**
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
* @
|
|
6
|
-
* @
|
|
4
|
+
* 解析云对象方法调用的输出结果
|
|
5
|
+
*
|
|
6
|
+
* @template O - 云对象方法返回数据的类型
|
|
7
|
+
* @param output - 云对象方法调用的输出结果
|
|
8
|
+
* @param fallbackErrorMessage - 当输出中没有错误信息时使用的默认错误消息
|
|
9
|
+
* @returns 云对象方法返回成功时的数据部分
|
|
10
|
+
* @throws 当云对象方法调用失败时,抛出包含错误信息的异常
|
|
7
11
|
*/
|
|
8
|
-
export declare function
|
|
12
|
+
export declare function parseCloudMethodOutput<O>(output: CloudMethodOutput<O>, fallbackErrorMessage?: string): O;
|
|
13
|
+
/**
|
|
14
|
+
* 解析数据库执行结果
|
|
15
|
+
* @param res 客户端、云端响应结果
|
|
16
|
+
* @returns 处理后的结果
|
|
17
|
+
*/
|
|
18
|
+
export declare function parseDatabaseOutput<T>(res: ClientDatabaseOutput<T> | CloudDatabaseOutput<T>): T | Omit<T & {
|
|
19
|
+
code?: number | string;
|
|
20
|
+
errCode?: number | string;
|
|
21
|
+
errMsg?: string;
|
|
22
|
+
message?: string;
|
|
23
|
+
}, "errCode" | "errMsg" | "code" | "message">;
|
package/dist/_helpers.mjs
CHANGED
|
@@ -1,11 +1,23 @@
|
|
|
1
1
|
import { errorAssign } from "@cloudcome/utils-core/error";
|
|
2
|
-
|
|
2
|
+
import { objectOmit } from "@cloudcome/utils-core/object";
|
|
3
|
+
function parseCloudMethodOutput(output, fallbackErrorMessage = "") {
|
|
3
4
|
if (output.errCode) {
|
|
4
5
|
throw errorAssign(new Error(output.errMsg || fallbackErrorMessage), output);
|
|
5
6
|
}
|
|
6
7
|
return output.data;
|
|
7
8
|
}
|
|
9
|
+
function parseDatabaseOutput(res) {
|
|
10
|
+
const keys = Object.keys(res);
|
|
11
|
+
const isClient = keys.length === 1 && keys[0] === "result";
|
|
12
|
+
if (isClient) {
|
|
13
|
+
const { result } = res;
|
|
14
|
+
if (!result.errCode) return objectOmit(result, ["errCode", "errMsg", "code", "message"]);
|
|
15
|
+
throw errorAssign(new Error(result.errMsg), result);
|
|
16
|
+
}
|
|
17
|
+
return res;
|
|
18
|
+
}
|
|
8
19
|
export {
|
|
9
|
-
|
|
20
|
+
parseDatabaseOutput as a,
|
|
21
|
+
parseCloudMethodOutput as p
|
|
10
22
|
};
|
|
11
23
|
//# sourceMappingURL=_helpers.mjs.map
|
package/dist/_helpers.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"_helpers.mjs","sources":["../src/_helpers.ts"],"sourcesContent":["import { errorAssign } from '@cloudcome/utils-core/error';\nimport type {
|
|
1
|
+
{"version":3,"file":"_helpers.mjs","sources":["../src/_helpers.ts"],"sourcesContent":["import { errorAssign } from '@cloudcome/utils-core/error';\nimport { objectOmit } from '@cloudcome/utils-core/object';\nimport type { CloudMethodOutput } from './cloud';\nimport type { ClientDatabaseOutput, CloudDatabaseOutput } from './database';\n\n/**\n * 解析云对象方法调用的输出结果\n *\n * @template O - 云对象方法返回数据的类型\n * @param output - 云对象方法调用的输出结果\n * @param fallbackErrorMessage - 当输出中没有错误信息时使用的默认错误消息\n * @returns 云对象方法返回成功时的数据部分\n * @throws 当云对象方法调用失败时,抛出包含错误信息的异常\n */\nexport function parseCloudMethodOutput<O>(output: CloudMethodOutput<O>, fallbackErrorMessage = ''): O {\n if (output.errCode) {\n throw errorAssign(new Error(output.errMsg || fallbackErrorMessage), output);\n }\n\n return output.data;\n}\n\n/**\n * 解析数据库执行结果\n * @param res 客户端、云端响应结果\n * @returns 处理后的结果\n */\nexport function parseDatabaseOutput<T>(res: ClientDatabaseOutput<T> | CloudDatabaseOutput<T>) {\n const keys = Object.keys(res as AnyObject);\n // 客户端 { result: {errCode: 0, errMsg: 'ok'} & 数据 }\n const isClient = keys.length === 1 && keys[0] === 'result';\n\n if (isClient) {\n const { result } = res as ClientDatabaseOutput<T>;\n if (!result.errCode) return objectOmit(result, ['errCode', 'errMsg', 'code', 'message']);\n throw errorAssign(new Error(result.errMsg), result);\n }\n\n // 云端 数据\n return res as T;\n}\n"],"names":[],"mappings":";;AAcgB,SAAA,uBAA0B,QAA8B,uBAAuB,IAAO;AACpG,MAAI,OAAO,SAAS;AAClB,UAAM,YAAY,IAAI,MAAM,OAAO,UAAU,oBAAoB,GAAG,MAAM;AAAA,EAAA;AAG5E,SAAO,OAAO;AAChB;AAOO,SAAS,oBAAuB,KAAuD;AACtF,QAAA,OAAO,OAAO,KAAK,GAAgB;AAEzC,QAAM,WAAW,KAAK,WAAW,KAAK,KAAK,CAAC,MAAM;AAElD,MAAI,UAAU;AACN,UAAA,EAAE,WAAW;AACf,QAAA,CAAC,OAAO,QAAgB,QAAA,WAAW,QAAQ,CAAC,WAAW,UAAU,QAAQ,SAAS,CAAC;AACvF,UAAM,YAAY,IAAI,MAAM,OAAO,MAAM,GAAG,MAAM;AAAA,EAAA;AAI7C,SAAA;AACT;"}
|
package/dist/client.cjs
CHANGED
|
@@ -2,16 +2,17 @@
|
|
|
2
2
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
3
|
const request = require("@cloudcome/utils-vue/request");
|
|
4
4
|
const _helpers = require("./_helpers.cjs");
|
|
5
|
-
function
|
|
5
|
+
function importCloudObject(objectName, options) {
|
|
6
6
|
const server = options?._mockServer || uniCloud.importObject(objectName, options);
|
|
7
7
|
const fallbackErrorMessage = options?.fallbackErrorMessage || "请求失败";
|
|
8
|
-
const
|
|
8
|
+
const useCloudMethod = (method, caller, options2) => {
|
|
9
9
|
return request.useRequest(async (...inputs) => {
|
|
10
|
-
const
|
|
11
|
-
|
|
10
|
+
const request2 = server[method];
|
|
11
|
+
const output = await caller(request2, ...inputs);
|
|
12
|
+
return _helpers.parseCloudMethodOutput(output, fallbackErrorMessage);
|
|
12
13
|
}, options2);
|
|
13
14
|
};
|
|
14
|
-
return
|
|
15
|
+
return useCloudMethod;
|
|
15
16
|
}
|
|
16
17
|
function useDatabase(caller, options) {
|
|
17
18
|
const db = options?._mockDatabase || uniCloud.database();
|
|
@@ -21,7 +22,7 @@ function useDatabase(caller, options) {
|
|
|
21
22
|
throw new Error(result.errMsg || "请求失败");
|
|
22
23
|
}, options);
|
|
23
24
|
}
|
|
24
|
-
exports.
|
|
25
|
-
exports.
|
|
25
|
+
exports.parseCloudMethodOutput = _helpers.parseCloudMethodOutput;
|
|
26
|
+
exports.importCloudObject = importCloudObject;
|
|
26
27
|
exports.useDatabase = useDatabase;
|
|
27
28
|
//# sourceMappingURL=client.cjs.map
|
package/dist/client.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.cjs","sources":["../src/client.ts"],"sourcesContent":["import type {
|
|
1
|
+
{"version":3,"file":"client.cjs","sources":["../src/client.ts"],"sourcesContent":["import type { CloudMethodOutput } from '@/cloud';\nimport type { ClientDatabaseOutput } from '@/database';\nimport type { AnyArray, AnyFunction } from '@cloudcome/utils-core/types';\nimport {\n type UseRequestOptions,\n type UseRequestOutput,\n type UseRequestOutputFilled,\n useRequest,\n} from '@cloudcome/utils-vue/request';\nimport { parseCloudMethodOutput } from './_helpers';\n\ntype _ImportObject = UniCloudNamespace.UniCloud['importObject'];\ntype _ImportObjectArgs = Parameters<_ImportObject>;\n\nexport type CreateUseCloudObjectOptions = _ImportObjectArgs[1] & {\n /**\n * 模拟云对象,用于单元测试\n * @private\n */\n // biome-ignore lint/suspicious/noExplicitAny: <explanation>\n _mockServer?: any;\n\n /**\n * 回退错误信息\n * @default '请求失败'\n */\n fallbackErrorMessage?: string;\n};\n\n/**\n * 云对象请求函数类型定义\n * @template F 云对象方法函数类型\n * @param input 云对象方法的参数数组\n * @returns 返回云对象方法执行结果的Promise\n */\nexport type CloudObjectRequest = <F extends AnyFunction>(\n ...input: Parameters<F>\n) => Promise<CloudMethodOutput<ReturnType<F>>>;\n\n/**\n * 用于调用云对象方法的hook函数类型定义\n * @template I 输入参数类型数组\n * @template O 输出结果类型\n */\nexport type UseCloudMethod = {\n /**\n * 重载签名:当提供 placeholder 选项时,返回包含初始值的输出类型\n * @param method 云对象方法名\n * @param caller 调用云对象方法的函数\n * @param options 包含 placeholder 的请求配置选项\n * @returns 返回包含初始值的请求输出\n */\n <I extends AnyArray, O>(\n method: string,\n caller: (request: CloudObjectRequest, ...inputs: I) => Promise<CloudMethodOutput<O>>,\n options: Omit<UseRequestOptions<I, O>, 'placeholder'> & { placeholder: () => O },\n ): UseRequestOutputFilled<I, O>;\n\n /**\n * 重载签名:当不提供 placeholder 选项时,返回普通输出类型\n * @param method 云对象方法名\n * @param caller 调用云对象方法的函数\n * @param options 可选的请求配置选项\n * @returns 返回普通的请求输出\n */\n <I extends AnyArray, O>(\n method: string,\n caller: (request: CloudObjectRequest, ...inputs: I) => Promise<CloudMethodOutput<O>>,\n options?: UseRequestOptions<I, O>,\n ): UseRequestOutput<I, O>;\n};\n/**\n * 导入云对象并创建一个用于调用云对象的hook\n * @param objectName 云对象名称\n * @param options 配置选项,包含模拟服务器、回退错误信息等\n * @returns 返回一个可用于调用云对象方法的hook函数\n */\nexport function importCloudObject(objectName: _ImportObjectArgs[0], options?: CreateUseCloudObjectOptions) {\n const server = options?._mockServer || uniCloud.importObject(objectName, options);\n const fallbackErrorMessage = options?.fallbackErrorMessage || '请求失败';\n\n /**\n * 用于调用云对象方法的hook函数\n * @template I 输入参数类型\n * @template O 输出结果类型\n * @param method 云对象方法名\n * @param caller 调用云对象的函数\n * @param options 配置选项,包含请求相关的配置\n * @returns 返回一个请求hook,用于处理云对象调用\n */\n const useCloudMethod: UseCloudMethod = (method, caller, options) => {\n // 使用请求hook处理云对象调用\n return useRequest(async (...inputs) => {\n const request = server[method];\n const output = await caller(request, ...inputs);\n return parseCloudMethodOutput(output, fallbackErrorMessage);\n }, options);\n };\n\n return useCloudMethod;\n}\n\nexport type UseDatabaseOptions<I extends AnyArray, O> = UseRequestOptions<I, O> & {\n /**\n * 模拟数据库,用于单元测试\n */\n // biome-ignore lint/suspicious/noExplicitAny: <explanation>\n _mockDatabase?: any;\n};\n\n/**\n * 创建一个用于调用云数据库的hook\n * @param caller 调用云数据库的函数,接收数据库实例和输入参数,返回Promise\n * @param options 配置选项\n * @returns 返回一个请求hook,用于处理云数据库调用\n */\nexport function useDatabase<I extends AnyArray, O>(\n caller: (db: UniCloud.Database, ...inputs: I) => Promise<ClientDatabaseOutput<O>>,\n options: Omit<UseDatabaseOptions<I, O>, 'placeholder'> & { placeholder: () => O },\n): UseRequestOutputFilled<I, O>;\nexport function useDatabase<I extends AnyArray, O>(\n caller: (db: UniCloud.Database, ...inputs: I) => Promise<ClientDatabaseOutput<O>>,\n options?: UseDatabaseOptions<I, O>,\n): UseRequestOutput<I, O>;\nexport function useDatabase<I extends AnyArray, O>(\n caller: (db: UniCloud.Database, ...inputs: I) => Promise<ClientDatabaseOutput<O>>,\n options?: UseDatabaseOptions<I, O>,\n): UseRequestOutput<I, O> {\n // 获取数据库实例,优先使用模拟数据库(用于测试),否则使用uniCloud数据库\n const db = options?._mockDatabase || uniCloud.database();\n return useRequest(async (...inputs: I) => {\n const { result } = await caller(db, ...inputs);\n if (!result.errCode) return result;\n throw new Error(result.errMsg || '请求失败');\n }, options);\n}\n\nexport { parseCloudMethodOutput } from './_helpers';\n"],"names":["options","useRequest","request","parseCloudMethodOutput"],"mappings":";;;;AA6EgB,SAAA,kBAAkB,YAAkC,SAAuC;AACzG,QAAM,SAAS,SAAS,eAAe,SAAS,aAAa,YAAY,OAAO;AAC1E,QAAA,uBAAuB,SAAS,wBAAwB;AAW9D,QAAM,iBAAiC,CAAC,QAAQ,QAAQA,aAAY;AAE3D,WAAAC,QAAA,WAAW,UAAU,WAAW;AAC/B,YAAAC,WAAU,OAAO,MAAM;AAC7B,YAAM,SAAS,MAAM,OAAOA,UAAS,GAAG,MAAM;AACvC,aAAAC,SAAA,uBAAuB,QAAQ,oBAAoB;AAAA,OACzDH,QAAO;AAAA,EACZ;AAEO,SAAA;AACT;AAwBgB,SAAA,YACd,QACA,SACwB;AAExB,QAAM,KAAK,SAAS,iBAAiB,SAAS,SAAS;AAChD,SAAAC,QAAA,WAAW,UAAU,WAAc;AACxC,UAAM,EAAE,OAAO,IAAI,MAAM,OAAO,IAAI,GAAG,MAAM;AACzC,QAAA,CAAC,OAAO,QAAgB,QAAA;AAC5B,UAAM,IAAI,MAAM,OAAO,UAAU,MAAM;AAAA,KACtC,OAAO;AACZ;;;;"}
|
package/dist/client.d.ts
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import { AnyArray } from '@cloudcome/utils-core/types';
|
|
1
|
+
import { CloudMethodOutput } from './cloud';
|
|
2
|
+
import { ClientDatabaseOutput } from './database';
|
|
3
|
+
import { AnyArray, AnyFunction } from '@cloudcome/utils-core/types';
|
|
4
4
|
import { UseRequestOptions, UseRequestOutput, UseRequestOutputFilled } from '@cloudcome/utils-vue/request';
|
|
5
|
-
type
|
|
6
|
-
type
|
|
7
|
-
export type
|
|
8
|
-
export type CreateUseCloudObjectOptions = ImportObjectArgs[1] & {
|
|
5
|
+
type _ImportObject = UniCloudNamespace.UniCloud['importObject'];
|
|
6
|
+
type _ImportObjectArgs = Parameters<_ImportObject>;
|
|
7
|
+
export type CreateUseCloudObjectOptions = _ImportObjectArgs[1] & {
|
|
9
8
|
/**
|
|
10
9
|
* 模拟云对象,用于单元测试
|
|
11
10
|
* @private
|
|
@@ -17,19 +16,45 @@ export type CreateUseCloudObjectOptions = ImportObjectArgs[1] & {
|
|
|
17
16
|
*/
|
|
18
17
|
fallbackErrorMessage?: string;
|
|
19
18
|
};
|
|
20
|
-
|
|
21
|
-
|
|
19
|
+
/**
|
|
20
|
+
* 云对象请求函数类型定义
|
|
21
|
+
* @template F 云对象方法函数类型
|
|
22
|
+
* @param input 云对象方法的参数数组
|
|
23
|
+
* @returns 返回云对象方法执行结果的Promise
|
|
24
|
+
*/
|
|
25
|
+
export type CloudObjectRequest = <F extends AnyFunction>(...input: Parameters<F>) => Promise<CloudMethodOutput<ReturnType<F>>>;
|
|
26
|
+
/**
|
|
27
|
+
* 用于调用云对象方法的hook函数类型定义
|
|
28
|
+
* @template I 输入参数类型数组
|
|
29
|
+
* @template O 输出结果类型
|
|
30
|
+
*/
|
|
31
|
+
export type UseCloudMethod = {
|
|
32
|
+
/**
|
|
33
|
+
* 重载签名:当提供 placeholder 选项时,返回包含初始值的输出类型
|
|
34
|
+
* @param method 云对象方法名
|
|
35
|
+
* @param caller 调用云对象方法的函数
|
|
36
|
+
* @param options 包含 placeholder 的请求配置选项
|
|
37
|
+
* @returns 返回包含初始值的请求输出
|
|
38
|
+
*/
|
|
39
|
+
<I extends AnyArray, O>(method: string, caller: (request: CloudObjectRequest, ...inputs: I) => Promise<CloudMethodOutput<O>>, options: Omit<UseRequestOptions<I, O>, 'placeholder'> & {
|
|
22
40
|
placeholder: () => O;
|
|
23
41
|
}): UseRequestOutputFilled<I, O>;
|
|
24
|
-
|
|
42
|
+
/**
|
|
43
|
+
* 重载签名:当不提供 placeholder 选项时,返回普通输出类型
|
|
44
|
+
* @param method 云对象方法名
|
|
45
|
+
* @param caller 调用云对象方法的函数
|
|
46
|
+
* @param options 可选的请求配置选项
|
|
47
|
+
* @returns 返回普通的请求输出
|
|
48
|
+
*/
|
|
49
|
+
<I extends AnyArray, O>(method: string, caller: (request: CloudObjectRequest, ...inputs: I) => Promise<CloudMethodOutput<O>>, options?: UseRequestOptions<I, O>): UseRequestOutput<I, O>;
|
|
25
50
|
};
|
|
26
51
|
/**
|
|
27
|
-
*
|
|
28
|
-
* @param objectName
|
|
29
|
-
* @param options
|
|
30
|
-
* @returns
|
|
52
|
+
* 导入云对象并创建一个用于调用云对象的hook
|
|
53
|
+
* @param objectName 云对象名称
|
|
54
|
+
* @param options 配置选项,包含模拟服务器、回退错误信息等
|
|
55
|
+
* @returns 返回一个可用于调用云对象方法的hook函数
|
|
31
56
|
*/
|
|
32
|
-
export declare function
|
|
57
|
+
export declare function importCloudObject(objectName: _ImportObjectArgs[0], options?: CreateUseCloudObjectOptions): UseCloudMethod;
|
|
33
58
|
export type UseDatabaseOptions<I extends AnyArray, O> = UseRequestOptions<I, O> & {
|
|
34
59
|
/**
|
|
35
60
|
* 模拟数据库,用于单元测试
|
|
@@ -42,8 +67,8 @@ export type UseDatabaseOptions<I extends AnyArray, O> = UseRequestOptions<I, O>
|
|
|
42
67
|
* @param options 配置选项
|
|
43
68
|
* @returns 返回一个请求hook,用于处理云数据库调用
|
|
44
69
|
*/
|
|
45
|
-
export declare function useDatabase<I extends AnyArray, O>(caller: (db: UniCloud.Database, ...inputs: I) => Promise<
|
|
70
|
+
export declare function useDatabase<I extends AnyArray, O>(caller: (db: UniCloud.Database, ...inputs: I) => Promise<ClientDatabaseOutput<O>>, options: Omit<UseDatabaseOptions<I, O>, 'placeholder'> & {
|
|
46
71
|
placeholder: () => O;
|
|
47
72
|
}): UseRequestOutputFilled<I, O>;
|
|
48
|
-
export declare function useDatabase<I extends AnyArray, O>(caller: (db: UniCloud.Database, ...inputs: I) => Promise<
|
|
49
|
-
export {
|
|
73
|
+
export declare function useDatabase<I extends AnyArray, O>(caller: (db: UniCloud.Database, ...inputs: I) => Promise<ClientDatabaseOutput<O>>, options?: UseDatabaseOptions<I, O>): UseRequestOutput<I, O>;
|
|
74
|
+
export { parseCloudMethodOutput } from './_helpers';
|
package/dist/client.mjs
CHANGED
|
@@ -1,15 +1,16 @@
|
|
|
1
1
|
import { useRequest } from "@cloudcome/utils-vue/request";
|
|
2
|
-
import { p as
|
|
3
|
-
function
|
|
2
|
+
import { p as parseCloudMethodOutput } from "./_helpers.mjs";
|
|
3
|
+
function importCloudObject(objectName, options) {
|
|
4
4
|
const server = options?._mockServer || uniCloud.importObject(objectName, options);
|
|
5
5
|
const fallbackErrorMessage = options?.fallbackErrorMessage || "请求失败";
|
|
6
|
-
const
|
|
6
|
+
const useCloudMethod = (method, caller, options2) => {
|
|
7
7
|
return useRequest(async (...inputs) => {
|
|
8
|
-
const
|
|
9
|
-
|
|
8
|
+
const request = server[method];
|
|
9
|
+
const output = await caller(request, ...inputs);
|
|
10
|
+
return parseCloudMethodOutput(output, fallbackErrorMessage);
|
|
10
11
|
}, options2);
|
|
11
12
|
};
|
|
12
|
-
return
|
|
13
|
+
return useCloudMethod;
|
|
13
14
|
}
|
|
14
15
|
function useDatabase(caller, options) {
|
|
15
16
|
const db = options?._mockDatabase || uniCloud.database();
|
|
@@ -20,8 +21,8 @@ function useDatabase(caller, options) {
|
|
|
20
21
|
}, options);
|
|
21
22
|
}
|
|
22
23
|
export {
|
|
23
|
-
|
|
24
|
-
|
|
24
|
+
importCloudObject,
|
|
25
|
+
parseCloudMethodOutput,
|
|
25
26
|
useDatabase
|
|
26
27
|
};
|
|
27
28
|
//# sourceMappingURL=client.mjs.map
|
package/dist/client.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.mjs","sources":["../src/client.ts"],"sourcesContent":["import type {
|
|
1
|
+
{"version":3,"file":"client.mjs","sources":["../src/client.ts"],"sourcesContent":["import type { CloudMethodOutput } from '@/cloud';\nimport type { ClientDatabaseOutput } from '@/database';\nimport type { AnyArray, AnyFunction } from '@cloudcome/utils-core/types';\nimport {\n type UseRequestOptions,\n type UseRequestOutput,\n type UseRequestOutputFilled,\n useRequest,\n} from '@cloudcome/utils-vue/request';\nimport { parseCloudMethodOutput } from './_helpers';\n\ntype _ImportObject = UniCloudNamespace.UniCloud['importObject'];\ntype _ImportObjectArgs = Parameters<_ImportObject>;\n\nexport type CreateUseCloudObjectOptions = _ImportObjectArgs[1] & {\n /**\n * 模拟云对象,用于单元测试\n * @private\n */\n // biome-ignore lint/suspicious/noExplicitAny: <explanation>\n _mockServer?: any;\n\n /**\n * 回退错误信息\n * @default '请求失败'\n */\n fallbackErrorMessage?: string;\n};\n\n/**\n * 云对象请求函数类型定义\n * @template F 云对象方法函数类型\n * @param input 云对象方法的参数数组\n * @returns 返回云对象方法执行结果的Promise\n */\nexport type CloudObjectRequest = <F extends AnyFunction>(\n ...input: Parameters<F>\n) => Promise<CloudMethodOutput<ReturnType<F>>>;\n\n/**\n * 用于调用云对象方法的hook函数类型定义\n * @template I 输入参数类型数组\n * @template O 输出结果类型\n */\nexport type UseCloudMethod = {\n /**\n * 重载签名:当提供 placeholder 选项时,返回包含初始值的输出类型\n * @param method 云对象方法名\n * @param caller 调用云对象方法的函数\n * @param options 包含 placeholder 的请求配置选项\n * @returns 返回包含初始值的请求输出\n */\n <I extends AnyArray, O>(\n method: string,\n caller: (request: CloudObjectRequest, ...inputs: I) => Promise<CloudMethodOutput<O>>,\n options: Omit<UseRequestOptions<I, O>, 'placeholder'> & { placeholder: () => O },\n ): UseRequestOutputFilled<I, O>;\n\n /**\n * 重载签名:当不提供 placeholder 选项时,返回普通输出类型\n * @param method 云对象方法名\n * @param caller 调用云对象方法的函数\n * @param options 可选的请求配置选项\n * @returns 返回普通的请求输出\n */\n <I extends AnyArray, O>(\n method: string,\n caller: (request: CloudObjectRequest, ...inputs: I) => Promise<CloudMethodOutput<O>>,\n options?: UseRequestOptions<I, O>,\n ): UseRequestOutput<I, O>;\n};\n/**\n * 导入云对象并创建一个用于调用云对象的hook\n * @param objectName 云对象名称\n * @param options 配置选项,包含模拟服务器、回退错误信息等\n * @returns 返回一个可用于调用云对象方法的hook函数\n */\nexport function importCloudObject(objectName: _ImportObjectArgs[0], options?: CreateUseCloudObjectOptions) {\n const server = options?._mockServer || uniCloud.importObject(objectName, options);\n const fallbackErrorMessage = options?.fallbackErrorMessage || '请求失败';\n\n /**\n * 用于调用云对象方法的hook函数\n * @template I 输入参数类型\n * @template O 输出结果类型\n * @param method 云对象方法名\n * @param caller 调用云对象的函数\n * @param options 配置选项,包含请求相关的配置\n * @returns 返回一个请求hook,用于处理云对象调用\n */\n const useCloudMethod: UseCloudMethod = (method, caller, options) => {\n // 使用请求hook处理云对象调用\n return useRequest(async (...inputs) => {\n const request = server[method];\n const output = await caller(request, ...inputs);\n return parseCloudMethodOutput(output, fallbackErrorMessage);\n }, options);\n };\n\n return useCloudMethod;\n}\n\nexport type UseDatabaseOptions<I extends AnyArray, O> = UseRequestOptions<I, O> & {\n /**\n * 模拟数据库,用于单元测试\n */\n // biome-ignore lint/suspicious/noExplicitAny: <explanation>\n _mockDatabase?: any;\n};\n\n/**\n * 创建一个用于调用云数据库的hook\n * @param caller 调用云数据库的函数,接收数据库实例和输入参数,返回Promise\n * @param options 配置选项\n * @returns 返回一个请求hook,用于处理云数据库调用\n */\nexport function useDatabase<I extends AnyArray, O>(\n caller: (db: UniCloud.Database, ...inputs: I) => Promise<ClientDatabaseOutput<O>>,\n options: Omit<UseDatabaseOptions<I, O>, 'placeholder'> & { placeholder: () => O },\n): UseRequestOutputFilled<I, O>;\nexport function useDatabase<I extends AnyArray, O>(\n caller: (db: UniCloud.Database, ...inputs: I) => Promise<ClientDatabaseOutput<O>>,\n options?: UseDatabaseOptions<I, O>,\n): UseRequestOutput<I, O>;\nexport function useDatabase<I extends AnyArray, O>(\n caller: (db: UniCloud.Database, ...inputs: I) => Promise<ClientDatabaseOutput<O>>,\n options?: UseDatabaseOptions<I, O>,\n): UseRequestOutput<I, O> {\n // 获取数据库实例,优先使用模拟数据库(用于测试),否则使用uniCloud数据库\n const db = options?._mockDatabase || uniCloud.database();\n return useRequest(async (...inputs: I) => {\n const { result } = await caller(db, ...inputs);\n if (!result.errCode) return result;\n throw new Error(result.errMsg || '请求失败');\n }, options);\n}\n\nexport { parseCloudMethodOutput } from './_helpers';\n"],"names":["options"],"mappings":";;AA6EgB,SAAA,kBAAkB,YAAkC,SAAuC;AACzG,QAAM,SAAS,SAAS,eAAe,SAAS,aAAa,YAAY,OAAO;AAC1E,QAAA,uBAAuB,SAAS,wBAAwB;AAW9D,QAAM,iBAAiC,CAAC,QAAQ,QAAQA,aAAY;AAE3D,WAAA,WAAW,UAAU,WAAW;AAC/B,YAAA,UAAU,OAAO,MAAM;AAC7B,YAAM,SAAS,MAAM,OAAO,SAAS,GAAG,MAAM;AACvC,aAAA,uBAAuB,QAAQ,oBAAoB;AAAA,OACzDA,QAAO;AAAA,EACZ;AAEO,SAAA;AACT;AAwBgB,SAAA,YACd,QACA,SACwB;AAExB,QAAM,KAAK,SAAS,iBAAiB,SAAS,SAAS;AAChD,SAAA,WAAW,UAAU,WAAc;AACxC,UAAM,EAAE,OAAO,IAAI,MAAM,OAAO,IAAI,GAAG,MAAM;AACzC,QAAA,CAAC,OAAO,QAAgB,QAAA;AAC5B,UAAM,IAAI,MAAM,OAAO,UAAU,MAAM;AAAA,KACtC,OAAO;AACZ;"}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { MaybePromise } from '@cloudcome/utils-core/types';
|
|
2
|
+
import { default as z, ZodObject } from 'zod';
|
|
3
|
+
import { CloudMethod, CloudObjectThis } from './types';
|
|
4
|
+
import { UniIdCommonModule } from './uni-id';
|
|
5
|
+
type _CloudObjectThisAppendUser = {
|
|
6
|
+
id: string;
|
|
7
|
+
role: string[];
|
|
8
|
+
permission: string[];
|
|
9
|
+
isAdmin: boolean;
|
|
10
|
+
};
|
|
11
|
+
type _CloudObjectThisAppend = {
|
|
12
|
+
options: Required<CreateCloudObjectOptions>;
|
|
13
|
+
user: _CloudObjectThisAppendUser;
|
|
14
|
+
};
|
|
15
|
+
export type CloudObjectContext = CloudObjectThis & _CloudObjectThisAppend;
|
|
16
|
+
export type BuildCloudMethodCreatorOptions = {
|
|
17
|
+
/**
|
|
18
|
+
* UniId 通用模块
|
|
19
|
+
* 用于处理用户身份验证和权限管理
|
|
20
|
+
* 如果提供,将在云对象执行前验证用户身份
|
|
21
|
+
*/
|
|
22
|
+
uniIdCommonModule?: UniIdCommonModule;
|
|
23
|
+
/**
|
|
24
|
+
* 需要用户登录态的错误码
|
|
25
|
+
* @default 'uni-id-check-token-failed'
|
|
26
|
+
*/
|
|
27
|
+
requiredUserErrCode?: number | string;
|
|
28
|
+
/**
|
|
29
|
+
* 需要用户登录态的错误消息
|
|
30
|
+
* @default '需要登录后才能进行此操作'
|
|
31
|
+
*/
|
|
32
|
+
requiredUserErrMsg?: string;
|
|
33
|
+
/**
|
|
34
|
+
* 仅允许本地环境运行的错误消息
|
|
35
|
+
* @default '运行环境不匹配'
|
|
36
|
+
*/
|
|
37
|
+
onlyLocalEnvErrMsg?: string;
|
|
38
|
+
/**
|
|
39
|
+
* 响应附加数据函数
|
|
40
|
+
* 用于在云对象响应中添加额外的上下文信息
|
|
41
|
+
* @param objectThis 云对象上下文
|
|
42
|
+
* @returns 返回要附加到响应中的数据对象
|
|
43
|
+
*/
|
|
44
|
+
respondAppend?: (objectThis: CloudObjectThis) => AnyObject;
|
|
45
|
+
};
|
|
46
|
+
export type CreateCloudObjectOptions = {
|
|
47
|
+
/**
|
|
48
|
+
* 是否需要用户登录态
|
|
49
|
+
* @default false
|
|
50
|
+
*/
|
|
51
|
+
requiredUser?: boolean;
|
|
52
|
+
/**
|
|
53
|
+
* 是否仅在本地环境运行
|
|
54
|
+
* @default false
|
|
55
|
+
*/
|
|
56
|
+
onlyLocalEnv?: boolean;
|
|
57
|
+
};
|
|
58
|
+
/**
|
|
59
|
+
* 云对象方法创建器类型定义
|
|
60
|
+
*
|
|
61
|
+
* 用于定义云对象方法的创建函数类型,支持两种重载形式:
|
|
62
|
+
* 1. 带输入验证的版本:接收schema、处理函数和选项
|
|
63
|
+
* 2. 无输入参数的版本:仅接收处理函数和选项
|
|
64
|
+
*/
|
|
65
|
+
export type CreateCloudMethod = {
|
|
66
|
+
/**
|
|
67
|
+
* 带输入验证的云对象方法创建器
|
|
68
|
+
* @template S - Zod验证模式类型
|
|
69
|
+
* @template O - 返回值类型
|
|
70
|
+
* @param schema - Zod验证模式,用于验证输入数据
|
|
71
|
+
* @param fn - 业务逻辑处理函数,接收上下文和验证后的输入数据
|
|
72
|
+
* @param options - 云对象创建选项
|
|
73
|
+
* @returns 云对象方法函数
|
|
74
|
+
*/
|
|
75
|
+
<S extends ZodObject, O>(schema: S, fn: (context: CloudObjectContext, input: z.infer<S>) => MaybePromise<O>, options?: CreateCloudObjectOptions): CloudMethod<z.infer<S>, O>;
|
|
76
|
+
/**
|
|
77
|
+
* 无输入参数的云对象方法创建器
|
|
78
|
+
* @template O - 返回值类型
|
|
79
|
+
* @param fn - 业务逻辑处理函数,仅接收上下文
|
|
80
|
+
* @param options - 云对象创建选项
|
|
81
|
+
* @returns 云对象方法函数
|
|
82
|
+
*/
|
|
83
|
+
<O>(fn: (context: CloudObjectContext) => MaybePromise<O>, options?: CreateCloudObjectOptions): CloudMethod<void, O>;
|
|
84
|
+
};
|
|
85
|
+
/**
|
|
86
|
+
* 构建云对象方法创建器
|
|
87
|
+
*
|
|
88
|
+
* 该函数用于创建云对象方法的工厂函数,支持输入验证、用户身份验证、环境检查等功能。
|
|
89
|
+
* 返回的创建器函数可以根据不同的配置创建云对象方法。
|
|
90
|
+
*
|
|
91
|
+
* @param options 构建选项,用于配置云对象方法创建器的行为
|
|
92
|
+
* @returns 返回一个云对象方法创建器函数
|
|
93
|
+
*
|
|
94
|
+
* @example
|
|
95
|
+
* // 创建一个需要用户登录的云对象方法
|
|
96
|
+
* const createMethod = buildCloudMethodCreator({ uniIdCommonModule });
|
|
97
|
+
* const myMethod = createMethod(async (context) => {
|
|
98
|
+
* return { message: 'Hello ' + context.user.id };
|
|
99
|
+
* }, { requiredUser: true });
|
|
100
|
+
*/
|
|
101
|
+
export declare function buildCloudMethodCreator(options?: BuildCloudMethodCreatorOptions): CreateCloudMethod;
|
|
102
|
+
export {};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { CloudModuleOutput } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* 解析云模块输出结果
|
|
4
|
+
*
|
|
5
|
+
* 该函数用于处理云模块的输出,如果输出中包含错误码,则抛出相应的错误;
|
|
6
|
+
* 否则返回去除错误码和错误信息后的数据部分。
|
|
7
|
+
*
|
|
8
|
+
* @template O - 输出数据的类型
|
|
9
|
+
* @param output - 云模块的输出结果,包含errCode、errMsg和数据部分
|
|
10
|
+
* @param fallbackErrorMessage - 当输出中没有错误信息时使用的默认错误消息
|
|
11
|
+
* @returns 返回去除errCode和errMsg字段后的数据对象
|
|
12
|
+
* @throws {CloudObjectError} 当output中存在errCode时抛出包含错误码和错误信息的异常
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* // 成功情况
|
|
16
|
+
* const result = parseCloudModuleOutput({ value: 'success', errCode: 0, errMsg: '' });
|
|
17
|
+
* // 返回: { value: 'success' }
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* // 错误情况
|
|
21
|
+
* try {
|
|
22
|
+
* parseCloudModuleOutput({ errCode: 404, errMsg: 'Not Found' });
|
|
23
|
+
* } catch (error) {
|
|
24
|
+
* // 抛出错误: CloudObjectError('Not Found', 404)
|
|
25
|
+
* }
|
|
26
|
+
*/
|
|
27
|
+
export declare function parseCloudModuleOutput<O>(output: CloudModuleOutput<O>, fallbackErrorMessage?: string): Omit<O, 'errCode' | 'errMsg'>;
|
package/dist/cloud/respond.d.ts
CHANGED
|
@@ -1,24 +1,18 @@
|
|
|
1
1
|
import { MaybePromise } from '@cloudcome/utils-core/types';
|
|
2
|
-
import {
|
|
2
|
+
import { CloudMethodOutput } from './types';
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
* @
|
|
7
|
-
* @
|
|
4
|
+
* 执行云对象方法并标准化响应格式
|
|
5
|
+
*
|
|
6
|
+
* @template O - 函数返回值的类型
|
|
7
|
+
* @param fn - 要执行的异步函数
|
|
8
|
+
* @param append - 要附加到响应中的额外数据
|
|
9
|
+
* @returns 标准化的云对象响应对象
|
|
8
10
|
*
|
|
9
11
|
* @example
|
|
10
12
|
* ```typescript
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
* });
|
|
15
|
-
* // 返回: { errCode: 0, errMsg: '', data: { name: 'test', value: 123 } }
|
|
16
|
-
*
|
|
17
|
-
* // 失败情况
|
|
18
|
-
* const result = await respondCloudObject(() => {
|
|
19
|
-
* throw new Error('操作失败');
|
|
20
|
-
* });
|
|
21
|
-
* // 返回: { errCode: -1, errMsg: '操作失败', data: null }
|
|
13
|
+
* const result = await respondCloudMethod(async () => {
|
|
14
|
+
* return await getData();
|
|
15
|
+
* }, { extra: 'data' });
|
|
22
16
|
* ```
|
|
23
17
|
*/
|
|
24
|
-
export declare function
|
|
18
|
+
export declare function respondCloudMethod<O>(fn: () => MaybePromise<O>, append?: AnyObject): Promise<CloudMethodOutput<O>>;
|
package/dist/cloud/types.d.ts
CHANGED
|
@@ -62,7 +62,7 @@ export type ClientInfo = {
|
|
|
62
62
|
};
|
|
63
63
|
/**
|
|
64
64
|
* 云环境信息类型定义
|
|
65
|
-
*
|
|
65
|
+
* 包含了云对象运行环境的相关信息
|
|
66
66
|
*/
|
|
67
67
|
export type CloudInfo = {
|
|
68
68
|
/** 云服务提供商 */
|
|
@@ -96,7 +96,7 @@ export type HttpInfo = {
|
|
|
96
96
|
/** 是否为Base64编码 */
|
|
97
97
|
isBase64Encoded: boolean;
|
|
98
98
|
};
|
|
99
|
-
export type
|
|
99
|
+
export type CloudObjectThis = {
|
|
100
100
|
/**
|
|
101
101
|
* 获取客户端信息
|
|
102
102
|
*/
|
|
@@ -124,10 +124,11 @@ export type UniCloudObjectThis = {
|
|
|
124
124
|
getHttpInfo: () => HttpInfo | undefined;
|
|
125
125
|
};
|
|
126
126
|
/**
|
|
127
|
-
*
|
|
128
|
-
*
|
|
127
|
+
* 云对象方法输出类型定义
|
|
128
|
+
* 用于统一云对象方法返回格式
|
|
129
|
+
* @template T 返回数据的类型
|
|
129
130
|
*/
|
|
130
|
-
export type
|
|
131
|
+
export type CloudMethodOutput<T> = {
|
|
131
132
|
/** 错误码,可选 */
|
|
132
133
|
errCode?: number | string;
|
|
133
134
|
/** 错误信息,可选 */
|
|
@@ -139,19 +140,19 @@ export type UniCloudObjectOutput<T> = {
|
|
|
139
140
|
* 提取云对象输出类型中的数据类型
|
|
140
141
|
* 用于从 UniCloudObjectOutput<T> 中提取 T 类型
|
|
141
142
|
*/
|
|
142
|
-
export type ExtractUniCloudOutput<T> = T extends
|
|
143
|
+
export type ExtractUniCloudOutput<T> = T extends CloudMethodOutput<infer U> ? Awaited<U> : never;
|
|
143
144
|
/**
|
|
144
145
|
* 云模块输出类型定义
|
|
145
146
|
* 用于统一云模块返回格式
|
|
146
147
|
*/
|
|
147
|
-
export type
|
|
148
|
+
export type CloudModuleOutput<T> = {
|
|
148
149
|
/** 错误码,可选 */
|
|
149
150
|
errCode?: number | string;
|
|
150
151
|
/** 错误信息,可选 */
|
|
151
152
|
errMsg?: string;
|
|
152
153
|
} & T;
|
|
153
154
|
/**
|
|
154
|
-
*
|
|
155
|
+
* 云对象方法类型定义
|
|
155
156
|
* 定义了云对象方法的函数签名
|
|
156
157
|
* @template I 输入参数类型
|
|
157
158
|
* @template O 输出数据类型
|
|
@@ -159,9 +160,23 @@ export type UniCloudModuleOutput<T> = {
|
|
|
159
160
|
* @param input 输入参数
|
|
160
161
|
* @returns 返回包含输出数据的Promise
|
|
161
162
|
*/
|
|
162
|
-
export type
|
|
163
|
+
export type CloudMethod<I, O> = (
|
|
163
164
|
/** 云对象上下文 */
|
|
164
|
-
this:
|
|
165
|
+
this: CloudObjectThis,
|
|
165
166
|
/** 输入参数 */
|
|
166
|
-
input: I) => Promise<
|
|
167
|
-
|
|
167
|
+
input: I) => Promise<CloudMethodOutput<O>>;
|
|
168
|
+
/**
|
|
169
|
+
* 提取云对象方法输入参数类型
|
|
170
|
+
* 用于从 UniCloudExpose<I, O> 中提取输入参数类型 I
|
|
171
|
+
*/
|
|
172
|
+
export type ExtractCloudMethodInput<T> = T extends CloudMethod<infer I, infer O> ? I : never;
|
|
173
|
+
/**
|
|
174
|
+
* 提取云对象方法输出数据类型
|
|
175
|
+
* 用于从 UniCloudExpose<I, O> 中提取输出数据类型 O
|
|
176
|
+
*/
|
|
177
|
+
export type ExtractCloudMethodData<T> = T extends CloudMethod<infer I, infer O> ? O : never;
|
|
178
|
+
/**
|
|
179
|
+
* 提取云对象方法签名类型
|
|
180
|
+
* 用于从 UniCloudExpose<I, O> 中提取函数签名 (input: I) => O
|
|
181
|
+
*/
|
|
182
|
+
export type ExtractCloudMethodFunction<T> = T extends CloudMethod<infer I, infer O> ? (input: I) => O : never;
|
package/dist/cloud/uni-id.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ClientInfo,
|
|
1
|
+
import { ClientInfo, CloudModuleOutput } from './types';
|
|
2
2
|
/**
|
|
3
3
|
* uni-id-common 模块
|
|
4
4
|
*/
|
|
@@ -10,7 +10,7 @@ export type UniIdCommonModule = {
|
|
|
10
10
|
export type UniIdCommonInstance = {
|
|
11
11
|
checkToken: (token: string) => Promise<UniIdUser | undefined>;
|
|
12
12
|
};
|
|
13
|
-
export type UniIdUser =
|
|
13
|
+
export type UniIdUser = CloudModuleOutput<{
|
|
14
14
|
uid?: string;
|
|
15
15
|
role?: string[];
|
|
16
16
|
permission?: string[];
|