@cloudbase/wx-cloud-client-sdk 1.2.1 → 1.3.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/lib/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  import { CloudBaseInstance, ExtendedCloudBaseInstance } from './types';
2
2
  export declare function init(cloud: CloudBaseInstance): ExtendedCloudBaseInstance;
3
3
  export * from './types';
4
+ export { generateHTTPClient } from './orm/http-orm-client';
@@ -0,0 +1,5 @@
1
+ import { SDKRequestInterface } from '@cloudbase/adapter-interface';
2
+ import { OrmClient, CallFunction } from '../types';
3
+ type ModelFetch = NonNullable<SDKRequestInterface['fetch']>;
4
+ export declare const generateHTTPClient: (callFunction: CallFunction, fetch: ModelFetch, baseUrl: string) => OrmClient;
5
+ export {};
@@ -0,0 +1,2 @@
1
+ import type { CallFunction, OrmRawQueryClient } from '../types';
2
+ export declare const createRawQueryClient: (callFunction: CallFunction) => OrmRawQueryClient;
@@ -10,7 +10,7 @@ export interface Model {
10
10
  updateBy?: Relation;
11
11
  _openid?: string;
12
12
  }
13
- export declare type EnvType = 'prod' | 'pre';
13
+ export type EnvType = 'prod' | 'pre';
14
14
  /**
15
15
  * 模型方法的定义
16
16
  * =====================================================================
@@ -19,7 +19,7 @@ export declare type EnvType = 'prod' | 'pre';
19
19
  * 模型操作方法的返回类型定义。
20
20
  * @template T 返回数据的类型。
21
21
  */
22
- export declare type MethodResponse<T> = {
22
+ export type MethodResponse<T> = {
23
23
  /**
24
24
  * 返回的数据。
25
25
  */
@@ -293,14 +293,14 @@ export interface DataModelMethods<T> {
293
293
  * 数据创建方法的返回类型定义。
294
294
  * @template T 模型字段的类型。
295
295
  */
296
- export declare type CreateResponse<T> = {
296
+ export type CreateResponse<T> = {
297
297
  id: string;
298
298
  };
299
299
  /**
300
300
  * 创建多条记录的响应类型定义。
301
301
  * @template T 模型字段的类型。
302
302
  */
303
- export declare type CreateManyResponse<T> = {
303
+ export type CreateManyResponse<T> = {
304
304
  /**
305
305
  * 创建的记录的ID列表。
306
306
  */
@@ -310,7 +310,7 @@ export declare type CreateManyResponse<T> = {
310
310
  * 更新操作的响应类型定义。
311
311
  * @template T 模型字段的类型。
312
312
  */
313
- export declare type UpdateResponse<T> = {
313
+ export type UpdateResponse<T> = {
314
314
  /**
315
315
  * 更新操作影响的记录数量。
316
316
  */
@@ -320,7 +320,7 @@ export declare type UpdateResponse<T> = {
320
320
  * 创建或者更新的响应类型定义。
321
321
  * @template T 模型字段的类型。
322
322
  */
323
- export declare type UpsertResponse<T> = {
323
+ export type UpsertResponse<T> = {
324
324
  /**
325
325
  * 变更的条数,返回非 0 值代表更新成功
326
326
  */
@@ -334,7 +334,7 @@ export declare type UpsertResponse<T> = {
334
334
  * 删除操作的响应类型定义,用于表示删除操作影响的记录数量。
335
335
  * @template T 模型字段的类型。
336
336
  */
337
- export declare type DeleteResponse<T> = {
337
+ export type DeleteResponse<T> = {
338
338
  /**
339
339
  * 删除操作影响的记录数量。
340
340
  * 如果count为0,表示没有记录被删除;
@@ -346,12 +346,12 @@ export declare type DeleteResponse<T> = {
346
346
  * 删除多条记录的响应类型定义,与更新操作的响应类型相同。
347
347
  * @template T 模型字段的类型。
348
348
  */
349
- export declare type DeleteManyResponse<T> = UpdateResponse<T>;
349
+ export type DeleteManyResponse<T> = UpdateResponse<T>;
350
350
  /**
351
351
  * 数据列表方法的返回类型定义。
352
352
  * @template T 模型字段的类型。
353
353
  */
354
- export declare type ListResponse<T> = MethodResponse<{
354
+ export type ListResponse<T> = MethodResponse<{
355
355
  records: T[];
356
356
  total?: number;
357
357
  }>;
@@ -359,7 +359,7 @@ export declare type ListResponse<T> = MethodResponse<{
359
359
  * list 方法参数定义。
360
360
  * @template T 模型字段的类型。
361
361
  */
362
- export declare type ListParams<T> = {
362
+ export type ListParams<T> = {
363
363
  filter?: FilterParams<T>;
364
364
  select?: SelectParams<T> | {
365
365
  $master: boolean;
@@ -392,7 +392,7 @@ export declare type ListParams<T> = {
392
392
  where: {},
393
393
  }
394
394
  */
395
- export declare type FilterParams<T> = {
395
+ export type FilterParams<T> = {
396
396
  /**
397
397
  * 基础查询
398
398
  */
@@ -448,7 +448,7 @@ export declare type FilterParams<T> = {
448
448
  * ]
449
449
  * }
450
450
  */
451
- export declare type FilterObject<T> = {
451
+ export type FilterObject<T> = {
452
452
  [operator in LogicalOperator]?: FilterConditionItem<T>[] | FilterObject<T>;
453
453
  };
454
454
  /**
@@ -497,7 +497,7 @@ export declare type FilterObject<T> = {
497
497
  * }
498
498
  * ```
499
499
  */
500
- export declare type FilterConditionItem<T> = {
500
+ export type FilterConditionItem<T> = {
501
501
  [key in keyof T]?: FilterCondition;
502
502
  };
503
503
  /**
@@ -508,7 +508,7 @@ export declare type FilterConditionItem<T> = {
508
508
  * "$eq": "val"
509
509
  * }
510
510
  */
511
- export declare type FilterCondition = {
511
+ export type FilterCondition = {
512
512
  [key in ComparisonOperator]?: any;
513
513
  };
514
514
  /**
@@ -516,7 +516,7 @@ export declare type FilterCondition = {
516
516
  * @example
517
517
  * $eq: 等于
518
518
  */
519
- export declare type ComparisonOperator = BasicComparisonOperator | SpecialComparisonOperator;
519
+ export type ComparisonOperator = BasicComparisonOperator | SpecialComparisonOperator;
520
520
  /**
521
521
  * 排序参数结构定义。
522
522
  *
@@ -525,7 +525,7 @@ export declare type ComparisonOperator = BasicComparisonOperator | SpecialCompar
525
525
  "createdAt": "asc",
526
526
  }
527
527
  */
528
- export declare type OrderByParams = {
528
+ export type OrderByParams = {
529
529
  [key: string]: 'asc' | 'desc';
530
530
  };
531
531
  /**
@@ -549,7 +549,7 @@ export declare type OrderByParams = {
549
549
  }
550
550
  }
551
551
  */
552
- export declare type SelectParams<T> = {
552
+ export type SelectParams<T> = {
553
553
  [K in keyof T]?: T[K] extends Array<infer U> | undefined ? SelectParams<U> | boolean : T[K] extends object | undefined ? SelectParams<T[K]> | boolean : boolean;
554
554
  } & {
555
555
  $master?: boolean;
@@ -557,29 +557,29 @@ export declare type SelectParams<T> = {
557
557
  /**
558
558
  * 基础比较运算符类型定义。
559
559
  */
560
- export declare type BasicComparisonOperator = '$eq' | '$neq' | '$gt' | '$gte' | '$lt' | '$lte' | '$in' | '$nin';
560
+ export type BasicComparisonOperator = '$eq' | '$neq' | '$gt' | '$gte' | '$lt' | '$lte' | '$in' | '$nin';
561
561
  /**
562
562
  * 特殊比较运算符类型定义
563
563
  */
564
- export declare type SpecialComparisonOperator = '$search' | '$nsearch' | '$empty' | '$nempty';
564
+ export type SpecialComparisonOperator = '$search' | '$nsearch' | '$empty' | '$nempty';
565
565
  /**
566
566
  * 逻辑运算符类型定义。
567
567
  */
568
- export declare type LogicalOperator = '$and' | '$or';
569
- export declare type RelationField<T> = T extends {
568
+ export type LogicalOperator = '$and' | '$or';
569
+ export type RelationField<T> = T extends {
570
570
  [key: string]: infer U;
571
571
  } ? U : never;
572
572
  /**
573
573
  * @hidden
574
574
  */
575
- declare type RelationKeys<T> = T extends any ? {
575
+ type RelationKeys<T> = T extends any ? {
576
576
  [K in keyof T]: T[K] extends Model | Model[] ? K : never;
577
577
  }[keyof T] : never;
578
578
  /**
579
579
  * 关联关系类型定义。
580
580
  * @hidden
581
581
  */
582
- export declare type Relation = string;
582
+ export type Relation = string;
583
583
  export interface SQLCommandParams {
584
584
  /**
585
585
  * 超时时间,默认是 5s,最大不超过 15s
@@ -685,7 +685,7 @@ export interface RunMysqlCommandParams {
685
685
  * 云函数调用接口,包含调用函数和认证信息。
686
686
  * @hidden
687
687
  */
688
- export declare type CloudBaseInstance = {
688
+ export type CloudBaseInstance = {
689
689
  callFunction: CallFunction;
690
690
  auth: any;
691
691
  };
@@ -702,7 +702,7 @@ export interface ExtendedCloudBaseInstance extends CloudBaseInstance {
702
702
  * @param args - 包含函数名称、数据源名称、方法名称和参数的对象。
703
703
  * @returns 返回一个Promise,解析为任意类型。
704
704
  */
705
- export declare type CallFunction = (args: {
705
+ export type CallFunction = (args: {
706
706
  name: string;
707
707
  data: Record<string, any>;
708
708
  }) => Promise<any>;
@@ -143,7 +143,7 @@ function getUserAgent() {
143
143
  return ua_1;
144
144
  }
145
145
  }
146
- var VERSION = "1.2.1";
146
+ var VERSION = "1.3.0";
147
147
 
148
148
  var callDataSource = function (_a) {
149
149
  var dataSourceName = _a.dataSourceName, methodName = _a.methodName, params = _a.params, realMethodName = _a.realMethodName, callFunction = _a.callFunction, _b = _a.envType, envType = _b === void 0 ? 'prod' : _b, mode = _a.mode;
@@ -265,6 +265,45 @@ var runMysqlCommand = function (_a) {
265
265
  });
266
266
  };
267
267
 
268
+ var createRawQueryClient = function (callFunction) { return ({
269
+ $runSQL: function (sql, params, config) {
270
+ return __awaiter(this, void 0, void 0, function () {
271
+ var res;
272
+ return __generator(this, function (_a) {
273
+ switch (_a.label) {
274
+ case 0: return [4 /*yield*/, runMysqlCommand({
275
+ sql: sql,
276
+ params: params,
277
+ config: __assign(__assign({}, config), { preparedStatements: true }),
278
+ callFunction: callFunction
279
+ })];
280
+ case 1:
281
+ res = _a.sent();
282
+ return [2 /*return*/, res];
283
+ }
284
+ });
285
+ });
286
+ },
287
+ $runSQLRaw: function (sql, config) {
288
+ return __awaiter(this, void 0, void 0, function () {
289
+ var res;
290
+ return __generator(this, function (_a) {
291
+ switch (_a.label) {
292
+ case 0: return [4 /*yield*/, runMysqlCommand({
293
+ sql: sql,
294
+ params: [],
295
+ config: __assign(__assign({}, config), { preparedStatements: false }),
296
+ callFunction: callFunction
297
+ })];
298
+ case 1:
299
+ res = _a.sent();
300
+ return [2 /*return*/, res];
301
+ }
302
+ });
303
+ });
304
+ }
305
+ }); };
306
+
268
307
  var CRUD_METHODS = {
269
308
  create: {
270
309
  methodName: 'wedaCreateV2'
@@ -352,48 +391,11 @@ var generateClientByDataSourceName = function (dataSourceName, callFunction) {
352
391
  };
353
392
  // 使用 TypeScript 的 Proxy 来定义一个动态的客户端
354
393
  var generateClient = function (callFunction) {
355
- var rawQueryClient = {
356
- $runSQL: function (sql, params, config) {
357
- return __awaiter(this, void 0, void 0, function () {
358
- var res;
359
- return __generator(this, function (_a) {
360
- switch (_a.label) {
361
- case 0: return [4 /*yield*/, runMysqlCommand({
362
- sql: sql,
363
- params: params,
364
- config: __assign(__assign({}, config), { preparedStatements: true }),
365
- callFunction: callFunction
366
- })];
367
- case 1:
368
- res = _a.sent();
369
- return [2 /*return*/, res];
370
- }
371
- });
372
- });
373
- },
374
- $runSQLRaw: function (sql, config) {
375
- return __awaiter(this, void 0, void 0, function () {
376
- var res;
377
- return __generator(this, function (_a) {
378
- switch (_a.label) {
379
- case 0: return [4 /*yield*/, runMysqlCommand({
380
- sql: sql,
381
- params: [],
382
- config: __assign(__assign({}, config), { preparedStatements: false }),
383
- callFunction: callFunction
384
- })];
385
- case 1:
386
- res = _a.sent();
387
- return [2 /*return*/, res];
388
- }
389
- });
390
- });
391
- }
392
- };
394
+ var rawQueryClient = createRawQueryClient(callFunction);
393
395
  return new Proxy({}, {
394
396
  get: function (target, prop) {
395
397
  if (typeof prop === 'string') {
396
- if (rawQueryClient.hasOwnProperty(prop)) {
398
+ if (Object.prototype.hasOwnProperty.call(rawQueryClient, prop)) {
397
399
  return rawQueryClient[prop];
398
400
  }
399
401
  // 返回一个函数,这个函数接受任意参数并返回一个 Promise
@@ -403,6 +405,109 @@ var generateClient = function (callFunction) {
403
405
  });
404
406
  };
405
407
 
408
+ var READ_DEFAULT_PARAMS = {
409
+ filter: {
410
+ where: {}
411
+ },
412
+ select: {
413
+ $master: true
414
+ }
415
+ };
416
+ function createDefaultMethod(methodName) {
417
+ return {
418
+ getUrl: function (modelName) { return "".concat(modelName, "/").concat(methodName); },
419
+ method: 'post'
420
+ };
421
+ }
422
+ var HTTP_DATA_MODEL_METHODS = {
423
+ get: __assign(__assign({}, createDefaultMethod('get')), { defaultParams: __assign({}, READ_DEFAULT_PARAMS) }),
424
+ list: __assign(__assign({}, createDefaultMethod('list')), { defaultParams: __assign({}, READ_DEFAULT_PARAMS) }),
425
+ create: createDefaultMethod('create'),
426
+ createMany: createDefaultMethod('createMany'),
427
+ update: __assign(__assign({}, createDefaultMethod('update')), { method: 'put' }),
428
+ updateMany: __assign(__assign({}, createDefaultMethod('updateMany')), { method: 'put' }),
429
+ upsert: createDefaultMethod('upsert'),
430
+ "delete": createDefaultMethod('delete'),
431
+ deleteMany: createDefaultMethod('deleteMany')
432
+ };
433
+ var UNKNOWN_ERROR_MESSAGE = 'Unknown error occurred';
434
+ var NOT_SUPPORTED_CODE = 'NotSupported';
435
+ var generateHTTPClient = function (callFunction, fetch, baseUrl) {
436
+ var rawQueryClient = createRawQueryClient(callFunction);
437
+ return new Proxy({}, {
438
+ get: function (_, modelName) {
439
+ if (typeof modelName !== 'string')
440
+ return undefined;
441
+ if (Object.prototype.hasOwnProperty.call(rawQueryClient, modelName)) {
442
+ return rawQueryClient[modelName];
443
+ }
444
+ return generateHTTPClientByDataSourceName(baseUrl, modelName, fetch);
445
+ }
446
+ });
447
+ };
448
+ var createWxCloudSDKError = function (message, modelName, methodName, code, requestId) {
449
+ return new WxCloudSDKError("\u3010\u9519\u8BEF\u3011".concat(message, "\n\u3010\u64CD\u4F5C\u3011\u8C03\u7528 models.").concat(modelName, ".").concat(methodName, "\n\u3010\u9519\u8BEF\u7801\u3011").concat(code, "\n\u3010\u8BF7\u6C42ID\u3011").concat(requestId), {
450
+ code: code,
451
+ requestId: requestId
452
+ });
453
+ };
454
+ var generateHTTPClientByDataSourceName = function (baseUrl, modelName, fetch) {
455
+ var client = new Proxy({}, {
456
+ get: function (_, methodName) {
457
+ var httpDataModelMethod = HTTP_DATA_MODEL_METHODS[methodName];
458
+ if (!httpDataModelMethod) {
459
+ var error = new Error("\u4E0D\u652F\u6301\u7684\u64CD\u4F5C: ".concat(methodName));
460
+ throw new WxCloudSDKError(error.message || UNKNOWN_ERROR_MESSAGE, {
461
+ originError: error,
462
+ code: NOT_SUPPORTED_CODE,
463
+ requestId: 'N/A'
464
+ });
465
+ }
466
+ return function (params) {
467
+ if (params === void 0) { params = {}; }
468
+ return __awaiter(void 0, void 0, void 0, function () {
469
+ var getUrl, method, _a, defaultParams, effectiveParams, url, result, error_1;
470
+ var _b;
471
+ return __generator(this, function (_c) {
472
+ switch (_c.label) {
473
+ case 0:
474
+ getUrl = httpDataModelMethod.getUrl, method = httpDataModelMethod.method, _a = httpDataModelMethod.defaultParams, defaultParams = _a === void 0 ? {} : _a;
475
+ effectiveParams = Object.assign({}, defaultParams, params);
476
+ url = [baseUrl, getUrl(modelName)].join('/');
477
+ _c.label = 1;
478
+ case 1:
479
+ _c.trys.push([1, 3, , 4]);
480
+ return [4 /*yield*/, fetch({
481
+ url: url,
482
+ body: JSON.stringify(effectiveParams),
483
+ method: method
484
+ })];
485
+ case 2:
486
+ result = _c.sent();
487
+ if (result.code) {
488
+ // 抛出错误
489
+ throw createWxCloudSDKError(result === null || result === void 0 ? void 0 : result.message, modelName, methodName, result === null || result === void 0 ? void 0 : result.code, result === null || result === void 0 ? void 0 : result.requestId);
490
+ }
491
+ if (methodName === 'get') {
492
+ // 和 callFunction 实现保持一致
493
+ Object.assign(result, { data: (_b = result.data.record) !== null && _b !== void 0 ? _b : result.data });
494
+ }
495
+ return [2 /*return*/, result];
496
+ case 3:
497
+ error_1 = _c.sent();
498
+ throw new WxCloudSDKError((error_1 === null || error_1 === void 0 ? void 0 : error_1.message) || UNKNOWN_ERROR_MESSAGE, {
499
+ originError: error_1
500
+ });
501
+ case 4: return [2 /*return*/];
502
+ }
503
+ });
504
+ });
505
+ };
506
+ }
507
+ });
508
+ return client;
509
+ };
510
+
406
511
  function init(cloud) {
407
512
  if (!cloud) {
408
513
  throw new Error('cloud is required');
@@ -410,9 +515,10 @@ function init(cloud) {
410
515
  if (!cloud.callFunction) {
411
516
  throw new Error('cloud.callFunction is required');
412
517
  }
413
- var OrmClientImpl = generateClient(cloud.callFunction.bind(cloud));
414
- cloud.models = OrmClientImpl;
518
+ var ormClientImpl = generateClient(cloud.callFunction.bind(cloud));
519
+ cloud.models = ormClientImpl;
415
520
  return cloud;
416
521
  }
417
522
 
523
+ exports.generateHTTPClient = generateHTTPClient;
418
524
  exports.init = init;
@@ -141,7 +141,7 @@ function getUserAgent() {
141
141
  return ua_1;
142
142
  }
143
143
  }
144
- var VERSION = "1.2.1";
144
+ var VERSION = "1.3.0";
145
145
 
146
146
  var callDataSource = function (_a) {
147
147
  var dataSourceName = _a.dataSourceName, methodName = _a.methodName, params = _a.params, realMethodName = _a.realMethodName, callFunction = _a.callFunction, _b = _a.envType, envType = _b === void 0 ? 'prod' : _b, mode = _a.mode;
@@ -263,6 +263,45 @@ var runMysqlCommand = function (_a) {
263
263
  });
264
264
  };
265
265
 
266
+ var createRawQueryClient = function (callFunction) { return ({
267
+ $runSQL: function (sql, params, config) {
268
+ return __awaiter(this, void 0, void 0, function () {
269
+ var res;
270
+ return __generator(this, function (_a) {
271
+ switch (_a.label) {
272
+ case 0: return [4 /*yield*/, runMysqlCommand({
273
+ sql: sql,
274
+ params: params,
275
+ config: __assign(__assign({}, config), { preparedStatements: true }),
276
+ callFunction: callFunction
277
+ })];
278
+ case 1:
279
+ res = _a.sent();
280
+ return [2 /*return*/, res];
281
+ }
282
+ });
283
+ });
284
+ },
285
+ $runSQLRaw: function (sql, config) {
286
+ return __awaiter(this, void 0, void 0, function () {
287
+ var res;
288
+ return __generator(this, function (_a) {
289
+ switch (_a.label) {
290
+ case 0: return [4 /*yield*/, runMysqlCommand({
291
+ sql: sql,
292
+ params: [],
293
+ config: __assign(__assign({}, config), { preparedStatements: false }),
294
+ callFunction: callFunction
295
+ })];
296
+ case 1:
297
+ res = _a.sent();
298
+ return [2 /*return*/, res];
299
+ }
300
+ });
301
+ });
302
+ }
303
+ }); };
304
+
266
305
  var CRUD_METHODS = {
267
306
  create: {
268
307
  methodName: 'wedaCreateV2'
@@ -350,48 +389,11 @@ var generateClientByDataSourceName = function (dataSourceName, callFunction) {
350
389
  };
351
390
  // 使用 TypeScript 的 Proxy 来定义一个动态的客户端
352
391
  var generateClient = function (callFunction) {
353
- var rawQueryClient = {
354
- $runSQL: function (sql, params, config) {
355
- return __awaiter(this, void 0, void 0, function () {
356
- var res;
357
- return __generator(this, function (_a) {
358
- switch (_a.label) {
359
- case 0: return [4 /*yield*/, runMysqlCommand({
360
- sql: sql,
361
- params: params,
362
- config: __assign(__assign({}, config), { preparedStatements: true }),
363
- callFunction: callFunction
364
- })];
365
- case 1:
366
- res = _a.sent();
367
- return [2 /*return*/, res];
368
- }
369
- });
370
- });
371
- },
372
- $runSQLRaw: function (sql, config) {
373
- return __awaiter(this, void 0, void 0, function () {
374
- var res;
375
- return __generator(this, function (_a) {
376
- switch (_a.label) {
377
- case 0: return [4 /*yield*/, runMysqlCommand({
378
- sql: sql,
379
- params: [],
380
- config: __assign(__assign({}, config), { preparedStatements: false }),
381
- callFunction: callFunction
382
- })];
383
- case 1:
384
- res = _a.sent();
385
- return [2 /*return*/, res];
386
- }
387
- });
388
- });
389
- }
390
- };
392
+ var rawQueryClient = createRawQueryClient(callFunction);
391
393
  return new Proxy({}, {
392
394
  get: function (target, prop) {
393
395
  if (typeof prop === 'string') {
394
- if (rawQueryClient.hasOwnProperty(prop)) {
396
+ if (Object.prototype.hasOwnProperty.call(rawQueryClient, prop)) {
395
397
  return rawQueryClient[prop];
396
398
  }
397
399
  // 返回一个函数,这个函数接受任意参数并返回一个 Promise
@@ -401,6 +403,109 @@ var generateClient = function (callFunction) {
401
403
  });
402
404
  };
403
405
 
406
+ var READ_DEFAULT_PARAMS = {
407
+ filter: {
408
+ where: {}
409
+ },
410
+ select: {
411
+ $master: true
412
+ }
413
+ };
414
+ function createDefaultMethod(methodName) {
415
+ return {
416
+ getUrl: function (modelName) { return "".concat(modelName, "/").concat(methodName); },
417
+ method: 'post'
418
+ };
419
+ }
420
+ var HTTP_DATA_MODEL_METHODS = {
421
+ get: __assign(__assign({}, createDefaultMethod('get')), { defaultParams: __assign({}, READ_DEFAULT_PARAMS) }),
422
+ list: __assign(__assign({}, createDefaultMethod('list')), { defaultParams: __assign({}, READ_DEFAULT_PARAMS) }),
423
+ create: createDefaultMethod('create'),
424
+ createMany: createDefaultMethod('createMany'),
425
+ update: __assign(__assign({}, createDefaultMethod('update')), { method: 'put' }),
426
+ updateMany: __assign(__assign({}, createDefaultMethod('updateMany')), { method: 'put' }),
427
+ upsert: createDefaultMethod('upsert'),
428
+ "delete": createDefaultMethod('delete'),
429
+ deleteMany: createDefaultMethod('deleteMany')
430
+ };
431
+ var UNKNOWN_ERROR_MESSAGE = 'Unknown error occurred';
432
+ var NOT_SUPPORTED_CODE = 'NotSupported';
433
+ var generateHTTPClient = function (callFunction, fetch, baseUrl) {
434
+ var rawQueryClient = createRawQueryClient(callFunction);
435
+ return new Proxy({}, {
436
+ get: function (_, modelName) {
437
+ if (typeof modelName !== 'string')
438
+ return undefined;
439
+ if (Object.prototype.hasOwnProperty.call(rawQueryClient, modelName)) {
440
+ return rawQueryClient[modelName];
441
+ }
442
+ return generateHTTPClientByDataSourceName(baseUrl, modelName, fetch);
443
+ }
444
+ });
445
+ };
446
+ var createWxCloudSDKError = function (message, modelName, methodName, code, requestId) {
447
+ return new WxCloudSDKError("\u3010\u9519\u8BEF\u3011".concat(message, "\n\u3010\u64CD\u4F5C\u3011\u8C03\u7528 models.").concat(modelName, ".").concat(methodName, "\n\u3010\u9519\u8BEF\u7801\u3011").concat(code, "\n\u3010\u8BF7\u6C42ID\u3011").concat(requestId), {
448
+ code: code,
449
+ requestId: requestId
450
+ });
451
+ };
452
+ var generateHTTPClientByDataSourceName = function (baseUrl, modelName, fetch) {
453
+ var client = new Proxy({}, {
454
+ get: function (_, methodName) {
455
+ var httpDataModelMethod = HTTP_DATA_MODEL_METHODS[methodName];
456
+ if (!httpDataModelMethod) {
457
+ var error = new Error("\u4E0D\u652F\u6301\u7684\u64CD\u4F5C: ".concat(methodName));
458
+ throw new WxCloudSDKError(error.message || UNKNOWN_ERROR_MESSAGE, {
459
+ originError: error,
460
+ code: NOT_SUPPORTED_CODE,
461
+ requestId: 'N/A'
462
+ });
463
+ }
464
+ return function (params) {
465
+ if (params === void 0) { params = {}; }
466
+ return __awaiter(void 0, void 0, void 0, function () {
467
+ var getUrl, method, _a, defaultParams, effectiveParams, url, result, error_1;
468
+ var _b;
469
+ return __generator(this, function (_c) {
470
+ switch (_c.label) {
471
+ case 0:
472
+ getUrl = httpDataModelMethod.getUrl, method = httpDataModelMethod.method, _a = httpDataModelMethod.defaultParams, defaultParams = _a === void 0 ? {} : _a;
473
+ effectiveParams = Object.assign({}, defaultParams, params);
474
+ url = [baseUrl, getUrl(modelName)].join('/');
475
+ _c.label = 1;
476
+ case 1:
477
+ _c.trys.push([1, 3, , 4]);
478
+ return [4 /*yield*/, fetch({
479
+ url: url,
480
+ body: JSON.stringify(effectiveParams),
481
+ method: method
482
+ })];
483
+ case 2:
484
+ result = _c.sent();
485
+ if (result.code) {
486
+ // 抛出错误
487
+ throw createWxCloudSDKError(result === null || result === void 0 ? void 0 : result.message, modelName, methodName, result === null || result === void 0 ? void 0 : result.code, result === null || result === void 0 ? void 0 : result.requestId);
488
+ }
489
+ if (methodName === 'get') {
490
+ // 和 callFunction 实现保持一致
491
+ Object.assign(result, { data: (_b = result.data.record) !== null && _b !== void 0 ? _b : result.data });
492
+ }
493
+ return [2 /*return*/, result];
494
+ case 3:
495
+ error_1 = _c.sent();
496
+ throw new WxCloudSDKError((error_1 === null || error_1 === void 0 ? void 0 : error_1.message) || UNKNOWN_ERROR_MESSAGE, {
497
+ originError: error_1
498
+ });
499
+ case 4: return [2 /*return*/];
500
+ }
501
+ });
502
+ });
503
+ };
504
+ }
505
+ });
506
+ return client;
507
+ };
508
+
404
509
  function init(cloud) {
405
510
  if (!cloud) {
406
511
  throw new Error('cloud is required');
@@ -408,9 +513,9 @@ function init(cloud) {
408
513
  if (!cloud.callFunction) {
409
514
  throw new Error('cloud.callFunction is required');
410
515
  }
411
- var OrmClientImpl = generateClient(cloud.callFunction.bind(cloud));
412
- cloud.models = OrmClientImpl;
516
+ var ormClientImpl = generateClient(cloud.callFunction.bind(cloud));
517
+ cloud.models = ormClientImpl;
413
518
  return cloud;
414
519
  }
415
520
 
416
- export { init };
521
+ export { generateHTTPClient, init };
@@ -147,7 +147,7 @@
147
147
  return ua_1;
148
148
  }
149
149
  }
150
- var VERSION = "1.2.1";
150
+ var VERSION = "1.3.0";
151
151
 
152
152
  var callDataSource = function (_a) {
153
153
  var dataSourceName = _a.dataSourceName, methodName = _a.methodName, params = _a.params, realMethodName = _a.realMethodName, callFunction = _a.callFunction, _b = _a.envType, envType = _b === void 0 ? 'prod' : _b, mode = _a.mode;
@@ -269,6 +269,45 @@
269
269
  });
270
270
  };
271
271
 
272
+ var createRawQueryClient = function (callFunction) { return ({
273
+ $runSQL: function (sql, params, config) {
274
+ return __awaiter(this, void 0, void 0, function () {
275
+ var res;
276
+ return __generator(this, function (_a) {
277
+ switch (_a.label) {
278
+ case 0: return [4 /*yield*/, runMysqlCommand({
279
+ sql: sql,
280
+ params: params,
281
+ config: __assign(__assign({}, config), { preparedStatements: true }),
282
+ callFunction: callFunction
283
+ })];
284
+ case 1:
285
+ res = _a.sent();
286
+ return [2 /*return*/, res];
287
+ }
288
+ });
289
+ });
290
+ },
291
+ $runSQLRaw: function (sql, config) {
292
+ return __awaiter(this, void 0, void 0, function () {
293
+ var res;
294
+ return __generator(this, function (_a) {
295
+ switch (_a.label) {
296
+ case 0: return [4 /*yield*/, runMysqlCommand({
297
+ sql: sql,
298
+ params: [],
299
+ config: __assign(__assign({}, config), { preparedStatements: false }),
300
+ callFunction: callFunction
301
+ })];
302
+ case 1:
303
+ res = _a.sent();
304
+ return [2 /*return*/, res];
305
+ }
306
+ });
307
+ });
308
+ }
309
+ }); };
310
+
272
311
  var CRUD_METHODS = {
273
312
  create: {
274
313
  methodName: 'wedaCreateV2'
@@ -356,48 +395,11 @@
356
395
  };
357
396
  // 使用 TypeScript 的 Proxy 来定义一个动态的客户端
358
397
  var generateClient = function (callFunction) {
359
- var rawQueryClient = {
360
- $runSQL: function (sql, params, config) {
361
- return __awaiter(this, void 0, void 0, function () {
362
- var res;
363
- return __generator(this, function (_a) {
364
- switch (_a.label) {
365
- case 0: return [4 /*yield*/, runMysqlCommand({
366
- sql: sql,
367
- params: params,
368
- config: __assign(__assign({}, config), { preparedStatements: true }),
369
- callFunction: callFunction
370
- })];
371
- case 1:
372
- res = _a.sent();
373
- return [2 /*return*/, res];
374
- }
375
- });
376
- });
377
- },
378
- $runSQLRaw: function (sql, config) {
379
- return __awaiter(this, void 0, void 0, function () {
380
- var res;
381
- return __generator(this, function (_a) {
382
- switch (_a.label) {
383
- case 0: return [4 /*yield*/, runMysqlCommand({
384
- sql: sql,
385
- params: [],
386
- config: __assign(__assign({}, config), { preparedStatements: false }),
387
- callFunction: callFunction
388
- })];
389
- case 1:
390
- res = _a.sent();
391
- return [2 /*return*/, res];
392
- }
393
- });
394
- });
395
- }
396
- };
398
+ var rawQueryClient = createRawQueryClient(callFunction);
397
399
  return new Proxy({}, {
398
400
  get: function (target, prop) {
399
401
  if (typeof prop === 'string') {
400
- if (rawQueryClient.hasOwnProperty(prop)) {
402
+ if (Object.prototype.hasOwnProperty.call(rawQueryClient, prop)) {
401
403
  return rawQueryClient[prop];
402
404
  }
403
405
  // 返回一个函数,这个函数接受任意参数并返回一个 Promise
@@ -407,6 +409,109 @@
407
409
  });
408
410
  };
409
411
 
412
+ var READ_DEFAULT_PARAMS = {
413
+ filter: {
414
+ where: {}
415
+ },
416
+ select: {
417
+ $master: true
418
+ }
419
+ };
420
+ function createDefaultMethod(methodName) {
421
+ return {
422
+ getUrl: function (modelName) { return "".concat(modelName, "/").concat(methodName); },
423
+ method: 'post'
424
+ };
425
+ }
426
+ var HTTP_DATA_MODEL_METHODS = {
427
+ get: __assign(__assign({}, createDefaultMethod('get')), { defaultParams: __assign({}, READ_DEFAULT_PARAMS) }),
428
+ list: __assign(__assign({}, createDefaultMethod('list')), { defaultParams: __assign({}, READ_DEFAULT_PARAMS) }),
429
+ create: createDefaultMethod('create'),
430
+ createMany: createDefaultMethod('createMany'),
431
+ update: __assign(__assign({}, createDefaultMethod('update')), { method: 'put' }),
432
+ updateMany: __assign(__assign({}, createDefaultMethod('updateMany')), { method: 'put' }),
433
+ upsert: createDefaultMethod('upsert'),
434
+ "delete": createDefaultMethod('delete'),
435
+ deleteMany: createDefaultMethod('deleteMany')
436
+ };
437
+ var UNKNOWN_ERROR_MESSAGE = 'Unknown error occurred';
438
+ var NOT_SUPPORTED_CODE = 'NotSupported';
439
+ var generateHTTPClient = function (callFunction, fetch, baseUrl) {
440
+ var rawQueryClient = createRawQueryClient(callFunction);
441
+ return new Proxy({}, {
442
+ get: function (_, modelName) {
443
+ if (typeof modelName !== 'string')
444
+ return undefined;
445
+ if (Object.prototype.hasOwnProperty.call(rawQueryClient, modelName)) {
446
+ return rawQueryClient[modelName];
447
+ }
448
+ return generateHTTPClientByDataSourceName(baseUrl, modelName, fetch);
449
+ }
450
+ });
451
+ };
452
+ var createWxCloudSDKError = function (message, modelName, methodName, code, requestId) {
453
+ return new WxCloudSDKError("\u3010\u9519\u8BEF\u3011".concat(message, "\n\u3010\u64CD\u4F5C\u3011\u8C03\u7528 models.").concat(modelName, ".").concat(methodName, "\n\u3010\u9519\u8BEF\u7801\u3011").concat(code, "\n\u3010\u8BF7\u6C42ID\u3011").concat(requestId), {
454
+ code: code,
455
+ requestId: requestId
456
+ });
457
+ };
458
+ var generateHTTPClientByDataSourceName = function (baseUrl, modelName, fetch) {
459
+ var client = new Proxy({}, {
460
+ get: function (_, methodName) {
461
+ var httpDataModelMethod = HTTP_DATA_MODEL_METHODS[methodName];
462
+ if (!httpDataModelMethod) {
463
+ var error = new Error("\u4E0D\u652F\u6301\u7684\u64CD\u4F5C: ".concat(methodName));
464
+ throw new WxCloudSDKError(error.message || UNKNOWN_ERROR_MESSAGE, {
465
+ originError: error,
466
+ code: NOT_SUPPORTED_CODE,
467
+ requestId: 'N/A'
468
+ });
469
+ }
470
+ return function (params) {
471
+ if (params === void 0) { params = {}; }
472
+ return __awaiter(void 0, void 0, void 0, function () {
473
+ var getUrl, method, _a, defaultParams, effectiveParams, url, result, error_1;
474
+ var _b;
475
+ return __generator(this, function (_c) {
476
+ switch (_c.label) {
477
+ case 0:
478
+ getUrl = httpDataModelMethod.getUrl, method = httpDataModelMethod.method, _a = httpDataModelMethod.defaultParams, defaultParams = _a === void 0 ? {} : _a;
479
+ effectiveParams = Object.assign({}, defaultParams, params);
480
+ url = [baseUrl, getUrl(modelName)].join('/');
481
+ _c.label = 1;
482
+ case 1:
483
+ _c.trys.push([1, 3, , 4]);
484
+ return [4 /*yield*/, fetch({
485
+ url: url,
486
+ body: JSON.stringify(effectiveParams),
487
+ method: method
488
+ })];
489
+ case 2:
490
+ result = _c.sent();
491
+ if (result.code) {
492
+ // 抛出错误
493
+ throw createWxCloudSDKError(result === null || result === void 0 ? void 0 : result.message, modelName, methodName, result === null || result === void 0 ? void 0 : result.code, result === null || result === void 0 ? void 0 : result.requestId);
494
+ }
495
+ if (methodName === 'get') {
496
+ // 和 callFunction 实现保持一致
497
+ Object.assign(result, { data: (_b = result.data.record) !== null && _b !== void 0 ? _b : result.data });
498
+ }
499
+ return [2 /*return*/, result];
500
+ case 3:
501
+ error_1 = _c.sent();
502
+ throw new WxCloudSDKError((error_1 === null || error_1 === void 0 ? void 0 : error_1.message) || UNKNOWN_ERROR_MESSAGE, {
503
+ originError: error_1
504
+ });
505
+ case 4: return [2 /*return*/];
506
+ }
507
+ });
508
+ });
509
+ };
510
+ }
511
+ });
512
+ return client;
513
+ };
514
+
410
515
  function init(cloud) {
411
516
  if (!cloud) {
412
517
  throw new Error('cloud is required');
@@ -414,11 +519,12 @@
414
519
  if (!cloud.callFunction) {
415
520
  throw new Error('cloud.callFunction is required');
416
521
  }
417
- var OrmClientImpl = generateClient(cloud.callFunction.bind(cloud));
418
- cloud.models = OrmClientImpl;
522
+ var ormClientImpl = generateClient(cloud.callFunction.bind(cloud));
523
+ cloud.models = ormClientImpl;
419
524
  return cloud;
420
525
  }
421
526
 
527
+ exports.generateHTTPClient = generateHTTPClient;
422
528
  exports.init = init;
423
529
 
424
530
  }));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cloudbase/wx-cloud-client-sdk",
3
- "version": "1.2.1",
3
+ "version": "1.3.0",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -26,13 +26,16 @@
26
26
  "deploy": "node ./scripts/deploy.mjs"
27
27
  },
28
28
  "devDependencies": {
29
+ "@cloudbase/adapter-interface": "^0.5.0",
30
+ "@cloudbase/js-sdk": "2.8.15-beta.0",
29
31
  "@rollup/plugin-commonjs": "^25.0.8",
30
32
  "@rollup/plugin-node-resolve": "^15.2.3",
31
33
  "@rollup/plugin-replace": "^5.0.7",
32
34
  "@rollup/plugin-typescript": "^11.1.6",
33
- "@tcwd/dev-tools": "workspace:^",
35
+ "@tcwd/dev-tools": "^1.0.1",
34
36
  "jest": "^29.7.0",
35
37
  "jest-environment-jsdom": "^29.7.0",
38
+ "node-fetch": "2",
36
39
  "parcel-bundler": "1.6.1",
37
40
  "rollup": "^4.18.0",
38
41
  "ts-jest": "^29.2.2",
@@ -47,4 +50,4 @@
47
50
  "typescript",
48
51
  "javascript"
49
52
  ]
50
- }
53
+ }