apaas-oapi-client 0.1.18 → 0.1.20

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/UserManual.md CHANGED
@@ -191,16 +191,30 @@ const res = await client.object.create.record({
191
191
  console.log(res);
192
192
  ```
193
193
 
194
- ### **批量创建**
194
+ ### **批量创建(最多 100 条)**
195
195
 
196
- > ⚠️ 每次最多创建 100 条,SDK 已自动分组限流
196
+ ```JavaScript
197
+ const res = await client.object.create.records({
198
+ object_name: 'object_event_log',
199
+ records: [
200
+ { name: 'Sample text 1', content: 'Sample text 1' },
201
+ { name: 'Sample text 2', content: 'Sample text 2' }
202
+ ]
203
+ });
204
+ console.log(res);
205
+ ```
206
+
207
+ ### **批量创建(支持超过 100 条,自动拆分)**
208
+
209
+ > ⚠️ 超过 100 条会自动拆分为多次请求,SDK 已自动分组限流
197
210
 
198
211
  ```JavaScript
199
212
  const { total, items } = await client.object.create.recordsWithIterator({
200
213
  object_name: 'object_event_log',
201
214
  records: [
202
215
  { name: 'Sample text 1', content: 'Sample text 1' },
203
- { name: 'Sample text 2', content: 'Sample text 2' }
216
+ { name: 'Sample text 2', content: 'Sample text 2' },
217
+ // ... 可以超过 100 条
204
218
  ]
205
219
  });
206
220
  console.log('Total:', total);
@@ -224,12 +238,10 @@ console.log(res);
224
238
 
225
239
  ***
226
240
 
227
- ### **批量更新**
228
-
229
- > ⚠️ 每次最多更新 100 条,SDK 已自动分组限流
241
+ ### **批量更新(最多 100 条)**
230
242
 
231
243
  ```JavaScript
232
- const res = await client.object.update.recordsBatchUpdate({
244
+ const res = await client.object.update.records({
233
245
  object_name: 'object_store',
234
246
  records: [
235
247
  { _id: 'id1', field1: 'value1' },
@@ -239,6 +251,22 @@ const res = await client.object.update.recordsBatchUpdate({
239
251
  console.log(res);
240
252
  ```
241
253
 
254
+ ### **批量更新(支持超过 100 条,自动拆分)**
255
+
256
+ > ⚠️ 超过 100 条会自动拆分为多次请求,SDK 已自动分组限流
257
+
258
+ ```JavaScript
259
+ const results = await client.object.update.recordsWithIterator({
260
+ object_name: 'object_store',
261
+ records: [
262
+ { _id: 'id1', field1: 'value1' },
263
+ { _id: 'id2', field1: 'value2' },
264
+ // ... 可以超过 100 条
265
+ ]
266
+ });
267
+ console.log(results); // 返回所有子请求的结果数组
268
+ ```
269
+
242
270
  ***
243
271
 
244
272
 
@@ -257,18 +285,28 @@ console.log(res);
257
285
 
258
286
  ***
259
287
 
260
- ### **批量删除**
261
-
262
- > ⚠️ 每次最多删除 100 条,SDK 已自动分组限流
288
+ ### **批量删除(最多 100 条)**
263
289
 
264
290
  ```JavaScript
265
- const res = await client.object.delete.recordsBatchDelete({
291
+ const res = await client.object.delete.records({
266
292
  object_name: 'object_store',
267
293
  ids: ['id1', 'id2', 'id3']
268
294
  });
269
295
  console.log(res);
270
296
  ```
271
297
 
298
+ ### **批量删除(支持超过 100 条,自动拆分)**
299
+
300
+ > ⚠️ 超过 100 条会自动拆分为多次请求,SDK 已自动分组限流
301
+
302
+ ```JavaScript
303
+ const results = await client.object.delete.recordsWithIterator({
304
+ object_name: 'object_store',
305
+ ids: ['id1', 'id2', 'id3', /* ... 可以超过 100 条 */]
306
+ });
307
+ console.log(results); // 返回所有子请求的结果数组
308
+ ```
309
+
272
310
  ***
273
311
 
274
312
 
@@ -456,7 +494,7 @@ console.log('Items:', items);
456
494
 
457
495
  ```JavaScript
458
496
  const res = await client.page.detail({
459
- page_id: 'appPage_aadd64dlqamdo'
497
+ page_id: 'appPage_page'
460
498
  });
461
499
  console.log(res);
462
500
  ```
@@ -465,14 +503,14 @@ console.log(res);
465
503
 
466
504
  ```JavaScript
467
505
  const res = await client.page.url({
468
- page_id: 'appPage_aadd64dlqamdo',
469
- pageParams: { var_EzpvJsER: '1234567890' },
506
+ page_id: 'appPage_page',
507
+ pageParams: { var_page: '1234567890' },
470
508
  parentPageParams: {
471
- navId: 'aadd64bapmic2',
472
- pageApiName: 'page_aaddmtkqgicbg'
509
+ navId: 'page_nav_id',
510
+ pageApiName: 'page_name'
473
511
  },
474
- navId: 'aadd64bapmic2',
475
- tabId: 'tab_legqch3h'
512
+ navId: 'page_nav_id',
513
+ tabId: 'tab_id'
476
514
  });
477
515
  console.log(res);
478
516
  // 返回: { code: "0", msg: "success", data: { link: "https://..." } }
package/dist/index.js CHANGED
@@ -307,13 +307,17 @@ class Client {
307
307
  */
308
308
  records: async (params) => {
309
309
  const { object_name, records } = params;
310
- const url = `/v1/data/namespaces/${this.namespace}/objects/${object_name}/records/records_batch`;
310
+ const url = `/v1/data/namespaces/${this.namespace}/objects/${object_name}/records_batch`;
311
311
  this.log(LoggerLevel.info, `[object.update.records] Updating ${records.length} records`);
312
- const response = await this.axiosInstance.patch(url, { records }, { headers: { Authorization: `${this.accessToken}` } });
313
- this.log(LoggerLevel.info, `[object.update.records] Records updated: ${object_name}`);
314
- this.log(LoggerLevel.debug, `[object.update.records] Records updated: ${object_name}, code=${response.data.code}`);
315
- this.log(LoggerLevel.trace, `[object.update.records] Response: ${JSON.stringify(response.data)}`);
316
- return response.data;
312
+ const response = await functionLimiter(async () => {
313
+ await this.ensureTokenValid();
314
+ const res = await this.axiosInstance.patch(url, { records }, { headers: { Authorization: `${this.accessToken}` } });
315
+ this.log(LoggerLevel.info, `[object.update.records] Records updated: ${object_name}`);
316
+ this.log(LoggerLevel.debug, `[object.update.records] Records updated: ${object_name}, code=${res.data.code}`);
317
+ this.log(LoggerLevel.trace, `[object.update.records] Response: ${JSON.stringify(res.data)}`);
318
+ return res.data;
319
+ });
320
+ return response;
317
321
  },
318
322
  /**
319
323
  * 批量更新 - 支持超过 100 条数据,自动拆分
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "apaas-oapi-client",
3
- "version": "0.1.18",
3
+ "version": "0.1.20",
4
4
  "main": "dist/index.js",
5
5
  "exports": {
6
6
  ".": "./dist/index.js",
package/src/index.ts CHANGED
@@ -494,17 +494,23 @@ class Client {
494
494
  */
495
495
  records: async (params: { object_name: string; records: any[] }): Promise<any> => {
496
496
  const { object_name, records } = params;
497
- const url = `/v1/data/namespaces/${this.namespace}/objects/${object_name}/records/records_batch`;
497
+ const url = `/v1/data/namespaces/${this.namespace}/objects/${object_name}/records_batch`;
498
498
 
499
499
  this.log(LoggerLevel.info, `[object.update.records] Updating ${records.length} records`);
500
500
 
501
- const response = await this.axiosInstance.patch(url, { records }, { headers: { Authorization: `${this.accessToken}` } });
501
+ const response = await functionLimiter(async () => {
502
+ await this.ensureTokenValid();
503
+
504
+ const res = await this.axiosInstance.patch(url, { records }, { headers: { Authorization: `${this.accessToken}` } });
502
505
 
503
- this.log(LoggerLevel.info, `[object.update.records] Records updated: ${object_name}`);
504
- this.log(LoggerLevel.debug, `[object.update.records] Records updated: ${object_name}, code=${response.data.code}`);
505
- this.log(LoggerLevel.trace, `[object.update.records] Response: ${JSON.stringify(response.data)}`);
506
+ this.log(LoggerLevel.info, `[object.update.records] Records updated: ${object_name}`);
507
+ this.log(LoggerLevel.debug, `[object.update.records] Records updated: ${object_name}, code=${res.data.code}`);
508
+ this.log(LoggerLevel.trace, `[object.update.records] Response: ${JSON.stringify(res.data)}`);
509
+
510
+ return res.data;
511
+ });
506
512
 
507
- return response.data;
513
+ return response;
508
514
  },
509
515
 
510
516
  /**
@@ -1,568 +0,0 @@
1
- # 背景
2
-
3
- aPaaS 平台有完整的 Open API 能力,但是目前这些能力全都以单独接口的形式提供给开发者,不方便开发者调试和调用。
4
- 在此背景下,我们在一店一群项目的基础上,封装 aPaaS 平台 RESTful API 的 Node.js SDK,简化接口调用,内置限流与 token 缓存功能。
5
-
6
- ## ✨ **功能特性**
7
-
8
- - ✅ 获取 accessToken,自动管理 token 有效期
9
-
10
- - ✅ record 单条查询、批量查询(支持分页迭代)
11
-
12
- - ✅ record 单条创建、批量创建(支持分页迭代)
13
-
14
- - ✅ record 单条更新、批量更新
15
-
16
- - ✅ record 单条删除、批量删除
17
-
18
- - ✅ 内置 Bottleneck 限流器,基于 API 接口配置限流规则
19
-
20
- - ✅ 自定义日志等级
21
-
22
- - ……
23
-
24
-
25
- <br>
26
-
27
- <br>
28
-
29
- **📦 安装**
30
-
31
- ```Bash
32
- npm install apaas-oapi-client
33
- # or
34
- yarn add apaas-oapi-client
35
- ```
36
-
37
- ***
38
-
39
- <br>
40
-
41
- # **🚀 快速开始**
42
-
43
- ```JavaScript
44
- const { apaas } = require('apaas-oapi-client');
45
-
46
- async function main() {
47
- const client = new apaas.Client({
48
- clientId: 'your_client_id',
49
- clientSecret: 'your_client_secret',
50
- namespace: 'app_xxx'
51
- });
52
-
53
- await client.init();
54
- client.setLoggerLevel(3); // 设置日志等级 (0-5)
55
-
56
- console.log('Access Token:', client.token);
57
- console.log('Namespace:', client.currentNamespace);
58
- }
59
-
60
- main();
61
- ```
62
-
63
- ***
64
-
65
- <br>
66
-
67
- ## **🔐 认证**
68
-
69
- ### **初始化 Client**
70
-
71
- | **参数** | **类型** | **说明** |
72
- | :-- | :-- | :-- |
73
- | clientId | string | 应用 clientId |
74
- | clientSecret | string | 应用 clientSecret |
75
- | namespace | string | 命名空间 |
76
- | disableTokenCache | boolean | 是否禁用 token 缓存,默认 false |
77
-
78
- ***
79
-
80
- <br>
81
-
82
- ## **📝 日志等级**
83
-
84
- 可调用 setLoggerLevel(level) 设置日志等级。
85
-
86
- | **Level** | **名称** | **说明** |
87
- | :-- | :-- | :-- |
88
- | 0 | fatal | 严重错误 |
89
- | 1 | error | 错误 |
90
- | 2 | warn | 警告 |
91
- | 3 | info | 信息(默认) |
92
- | 4 | debug | 调试信息 |
93
- | 5 | trace | 追踪 |
94
-
95
- ***
96
-
97
- <br>
98
-
99
- # 💾 **数据模块**
100
-
101
- ## **📋 对象列表接口**
102
-
103
- ### **获取所有对象(数据表)**
104
-
105
- ```JavaScript
106
- const res = await client.object.list({
107
- offset: 0,
108
- limit: 100,
109
- filter: {
110
- type: 'custom',
111
- quickQuery: 'store'
112
- }
113
- });
114
- console.log(res);
115
- ```
116
-
117
- ***
118
-
119
- ## **🔍 查询接口**
120
-
121
- 查询条件请根据实际需求自行拼装。详情参考 API 接口文档示例。
122
-
123
- ### **单条查询**
124
-
125
- ```JavaScript
126
- const res = await client.object.search.record({
127
- object_name: 'object_store',
128
- record_id: 'your_record_id',
129
- select: ['field1', 'field2']
130
- });
131
- console.log(res);
132
- ```
133
-
134
- ***
135
-
136
- ### **批量查询**
137
-
138
- 每次查询最多返回 100 条记录。
139
-
140
- ```JavaScript
141
- const res = await client.object.search.records({
142
- object_name: 'object_store',
143
- data: {
144
- need_total_count: true,
145
- page_size: 100,
146
- offset: 0
147
- }
148
- });
149
- console.log(res);
150
- ```
151
-
152
- ***
153
-
154
- ### **分页查询所有记录**
155
-
156
- 在上一个请求的基础上,封装每次查询最多返回 100 条记录。
157
-
158
- ```JavaScript
159
- const { total, items } = await client.object.search.recordsWithIterator({
160
- object_name: 'object_store',
161
- data: {
162
- need_total_count: true,
163
- page_size: 100,
164
- offset: 0
165
- }
166
- });
167
-
168
- console.log('Total:', total);
169
- console.log('Items:', items);
170
- ```
171
-
172
- ***
173
-
174
- <br>
175
-
176
-
177
- ***
178
-
179
- ## **➕ 创建接口**
180
-
181
- ### **单条创建**
182
-
183
- ```JavaScript
184
- const res = await client.object.create.record({
185
- object_name: 'object_event_log',
186
- record: {
187
- name: 'Sample text',
188
- content: 'Sample text'
189
- }
190
- });
191
- console.log(res);
192
- ```
193
-
194
- ### **批量创建**
195
-
196
- > ⚠️ 每次最多创建 100 条,SDK 已自动分组限流
197
-
198
- ```JavaScript
199
- const { total, items } = await client.object.create.recordsWithIterator({
200
- object_name: 'object_event_log',
201
- records: [
202
- { name: 'Sample text 1', content: 'Sample text 1' },
203
- { name: 'Sample text 2', content: 'Sample text 2' }
204
- ]
205
- });
206
- console.log('Total:', total);
207
- console.log('Items:', items);
208
- ```
209
-
210
-
211
- <br>
212
- ## **✏️ 更新接口**
213
-
214
- ### **单条更新**
215
-
216
- ```JavaScript
217
- const res = await client.object.update.record({
218
- object_name: 'object_store',
219
- record_id: 'your_record_id',
220
- record: { field1: 'newValue' }
221
- });
222
- console.log(res);
223
- ```
224
-
225
- ***
226
-
227
- ### **批量更新**
228
-
229
- > ⚠️ 每次最多更新 100 条,SDK 已自动分组限流
230
-
231
- ```JavaScript
232
- const res = await client.object.update.recordsBatchUpdate({
233
- object_name: 'object_store',
234
- records: [
235
- { _id: 'id1', field1: 'value1' },
236
- { _id: 'id2', field1: 'value2' }
237
- ]
238
- });
239
- console.log(res);
240
- ```
241
-
242
- ***
243
-
244
- <br>
245
-
246
- ## **🗑️ 删除接口**
247
-
248
- ### **单条删除**
249
-
250
- ```JavaScript
251
- const res = await client.object.delete.record({
252
- object_name: 'object_store',
253
- record_id: 'your_record_id'
254
- });
255
- console.log(res);
256
- ```
257
-
258
- ***
259
-
260
- ### **批量删除**
261
-
262
- > ⚠️ 每次最多删除 100 条,SDK 已自动分组限流
263
-
264
- ```JavaScript
265
- const res = await client.object.delete.recordsBatchDelete({
266
- object_name: 'object_store',
267
- ids: ['id1', 'id2', 'id3']
268
- });
269
- console.log(res);
270
- ```
271
-
272
- ***
273
-
274
- <br>
275
-
276
- ## **📊 对象元数据接口**
277
-
278
- ### **获取指定对象字段元数据**
279
-
280
- ```JavaScript
281
- const res = await client.object.metadata.field({
282
- object_name: '_user',
283
- field_name: '_id'
284
- });
285
- console.log(res);
286
- ```
287
-
288
- ### **获取指定对象所有字段信息**
289
-
290
- ```JavaScript
291
- const res = await client.object.metadata.fields({
292
- object_name: 'object_store'
293
- });
294
- console.log(res);
295
- ```
296
-
297
- ***
298
-
299
- <br>
300
-
301
- # **📎 附件模块**
302
-
303
- ## **文件操作**
304
-
305
- ### **上传文件**
306
-
307
- ```JavaScript
308
- const fs = require('fs');
309
-
310
- const res = await client.attachment.file.upload({
311
- file: fs.createReadStream('/path/to/file.zip')
312
- });
313
- console.log(res);
314
- // 返回: { code: "0", msg: "success", data: { fileId, type, name, size } }
315
- ```
316
-
317
- ### **下载文件**
318
-
319
- ```JavaScript
320
- const fileData = await client.attachment.file.download({
321
- file_id: '625d2f602af94d46972073db32a99ed2'
322
- });
323
- // 返回文件二进制流
324
- ```
325
-
326
- ### **删除文件**
327
-
328
- ```JavaScript
329
- const res = await client.attachment.file.delete({
330
- file_id: '625d2f602af94d46972073db32a99ed2'
331
- });
332
- console.log(res);
333
- ```
334
-
335
- ## **头像图片操作**
336
-
337
- ### **上传头像图片**
338
-
339
- ```JavaScript
340
- const fs = require('fs');
341
-
342
- const res = await client.attachment.avatar.upload({
343
- image: fs.createReadStream('/path/to/avatar.jpg')
344
- });
345
- console.log(res);
346
- ```
347
-
348
- ### **下载头像图片**
349
-
350
- ```JavaScript
351
- const imageData = await client.attachment.avatar.download({
352
- image_id: 'c70d03b21d3c40468ee710d984cfb7a8_o'
353
- });
354
- // 返回图片二进制流
355
- ```
356
-
357
- ***
358
-
359
- <br>
360
-
361
- # **💽 全局数据模块**
362
-
363
- ## **全局选项**
364
-
365
- ### **查询全局选项详情**
366
-
367
- ```JavaScript
368
- const res = await client.global.options.detail({
369
- api_name: 'global_option_abc'
370
- });
371
- console.log(res);
372
- ```
373
-
374
- ### **查询全局选项列表**
375
-
376
- ```JavaScript
377
- const res = await client.global.options.list({
378
- limit: 10,
379
- offset: 0,
380
- filter: { quickQuery: 'Sample Text' }
381
- });
382
- console.log(res);
383
- ```
384
-
385
- ### **分页查询所有全局选项**
386
-
387
- ```JavaScript
388
- const { total, items } = await client.global.options.listWithIterator({
389
- limit: 100,
390
- filter: { quickQuery: 'Sample Text' }
391
- });
392
- console.log('Total:', total);
393
- console.log('Items:', items);
394
- ```
395
-
396
- ## **环境变量**
397
-
398
- ### **查询环境变量详情**
399
-
400
- ```JavaScript
401
- const res = await client.global.variables.detail({
402
- api_name: 'global_variable_abc'
403
- });
404
- console.log(res);
405
- ```
406
-
407
- ### **查询环境变量列表**
408
-
409
- ```JavaScript
410
- const res = await client.global.variables.list({
411
- limit: 10,
412
- offset: 0,
413
- filter: { quickQuery: 'Sample Text' }
414
- });
415
- console.log(res);
416
- ```
417
-
418
- ### **分页查询所有环境变量**
419
-
420
- ```JavaScript
421
- const { total, items } = await client.global.variables.listWithIterator({
422
- limit: 100,
423
- filter: { quickQuery: 'Sample Text' }
424
- });
425
- console.log('Total:', total);
426
- console.log('Items:', items);
427
- ```
428
-
429
- ***
430
-
431
- <br>
432
-
433
- # **📄 页面模块**
434
-
435
- ### **获取所有页面**
436
-
437
- ```JavaScript
438
- const res = await client.page.list({
439
- limit: 10,
440
- offset: 0
441
- });
442
- console.log(res);
443
- ```
444
-
445
- ### **分页查询所有页面**
446
-
447
- ```JavaScript
448
- const { total, items } = await client.page.listWithIterator({
449
- limit: 100
450
- });
451
- console.log('Total:', total);
452
- console.log('Items:', items);
453
- ```
454
-
455
- ### **获取页面详情**
456
-
457
- ```JavaScript
458
- const res = await client.page.detail({
459
- page_id: 'appPage_aadd64dlqamdo'
460
- });
461
- console.log(res);
462
- ```
463
-
464
- ### **获取页面访问地址**
465
-
466
- ```JavaScript
467
- const res = await client.page.url({
468
- page_id: 'appPage_aadd64dlqamdo',
469
- pageParams: { var_EzpvJsER: '1234567890' },
470
- parentPageParams: {
471
- navId: 'aadd64bapmic2',
472
- pageApiName: 'page_aaddmtkqgicbg'
473
- },
474
- navId: 'aadd64bapmic2',
475
- tabId: 'tab_legqch3h'
476
- });
477
- console.log(res);
478
- // 返回: { code: "0", msg: "success", data: { link: "https://..." } }
479
- ```
480
-
481
- ***
482
-
483
- <br>
484
-
485
- # **🏢 部门模块**
486
-
487
- ## **部门 ID 交换**
488
-
489
- ### **单个部门 ID 交换**
490
-
491
- ```JavaScript
492
- const res = await client.department.exchange({
493
- department_id_type: 'external_department_id',
494
- department_id: 'Y806608904'
495
- });
496
- console.log(res);
497
- ```
498
-
499
- ### **批量部门 ID 交换**
500
-
501
- 每次最多 100 个,SDK 已自动拆分限流。
502
-
503
- ```JavaScript
504
- const res = await client.department.batchExchange({
505
- department_id_type: 'external_department_id',
506
- department_ids: ['id1', 'id2', 'id3']
507
- });
508
- console.log(res);
509
- ```
510
-
511
- ***
512
-
513
- <br>
514
-
515
- # **☁️ 云函数模块**
516
-
517
- ```JavaScript
518
- const res = await client.function.invoke({
519
- name: 'StoreMemberUpdate',
520
- params: { key: 'value' }
521
- });
522
- console.log(res);
523
- ```
524
-
525
- ***
526
-
527
- <br>
528
-
529
- ## **🛠️ 高级**
530
-
531
- ### **获取当前 token**
532
-
533
- ```JavaScript
534
- console.log(client.token);
535
- ```
536
-
537
- ### **获取 token 过期时间**
538
-
539
- ```JavaScript
540
- console.log(client.tokenExpireTime); // 返回剩余秒数
541
- ```
542
-
543
- ### **获取当前 namespace**
544
-
545
- ```JavaScript
546
- console.log(client.currentNamespace);
547
- ```
548
-
549
- ***
550
-
551
- <br>
552
-
553
- ## **💡 备注**
554
-
555
- - 本 SDK 默认使用 [axios](https://www.npmjs.com/package/axios) 请求。
556
-
557
- - 内置 [bottleneck](https://www.npmjs.com/package/bottleneck) 进行请求限流。
558
-
559
- - 日志打印默认使用 console.log 并带时间戳,可通过 setLoggerLevel 动态控制输出等级。
560
-
561
-
562
- ***
563
-
564
- <br>
565
-
566
- > 由 [aPaaS OAPI Client SDK](https://www.npmjs.com/package/apaas-oapi-client) 提供支持,如有问题请提交 Issue 反馈。
567
-
568
- ---