@cloudbase/wx-cloud-client-sdk 1.0.0-alpha.2 → 1.1.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/README.md ADDED
@@ -0,0 +1,9 @@
1
+ # 微信云开发客户端 SDK
2
+
3
+ ## 使用
4
+
5
+ [初始化 SDK](https://docs.cloudbase.net/model/init-sdk)
6
+
7
+ ## 参考文档
8
+
9
+ [类型声明](https://docs.cloudbase.net/model/sdk-reference/globals)
@@ -0,0 +1,10 @@
1
+ import { MethodResponse, CallDataSourceParams, RunMysqlCommandParams } from '../types';
2
+ export declare const enum EQUERY_PARAM_TYPE {
3
+ ARRAY = "ARRAY",
4
+ BOOLEAN = "BOOLEAN",
5
+ NUMBER = "NUMBER",
6
+ OBJECT = "OBJECT",
7
+ STRING = "STRING"
8
+ }
9
+ export declare const callDataSource: ({ dataSourceName, methodName, params, realMethodName, callFunction, mode, }: CallDataSourceParams) => Promise<MethodResponse<any>>;
10
+ export declare const runMysqlCommand: ({ sql, params, config, callFunction, unsafe }: RunMysqlCommandParams) => Promise<MethodResponse<any>>;
@@ -87,6 +87,32 @@ export interface DataModelMethods<T> {
87
87
  data: T;
88
88
  filter: FilterParams<T>;
89
89
  }) => Promise<MethodResponse<UpdateResponse<T>>>;
90
+ /**
91
+ * 创建或者更新的方法
92
+ * @param {Object} params - 包含创建或者更新对象以及和筛选条件的参数对象。
93
+ * @returns {Promise<MethodResponse<UpsertResponse<T>>>} 包含更新响应的Promise对象。
94
+ * @example
95
+ * models.<model_name>.upsert({
96
+ * update: {
97
+ * // 更新的数据字段
98
+ * },
99
+ * create: {
100
+ * // 创建的数据字段
101
+ * },
102
+ * filter: {
103
+ * where: {
104
+ * // 筛选条件
105
+ * }
106
+ * }
107
+ * }).then(({ data }) => {
108
+ * console.log(data.count); // 输出更新的数据条数
109
+ * });
110
+ */
111
+ upsert: (params: {
112
+ update: T;
113
+ create: T;
114
+ filter: FilterParams<T>;
115
+ }) => Promise<MethodResponse<UpsertResponse<T>>>;
90
116
  /**
91
117
  * 更新多条数据的方法。
92
118
  * @param {Object} params - 包含更新数据和筛选条件的参数对象。
@@ -253,6 +279,20 @@ export declare type UpdateResponse<T> = {
253
279
  */
254
280
  count: number;
255
281
  };
282
+ /**
283
+ * 创建或者更新的响应类型定义。
284
+ * @template T 模型字段的类型。
285
+ */
286
+ export declare type UpsertResponse<T> = {
287
+ /**
288
+ * 变更的条数,返回非 0 值代表更新成功
289
+ */
290
+ count: number;
291
+ /**
292
+ * 变更的条数,返回非 "" 值代表创建成功
293
+ */
294
+ id: string;
295
+ };
256
296
  /**
257
297
  * 删除操作的响应类型定义,用于表示删除操作影响的记录数量。
258
298
  * @template T 模型字段的类型。
@@ -503,6 +543,47 @@ declare type RelationKeys<T> = T extends any ? {
503
543
  * @hidden
504
544
  */
505
545
  export declare type Relation = string;
546
+ export interface SQLCommandParams {
547
+ /**
548
+ * 超时时间,默认是 5s,最大不超过 15s
549
+ */
550
+ timeout?: number;
551
+ }
552
+ /**
553
+ * 运行原生SQL的ORM客户端接口
554
+ */
555
+ export interface OrmRawQueryClient {
556
+ $runSQL?: (
557
+ /**
558
+ * sql 语句
559
+ */
560
+ sql: string,
561
+ /**
562
+ * sql 模版变量
563
+ */
564
+ params?: Record<string, any>,
565
+ /**
566
+ * 配置
567
+ */
568
+ config?: SQLCommandParams) => Promise<MethodResponse<{
569
+ executeResultList: Record<string, any>[];
570
+ total: number;
571
+ backendExecute: string;
572
+ }>>;
573
+ $runSQLRaw?: (
574
+ /**
575
+ * sql 语句
576
+ */
577
+ sql: string,
578
+ /**
579
+ * 配置
580
+ */
581
+ config?: SQLCommandParams) => Promise<MethodResponse<{
582
+ executeResultList: Record<string, any>[];
583
+ total: number;
584
+ backendExecute: string;
585
+ }>>;
586
+ }
506
587
  /**
507
588
  * ORM客户端接口,包含不同模型名称到其操作方法的映射。
508
589
  * @hidden
@@ -518,7 +599,7 @@ export interface CallDataSourceParams {
518
599
  /**
519
600
  * 数据源的名称,标识调用的数据源。
520
601
  */
521
- dataSourceName: string;
602
+ dataSourceName?: string;
522
603
  /**
523
604
  * 方法名称,标识要调用的数据源中的具体方法。
524
605
  */
@@ -535,6 +616,21 @@ export interface CallDataSourceParams {
535
616
  * 调用函数的方法,用于执行实际的云函数调用。
536
617
  */
537
618
  callFunction: CallFunction;
619
+ /**
620
+ * @deprecated 使用 dataSourceName 替代。
621
+ */
622
+ mode?: string;
623
+ }
624
+ /**
625
+ * 调用runMySQLCoommand 参数的结构定义,用于封装调用云函数时所需的参数。
626
+ * @hidden
627
+ */
628
+ export interface RunMysqlCommandParams {
629
+ sql: string;
630
+ params?: Record<string, any>;
631
+ callFunction: CallFunction;
632
+ config?: any;
633
+ unsafe?: boolean;
538
634
  }
539
635
  /**
540
636
  * 云函数调用接口,包含调用函数和认证信息。
@@ -549,7 +645,7 @@ export declare type CloudBaseInstance = {
549
645
  * @hidden
550
646
  */
551
647
  export interface ExtendedCloudBaseInstance extends CloudBaseInstance {
552
- models: OrmClient;
648
+ models: OrmClient & OrmRawQueryClient;
553
649
  }
554
650
  /**
555
651
  * 云函数调用方法定义。
@@ -559,10 +655,6 @@ export interface ExtendedCloudBaseInstance extends CloudBaseInstance {
559
655
  */
560
656
  export declare type CallFunction = (args: {
561
657
  name: string;
562
- data: {
563
- dataSourceName: string;
564
- methodName: string;
565
- params: Record<string, any>;
566
- };
658
+ data: Record<string, any>;
567
659
  }) => Promise<any>;
568
660
  export {};
package/lib/utils.d.ts ADDED
@@ -0,0 +1,10 @@
1
+ /**
2
+ * 获取全局对象 window
3
+ * 小程序中可用, 但小程序中对象信息残缺, 无法访问 navigator 对象, ua 信息也无意义
4
+ */
5
+ export declare function getGlobalObj(): false | typeof globalThis;
6
+ /** 获取referrer 信息, 担心小程序中报错, 故catch */
7
+ export declare function getReferrer(): any;
8
+ /** 获取用户UA, 小程序中使用 getSystemInfo 替代 */
9
+ export declare function getUserAgent(): any;
10
+ export declare const VERSION: string | undefined;
@@ -93,10 +93,62 @@ var WxCloudSDKError = /** @class */ (function (_super) {
93
93
  return WxCloudSDKError;
94
94
  }(Error));
95
95
 
96
+ /**
97
+ * 获取全局对象 window
98
+ * 小程序中可用, 但小程序中对象信息残缺, 无法访问 navigator 对象, ua 信息也无意义
99
+ */
100
+ function getGlobalObj() {
101
+ // @ts-ignore
102
+ return (typeof window !== 'undefined' && window) || (typeof globalThis !== 'undefined' && globalThis);
103
+ }
104
+ /** 获取referrer 信息, 担心小程序中报错, 故catch */
105
+ function getReferrer() {
106
+ try {
107
+ var globalObj = getGlobalObj();
108
+ if (!globalObj)
109
+ return;
110
+ // 浏览器中
111
+ // @ts-ignore
112
+ if (typeof wx === 'undefined') {
113
+ // @ts-ignore
114
+ return getGlobalObj().location.href;
115
+ }
116
+ // 当前页面路由
117
+ // @ts-ignore
118
+ return globalObj.__wxRoute;
119
+ }
120
+ catch (_a) { }
121
+ }
122
+ /** 获取用户UA, 小程序中使用 getSystemInfo 替代 */
123
+ function getUserAgent() {
124
+ var globalObj = getGlobalObj();
125
+ // @ts-ignore
126
+ if (globalObj === null || globalObj === void 0 ? void 0 : globalObj.navigator)
127
+ return globalObj.navigator.userAgent;
128
+ // 微信小程序
129
+ // @ts-ignore
130
+ if (typeof wx !== 'undefined' && wx.getSystemInfo) {
131
+ var ua_1;
132
+ // 同步接口
133
+ // @ts-ignore
134
+ wx.getSystemInfo({
135
+ success: function (res) {
136
+ if (!res)
137
+ return;
138
+ ua_1 = ['brand', 'model', 'version', 'system', 'platform', 'SDKVersion', 'language']
139
+ .map(function (k) { return "".concat(k, ": ").concat(res[k]); })
140
+ .join(', ');
141
+ }
142
+ });
143
+ return ua_1;
144
+ }
145
+ }
146
+ var VERSION = "1.1.0";
147
+
96
148
  var callDataSource = function (_a) {
97
- var dataSourceName = _a.dataSourceName, methodName = _a.methodName, params = _a.params, realMethodName = _a.realMethodName, callFunction = _a.callFunction;
149
+ var dataSourceName = _a.dataSourceName, methodName = _a.methodName, params = _a.params, realMethodName = _a.realMethodName, callFunction = _a.callFunction, mode = _a.mode;
98
150
  return __awaiter(void 0, void 0, void 0, function () {
99
- var result, response, error_1;
151
+ var result, response, requestId, error_1;
100
152
  var _b, _c;
101
153
  return __generator(this, function (_d) {
102
154
  switch (_d.label) {
@@ -113,20 +165,28 @@ var callDataSource = function (_a) {
113
165
  data: {
114
166
  dataSourceName: dataSourceName,
115
167
  methodName: methodName,
116
- params: params
168
+ params: params,
169
+ userAgent: getUserAgent(),
170
+ referrer: getReferrer(),
171
+ 'x-sdk-version': VERSION,
172
+ /**
173
+ * todo 移除此字段
174
+ */
175
+ mode: mode
117
176
  }
118
177
  })];
119
178
  case 2:
120
179
  response = _d.sent();
180
+ requestId = ((_b = response === null || response === void 0 ? void 0 : response.result) === null || _b === void 0 ? void 0 : _b.requestId) || (response === null || response === void 0 ? void 0 : response.requestId) || (response === null || response === void 0 ? void 0 : response.requestID);
121
181
  if (response === null || response === void 0 ? void 0 : response.result.code) {
122
- throw new WxCloudSDKError("\u3010\u9519\u8BEF\u3011".concat(response === null || response === void 0 ? void 0 : response.result.message, "\n\u3010\u64CD\u4F5C\u3011\u8C03\u7528 ").concat(dataSourceName, ".").concat(realMethodName, "\n\u3010\u8BF7\u6C42ID\u3011").concat((response === null || response === void 0 ? void 0 : response.requestId) || 'N/A'), {
182
+ throw new WxCloudSDKError("\u3010\u9519\u8BEF\u3011".concat(response === null || response === void 0 ? void 0 : response.result.message, "\n\u3010\u64CD\u4F5C\u3011\u8C03\u7528 models.").concat(dataSourceName ? "".concat(dataSourceName, ".") : "").concat(realMethodName, "\n\u3010\u9519\u8BEF\u7801\u3011").concat(response === null || response === void 0 ? void 0 : response.result.code, "\n\u3010\u8BF7\u6C42ID\u3011").concat(requestId || 'N/A'), {
123
183
  code: response === null || response === void 0 ? void 0 : response.result.code,
124
- requestId: response === null || response === void 0 ? void 0 : response.requestId
184
+ requestId: requestId
125
185
  });
126
186
  }
127
187
  else {
128
- result.data = ((_b = response === null || response === void 0 ? void 0 : response.result) === null || _b === void 0 ? void 0 : _b.data) || {};
129
- result.requestId = ((_c = response === null || response === void 0 ? void 0 : response.result) === null || _c === void 0 ? void 0 : _c.requestId) || '';
188
+ result.data = ((_c = response === null || response === void 0 ? void 0 : response.result) === null || _c === void 0 ? void 0 : _c.data) || {};
189
+ result.requestId = requestId;
130
190
  }
131
191
  return [3 /*break*/, 4];
132
192
  case 3:
@@ -136,7 +196,7 @@ var callDataSource = function (_a) {
136
196
  }
137
197
  else {
138
198
  console.log(error_1);
139
- throw new WxCloudSDKError("\u3010\u9519\u8BEF\u3011".concat(error_1.message, "\n \u3010\u64CD\u4F5C\u3011\u8C03\u7528 ").concat(dataSourceName, ".").concat(realMethodName, "\n \u3010\u8BF7\u6C42ID\u3011N/A"), {
199
+ throw new WxCloudSDKError("\u3010\u9519\u8BEF\u3011".concat(error_1.message, "\n \u3010\u64CD\u4F5C\u3011\u8C03\u7528 models.").concat(dataSourceName ? "".concat(dataSourceName, ".") : "").concat(realMethodName, "\n \u3010\u8BF7\u6C42ID\u3011N/A"), {
140
200
  code: 'UnknownError',
141
201
  originError: error_1
142
202
  });
@@ -146,6 +206,63 @@ var callDataSource = function (_a) {
146
206
  });
147
207
  });
148
208
  };
209
+ var runMysqlCommand = function (_a) {
210
+ var sql = _a.sql, params = _a.params, config = _a.config, callFunction = _a.callFunction, _b = _a.unsafe, unsafe = _b === void 0 ? false : _b;
211
+ return __awaiter(void 0, void 0, void 0, function () {
212
+ return __generator(this, function (_c) {
213
+ return [2 /*return*/, callDataSource({
214
+ realMethodName: '$runSQL',
215
+ methodName: 'callWedaApi',
216
+ params: {
217
+ action: 'RunMysqlCommand',
218
+ data: {
219
+ sqlTemplate: sql,
220
+ config: config,
221
+ parameter: unsafe
222
+ ? ''
223
+ : Object.entries(params || {}).reduce(function (list, _a) {
224
+ var key = _a[0], value = _a[1];
225
+ if (value !== undefined) {
226
+ var type = "OBJECT" /* EQUERY_PARAM_TYPE.OBJECT */;
227
+ var typeofValue = typeof value;
228
+ switch (typeofValue) {
229
+ case 'boolean': {
230
+ type = "BOOLEAN" /* EQUERY_PARAM_TYPE.BOOLEAN */;
231
+ break;
232
+ }
233
+ case 'number': {
234
+ type = "NUMBER" /* EQUERY_PARAM_TYPE.NUMBER */;
235
+ break;
236
+ }
237
+ case 'string': {
238
+ type = "STRING" /* EQUERY_PARAM_TYPE.STRING */;
239
+ break;
240
+ }
241
+ default: {
242
+ if (Array.isArray(value)) {
243
+ type = "ARRAY" /* EQUERY_PARAM_TYPE.ARRAY */;
244
+ }
245
+ else {
246
+ type = "OBJECT" /* EQUERY_PARAM_TYPE.OBJECT */;
247
+ }
248
+ }
249
+ }
250
+ list.push({
251
+ key: key,
252
+ type: type,
253
+ value: type === "STRING" /* EQUERY_PARAM_TYPE.STRING */ ? value : JSON.stringify(value)
254
+ });
255
+ }
256
+ return list;
257
+ }, []) || []
258
+ }
259
+ },
260
+ callFunction: callFunction,
261
+ mode: 'sdk'
262
+ })];
263
+ });
264
+ });
265
+ };
149
266
 
150
267
  var CRUD_METHODS = {
151
268
  create: {
@@ -157,6 +274,9 @@ var CRUD_METHODS = {
157
274
  update: {
158
275
  methodName: 'wedaUpdateV2'
159
276
  },
277
+ upsert: {
278
+ methodName: 'wedaUpsertV2'
279
+ },
160
280
  updateMany: {
161
281
  methodName: 'wedaBatchUpdateV2'
162
282
  },
@@ -230,9 +350,50 @@ var generateClientByDataSourceName = function (dataSourceName, callFunction) {
230
350
  };
231
351
  // 使用 TypeScript 的 Proxy 来定义一个动态的客户端
232
352
  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
+ };
233
391
  return new Proxy({}, {
234
392
  get: function (target, prop) {
235
393
  if (typeof prop === 'string') {
394
+ if (rawQueryClient.hasOwnProperty(prop)) {
395
+ return rawQueryClient[prop];
396
+ }
236
397
  // 返回一个函数,这个函数接受任意参数并返回一个 Promise
237
398
  return generateClientByDataSourceName(prop, callFunction);
238
399
  }
@@ -91,10 +91,62 @@ var WxCloudSDKError = /** @class */ (function (_super) {
91
91
  return WxCloudSDKError;
92
92
  }(Error));
93
93
 
94
+ /**
95
+ * 获取全局对象 window
96
+ * 小程序中可用, 但小程序中对象信息残缺, 无法访问 navigator 对象, ua 信息也无意义
97
+ */
98
+ function getGlobalObj() {
99
+ // @ts-ignore
100
+ return (typeof window !== 'undefined' && window) || (typeof globalThis !== 'undefined' && globalThis);
101
+ }
102
+ /** 获取referrer 信息, 担心小程序中报错, 故catch */
103
+ function getReferrer() {
104
+ try {
105
+ var globalObj = getGlobalObj();
106
+ if (!globalObj)
107
+ return;
108
+ // 浏览器中
109
+ // @ts-ignore
110
+ if (typeof wx === 'undefined') {
111
+ // @ts-ignore
112
+ return getGlobalObj().location.href;
113
+ }
114
+ // 当前页面路由
115
+ // @ts-ignore
116
+ return globalObj.__wxRoute;
117
+ }
118
+ catch (_a) { }
119
+ }
120
+ /** 获取用户UA, 小程序中使用 getSystemInfo 替代 */
121
+ function getUserAgent() {
122
+ var globalObj = getGlobalObj();
123
+ // @ts-ignore
124
+ if (globalObj === null || globalObj === void 0 ? void 0 : globalObj.navigator)
125
+ return globalObj.navigator.userAgent;
126
+ // 微信小程序
127
+ // @ts-ignore
128
+ if (typeof wx !== 'undefined' && wx.getSystemInfo) {
129
+ var ua_1;
130
+ // 同步接口
131
+ // @ts-ignore
132
+ wx.getSystemInfo({
133
+ success: function (res) {
134
+ if (!res)
135
+ return;
136
+ ua_1 = ['brand', 'model', 'version', 'system', 'platform', 'SDKVersion', 'language']
137
+ .map(function (k) { return "".concat(k, ": ").concat(res[k]); })
138
+ .join(', ');
139
+ }
140
+ });
141
+ return ua_1;
142
+ }
143
+ }
144
+ var VERSION = "1.1.0";
145
+
94
146
  var callDataSource = function (_a) {
95
- var dataSourceName = _a.dataSourceName, methodName = _a.methodName, params = _a.params, realMethodName = _a.realMethodName, callFunction = _a.callFunction;
147
+ var dataSourceName = _a.dataSourceName, methodName = _a.methodName, params = _a.params, realMethodName = _a.realMethodName, callFunction = _a.callFunction, mode = _a.mode;
96
148
  return __awaiter(void 0, void 0, void 0, function () {
97
- var result, response, error_1;
149
+ var result, response, requestId, error_1;
98
150
  var _b, _c;
99
151
  return __generator(this, function (_d) {
100
152
  switch (_d.label) {
@@ -111,20 +163,28 @@ var callDataSource = function (_a) {
111
163
  data: {
112
164
  dataSourceName: dataSourceName,
113
165
  methodName: methodName,
114
- params: params
166
+ params: params,
167
+ userAgent: getUserAgent(),
168
+ referrer: getReferrer(),
169
+ 'x-sdk-version': VERSION,
170
+ /**
171
+ * todo 移除此字段
172
+ */
173
+ mode: mode
115
174
  }
116
175
  })];
117
176
  case 2:
118
177
  response = _d.sent();
178
+ requestId = ((_b = response === null || response === void 0 ? void 0 : response.result) === null || _b === void 0 ? void 0 : _b.requestId) || (response === null || response === void 0 ? void 0 : response.requestId) || (response === null || response === void 0 ? void 0 : response.requestID);
119
179
  if (response === null || response === void 0 ? void 0 : response.result.code) {
120
- throw new WxCloudSDKError("\u3010\u9519\u8BEF\u3011".concat(response === null || response === void 0 ? void 0 : response.result.message, "\n\u3010\u64CD\u4F5C\u3011\u8C03\u7528 ").concat(dataSourceName, ".").concat(realMethodName, "\n\u3010\u8BF7\u6C42ID\u3011").concat((response === null || response === void 0 ? void 0 : response.requestId) || 'N/A'), {
180
+ throw new WxCloudSDKError("\u3010\u9519\u8BEF\u3011".concat(response === null || response === void 0 ? void 0 : response.result.message, "\n\u3010\u64CD\u4F5C\u3011\u8C03\u7528 models.").concat(dataSourceName ? "".concat(dataSourceName, ".") : "").concat(realMethodName, "\n\u3010\u9519\u8BEF\u7801\u3011").concat(response === null || response === void 0 ? void 0 : response.result.code, "\n\u3010\u8BF7\u6C42ID\u3011").concat(requestId || 'N/A'), {
121
181
  code: response === null || response === void 0 ? void 0 : response.result.code,
122
- requestId: response === null || response === void 0 ? void 0 : response.requestId
182
+ requestId: requestId
123
183
  });
124
184
  }
125
185
  else {
126
- result.data = ((_b = response === null || response === void 0 ? void 0 : response.result) === null || _b === void 0 ? void 0 : _b.data) || {};
127
- result.requestId = ((_c = response === null || response === void 0 ? void 0 : response.result) === null || _c === void 0 ? void 0 : _c.requestId) || '';
186
+ result.data = ((_c = response === null || response === void 0 ? void 0 : response.result) === null || _c === void 0 ? void 0 : _c.data) || {};
187
+ result.requestId = requestId;
128
188
  }
129
189
  return [3 /*break*/, 4];
130
190
  case 3:
@@ -134,7 +194,7 @@ var callDataSource = function (_a) {
134
194
  }
135
195
  else {
136
196
  console.log(error_1);
137
- throw new WxCloudSDKError("\u3010\u9519\u8BEF\u3011".concat(error_1.message, "\n \u3010\u64CD\u4F5C\u3011\u8C03\u7528 ").concat(dataSourceName, ".").concat(realMethodName, "\n \u3010\u8BF7\u6C42ID\u3011N/A"), {
197
+ throw new WxCloudSDKError("\u3010\u9519\u8BEF\u3011".concat(error_1.message, "\n \u3010\u64CD\u4F5C\u3011\u8C03\u7528 models.").concat(dataSourceName ? "".concat(dataSourceName, ".") : "").concat(realMethodName, "\n \u3010\u8BF7\u6C42ID\u3011N/A"), {
138
198
  code: 'UnknownError',
139
199
  originError: error_1
140
200
  });
@@ -144,6 +204,63 @@ var callDataSource = function (_a) {
144
204
  });
145
205
  });
146
206
  };
207
+ var runMysqlCommand = function (_a) {
208
+ var sql = _a.sql, params = _a.params, config = _a.config, callFunction = _a.callFunction, _b = _a.unsafe, unsafe = _b === void 0 ? false : _b;
209
+ return __awaiter(void 0, void 0, void 0, function () {
210
+ return __generator(this, function (_c) {
211
+ return [2 /*return*/, callDataSource({
212
+ realMethodName: '$runSQL',
213
+ methodName: 'callWedaApi',
214
+ params: {
215
+ action: 'RunMysqlCommand',
216
+ data: {
217
+ sqlTemplate: sql,
218
+ config: config,
219
+ parameter: unsafe
220
+ ? ''
221
+ : Object.entries(params || {}).reduce(function (list, _a) {
222
+ var key = _a[0], value = _a[1];
223
+ if (value !== undefined) {
224
+ var type = "OBJECT" /* EQUERY_PARAM_TYPE.OBJECT */;
225
+ var typeofValue = typeof value;
226
+ switch (typeofValue) {
227
+ case 'boolean': {
228
+ type = "BOOLEAN" /* EQUERY_PARAM_TYPE.BOOLEAN */;
229
+ break;
230
+ }
231
+ case 'number': {
232
+ type = "NUMBER" /* EQUERY_PARAM_TYPE.NUMBER */;
233
+ break;
234
+ }
235
+ case 'string': {
236
+ type = "STRING" /* EQUERY_PARAM_TYPE.STRING */;
237
+ break;
238
+ }
239
+ default: {
240
+ if (Array.isArray(value)) {
241
+ type = "ARRAY" /* EQUERY_PARAM_TYPE.ARRAY */;
242
+ }
243
+ else {
244
+ type = "OBJECT" /* EQUERY_PARAM_TYPE.OBJECT */;
245
+ }
246
+ }
247
+ }
248
+ list.push({
249
+ key: key,
250
+ type: type,
251
+ value: type === "STRING" /* EQUERY_PARAM_TYPE.STRING */ ? value : JSON.stringify(value)
252
+ });
253
+ }
254
+ return list;
255
+ }, []) || []
256
+ }
257
+ },
258
+ callFunction: callFunction,
259
+ mode: 'sdk'
260
+ })];
261
+ });
262
+ });
263
+ };
147
264
 
148
265
  var CRUD_METHODS = {
149
266
  create: {
@@ -155,6 +272,9 @@ var CRUD_METHODS = {
155
272
  update: {
156
273
  methodName: 'wedaUpdateV2'
157
274
  },
275
+ upsert: {
276
+ methodName: 'wedaUpsertV2'
277
+ },
158
278
  updateMany: {
159
279
  methodName: 'wedaBatchUpdateV2'
160
280
  },
@@ -228,9 +348,50 @@ var generateClientByDataSourceName = function (dataSourceName, callFunction) {
228
348
  };
229
349
  // 使用 TypeScript 的 Proxy 来定义一个动态的客户端
230
350
  var generateClient = function (callFunction) {
351
+ var rawQueryClient = {
352
+ $runSQL: function (sql, params, config) {
353
+ return __awaiter(this, void 0, void 0, function () {
354
+ var res;
355
+ return __generator(this, function (_a) {
356
+ switch (_a.label) {
357
+ case 0: return [4 /*yield*/, runMysqlCommand({
358
+ sql: sql,
359
+ params: params,
360
+ config: __assign(__assign({}, config), { preparedStatements: true }),
361
+ callFunction: callFunction
362
+ })];
363
+ case 1:
364
+ res = _a.sent();
365
+ return [2 /*return*/, res];
366
+ }
367
+ });
368
+ });
369
+ },
370
+ $runSQLRaw: function (sql, config) {
371
+ return __awaiter(this, void 0, void 0, function () {
372
+ var res;
373
+ return __generator(this, function (_a) {
374
+ switch (_a.label) {
375
+ case 0: return [4 /*yield*/, runMysqlCommand({
376
+ sql: sql,
377
+ params: [],
378
+ config: __assign(__assign({}, config), { preparedStatements: false }),
379
+ callFunction: callFunction
380
+ })];
381
+ case 1:
382
+ res = _a.sent();
383
+ return [2 /*return*/, res];
384
+ }
385
+ });
386
+ });
387
+ }
388
+ };
231
389
  return new Proxy({}, {
232
390
  get: function (target, prop) {
233
391
  if (typeof prop === 'string') {
392
+ if (rawQueryClient.hasOwnProperty(prop)) {
393
+ return rawQueryClient[prop];
394
+ }
234
395
  // 返回一个函数,这个函数接受任意参数并返回一个 Promise
235
396
  return generateClientByDataSourceName(prop, callFunction);
236
397
  }
@@ -97,10 +97,62 @@
97
97
  return WxCloudSDKError;
98
98
  }(Error));
99
99
 
100
+ /**
101
+ * 获取全局对象 window
102
+ * 小程序中可用, 但小程序中对象信息残缺, 无法访问 navigator 对象, ua 信息也无意义
103
+ */
104
+ function getGlobalObj() {
105
+ // @ts-ignore
106
+ return (typeof window !== 'undefined' && window) || (typeof globalThis !== 'undefined' && globalThis);
107
+ }
108
+ /** 获取referrer 信息, 担心小程序中报错, 故catch */
109
+ function getReferrer() {
110
+ try {
111
+ var globalObj = getGlobalObj();
112
+ if (!globalObj)
113
+ return;
114
+ // 浏览器中
115
+ // @ts-ignore
116
+ if (typeof wx === 'undefined') {
117
+ // @ts-ignore
118
+ return getGlobalObj().location.href;
119
+ }
120
+ // 当前页面路由
121
+ // @ts-ignore
122
+ return globalObj.__wxRoute;
123
+ }
124
+ catch (_a) { }
125
+ }
126
+ /** 获取用户UA, 小程序中使用 getSystemInfo 替代 */
127
+ function getUserAgent() {
128
+ var globalObj = getGlobalObj();
129
+ // @ts-ignore
130
+ if (globalObj === null || globalObj === void 0 ? void 0 : globalObj.navigator)
131
+ return globalObj.navigator.userAgent;
132
+ // 微信小程序
133
+ // @ts-ignore
134
+ if (typeof wx !== 'undefined' && wx.getSystemInfo) {
135
+ var ua_1;
136
+ // 同步接口
137
+ // @ts-ignore
138
+ wx.getSystemInfo({
139
+ success: function (res) {
140
+ if (!res)
141
+ return;
142
+ ua_1 = ['brand', 'model', 'version', 'system', 'platform', 'SDKVersion', 'language']
143
+ .map(function (k) { return "".concat(k, ": ").concat(res[k]); })
144
+ .join(', ');
145
+ }
146
+ });
147
+ return ua_1;
148
+ }
149
+ }
150
+ var VERSION = "1.1.0";
151
+
100
152
  var callDataSource = function (_a) {
101
- var dataSourceName = _a.dataSourceName, methodName = _a.methodName, params = _a.params, realMethodName = _a.realMethodName, callFunction = _a.callFunction;
153
+ var dataSourceName = _a.dataSourceName, methodName = _a.methodName, params = _a.params, realMethodName = _a.realMethodName, callFunction = _a.callFunction, mode = _a.mode;
102
154
  return __awaiter(void 0, void 0, void 0, function () {
103
- var result, response, error_1;
155
+ var result, response, requestId, error_1;
104
156
  var _b, _c;
105
157
  return __generator(this, function (_d) {
106
158
  switch (_d.label) {
@@ -117,20 +169,28 @@
117
169
  data: {
118
170
  dataSourceName: dataSourceName,
119
171
  methodName: methodName,
120
- params: params
172
+ params: params,
173
+ userAgent: getUserAgent(),
174
+ referrer: getReferrer(),
175
+ 'x-sdk-version': VERSION,
176
+ /**
177
+ * todo 移除此字段
178
+ */
179
+ mode: mode
121
180
  }
122
181
  })];
123
182
  case 2:
124
183
  response = _d.sent();
184
+ requestId = ((_b = response === null || response === void 0 ? void 0 : response.result) === null || _b === void 0 ? void 0 : _b.requestId) || (response === null || response === void 0 ? void 0 : response.requestId) || (response === null || response === void 0 ? void 0 : response.requestID);
125
185
  if (response === null || response === void 0 ? void 0 : response.result.code) {
126
- throw new WxCloudSDKError("\u3010\u9519\u8BEF\u3011".concat(response === null || response === void 0 ? void 0 : response.result.message, "\n\u3010\u64CD\u4F5C\u3011\u8C03\u7528 ").concat(dataSourceName, ".").concat(realMethodName, "\n\u3010\u8BF7\u6C42ID\u3011").concat((response === null || response === void 0 ? void 0 : response.requestId) || 'N/A'), {
186
+ throw new WxCloudSDKError("\u3010\u9519\u8BEF\u3011".concat(response === null || response === void 0 ? void 0 : response.result.message, "\n\u3010\u64CD\u4F5C\u3011\u8C03\u7528 models.").concat(dataSourceName ? "".concat(dataSourceName, ".") : "").concat(realMethodName, "\n\u3010\u9519\u8BEF\u7801\u3011").concat(response === null || response === void 0 ? void 0 : response.result.code, "\n\u3010\u8BF7\u6C42ID\u3011").concat(requestId || 'N/A'), {
127
187
  code: response === null || response === void 0 ? void 0 : response.result.code,
128
- requestId: response === null || response === void 0 ? void 0 : response.requestId
188
+ requestId: requestId
129
189
  });
130
190
  }
131
191
  else {
132
- result.data = ((_b = response === null || response === void 0 ? void 0 : response.result) === null || _b === void 0 ? void 0 : _b.data) || {};
133
- result.requestId = ((_c = response === null || response === void 0 ? void 0 : response.result) === null || _c === void 0 ? void 0 : _c.requestId) || '';
192
+ result.data = ((_c = response === null || response === void 0 ? void 0 : response.result) === null || _c === void 0 ? void 0 : _c.data) || {};
193
+ result.requestId = requestId;
134
194
  }
135
195
  return [3 /*break*/, 4];
136
196
  case 3:
@@ -140,7 +200,7 @@
140
200
  }
141
201
  else {
142
202
  console.log(error_1);
143
- throw new WxCloudSDKError("\u3010\u9519\u8BEF\u3011".concat(error_1.message, "\n \u3010\u64CD\u4F5C\u3011\u8C03\u7528 ").concat(dataSourceName, ".").concat(realMethodName, "\n \u3010\u8BF7\u6C42ID\u3011N/A"), {
203
+ throw new WxCloudSDKError("\u3010\u9519\u8BEF\u3011".concat(error_1.message, "\n \u3010\u64CD\u4F5C\u3011\u8C03\u7528 models.").concat(dataSourceName ? "".concat(dataSourceName, ".") : "").concat(realMethodName, "\n \u3010\u8BF7\u6C42ID\u3011N/A"), {
144
204
  code: 'UnknownError',
145
205
  originError: error_1
146
206
  });
@@ -150,6 +210,63 @@
150
210
  });
151
211
  });
152
212
  };
213
+ var runMysqlCommand = function (_a) {
214
+ var sql = _a.sql, params = _a.params, config = _a.config, callFunction = _a.callFunction, _b = _a.unsafe, unsafe = _b === void 0 ? false : _b;
215
+ return __awaiter(void 0, void 0, void 0, function () {
216
+ return __generator(this, function (_c) {
217
+ return [2 /*return*/, callDataSource({
218
+ realMethodName: '$runSQL',
219
+ methodName: 'callWedaApi',
220
+ params: {
221
+ action: 'RunMysqlCommand',
222
+ data: {
223
+ sqlTemplate: sql,
224
+ config: config,
225
+ parameter: unsafe
226
+ ? ''
227
+ : Object.entries(params || {}).reduce(function (list, _a) {
228
+ var key = _a[0], value = _a[1];
229
+ if (value !== undefined) {
230
+ var type = "OBJECT" /* EQUERY_PARAM_TYPE.OBJECT */;
231
+ var typeofValue = typeof value;
232
+ switch (typeofValue) {
233
+ case 'boolean': {
234
+ type = "BOOLEAN" /* EQUERY_PARAM_TYPE.BOOLEAN */;
235
+ break;
236
+ }
237
+ case 'number': {
238
+ type = "NUMBER" /* EQUERY_PARAM_TYPE.NUMBER */;
239
+ break;
240
+ }
241
+ case 'string': {
242
+ type = "STRING" /* EQUERY_PARAM_TYPE.STRING */;
243
+ break;
244
+ }
245
+ default: {
246
+ if (Array.isArray(value)) {
247
+ type = "ARRAY" /* EQUERY_PARAM_TYPE.ARRAY */;
248
+ }
249
+ else {
250
+ type = "OBJECT" /* EQUERY_PARAM_TYPE.OBJECT */;
251
+ }
252
+ }
253
+ }
254
+ list.push({
255
+ key: key,
256
+ type: type,
257
+ value: type === "STRING" /* EQUERY_PARAM_TYPE.STRING */ ? value : JSON.stringify(value)
258
+ });
259
+ }
260
+ return list;
261
+ }, []) || []
262
+ }
263
+ },
264
+ callFunction: callFunction,
265
+ mode: 'sdk'
266
+ })];
267
+ });
268
+ });
269
+ };
153
270
 
154
271
  var CRUD_METHODS = {
155
272
  create: {
@@ -161,6 +278,9 @@
161
278
  update: {
162
279
  methodName: 'wedaUpdateV2'
163
280
  },
281
+ upsert: {
282
+ methodName: 'wedaUpsertV2'
283
+ },
164
284
  updateMany: {
165
285
  methodName: 'wedaBatchUpdateV2'
166
286
  },
@@ -234,9 +354,50 @@
234
354
  };
235
355
  // 使用 TypeScript 的 Proxy 来定义一个动态的客户端
236
356
  var generateClient = function (callFunction) {
357
+ var rawQueryClient = {
358
+ $runSQL: function (sql, params, config) {
359
+ return __awaiter(this, void 0, void 0, function () {
360
+ var res;
361
+ return __generator(this, function (_a) {
362
+ switch (_a.label) {
363
+ case 0: return [4 /*yield*/, runMysqlCommand({
364
+ sql: sql,
365
+ params: params,
366
+ config: __assign(__assign({}, config), { preparedStatements: true }),
367
+ callFunction: callFunction
368
+ })];
369
+ case 1:
370
+ res = _a.sent();
371
+ return [2 /*return*/, res];
372
+ }
373
+ });
374
+ });
375
+ },
376
+ $runSQLRaw: function (sql, config) {
377
+ return __awaiter(this, void 0, void 0, function () {
378
+ var res;
379
+ return __generator(this, function (_a) {
380
+ switch (_a.label) {
381
+ case 0: return [4 /*yield*/, runMysqlCommand({
382
+ sql: sql,
383
+ params: [],
384
+ config: __assign(__assign({}, config), { preparedStatements: false }),
385
+ callFunction: callFunction
386
+ })];
387
+ case 1:
388
+ res = _a.sent();
389
+ return [2 /*return*/, res];
390
+ }
391
+ });
392
+ });
393
+ }
394
+ };
237
395
  return new Proxy({}, {
238
396
  get: function (target, prop) {
239
397
  if (typeof prop === 'string') {
398
+ if (rawQueryClient.hasOwnProperty(prop)) {
399
+ return rawQueryClient[prop];
400
+ }
240
401
  // 返回一个函数,这个函数接受任意参数并返回一个 Promise
241
402
  return generateClientByDataSourceName(prop, callFunction);
242
403
  }
package/package.json CHANGED
@@ -1,6 +1,9 @@
1
1
  {
2
2
  "name": "@cloudbase/wx-cloud-client-sdk",
3
- "version": "1.0.0-alpha.2",
3
+ "version": "1.1.0",
4
+ "publishConfig": {
5
+ "access": "public"
6
+ },
4
7
  "description": "wx cloud client sdk",
5
8
  "main": "lib/wxCloudClientSDK.cjs.js",
6
9
  "module": "lib/wxCloudClientSDK.esm.js",
@@ -17,12 +20,15 @@
17
20
  "build-demo": "npx rimraf dist/* && parcel build demo/index.html --public-url ./",
18
21
  "publish": "npm publish --access public",
19
22
  "publish-demo": "tcb hosting deploy dist wx-cloud-client-sdk-demo -e lowcode-4gs26nnz095f6f4d",
20
- "docs": "npx rimraf docs/* && typedoc --options typedoc.json"
23
+ "docs": "typedoc --options typedoc.json",
24
+ "deploy": "node ./scripts/deploy.mjs"
21
25
  },
22
26
  "devDependencies": {
23
27
  "@rollup/plugin-commonjs": "^25.0.8",
24
28
  "@rollup/plugin-node-resolve": "^15.2.3",
29
+ "@rollup/plugin-replace": "^5.0.7",
25
30
  "@rollup/plugin-typescript": "^11.1.6",
31
+ "@tcwd/dev-tools": "^1.0.1",
26
32
  "parcel-bundler": "1.6.1",
27
33
  "rollup": "^4.18.0",
28
34
  "typedoc": "^0.25.13",
package/lib/demo/app.d.ts DELETED
@@ -1 +0,0 @@
1
- export {};
@@ -1,2 +0,0 @@
1
- import { MethodResponse, CallDataSourceParams } from '../types';
2
- export declare const callDataSource: ({ dataSourceName, methodName, params, realMethodName, callFunction, }: CallDataSourceParams) => Promise<MethodResponse<any>>;
File without changes
File without changes
File without changes