zentao-api 0.6.6 → 0.6.7
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 +29 -2
- package/dist/browser/zentao-api.global.js +21 -21
- package/dist/client/index.d.ts +26 -2
- package/dist/client/index.js +109 -10
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/misc/errors.d.ts +5 -0
- package/dist/misc/errors.js +8 -0
- package/dist/misc/zentao-version.d.ts +11 -0
- package/dist/misc/zentao-version.js +35 -0
- package/dist/modules/define.d.ts +4 -3
- package/dist/modules/define.js +4 -3
- package/dist/modules/generated.d.ts +228 -0
- package/dist/modules/generated.js +228 -0
- package/dist/modules/override.d.ts +2 -0
- package/dist/modules/override.js +3 -0
- package/dist/modules/query.d.ts +13 -8
- package/dist/modules/query.js +25 -11
- package/dist/modules/registry-store.js +3 -0
- package/dist/profiles/index.d.ts +3 -1
- package/dist/profiles/index.js +12 -0
- package/dist/request/index.d.ts +3 -0
- package/dist/request/index.js +37 -5
- package/dist/types/client.d.ts +5 -0
- package/dist/types/module.d.ts +7 -0
- package/dist/types/options.d.ts +8 -0
- package/dist/types/profile.d.ts +2 -0
- package/dist/types/response.d.ts +9 -0
- package/dist/version.js +2 -2
- package/package.json +1 -1
package/dist/client/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { ClientRequestOptions, ZentaoClientOptions } from '../types/index.js';
|
|
1
|
+
import type { ClientRequestOptions, GetZentaoConfigOptions, ServerConfig, ZentaoClientOptions } from '../types/index.js';
|
|
2
2
|
/**
|
|
3
3
|
* 禅道 API 客户端,封装一次次原始 HTTP 调用。
|
|
4
4
|
*
|
|
@@ -20,6 +20,10 @@ export declare class ZentaoClient {
|
|
|
20
20
|
private token?;
|
|
21
21
|
private readonly timeout?;
|
|
22
22
|
private readonly insecure?;
|
|
23
|
+
private profileKey?;
|
|
24
|
+
private serverConfig?;
|
|
25
|
+
private serverConfigFetchedAt?;
|
|
26
|
+
private configRequest?;
|
|
23
27
|
/**
|
|
24
28
|
* 使用完整配置创建客户端。
|
|
25
29
|
*
|
|
@@ -65,6 +69,24 @@ export declare class ZentaoClient {
|
|
|
65
69
|
responseType: 'blob';
|
|
66
70
|
}): Promise<Blob>;
|
|
67
71
|
request<T = unknown>(path: string, options?: ClientRequestOptions): Promise<T>;
|
|
72
|
+
/** API 与站点配置共用的传输层;配置请求不传入 Token。 */
|
|
73
|
+
private fetchUrl;
|
|
74
|
+
/**
|
|
75
|
+
* 获取禅道站点 `/?mode=getconfig` 配置,不发送 API Token。
|
|
76
|
+
*
|
|
77
|
+
* 默认复用不超过 24 小时的缓存;缺失、过期或时间异常时重新获取。
|
|
78
|
+
* `forceRefresh: true` 忽略缓存。同一客户端的并发刷新共用首次调用的传输选项;
|
|
79
|
+
* 后加入的调用仍可通过自己的 signal 取消等待。
|
|
80
|
+
* 成功后更新实例缓存;启用 `persistProfiles` 且绑定了 profile 时仅更新其配置和获取时间。
|
|
81
|
+
* 返回独立副本,修改返回值不会改变缓存。此方法不会忽略配置获取错误。
|
|
82
|
+
*
|
|
83
|
+
* @param options - 缓存、超时、TLS 与取消选项。
|
|
84
|
+
* @returns 服务器配置。
|
|
85
|
+
* @throws {ZentaoError} 传输错误、`E_INVALID_ZENTAO_CONFIG`、`E_INVALID_ZENTAO_VERSION` 或 profile 存储错误。
|
|
86
|
+
*/
|
|
87
|
+
getZentaoConfig(options?: GetZentaoConfigOptions): Promise<ServerConfig>;
|
|
88
|
+
private loadZentaoConfig;
|
|
89
|
+
private waitForConfig;
|
|
68
90
|
/**
|
|
69
91
|
* 发起 `GET` 请求。
|
|
70
92
|
*
|
|
@@ -106,7 +128,9 @@ export declare class ZentaoClient {
|
|
|
106
128
|
/**
|
|
107
129
|
* 使用账号密码登录禅道。
|
|
108
130
|
*
|
|
109
|
-
*
|
|
131
|
+
* 验证成功后强制获取一次站点配置,再把返回的 Token 写入当前客户端实例;
|
|
132
|
+
* 配置获取失败默认阻断登录,只有全局 `skipVersionCheckOnConfigError` 可允许继续。
|
|
133
|
+
* 全局 `version` 不跳过登录时的配置获取。
|
|
110
134
|
* 当全局 `persistProfiles` 为真时,会同时把账号、Token、用户信息、服务端配置和
|
|
111
135
|
* 客户端偏好(仅在显式设置过 `timeout` / `insecure` 时)持久化为本地 profile,
|
|
112
136
|
* 并切换为当前 profile,方便下次通过 {@link ZentaoClient.fromProfile} 直接登录态恢复。
|
package/dist/client/index.js
CHANGED
|
@@ -1,9 +1,14 @@
|
|
|
1
|
-
import { ZentaoError } from '../misc/errors.js';
|
|
1
|
+
import { isZentaoConfigFetchError, ZentaoError } from '../misc/errors.js';
|
|
2
|
+
import { parseZentaoVersion } from '../misc/zentao-version.js';
|
|
2
3
|
import { assertInsecureSupported, fetchWithInsecureTls } from '../misc/environment.js';
|
|
3
4
|
import { getGlobalOptions, setGlobalOptions } from '../misc/global-options.js';
|
|
4
|
-
import { addProfile, switchProfile } from '../profiles/index.js';
|
|
5
|
+
import { addProfile, switchProfile, updateProfileServerConfig } from '../profiles/index.js';
|
|
5
6
|
import { isRecord, normalizeSiteUrl } from '../utils/index.js';
|
|
6
7
|
const DEFAULT_TIMEOUT = 10000;
|
|
8
|
+
const CONFIG_MAX_AGE = 24 * 60 * 60 * 1000;
|
|
9
|
+
function isServerConfig(value) {
|
|
10
|
+
return isRecord(value) && typeof value.version === 'string' && value.version.trim().length > 0;
|
|
11
|
+
}
|
|
7
12
|
function appendQueryValue(search, key, value) {
|
|
8
13
|
if (value === undefined)
|
|
9
14
|
return;
|
|
@@ -298,6 +303,10 @@ export class ZentaoClient {
|
|
|
298
303
|
token;
|
|
299
304
|
timeout;
|
|
300
305
|
insecure;
|
|
306
|
+
profileKey;
|
|
307
|
+
serverConfig;
|
|
308
|
+
serverConfigFetchedAt;
|
|
309
|
+
configRequest;
|
|
301
310
|
constructor(input) {
|
|
302
311
|
const options = typeof input === 'string' ? { baseUrl: input } : input;
|
|
303
312
|
this.siteUrl = normalizeSiteUrl(options.baseUrl);
|
|
@@ -307,20 +316,24 @@ export class ZentaoClient {
|
|
|
307
316
|
this.insecure = options.insecure;
|
|
308
317
|
}
|
|
309
318
|
async request(path, options = {}) {
|
|
319
|
+
return this.fetchUrl(buildUrl(this.baseUrl, path, options.query), options, this.token);
|
|
320
|
+
}
|
|
321
|
+
/** API 与站点配置共用的传输层;配置请求不传入 Token。 */
|
|
322
|
+
async fetchUrl(url, options, token, cache) {
|
|
310
323
|
const globals = getGlobalOptions();
|
|
311
324
|
const method = options.method ?? 'GET';
|
|
312
325
|
const timeout = options.timeout ?? globals.timeout ?? this.timeout ?? DEFAULT_TIMEOUT;
|
|
313
326
|
const insecure = options.insecure ?? globals.insecure ?? this.insecure;
|
|
314
327
|
assertInsecureSupported(insecure);
|
|
315
|
-
const url = buildUrl(this.baseUrl, path, options.query);
|
|
316
328
|
const headers = new Headers(options.headers);
|
|
317
|
-
if (
|
|
318
|
-
headers.set('Token',
|
|
329
|
+
if (token) {
|
|
330
|
+
headers.set('Token', token);
|
|
319
331
|
}
|
|
320
332
|
const init = {
|
|
321
333
|
method,
|
|
322
334
|
headers,
|
|
323
335
|
redirect: 'manual',
|
|
336
|
+
cache,
|
|
324
337
|
};
|
|
325
338
|
// GET 请求不携带 body,避免浏览器和部分代理拒绝请求。
|
|
326
339
|
if (options.body !== undefined && method !== 'GET') {
|
|
@@ -365,6 +378,73 @@ export class ZentaoClient {
|
|
|
365
378
|
cleanup();
|
|
366
379
|
}
|
|
367
380
|
}
|
|
381
|
+
/**
|
|
382
|
+
* 获取禅道站点 `/?mode=getconfig` 配置,不发送 API Token。
|
|
383
|
+
*
|
|
384
|
+
* 默认复用不超过 24 小时的缓存;缺失、过期或时间异常时重新获取。
|
|
385
|
+
* `forceRefresh: true` 忽略缓存。同一客户端的并发刷新共用首次调用的传输选项;
|
|
386
|
+
* 后加入的调用仍可通过自己的 signal 取消等待。
|
|
387
|
+
* 成功后更新实例缓存;启用 `persistProfiles` 且绑定了 profile 时仅更新其配置和获取时间。
|
|
388
|
+
* 返回独立副本,修改返回值不会改变缓存。此方法不会忽略配置获取错误。
|
|
389
|
+
*
|
|
390
|
+
* @param options - 缓存、超时、TLS 与取消选项。
|
|
391
|
+
* @returns 服务器配置。
|
|
392
|
+
* @throws {ZentaoError} 传输错误、`E_INVALID_ZENTAO_CONFIG`、`E_INVALID_ZENTAO_VERSION` 或 profile 存储错误。
|
|
393
|
+
*/
|
|
394
|
+
async getZentaoConfig(options = {}) {
|
|
395
|
+
const config = await this.loadZentaoConfig(options, getGlobalOptions().persistProfiles ? this.profileKey : undefined);
|
|
396
|
+
return structuredClone(config);
|
|
397
|
+
}
|
|
398
|
+
async loadZentaoConfig(options, profileKey) {
|
|
399
|
+
if (options.signal?.aborted)
|
|
400
|
+
throw new ZentaoError('E_ABORTED');
|
|
401
|
+
// 刷新进行中时等待其结果,避免同一批调用混用旧缓存和新配置。
|
|
402
|
+
if (this.configRequest)
|
|
403
|
+
return this.waitForConfig(this.configRequest, options.signal);
|
|
404
|
+
const fetchedAt = typeof this.serverConfigFetchedAt === 'string' ? Date.parse(this.serverConfigFetchedAt) : NaN;
|
|
405
|
+
const age = Date.now() - fetchedAt;
|
|
406
|
+
if (!options.forceRefresh && isServerConfig(this.serverConfig) && age >= 0 && age <= CONFIG_MAX_AGE) {
|
|
407
|
+
parseZentaoVersion(this.serverConfig.version);
|
|
408
|
+
return this.serverConfig;
|
|
409
|
+
}
|
|
410
|
+
const pending = this.fetchUrl(buildUrl(this.siteUrl, '/', { mode: 'getconfig' }), {
|
|
411
|
+
method: 'GET', timeout: options.timeout, insecure: options.insecure, signal: options.signal,
|
|
412
|
+
}, undefined, 'no-store').then(async (config) => {
|
|
413
|
+
if (!isServerConfig(config))
|
|
414
|
+
throw new ZentaoError('E_INVALID_ZENTAO_CONFIG');
|
|
415
|
+
parseZentaoVersion(config.version);
|
|
416
|
+
const timestamp = new Date().toISOString();
|
|
417
|
+
if (profileKey)
|
|
418
|
+
await updateProfileServerConfig(profileKey, config, timestamp);
|
|
419
|
+
this.serverConfig = config;
|
|
420
|
+
this.serverConfigFetchedAt = timestamp;
|
|
421
|
+
return config;
|
|
422
|
+
});
|
|
423
|
+
this.configRequest = pending;
|
|
424
|
+
try {
|
|
425
|
+
return await pending;
|
|
426
|
+
}
|
|
427
|
+
finally {
|
|
428
|
+
this.configRequest = undefined;
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
async waitForConfig(pending, signal) {
|
|
432
|
+
if (!signal)
|
|
433
|
+
return pending;
|
|
434
|
+
let onAbort;
|
|
435
|
+
const aborted = new Promise((_, reject) => {
|
|
436
|
+
onAbort = () => reject(new ZentaoError('E_ABORTED'));
|
|
437
|
+
signal.addEventListener('abort', onAbort, { once: true });
|
|
438
|
+
if (signal.aborted)
|
|
439
|
+
onAbort();
|
|
440
|
+
});
|
|
441
|
+
try {
|
|
442
|
+
return await Promise.race([pending, aborted]);
|
|
443
|
+
}
|
|
444
|
+
finally {
|
|
445
|
+
signal.removeEventListener('abort', onAbort);
|
|
446
|
+
}
|
|
447
|
+
}
|
|
368
448
|
/**
|
|
369
449
|
* 发起 `GET` 请求。
|
|
370
450
|
*
|
|
@@ -414,7 +494,9 @@ export class ZentaoClient {
|
|
|
414
494
|
/**
|
|
415
495
|
* 使用账号密码登录禅道。
|
|
416
496
|
*
|
|
417
|
-
*
|
|
497
|
+
* 验证成功后强制获取一次站点配置,再把返回的 Token 写入当前客户端实例;
|
|
498
|
+
* 配置获取失败默认阻断登录,只有全局 `skipVersionCheckOnConfigError` 可允许继续。
|
|
499
|
+
* 全局 `version` 不跳过登录时的配置获取。
|
|
418
500
|
* 当全局 `persistProfiles` 为真时,会同时把账号、Token、用户信息、服务端配置和
|
|
419
501
|
* 客户端偏好(仅在显式设置过 `timeout` / `insecure` 时)持久化为本地 profile,
|
|
420
502
|
* 并切换为当前 profile,方便下次通过 {@link ZentaoClient.fromProfile} 直接登录态恢复。
|
|
@@ -430,8 +512,17 @@ export class ZentaoClient {
|
|
|
430
512
|
if (response.status !== 'success' || !response.token) {
|
|
431
513
|
throw new ZentaoError('E_LOGIN_FAILED');
|
|
432
514
|
}
|
|
433
|
-
this.token = response.token;
|
|
434
515
|
const globals = getGlobalOptions();
|
|
516
|
+
let serverConfig;
|
|
517
|
+
try {
|
|
518
|
+
// 共用 getZentaoConfig 的获取流程,但登录确认前不能刷新之前绑定的账号。
|
|
519
|
+
serverConfig = await this.loadZentaoConfig({ forceRefresh: true });
|
|
520
|
+
}
|
|
521
|
+
catch (error) {
|
|
522
|
+
if (!globals.skipVersionCheckOnConfigError || !isZentaoConfigFetchError(error))
|
|
523
|
+
throw error;
|
|
524
|
+
}
|
|
525
|
+
let profileKey;
|
|
435
526
|
if (globals.persistProfiles) {
|
|
436
527
|
const config = {};
|
|
437
528
|
const timeout = this.timeout ?? globals.timeout;
|
|
@@ -440,15 +531,19 @@ export class ZentaoClient {
|
|
|
440
531
|
config.timeout = timeout;
|
|
441
532
|
if (insecure !== undefined)
|
|
442
533
|
config.insecure = insecure;
|
|
443
|
-
await addProfile({
|
|
534
|
+
const profile = await addProfile({
|
|
444
535
|
server: this.siteUrl,
|
|
445
536
|
account,
|
|
446
537
|
token: response.token,
|
|
447
538
|
user: isRecord(response.user) ? response.user : undefined,
|
|
448
|
-
serverConfig:
|
|
539
|
+
serverConfig: serverConfig ? structuredClone(serverConfig) : undefined,
|
|
540
|
+
serverConfigFetchedAt: serverConfig ? this.serverConfigFetchedAt : undefined,
|
|
449
541
|
config: Object.keys(config).length > 0 ? config : undefined,
|
|
450
542
|
});
|
|
543
|
+
profileKey = profile.key;
|
|
451
544
|
}
|
|
545
|
+
this.token = response.token;
|
|
546
|
+
this.profileKey = profileKey;
|
|
452
547
|
return response.token;
|
|
453
548
|
}
|
|
454
549
|
/**
|
|
@@ -491,11 +586,15 @@ export class ZentaoClient {
|
|
|
491
586
|
// switchProfile 会在内部读取存储、校验 key 并刷新 lastUsedTime 后写回,
|
|
492
587
|
// 若 key 不存在会抛出 E_PROFILE_NOT_FOUND;不传 key 时由 switchCurrentProfile 处理。
|
|
493
588
|
const activeProfile = await switchProfile(profileKey);
|
|
494
|
-
|
|
589
|
+
const client = new ZentaoClient({
|
|
495
590
|
baseUrl: activeProfile.server,
|
|
496
591
|
token: activeProfile.token,
|
|
497
592
|
timeout: typeof activeProfile.config?.timeout === 'number' ? activeProfile.config.timeout : undefined,
|
|
498
593
|
insecure: typeof activeProfile.config?.insecure === 'boolean' ? activeProfile.config.insecure : undefined,
|
|
499
594
|
});
|
|
595
|
+
client.profileKey = activeProfile.key;
|
|
596
|
+
client.serverConfig = activeProfile.serverConfig;
|
|
597
|
+
client.serverConfigFetchedAt = activeProfile.serverConfigFetchedAt;
|
|
598
|
+
return client;
|
|
500
599
|
}
|
|
501
600
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ export { ZentaoClient } from './client/index.js';
|
|
|
2
2
|
export { ERRORS, ZentaoError, type ErrorCode } from './misc/errors.js';
|
|
3
3
|
export { getGlobalOptions, setGlobalOptions } from './misc/global-options.js';
|
|
4
4
|
export { ZENTAO_PROFILES_STORAGE_KEY, addProfile, deleteProfile, getAllProfiles, getProfile, getProfileKey, switchProfile, } from './profiles/index.js';
|
|
5
|
-
export { defineModuleActions, defineModules, type DefineModulesOptions, type ExportRegistryOptions, type ExportedModuleAction, type ExportedModuleDefinition, exportRegistry, extendModuleAction, getModuleNames, getModule, getModuleAction, getModuleActionParams, getObjectProps, } from './modules/registry.js';
|
|
5
|
+
export { defineModuleActions, defineModules, type DefineModulesOptions, type ExportRegistryOptions, type ExportedModuleAction, type ExportedModuleDefinition, exportRegistry, extendModuleAction, getModuleNames, isModuleName, getModule, getModuleAction, getModuleActionParams, getObjectProps, } from './modules/registry.js';
|
|
6
6
|
export { request, type BuiltinRequestName, type RequestParamsFor, type RequestResultFor, } from './request/index.js';
|
|
7
7
|
export { pickFields, pickFieldsSingle, filterData, searchData, sortData, processData, } from './utils/index.js';
|
|
8
8
|
export { BUILD, VERSION } from './version.js';
|
package/dist/index.js
CHANGED
|
@@ -2,7 +2,7 @@ export { ZentaoClient } from './client/index.js';
|
|
|
2
2
|
export { ERRORS, ZentaoError } from './misc/errors.js';
|
|
3
3
|
export { getGlobalOptions, setGlobalOptions } from './misc/global-options.js';
|
|
4
4
|
export { ZENTAO_PROFILES_STORAGE_KEY, addProfile, deleteProfile, getAllProfiles, getProfile, getProfileKey, switchProfile, } from './profiles/index.js';
|
|
5
|
-
export { defineModuleActions, defineModules, exportRegistry, extendModuleAction, getModuleNames, getModule, getModuleAction, getModuleActionParams, getObjectProps, } from './modules/registry.js';
|
|
5
|
+
export { defineModuleActions, defineModules, exportRegistry, extendModuleAction, getModuleNames, isModuleName, getModule, getModuleAction, getModuleActionParams, getObjectProps, } from './modules/registry.js';
|
|
6
6
|
export { request, } from './request/index.js';
|
|
7
7
|
export { pickFields, pickFieldsSingle, filterData, searchData, sortData, processData, } from './utils/index.js';
|
|
8
8
|
export { BUILD, VERSION } from './version.js';
|
package/dist/misc/errors.d.ts
CHANGED
|
@@ -13,6 +13,9 @@ export declare const ERRORS: {
|
|
|
13
13
|
readonly E_ABORTED: "Request was aborted.";
|
|
14
14
|
readonly E_INSECURE_BROWSER: "The insecure option is only supported in Node.js runtimes.";
|
|
15
15
|
readonly E_LOGIN_FAILED: "ZenTao login failed.";
|
|
16
|
+
readonly E_INVALID_ZENTAO_CONFIG: "ZenTao configuration must be an object with a non-empty version.";
|
|
17
|
+
readonly E_INVALID_ZENTAO_VERSION: "Invalid ZenTao version: {version}";
|
|
18
|
+
readonly E_UNSUPPORTED_ZENTAO_VERSION: "Action {action} does not support ZenTao {version}; minimum versions: {minVersion}";
|
|
16
19
|
readonly E_INVALID_PROFILE: "Invalid ZenTao profile.";
|
|
17
20
|
readonly E_NO_PROFILE: "No ZenTao profile is configured.";
|
|
18
21
|
readonly E_PROFILE_NOT_FOUND: "ZenTao profile not found: {profileKey}";
|
|
@@ -36,6 +39,8 @@ export declare const ERRORS: {
|
|
|
36
39
|
};
|
|
37
40
|
/** SDK 已知错误码,对应 {@link ERRORS} 的 key。 */
|
|
38
41
|
export type ErrorCode = keyof typeof ERRORS;
|
|
42
|
+
/** 仅配置获取失败可选择跳过;取消、版本错误和持久化错误仍须抛出。 @internal */
|
|
43
|
+
export declare function isZentaoConfigFetchError(error: unknown): boolean;
|
|
39
44
|
/**
|
|
40
45
|
* SDK 统一错误类型。
|
|
41
46
|
*
|
package/dist/misc/errors.js
CHANGED
|
@@ -13,6 +13,9 @@ export const ERRORS = {
|
|
|
13
13
|
E_ABORTED: 'Request was aborted.',
|
|
14
14
|
E_INSECURE_BROWSER: 'The insecure option is only supported in Node.js runtimes.',
|
|
15
15
|
E_LOGIN_FAILED: 'ZenTao login failed.',
|
|
16
|
+
E_INVALID_ZENTAO_CONFIG: 'ZenTao configuration must be an object with a non-empty version.',
|
|
17
|
+
E_INVALID_ZENTAO_VERSION: 'Invalid ZenTao version: {version}',
|
|
18
|
+
E_UNSUPPORTED_ZENTAO_VERSION: 'Action {action} does not support ZenTao {version}; minimum versions: {minVersion}',
|
|
16
19
|
E_INVALID_PROFILE: 'Invalid ZenTao profile.',
|
|
17
20
|
E_NO_PROFILE: 'No ZenTao profile is configured.',
|
|
18
21
|
E_PROFILE_NOT_FOUND: 'ZenTao profile not found: {profileKey}',
|
|
@@ -34,6 +37,11 @@ export const ERRORS = {
|
|
|
34
37
|
E_UPLOAD_FILE_TOO_LARGE: 'Upload file is {size} bytes and exceeds the {limit} byte limit.',
|
|
35
38
|
E_INVALID_UPLOAD_SOURCE: 'Invalid upload source for field: {field}',
|
|
36
39
|
};
|
|
40
|
+
/** 仅配置获取失败可选择跳过;取消、版本错误和持久化错误仍须抛出。 @internal */
|
|
41
|
+
export function isZentaoConfigFetchError(error) {
|
|
42
|
+
return error instanceof ZentaoError && (error.code === 'E_HTTP_ERROR' || error.code === 'E_NETWORK_ERROR'
|
|
43
|
+
|| error.code === 'E_TIMEOUT' || error.code === 'E_INVALID_ZENTAO_CONFIG');
|
|
44
|
+
}
|
|
37
45
|
/**
|
|
38
46
|
* SDK 统一错误类型。
|
|
39
47
|
*
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
interface ZentaoVersion {
|
|
2
|
+
edition: string;
|
|
3
|
+
parts: number[];
|
|
4
|
+
}
|
|
5
|
+
/** 只接受开源版、biz、max、ipd 系列的点分数字正式版本。 */
|
|
6
|
+
export declare function parseZentaoVersion(version: string): ZentaoVersion;
|
|
7
|
+
/** 注册和生成时共用,缺少版本、格式非法或同系列重复均拒绝。 */
|
|
8
|
+
export declare function validateMinVersion(value: unknown): asserts value is readonly string[];
|
|
9
|
+
/** 同系列逐段比较,缺少的数字段视为零;未列出的系列不支持。 */
|
|
10
|
+
export declare function supportsZentaoVersion(version: ZentaoVersion, minimums: readonly string[]): boolean;
|
|
11
|
+
export {};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { ZentaoError } from './errors.js';
|
|
2
|
+
/** 只接受开源版、biz、max、ipd 系列的点分数字正式版本。 */
|
|
3
|
+
export function parseZentaoVersion(version) {
|
|
4
|
+
const match = typeof version === 'string' ? version.trim().match(/^(biz|max|ipd)?(\d+(?:\.\d+)*)$/i) : null;
|
|
5
|
+
const parts = match?.[2].split('.').map(Number);
|
|
6
|
+
if (!match || !parts?.every(Number.isSafeInteger)) {
|
|
7
|
+
throw new ZentaoError('E_INVALID_ZENTAO_VERSION', { version: String(version) });
|
|
8
|
+
}
|
|
9
|
+
return { edition: (match[1] ?? '').toLowerCase(), parts };
|
|
10
|
+
}
|
|
11
|
+
/** 注册和生成时共用,缺少版本、格式非法或同系列重复均拒绝。 */
|
|
12
|
+
export function validateMinVersion(value) {
|
|
13
|
+
try {
|
|
14
|
+
if (!Array.isArray(value) || value.length === 0)
|
|
15
|
+
throw new Error('minVersion must be a non-empty array.');
|
|
16
|
+
const editions = Array.from(value, version => parseZentaoVersion(version).edition);
|
|
17
|
+
if (new Set(editions).size !== editions.length)
|
|
18
|
+
throw new Error('Duplicate editions in minVersion.');
|
|
19
|
+
}
|
|
20
|
+
catch (error) {
|
|
21
|
+
throw new ZentaoError('E_INVALID_ACTION_DEFINITION', undefined, error);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
/** 同系列逐段比较,缺少的数字段视为零;未列出的系列不支持。 */
|
|
25
|
+
export function supportsZentaoVersion(version, minimums) {
|
|
26
|
+
const minimum = minimums.map(parseZentaoVersion).find(item => item.edition === version.edition);
|
|
27
|
+
if (!minimum)
|
|
28
|
+
return false;
|
|
29
|
+
for (let index = 0; index < Math.max(version.parts.length, minimum.parts.length); index++) {
|
|
30
|
+
const difference = (version.parts[index] ?? 0) - (minimum.parts[index] ?? 0);
|
|
31
|
+
if (difference !== 0)
|
|
32
|
+
return difference > 0;
|
|
33
|
+
}
|
|
34
|
+
return true;
|
|
35
|
+
}
|
package/dist/modules/define.d.ts
CHANGED
|
@@ -21,7 +21,8 @@ export interface DefineModulesOptions {
|
|
|
21
21
|
*
|
|
22
22
|
* @param input - 单个或一组模块定义。
|
|
23
23
|
* @param options - 写入策略,参见 {@link DefineModulesOptions}。
|
|
24
|
-
* @throws {ZentaoError} `E_INVALID_MODULE_DEFINITION` —— 缺少 `name` 或 `actions`
|
|
24
|
+
* @throws {ZentaoError} `E_INVALID_MODULE_DEFINITION` —— 缺少 `name` 或 `actions` 字段;
|
|
25
|
+
* `E_INVALID_ACTION_DEFINITION` —— 动作字段非法或 `minVersion` 缺失、为空、格式错误、系列重复。
|
|
25
26
|
*/
|
|
26
27
|
export declare function defineModules(input: ModuleDefinition | ModuleDefinition[], options?: DefineModulesOptions): void;
|
|
27
28
|
/**
|
|
@@ -34,7 +35,7 @@ export declare function defineModules(input: ModuleDefinition | ModuleDefinition
|
|
|
34
35
|
* @param moduleName - 目标模块名(大小写不敏感)。
|
|
35
36
|
* @param input - 单个或一组动作定义。
|
|
36
37
|
* @throws {ZentaoError} `E_INVALID_MODULE`(模块未注册)、`E_INVALID_ACTION_DEFINITION`
|
|
37
|
-
* (动作缺少 `name` / `path
|
|
38
|
+
* (动作缺少 `name` / `path`、`minVersion` 无效,或 `method` / `resultType` 类型非法),或
|
|
38
39
|
* `E_INDETERMINATE_ACTION_METHOD` / `E_INDETERMINATE_ACTION_RESULT_TYPE`(省略字段且无法按 `type` 推导)。
|
|
39
40
|
*/
|
|
40
41
|
export declare function defineModuleActions(moduleName: string, input: ModuleAction | ModuleAction[]): void;
|
|
@@ -52,7 +53,7 @@ export declare function defineModuleActions(moduleName: string, input: ModuleAct
|
|
|
52
53
|
* @param actionName - 目标动作名(大小写不敏感)。
|
|
53
54
|
* @param action - 深度合并的补丁对象,或接收当前动作深克隆并返回完整动作定义的函数。
|
|
54
55
|
* @throws {ZentaoError} `E_INVALID_MODULE`(模块未注册)、`E_INVALID_ACTION`(动作不存在)、
|
|
55
|
-
* `E_INVALID_ACTION_DEFINITION`(合并结果缺少 `name` / `path
|
|
56
|
+
* `E_INVALID_ACTION_DEFINITION`(合并结果缺少 `name` / `path`、`minVersion` 无效,或 `method` / `resultType` 类型非法),
|
|
56
57
|
* 或 `E_INDETERMINATE_ACTION_METHOD` / `E_INDETERMINATE_ACTION_RESULT_TYPE`(省略字段且无法按 `type` 推导)。
|
|
57
58
|
*/
|
|
58
59
|
export declare function extendModuleAction(moduleName: string, actionName: string, action: Partial<ModuleAction> | ((action: ModuleAction) => ModuleAction)): void;
|
package/dist/modules/define.js
CHANGED
|
@@ -13,7 +13,8 @@ import { deepClone, deepMerge, findActionIndex, freezeAction, freezeModule, getM
|
|
|
13
13
|
*
|
|
14
14
|
* @param input - 单个或一组模块定义。
|
|
15
15
|
* @param options - 写入策略,参见 {@link DefineModulesOptions}。
|
|
16
|
-
* @throws {ZentaoError} `E_INVALID_MODULE_DEFINITION` —— 缺少 `name` 或 `actions`
|
|
16
|
+
* @throws {ZentaoError} `E_INVALID_MODULE_DEFINITION` —— 缺少 `name` 或 `actions` 字段;
|
|
17
|
+
* `E_INVALID_ACTION_DEFINITION` —— 动作字段非法或 `minVersion` 缺失、为空、格式错误、系列重复。
|
|
17
18
|
*/
|
|
18
19
|
export function defineModules(input, options = {}) {
|
|
19
20
|
const modules = getModulesState();
|
|
@@ -42,7 +43,7 @@ export function defineModules(input, options = {}) {
|
|
|
42
43
|
* @param moduleName - 目标模块名(大小写不敏感)。
|
|
43
44
|
* @param input - 单个或一组动作定义。
|
|
44
45
|
* @throws {ZentaoError} `E_INVALID_MODULE`(模块未注册)、`E_INVALID_ACTION_DEFINITION`
|
|
45
|
-
* (动作缺少 `name` / `path
|
|
46
|
+
* (动作缺少 `name` / `path`、`minVersion` 无效,或 `method` / `resultType` 类型非法),或
|
|
46
47
|
* `E_INDETERMINATE_ACTION_METHOD` / `E_INDETERMINATE_ACTION_RESULT_TYPE`(省略字段且无法按 `type` 推导)。
|
|
47
48
|
*/
|
|
48
49
|
export function defineModuleActions(moduleName, input) {
|
|
@@ -84,7 +85,7 @@ export function defineModuleActions(moduleName, input) {
|
|
|
84
85
|
* @param actionName - 目标动作名(大小写不敏感)。
|
|
85
86
|
* @param action - 深度合并的补丁对象,或接收当前动作深克隆并返回完整动作定义的函数。
|
|
86
87
|
* @throws {ZentaoError} `E_INVALID_MODULE`(模块未注册)、`E_INVALID_ACTION`(动作不存在)、
|
|
87
|
-
* `E_INVALID_ACTION_DEFINITION`(合并结果缺少 `name` / `path
|
|
88
|
+
* `E_INVALID_ACTION_DEFINITION`(合并结果缺少 `name` / `path`、`minVersion` 无效,或 `method` / `resultType` 类型非法),
|
|
88
89
|
* 或 `E_INDETERMINATE_ACTION_METHOD` / `E_INDETERMINATE_ACTION_RESULT_TYPE`(省略字段且无法按 `type` 推导)。
|
|
89
90
|
*/
|
|
90
91
|
export function extendModuleAction(moduleName, actionName, action) {
|