zentao-api 0.8.4 → 0.9.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.
Files changed (44) hide show
  1. package/README.md +48 -2
  2. package/dist/browser/zentao-api.global.js +28 -28
  3. package/dist/client/index.d.ts +4 -0
  4. package/dist/client/index.js +10 -0
  5. package/dist/index.d.ts +1 -1
  6. package/dist/index.js +1 -1
  7. package/dist/misc/errors.d.ts +4 -0
  8. package/dist/misc/errors.js +4 -0
  9. package/dist/misc/zentao-version.d.ts +3 -0
  10. package/dist/misc/zentao-version.js +9 -0
  11. package/dist/modules/define.d.ts +1 -0
  12. package/dist/modules/define.js +3 -2
  13. package/dist/modules/generated.d.ts +402 -0
  14. package/dist/modules/generated.js +447 -55
  15. package/dist/modules/object-props.js +212 -36
  16. package/dist/modules/override.js +101 -9
  17. package/dist/modules/query.d.ts +2 -0
  18. package/dist/modules/query.js +6 -0
  19. package/dist/modules/registry-store.d.ts +4 -4
  20. package/dist/modules/registry-store.js +15 -8
  21. package/dist/modules/result-object-type.d.ts +7 -0
  22. package/dist/modules/result-object-type.js +35 -0
  23. package/dist/request/html-file-request.d.ts +34 -0
  24. package/dist/request/html-file-request.js +366 -0
  25. package/dist/request/html-files.d.ts +28 -0
  26. package/dist/request/html-files.js +139 -0
  27. package/dist/request/index.d.ts +3 -3
  28. package/dist/request/index.js +78 -26
  29. package/dist/request/prepare-body.d.ts +2 -0
  30. package/dist/request/prepare-body.js +2 -2
  31. package/dist/types/index.d.ts +2 -0
  32. package/dist/types/index.js +2 -0
  33. package/dist/types/link.d.ts +52 -0
  34. package/dist/types/link.js +1 -0
  35. package/dist/types/module.d.ts +18 -0
  36. package/dist/types/options.d.ts +7 -4
  37. package/dist/types/response.d.ts +16 -0
  38. package/dist/types/upload.d.ts +64 -2
  39. package/dist/utils/index.d.ts +1 -0
  40. package/dist/utils/index.js +1 -0
  41. package/dist/utils/link.d.ts +30 -0
  42. package/dist/utils/link.js +225 -0
  43. package/dist/version.js +2 -2
  44. package/package.json +5 -2
@@ -3,7 +3,9 @@ import { extractResult } from './resolve.js';
3
3
  import { snapshotToMarkdown } from '../utils/doc-helper/markdown.js';
4
4
  import { isRecord } from '../utils/index.js';
5
5
  import { request } from '../request/index.js';
6
+ import { captureFileSession } from '../client/index.js';
6
7
  import { ZentaoError } from '../misc/errors.js';
8
+ import { VERSION_23_MINIMUMS } from '../misc/zentao-version.js';
7
9
  /** 这些内置接口要求 JSON,不能把 HTTP 200 的 PHP/SQL 错误文本视为成功。 */
8
10
  const requestJSON = async ({ request: command, body, bodyType, client, timeout, insecure, actionAssistant }) => {
9
11
  const response = await client.request(command.path, {
@@ -228,6 +230,7 @@ export function applyBuiltinOverrides() {
228
230
  path: '/products/{productID}/repos',
229
231
  pathParams: { productID: '产品 ID,正整数' },
230
232
  resultType: 'list',
233
+ resultObjectType: 'repo',
231
234
  resultGetter: 'repos',
232
235
  request: requestCoderJSON,
233
236
  });
@@ -340,6 +343,14 @@ export function applyBuiltinOverrides() {
340
343
  return { data, query };
341
344
  },
342
345
  });
346
+ // 部分服务端从 query 读取 productID;复制已解析的 body 值以保留 data 优先级与类型转换。
347
+ extendModuleAction('bug', 'create', {
348
+ beforeRequest: async ({ data, query }) => {
349
+ if (data?.productID === undefined)
350
+ return {};
351
+ return { query: { ...query, productID: data.productID } };
352
+ },
353
+ });
343
354
  // 确认表单会重置缺省字段;仅补齐可写字段,不回传详情中的其他属性。
344
355
  extendModuleAction('bug', 'confirm', {
345
356
  request: async ({ request: command, body, bodyType, client, timeout, insecure, actionAssistant, options }) => {
@@ -347,11 +358,13 @@ export function applyBuiltinOverrides() {
347
358
  const fields = ['status', 'assignedTo', 'type', 'pri', 'deadline', 'mailto'];
348
359
  const missing = fields.filter(field => data[field] === undefined);
349
360
  if (missing.length) {
361
+ const checkSession = data.uid === undefined ? undefined : client[captureFileSession]();
350
362
  const response = await request('bug/get', { id: command.id }, {
351
363
  client, timeout, insecure, actionAssistant, throwOnFail: true,
352
364
  forceRefreshConfig: options.forceRefreshConfig,
353
365
  skipVersionCheckOnConfigError: options.skipVersionCheckOnConfigError,
354
366
  });
367
+ checkSession?.();
355
368
  const current = response.data;
356
369
  if (!isRecord(current) || missing.some(field => current[field] === undefined)) {
357
370
  throw new ZentaoError('E_API_FAILED', { message: 'Bug details are incomplete; confirmation was not sent.' });
@@ -415,15 +428,93 @@ export function applyBuiltinOverrides() {
415
428
  return action;
416
429
  });
417
430
  });
418
- // OpenAPI 使用 JSON 描述 SDK 的本地文件路径入参,但实际请求仍需转换为 multipart/form-data
419
- extendModuleAction('file', 'create', (action) => {
420
- if (action.requestBody) {
421
- action.requestBody.mediaType = 'multipart/form-data';
422
- const properties = action.requestBody.schema?.properties;
423
- if (properties?.file)
424
- properties.file.format = 'binary';
425
- }
426
- return action;
431
+ defineModuleActions('my', {
432
+ name: 'info',
433
+ minVersion: VERSION_23_MINIMUMS,
434
+ display: '获取当前用户基础资料',
435
+ description: '返回当前 Token 用户的 id、dept、account、realname、nickname、avatar、role、type;avatar 可为空字符串或 null。',
436
+ type: 'get',
437
+ method: 'get',
438
+ path: '/my/info',
439
+ resultType: 'object',
440
+ resultObjectType: 'user',
441
+ resultGetter: 'user',
442
+ request: requestJSON,
443
+ });
444
+ defineModuleActions('file', [
445
+ {
446
+ name: 'get',
447
+ minVersion: ['22.5', 'biz13.5', 'max8.5', 'ipd5.5'],
448
+ display: '获取附件信息',
449
+ description: '查询附件元数据;fileID 支持数字 ID 或既有 Base64 编码 gid。读取文件内容请使用 file/read。',
450
+ type: 'get',
451
+ method: 'get',
452
+ path: '/files/{fileID}',
453
+ pathParams: { fileID: '附件 ID 或 Base64 编码 gid' },
454
+ resultType: 'object',
455
+ resultGetter: 'file',
456
+ // Base64 中的 +、/ 必须保持为路径参数的一部分。
457
+ beforeRequest: async ({ path }) => ({ path: `/files/${encodeURIComponent(path.slice('/files/'.length))}` }),
458
+ request: requestJSON,
459
+ },
460
+ {
461
+ name: 'read',
462
+ minVersion: VERSION_23_MINIMUMS,
463
+ display: '读取附件内容',
464
+ description: '默认返回 ArrayBuffer;stream=1 返回 Base64 字符串。两种模式均完整载入内存,大文件流式读取可使用底层客户端的 responseType: response。',
465
+ type: 'get',
466
+ method: 'get',
467
+ path: '/files/{fileID}/read',
468
+ pathParams: { fileID: '附件数字 ID' },
469
+ resultType: 'object',
470
+ resultObjectType: null,
471
+ resultGetter: data => data,
472
+ params: [{
473
+ name: 'stream',
474
+ type: 'number',
475
+ defaultValue: 0,
476
+ description: '返回格式',
477
+ options: [{ value: 0, label: '原始文件内容' }, { value: 1, label: 'Base64 文本' }],
478
+ }],
479
+ request: async ({ request: command, client, timeout, insecure, actionAssistant }) => {
480
+ const stream = command.query?.stream;
481
+ if (stream !== 0 && stream !== 1 && stream !== '0' && stream !== '1') {
482
+ throw new ZentaoError('E_INVALID_PARAM', { param: 'stream', value: String(stream) });
483
+ }
484
+ return client.request(command.path, {
485
+ method: 'GET', query: command.query, timeout, insecure, actionAssistant,
486
+ responseType: stream === 1 || stream === '1' ? 'text' : 'arrayBuffer',
487
+ });
488
+ },
489
+ },
490
+ ]);
491
+ // 附件可不关联业务对象,创建和更新时均不强制关联参数。
492
+ ['create', 'update'].forEach((actionName) => {
493
+ extendModuleAction('file', actionName, (action) => {
494
+ if (action.requestBody) {
495
+ const schema = action.requestBody.schema;
496
+ if (schema.required) {
497
+ schema.required = schema.required.filter(name => name !== 'objectType' && name !== 'objectID');
498
+ }
499
+ for (const name of ['objectType', 'objectID']) {
500
+ if (schema.properties[name])
501
+ schema.properties[name].required = false;
502
+ }
503
+ // OpenAPI 的本地文件路径入参仍需转换为 multipart/form-data。
504
+ if (actionName === 'create') {
505
+ action.requestBody.mediaType = 'multipart/form-data';
506
+ if (schema.properties.file)
507
+ schema.properties.file.format = 'binary';
508
+ schema.properties.extra = {
509
+ type: 'string',
510
+ defaultValue: 'editor',
511
+ description: '附件标记,默认 editor(文档编辑器图片),23.0 / biz14.0 / max9.0 / ipd6.0 起支持;显式空字符串用于普通附件,其他值由服务端按空字符串处理',
512
+ options: [{ value: '', label: '普通附件' }, { value: 'editor', label: '文档编辑器图片' }],
513
+ };
514
+ }
515
+ }
516
+ return action;
517
+ });
427
518
  });
428
519
  // 获取文档详情时支持将文档原始 JSON 内容转换为 Markdown
429
520
  extendModuleAction('doc', 'get', (action) => {
@@ -466,6 +557,7 @@ export function applyBuiltinOverrides() {
466
557
  method: 'get',
467
558
  path: '/storygrades',
468
559
  resultType: 'list',
560
+ resultObjectType: 'storygrade',
469
561
  resultGetter: 'grades',
470
562
  });
471
563
  // 需求创建时支持设置需求层级
@@ -28,6 +28,8 @@ export declare function getModuleAction(moduleName: string, actionName: string,
28
28
  /**
29
29
  * 获取指定模块下的某个动作的参数。
30
30
  *
31
+ * 说明以 `(HTML)` 结尾的参数返回 `textType: 'html'`,保留原始说明。
32
+ *
31
33
  * @param moduleName - 模块名(大小写不敏感)。
32
34
  * @param actionName - 动作名(大小写不敏感);支持 `ls` 作为 `list` 的别名。
33
35
  * @param options - 选项。
@@ -43,6 +43,8 @@ export function getModuleAction(moduleName, actionName, options) {
43
43
  /**
44
44
  * 获取指定模块下的某个动作的参数。
45
45
  *
46
+ * 说明以 `(HTML)` 结尾的参数返回 `textType: 'html'`,保留原始说明。
47
+ *
46
48
  * @param moduleName - 模块名(大小写不敏感)。
47
49
  * @param actionName - 动作名(大小写不敏感);支持 `ls` 作为 `list` 的别名。
48
50
  * @param options - 选项。
@@ -99,6 +101,10 @@ export function getModuleActionParams(moduleName, actionName, options) {
99
101
  });
100
102
  }
101
103
  }
104
+ for (const param of params) {
105
+ if (param.description?.endsWith('(HTML)'))
106
+ param.textType = 'html';
107
+ }
102
108
  return params;
103
109
  }
104
110
  /**
@@ -11,18 +11,18 @@ export declare function deepMerge<T>(base: T, patch: unknown): T;
11
11
  export declare function cloneBuiltinModules(): ModuleDefinition[];
12
12
  export declare function deepFreeze<T>(value: T): T;
13
13
  /**
14
- * 按 {@link ModuleAction.type} 就地补齐省略的 `method` / `resultType`。
14
+ * 按动作类型和所属模块就地补齐省略的 `method` / `resultType` / `resultObjectType`。
15
15
  *
16
16
  * 已显式声明的字段保持不变;当 `type` 无法推导出对应默认值时抛错,提示显式指定。
17
17
  *
18
18
  * @throws {ZentaoError} `E_INDETERMINATE_ACTION_METHOD` / `E_INDETERMINATE_ACTION_RESULT_TYPE`
19
19
  */
20
- export declare function normalizeAction(action: ModuleAction): ModuleAction;
21
- export declare function freezeAction(action: ModuleAction): ModuleAction;
20
+ export declare function normalizeAction(action: ModuleAction, moduleName: string): ModuleAction;
21
+ export declare function freezeAction(action: ModuleAction, moduleName: string): ModuleAction;
22
22
  export declare function freezeModule(module: ModuleDefinition): ModuleDefinition;
23
23
  export declare function freezeModules(source: ModuleDefinition[]): ModuleDefinition[];
24
24
  export declare function findActionIndex(source: readonly ModuleAction[], actionName: string): number;
25
- export declare function mergeActions(base: readonly ModuleAction[], extension: readonly ModuleAction[]): ModuleAction[];
25
+ export declare function mergeActions(base: readonly ModuleAction[], extension: readonly ModuleAction[], moduleName: string): ModuleAction[];
26
26
  export declare function mergeModule(base: ModuleDefinition, extension: ModuleDefinition): ModuleDefinition;
27
27
  export declare function buildModuleMap(source: readonly ModuleDefinition[]): Map<string, ModuleDefinition>;
28
28
  export declare function validateModule(module: ModuleDefinition): void;
@@ -2,6 +2,7 @@ import { ZentaoError } from '../misc/errors.js';
2
2
  import { validateMinVersion } from '../misc/zentao-version.js';
3
3
  import { isRecord } from '../utils/object.js';
4
4
  import { BUILTIN_MODULES } from './generated.js';
5
+ import { defaultResultObjectType, isResultObjectType } from './result-object-type.js';
5
6
  // 动作类型到 HTTP 方法 / 结果形态的默认推导表:当动作未显式声明 method / resultType 时按 type 补齐。
6
7
  const METHOD_BY_TYPE = {
7
8
  list: 'get',
@@ -76,13 +77,13 @@ export function deepFreeze(value) {
76
77
  return Object.freeze(value);
77
78
  }
78
79
  /**
79
- * 按 {@link ModuleAction.type} 就地补齐省略的 `method` / `resultType`。
80
+ * 按动作类型和所属模块就地补齐省略的 `method` / `resultType` / `resultObjectType`。
80
81
  *
81
82
  * 已显式声明的字段保持不变;当 `type` 无法推导出对应默认值时抛错,提示显式指定。
82
83
  *
83
84
  * @throws {ZentaoError} `E_INDETERMINATE_ACTION_METHOD` / `E_INDETERMINATE_ACTION_RESULT_TYPE`
84
85
  */
85
- export function normalizeAction(action) {
86
+ export function normalizeAction(action, moduleName) {
86
87
  if (action.method === undefined) {
87
88
  const method = METHOD_BY_TYPE[action.type];
88
89
  if (method === undefined) {
@@ -97,14 +98,17 @@ export function normalizeAction(action) {
97
98
  }
98
99
  action.resultType = resultType;
99
100
  }
101
+ if (action.resultObjectType === undefined) {
102
+ action.resultObjectType = defaultResultObjectType(moduleName, action.resultType);
103
+ }
100
104
  return action;
101
105
  }
102
- export function freezeAction(action) {
106
+ export function freezeAction(action, moduleName) {
103
107
  validateAction(action);
104
- return deepFreeze(normalizeAction(action));
108
+ return deepFreeze(normalizeAction(action, moduleName));
105
109
  }
106
110
  export function freezeModule(module) {
107
- module.actions.forEach(freezeAction);
111
+ module.actions.forEach(action => freezeAction(action, module.name));
108
112
  return deepFreeze(module);
109
113
  }
110
114
  export function freezeModules(source) {
@@ -115,11 +119,11 @@ export function findActionIndex(source, actionName) {
115
119
  const key = actionName.toLowerCase();
116
120
  return source.findIndex((action) => String(action.name).toLowerCase() === key);
117
121
  }
118
- export function mergeActions(base, extension) {
122
+ export function mergeActions(base, extension, moduleName) {
119
123
  const next = base.slice();
120
124
  for (const action of extension) {
121
125
  const index = findActionIndex(next, String(action.name));
122
- const frozen = freezeAction(deepClone(action));
126
+ const frozen = freezeAction(deepClone(action), moduleName);
123
127
  if (index >= 0) {
124
128
  next[index] = frozen;
125
129
  }
@@ -133,7 +137,7 @@ export function mergeModule(base, extension) {
133
137
  return freezeModule({
134
138
  ...base,
135
139
  ...deepClone(extension),
136
- actions: mergeActions(base.actions, extension.actions),
140
+ actions: mergeActions(base.actions, extension.actions, extension.name),
137
141
  });
138
142
  }
139
143
  export function buildModuleMap(source) {
@@ -156,6 +160,9 @@ export function validateAction(action) {
156
160
  if (action.resultType !== undefined && typeof action.resultType !== 'string') {
157
161
  throw new ZentaoError('E_INVALID_ACTION_DEFINITION');
158
162
  }
163
+ if (action.resultObjectType !== undefined && !isResultObjectType(action.resultObjectType)) {
164
+ throw new ZentaoError('E_INVALID_ACTION_DEFINITION');
165
+ }
159
166
  if (action.beforeRequest !== undefined && typeof action.beforeRequest !== 'function') {
160
167
  throw new ZentaoError('E_INVALID_ACTION_DEFINITION');
161
168
  }
@@ -0,0 +1,7 @@
1
+ import type { ModuleActionResultObjectType, ModuleActionResultType } from '../types/module.js';
2
+ /** 生成器和运行时注册共用的定义校验。 */
3
+ export declare function isResultObjectType(value: unknown): value is ModuleActionResultObjectType;
4
+ /** 仅对象和列表默认采用所属模块名;显式 null 由调用方保留。 */
5
+ export declare function defaultResultObjectType(moduleName: string, resultType: ModuleActionResultType): string | null;
6
+ /** 根据声明解析结果类型,不遍历或复制业务数据。 */
7
+ export declare function resolveResultObjectType(definition: ModuleActionResultObjectType | undefined, params: Record<string, unknown>, data: unknown): string | undefined;
@@ -0,0 +1,35 @@
1
+ function isNonblankString(value) {
2
+ return typeof value === 'string' && value.trim().length > 0;
3
+ }
4
+ function isPlainRecord(value) {
5
+ if (!value || typeof value !== 'object')
6
+ return false;
7
+ const prototype = Object.getPrototypeOf(value);
8
+ return prototype === Object.prototype || prototype === null;
9
+ }
10
+ /** 生成器和运行时注册共用的定义校验。 */
11
+ export function isResultObjectType(value) {
12
+ if (value === null || isNonblankString(value))
13
+ return true;
14
+ if (!isPlainRecord(value) || !Object.hasOwn(value, 'param') || !isNonblankString(value.param))
15
+ return false;
16
+ if (Object.keys(value).some(key => key !== 'param' && key !== 'map'))
17
+ return false;
18
+ return value.map === undefined || (isPlainRecord(value.map)
19
+ && Object.entries(value.map).every(([key, type]) => isNonblankString(key) && isNonblankString(type)));
20
+ }
21
+ /** 仅对象和列表默认采用所属模块名;显式 null 由调用方保留。 */
22
+ export function defaultResultObjectType(moduleName, resultType) {
23
+ return resultType === 'object' || resultType === 'list' ? moduleName : null;
24
+ }
25
+ /** 根据声明解析结果类型,不遍历或复制业务数据。 */
26
+ export function resolveResultObjectType(definition, params, data) {
27
+ if (definition == null || (!Array.isArray(data) && !isPlainRecord(data)))
28
+ return undefined;
29
+ if (typeof definition === 'string')
30
+ return definition;
31
+ const value = Object.hasOwn(params, definition.param) ? params[definition.param] : undefined;
32
+ if (!isNonblankString(value))
33
+ return undefined;
34
+ return definition.map && Object.hasOwn(definition.map, value) ? definition.map[value] : value;
35
+ }
@@ -0,0 +1,34 @@
1
+ import { type ZentaoClient } from '../client/index.js';
2
+ import type { FileHandlingOptions, FileWarning, ModuleActionRequest } from '../types/index.js';
3
+ type FileRequest = (action: 'create' | 'delete', params: Record<string, unknown>, checkSession?: () => void) => Promise<Record<string, unknown>>;
4
+ interface Context extends FileHandlingOptions {
5
+ client: ZentaoClient;
6
+ version: string | null;
7
+ timeout?: number;
8
+ insecure?: boolean;
9
+ requestFile: FileRequest;
10
+ }
11
+ /** Put the batch identifier in the upload query or the object's JSON body. */
12
+ export declare function setFileUid(command: ModuleActionRequest, value: unknown, version: string | null): string;
13
+ export declare function isFileWriteFailure(raw: Record<string, unknown>): boolean;
14
+ /** Owns only the images uploaded during this object save. */
15
+ export declare class HtmlFileRequest {
16
+ private readonly command;
17
+ private readonly context;
18
+ private uid?;
19
+ readonly warnings: FileWarning[];
20
+ private readonly uploads;
21
+ private linkProfile?;
22
+ checkSession?: () => void;
23
+ constructor(command: ModuleActionRequest, context: Context, uid?: string | undefined);
24
+ private profile;
25
+ private fileLink;
26
+ private warn;
27
+ prepare(): Promise<void>;
28
+ cleanup(): Promise<void>;
29
+ /** Preserve the primary error and attach enough context to reconcile uncertain writes. */
30
+ errorWithFiles(error: unknown): unknown;
31
+ saveError(error: unknown): Promise<unknown>;
32
+ complete(raw: unknown): Promise<void>;
33
+ }
34
+ export {};