apaas-oapi-client 0.1.35 → 0.1.37

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/src/index.ts CHANGED
@@ -2,6 +2,33 @@ import dayjs from 'dayjs';
2
2
  import axios, { AxiosInstance, AxiosError } from 'axios';
3
3
  import { LoggerLevel } from './logger';
4
4
  import { functionLimiter } from './limiter';
5
+ import {
6
+ FIELD_TYPES,
7
+ SYSTEM_FIELDS,
8
+ LANGUAGE_CODES,
9
+ SCHEMA_GUIDELINES,
10
+ CREATE_OBJECT_EXAMPLE,
11
+ UPDATE_OBJECT_ADD_FIELD_EXAMPLE,
12
+ UPDATE_OBJECT_REPLACE_FIELD_EXAMPLE,
13
+ UPDATE_OBJECT_REMOVE_FIELD_EXAMPLE,
14
+ UPDATE_OBJECT_SETTINGS_EXAMPLE,
15
+ getSystemFields,
16
+ getCustomFieldTypes,
17
+ getFieldType,
18
+ isSystemField,
19
+ createMultilingualText,
20
+ extractMultilingualText,
21
+ type FieldTypeMetadata,
22
+ type MultilingualText,
23
+ type ObjectSettings,
24
+ type FieldTypeDefinition,
25
+ type EncryptType,
26
+ type FieldOperator,
27
+ type CreateFieldDefinition,
28
+ type UpdateFieldDefinition,
29
+ type CreateObjectDefinition,
30
+ type UpdateObjectDefinition
31
+ } from './field-types';
5
32
 
6
33
  /**
7
34
  * 批量操作结果
@@ -593,7 +620,7 @@ class Client {
593
620
  if (obj.fields && obj.fields.length > 0) {
594
621
  // 对字段进行分类和排序
595
622
  const systemFieldOrder = ['_name', '_createdBy', '_createdAt', '_updatedBy', '_updatedAt'];
596
- const specialFieldTypes = ['formula', 'referenceField'];
623
+ const specialFieldTypes = ['formula', 'referenceField', 'rollup'];
597
624
 
598
625
  let idField: any = null;
599
626
  const normalFields: any[] = [];
@@ -678,6 +705,41 @@ class Client {
678
705
  }
679
706
  }
680
707
 
708
+ // rollup 类型:汇总字段
709
+ if (field.type?.name === 'rollup') {
710
+ otherSettings.push(`⚙️ 系统自动维护,不需要写/更新`);
711
+
712
+ // 汇总函数类型映射
713
+ const functionTypeMap: { [key: string]: string } = {
714
+ 'count': '计数',
715
+ 'sum': '求和',
716
+ 'avg': '平均值',
717
+ 'max': '最大值',
718
+ 'min': '最小值',
719
+ 'countDistinct': '去重计数'
720
+ };
721
+ const functionType = functionTypeMap[settings.functionType] || settings.functionType;
722
+ if (functionType) {
723
+ otherSettings.push(`📊 汇总类型: ${functionType}`);
724
+ }
725
+
726
+ if (settings.objectAPIName) {
727
+ otherSettings.push(`📦 汇总对象: \`${settings.objectAPIName}\``);
728
+ }
729
+
730
+ if (settings.lookupFieldAPIName) {
731
+ otherSettings.push(`🔗 关联字段: \`${settings.lookupFieldAPIName}\``);
732
+ }
733
+
734
+ if (settings.fieldAPIName) {
735
+ otherSettings.push(`📋 汇总字段: \`${settings.fieldAPIName}\``);
736
+ }
737
+
738
+ if (settings.rangeFilter && settings.rangeFilter.conditions && settings.rangeFilter.conditions.length > 0) {
739
+ otherSettings.push(`🔍 有过滤条件 (${settings.rangeFilter.conditions.length}个)`);
740
+ }
741
+ }
742
+
681
743
  // 根据不同类型展示不同的设置
682
744
  if (settings.maxLength) {
683
745
  otherSettings.push(`最大长度:${settings.maxLength}`);
@@ -2375,10 +2437,258 @@ class Client {
2375
2437
  }
2376
2438
  }
2377
2439
  };
2440
+
2441
+ /**
2442
+ * 数据对象结构管理模块(Schema)
2443
+ */
2444
+ public schema = {
2445
+ /**
2446
+ * 批量创建数据对象(表)
2447
+ * @description 批量创建一个或多个数据对象
2448
+ * @param params 请求参数 { objects: Array }
2449
+ * @returns 接口返回结果
2450
+ */
2451
+ create: async (params: {
2452
+ objects: Array<{
2453
+ api_name: string;
2454
+ label: {
2455
+ zh_cn: string;
2456
+ en_us: string;
2457
+ };
2458
+ settings?: {
2459
+ allow_search_fields?: string[];
2460
+ display_name?: string;
2461
+ search_layout?: string[];
2462
+ };
2463
+ fields: Array<{
2464
+ api_name: string;
2465
+ label: {
2466
+ zh_cn: string;
2467
+ en_us: string;
2468
+ };
2469
+ type: {
2470
+ name: string;
2471
+ settings?: any;
2472
+ };
2473
+ encrypt_type?: string | null;
2474
+ }>;
2475
+ }>;
2476
+ }): Promise<any> => {
2477
+ const { objects } = params;
2478
+ await this.ensureTokenValid();
2479
+
2480
+ const url = `/v1/namespaces/${this.namespace}/objects/batch_create`;
2481
+
2482
+ this.log(LoggerLevel.info, `[schema.create] Creating ${objects.length} object(s)`);
2483
+ this.log(LoggerLevel.trace, `[schema.create] Request URL: ${this.axiosInstance.defaults.baseURL}${url}`);
2484
+ this.log(LoggerLevel.trace, `[schema.create] Request Body: ${JSON.stringify({ objects }, null, 2)}`);
2485
+
2486
+ const res = await this.axiosInstance.post(
2487
+ url,
2488
+ { objects },
2489
+ {
2490
+ headers: {
2491
+ Authorization: `${this.accessToken}`,
2492
+ 'Content-Type': 'application/json'
2493
+ }
2494
+ }
2495
+ );
2496
+
2497
+ this.log(LoggerLevel.debug, `[schema.create] Objects created: code=${res.data.code}`);
2498
+ this.log(LoggerLevel.trace, `[schema.create] Response: ${JSON.stringify(res.data)}`);
2499
+
2500
+ return res.data;
2501
+ },
2502
+
2503
+ /**
2504
+ * 批量更新数据对象(表)
2505
+ * @description 批量更新一个或多个数据对象的配置
2506
+ * @param params 请求参数 { objects: Array }
2507
+ * @returns 接口返回结果
2508
+ * @example
2509
+ * ```typescript
2510
+ * // 添加新字段
2511
+ * await client.schema.update({
2512
+ * objects: [{
2513
+ * api_name: 'my_object',
2514
+ * fields: [{
2515
+ * operator: 'add',
2516
+ * api_name: 'new_field',
2517
+ * label: { zh_cn: '新字段', en_us: 'New Field' },
2518
+ * type: { name: 'text', settings: { required: false } },
2519
+ * encrypt_type: 'none'
2520
+ * }]
2521
+ * }]
2522
+ * });
2523
+ *
2524
+ * // 修改现有字段
2525
+ * await client.schema.update({
2526
+ * objects: [{
2527
+ * api_name: 'my_object',
2528
+ * fields: [{
2529
+ * operator: 'replace',
2530
+ * api_name: 'existing_field',
2531
+ * label: { zh_cn: '新标签', en_us: 'New Label' }
2532
+ * }]
2533
+ * }]
2534
+ * });
2535
+ *
2536
+ * // 删除字段
2537
+ * await client.schema.update({
2538
+ * objects: [{
2539
+ * api_name: 'my_object',
2540
+ * fields: [{
2541
+ * operator: 'remove',
2542
+ * api_name: 'field_to_delete'
2543
+ * }]
2544
+ * }]
2545
+ * });
2546
+ * ```
2547
+ */
2548
+ update: async (params: {
2549
+ objects: Array<{
2550
+ api_name: string;
2551
+ label?: {
2552
+ zh_cn: string;
2553
+ en_us: string;
2554
+ };
2555
+ settings?: {
2556
+ allow_search_fields?: string[];
2557
+ display_name?: string;
2558
+ search_layout?: string[];
2559
+ };
2560
+ fields?: Array<{
2561
+ /** 操作类型:add=添加字段, replace=修改字段, remove=删除字段 */
2562
+ operator: 'add' | 'replace' | 'remove';
2563
+ /** 字段 API 名称 */
2564
+ api_name: string;
2565
+ /** 字段标签(可选,operator=add 和 replace 时使用) */
2566
+ label?: {
2567
+ zh_cn: string;
2568
+ en_us: string;
2569
+ };
2570
+ /** 字段类型(可选,operator=add 和 replace 时使用) */
2571
+ type?: {
2572
+ name: string;
2573
+ settings?: any;
2574
+ };
2575
+ /** 加密类型(可选) */
2576
+ encrypt_type?: string | null;
2577
+ }>;
2578
+ }>;
2579
+ }): Promise<any> => {
2580
+ const { objects } = params;
2581
+ await this.ensureTokenValid();
2582
+
2583
+ const url = `/v1/namespaces/${this.namespace}/objects/batch_update`;
2584
+
2585
+ this.log(LoggerLevel.info, `[schema.update] Updating ${objects.length} object(s)`);
2586
+ this.log(LoggerLevel.trace, `[schema.update] Request URL: ${this.axiosInstance.defaults.baseURL}${url}`);
2587
+ this.log(LoggerLevel.trace, `[schema.update] Request Body: ${JSON.stringify({ objects }, null, 2)}`);
2588
+
2589
+ const res = await this.axiosInstance.post(
2590
+ url,
2591
+ { objects },
2592
+ {
2593
+ headers: {
2594
+ Authorization: `${this.accessToken}`,
2595
+ 'Content-Type': 'application/json'
2596
+ }
2597
+ }
2598
+ );
2599
+
2600
+ this.log(LoggerLevel.debug, `[schema.update] Objects updated: code=${res.data.code}`);
2601
+ this.log(LoggerLevel.trace, `[schema.update] Response: ${JSON.stringify(res.data)}`);
2602
+
2603
+ return res.data;
2604
+ },
2605
+
2606
+ /**
2607
+ * 批量删除数据对象(表)
2608
+ * @description 批量删除一个或多个数据对象
2609
+ * @param params 请求参数 { api_names: string[] }
2610
+ * @returns 接口返回结果
2611
+ * @example
2612
+ * ```typescript
2613
+ * // 删除单个对象
2614
+ * await client.schema.delete({ api_names: ['object_abc'] });
2615
+ *
2616
+ * // 删除多个对象
2617
+ * await client.schema.delete({ api_names: ['object_abc', 'object_def'] });
2618
+ * ```
2619
+ */
2620
+ delete: async (params: { api_names: string[] }): Promise<any> => {
2621
+ const { api_names } = params;
2622
+ await this.ensureTokenValid();
2623
+
2624
+ const url = `/v1/namespaces/${this.namespace}/objects/batch_delete`;
2625
+
2626
+ this.log(LoggerLevel.info, `[schema.delete] Deleting ${api_names.length} object(s): ${api_names.join(', ')}`);
2627
+
2628
+ // 直接传递字符串数组
2629
+ const requestBody = {
2630
+ api_names
2631
+ };
2632
+
2633
+ this.log(LoggerLevel.trace, `[schema.delete] Request URL: ${this.axiosInstance.defaults.baseURL}${url}`);
2634
+ this.log(LoggerLevel.trace, `[schema.delete] Request Body: ${JSON.stringify(requestBody, null, 2)}`);
2635
+
2636
+ const res = await this.axiosInstance.post(
2637
+ url,
2638
+ requestBody,
2639
+ {
2640
+ headers: {
2641
+ Authorization: `${this.accessToken}`,
2642
+ 'Content-Type': 'application/json'
2643
+ }
2644
+ }
2645
+ );
2646
+
2647
+ this.log(LoggerLevel.debug, `[schema.delete] Objects deleted: code=${res.data.code}`);
2648
+ this.log(LoggerLevel.trace, `[schema.delete] Response: ${JSON.stringify(res.data)}`);
2649
+
2650
+ return res.data;
2651
+ }
2652
+ };
2378
2653
  }
2379
2654
 
2380
2655
  export const apaas = {
2381
2656
  Client
2382
2657
  };
2383
2658
 
2384
- export type { BatchResult, RetryOptions };
2659
+ export type {
2660
+ BatchResult,
2661
+ RetryOptions,
2662
+ FieldTypeMetadata,
2663
+ // Schema operation types
2664
+ MultilingualText,
2665
+ ObjectSettings,
2666
+ FieldTypeDefinition,
2667
+ EncryptType,
2668
+ FieldOperator,
2669
+ CreateFieldDefinition,
2670
+ UpdateFieldDefinition,
2671
+ CreateObjectDefinition,
2672
+ UpdateObjectDefinition
2673
+ };
2674
+
2675
+ export {
2676
+ // Field type definitions and metadata
2677
+ FIELD_TYPES,
2678
+ SYSTEM_FIELDS,
2679
+ LANGUAGE_CODES,
2680
+ // Schema guidelines and examples
2681
+ SCHEMA_GUIDELINES,
2682
+ CREATE_OBJECT_EXAMPLE,
2683
+ UPDATE_OBJECT_ADD_FIELD_EXAMPLE,
2684
+ UPDATE_OBJECT_REPLACE_FIELD_EXAMPLE,
2685
+ UPDATE_OBJECT_REMOVE_FIELD_EXAMPLE,
2686
+ UPDATE_OBJECT_SETTINGS_EXAMPLE,
2687
+ // Helper functions
2688
+ getSystemFields,
2689
+ getCustomFieldTypes,
2690
+ getFieldType,
2691
+ isSystemField,
2692
+ createMultilingualText,
2693
+ extractMultilingualText
2694
+ };