zentao-api 0.9.0 → 0.9.3

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/dist/index.d.ts CHANGED
@@ -4,6 +4,6 @@ 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
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
- export { createLink, parseLink, pickFields, pickFieldsSingle, filterData, searchData, sortData, processData, } from './utils/index.js';
7
+ export { createLink, extractFileIdFromUrl, extractFileIdsFromHtml, parseLink, pickFields, pickFieldsSingle, filterData, searchData, sortData, processData, } from './utils/index.js';
8
8
  export { BUILD, VERSION } from './version.js';
9
9
  export type * from './types/index.js';
package/dist/index.js CHANGED
@@ -4,5 +4,5 @@ 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
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
- export { createLink, parseLink, pickFields, pickFieldsSingle, filterData, searchData, sortData, processData, } from './utils/index.js';
7
+ export { createLink, extractFileIdFromUrl, extractFileIdsFromHtml, parseLink, pickFields, pickFieldsSingle, filterData, searchData, sortData, processData, } from './utils/index.js';
8
8
  export { BUILD, VERSION } from './version.js';
@@ -96,6 +96,23 @@ const requestCoderJSON = async (info) => {
96
96
  * @internal
97
97
  */
98
98
  export function applyBuiltinOverrides() {
99
+ defineModules({
100
+ name: 'setting',
101
+ display: '系统设置',
102
+ actions: [{
103
+ name: 'workHours',
104
+ minVersion: VERSION_23_MINIMUMS,
105
+ display: '获取工时设置',
106
+ description: 'dailyWorkHours 是每日可用工时,单位为小时。effortUnit 是工时展示单位,day 表示日,hour 表示小时。',
107
+ type: 'get',
108
+ method: 'get',
109
+ path: '/settings/workhours',
110
+ resultType: 'object',
111
+ resultObjectType: null,
112
+ resultGetter: { dailyWorkHours: 'dailyWorkHours', effortUnit: 'effortUnit' },
113
+ request: requestJSON,
114
+ }],
115
+ });
99
116
  // 已纳入 OpenAPI 的接口仅保留 JSON 校验及兼容响应处理。
100
117
  for (const [moduleName, actionName] of [
101
118
  ['db', 'query'], ['db', 'tables'], ['db', 'table'],
@@ -429,13 +446,13 @@ export function applyBuiltinOverrides() {
429
446
  });
430
447
  });
431
448
  defineModuleActions('my', {
432
- name: 'info',
449
+ name: 'profile',
433
450
  minVersion: VERSION_23_MINIMUMS,
434
451
  display: '获取当前用户基础资料',
435
452
  description: '返回当前 Token 用户的 id、dept、account、realname、nickname、avatar、role、type;avatar 可为空字符串或 null。',
436
453
  type: 'get',
437
454
  method: 'get',
438
- path: '/my/info',
455
+ path: '/my/profile',
439
456
  resultType: 'object',
440
457
  resultObjectType: 'user',
441
458
  resultGetter: 'user',
@@ -1,13 +1,10 @@
1
- import { parseFragment } from 'parse5';
1
+ import { findHtmlImages } from '../utils/html.js';
2
2
  function isRecord(value) {
3
3
  return value !== null && typeof value === 'object' && !Array.isArray(value);
4
4
  }
5
5
  function nonempty(value) {
6
6
  return value?.trim() || undefined;
7
7
  }
8
- function attribute(element, name) {
9
- return element.attrs.find(item => item.name === name && !item.namespace)?.value;
10
- }
11
8
  function sourceFilename(path) {
12
9
  if (/^file:(?!\/\/)/i.test(path))
13
10
  return undefined;
@@ -37,48 +34,15 @@ function sourceFilename(path) {
37
34
  }
38
35
  }
39
36
  function parseReferences(field, html) {
40
- const fragment = parseFragment(html, { sourceCodeLocationInfo: true });
41
- const stack = [...fragment.childNodes].reverse();
42
- const references = [];
43
- const seenOffsets = new Set();
44
- while (stack.length) {
45
- const node = stack.pop();
46
- if ('childNodes' in node) {
47
- for (let i = node.childNodes.length - 1; i >= 0; i--)
48
- stack.push(node.childNodes[i]);
49
- }
50
- if (!('tagName' in node))
51
- continue;
52
- if ('content' in node) {
53
- const content = node.content;
54
- for (let i = content.childNodes.length - 1; i >= 0; i--)
55
- stack.push(content.childNodes[i]);
56
- }
57
- if (node.namespaceURI !== 'http://www.w3.org/1999/xhtml' || node.tagName !== 'img')
58
- continue;
59
- const path = attribute(node, 'src');
60
- const location = node.sourceCodeLocation;
61
- const source = location?.attrs?.src;
62
- const startTag = location?.startTag;
63
- if (!path || !source || !startTag || html[startTag.endOffset - 1] !== '>' || seenOffsets.has(source.startOffset))
64
- continue;
65
- seenOffsets.add(source.startOffset);
66
- let insertOffset = startTag.endOffset - 1;
37
+ return findHtmlImages(html).map(({ src, alt, attributes, endOffset }) => {
38
+ let insertOffset = endOffset - 1;
67
39
  // An unquoted value may itself end in '/', which is not a self-closing marker.
68
40
  const slash = insertOffset - 1;
69
- if (html[slash] === '/' && !Object.values(location.attrs).some(range => range.startOffset <= slash && range.endOffset > slash)) {
41
+ if (html[slash] === '/' && !Object.values(attributes).some(range => range.startOffset <= slash && range.endOffset > slash)) {
70
42
  insertOffset = slash;
71
43
  }
72
- references.push({
73
- field,
74
- path,
75
- name: nonempty(attribute(node, 'alt')) || sourceFilename(path),
76
- attributes: location.attrs,
77
- insertOffset,
78
- });
79
- }
80
- // HTML error recovery can reorder nodes; resolver calls still follow source order.
81
- return references.sort((a, b) => a.insertOffset - b.insertOffset);
44
+ return { field, path: src, name: nonempty(alt) || sourceFilename(src), attributes, insertOffset };
45
+ });
82
46
  }
83
47
  /** 只扫描最终请求体里声明为 HTML 的顶层字符串字段,不创建浏览器 DOM。 */
84
48
  export function parseHtmlFiles(data, action) {
@@ -90,7 +54,7 @@ export function parseHtmlFiles(data, action) {
90
54
  if (!isRecord(schema) || (schema.textType !== 'html' && !(typeof schema.description === 'string' && schema.description.endsWith('(HTML)'))))
91
55
  continue;
92
56
  const html = data[field];
93
- if (typeof html !== 'string' || !/<img(?=[\t\n\f\r />])/i.test(html))
57
+ if (typeof html !== 'string')
94
58
  continue;
95
59
  const references = parseReferences(field, html);
96
60
  if (references.length)
@@ -1,10 +1,10 @@
1
- import type { DataRecord, FileCreateParams, FileCreateResult, RequestOptions, ResponseData } from '../types/index.js';
1
+ import type { DataRecord, FileCreateParams, FileCreateResult, RequestOptions, ResponseData, WorkHourSettings } from '../types/index.js';
2
2
  import type { BuiltinActionMeta, BuiltinModuleName } from '../modules/generated.js';
3
3
  type BuiltinActionName<M extends BuiltinModuleName> = Extract<keyof BuiltinActionMeta[M], string>;
4
4
  type BuiltinListRequestName = BuiltinModuleName;
5
5
  type BuiltinNamedRequestName = {
6
6
  [M in BuiltinModuleName]: `${M}/${BuiltinActionName<M>}`;
7
- }[BuiltinModuleName] | 'my/info' | 'file/get' | 'file/read';
7
+ }[BuiltinModuleName] | 'my/profile' | 'file/get' | 'file/read' | 'setting/workHours';
8
8
  type BuiltinIdRequestName = `${BuiltinModuleName}/${number}`;
9
9
  /** 内置模块支持的请求名:`module`、`module/action` 或 `module/123`。 */
10
10
  export type BuiltinRequestName = BuiltinListRequestName | BuiltinNamedRequestName | BuiltinIdRequestName;
@@ -16,7 +16,7 @@ type ActionMetaOf<Name extends BuiltinRequestName> = ModuleNameOf<Name> extends
16
16
  /** 根据内置请求名推导出的参数类型。字段细节以运行时 registry 为准,这里保持可扩展。 */
17
17
  export type RequestParamsFor<Name extends BuiltinRequestName> = Name extends 'file/create' ? FileCreateParams : Record<string, unknown>;
18
18
  /** 根据内置请求名推导出的 `ResponseData.data` 类型。 */
19
- export type RequestResultFor<Name extends BuiltinRequestName> = ActionMetaOf<Name> extends {
19
+ export type RequestResultFor<Name extends BuiltinRequestName> = Name extends 'setting/workHours' ? WorkHourSettings : ActionMetaOf<Name> extends {
20
20
  resultType: 'list';
21
21
  } ? DataRecord[] : Name extends 'file/create' ? FileCreateResult : Name extends 'file/read' ? ArrayBuffer | string : ActionMetaOf<Name> extends {
22
22
  resultType: 'object';
@@ -183,7 +183,7 @@ export interface ModuleAction {
183
183
  request?: ModuleActionRequestCallback;
184
184
  }
185
185
  /** 内置模块名称,同时允许用户扩展自定义模块名。 */
186
- export type ModuleName = 'user' | 'program' | 'product' | 'project' | 'execution' | 'productplan' | 'story' | 'epic' | 'requirement' | 'bug' | 'testcase' | 'task' | 'feedback' | 'ticket' | 'system' | 'build' | 'testtask' | 'release' | 'file' | (string & {});
186
+ export type ModuleName = 'user' | 'program' | 'product' | 'project' | 'execution' | 'productplan' | 'story' | 'epic' | 'requirement' | 'bug' | 'testcase' | 'task' | 'feedback' | 'ticket' | 'system' | 'setting' | 'build' | 'testtask' | 'release' | 'file' | (string & {});
187
187
  /** 禅道模块定义,由多个动作组成。 */
188
188
  export interface ModuleDefinition {
189
189
  /** 模块名称,例如 `product`、`bug`。 */
@@ -55,6 +55,13 @@ export interface ApiListResponse extends ApiResponse {
55
55
  /** 原始分页信息。 */
56
56
  pager?: Pager;
57
57
  }
58
+ /** 禅道系统的工时设置。 */
59
+ export interface WorkHourSettings {
60
+ /** 每日可用工时,单位为小时。 */
61
+ dailyWorkHours: number;
62
+ /** 工时展示单位,`day` 表示日,`hour` 表示小时。 */
63
+ effortUnit: 'day' | 'hour';
64
+ }
58
65
  /** 登录接口响应结构。 */
59
66
  export interface LoginResponse extends ApiResponse {
60
67
  /** 登录成功后返回的 API Token。 */
@@ -0,0 +1,18 @@
1
+ import { type Token } from 'parse5';
2
+ interface HtmlImage {
3
+ src: string;
4
+ alt?: string;
5
+ attributes: Record<string, Token.Location>;
6
+ endOffset: number;
7
+ }
8
+ /** @internal 按源码顺序收集图片属性和起始标签位置。 */
9
+ export declare function findHtmlImages(html: string): HtmlImage[];
10
+ /**
11
+ * 按富文本 HTML 中 `<img>` 的出现顺序提取禅道图片引用,保留重复项。
12
+ *
13
+ * @param html - 禅道富文本 HTML。
14
+ * @param siteUrl - 禅道站点根地址,含安装目录,用于匹配带域名的图片地址。
15
+ * @returns 每项为 `[imgSrc, zentaoFileID]`。src 使用 HTML 实体解码后的属性值,保留相对路径。没有匹配图片时返回空数组。
16
+ */
17
+ export declare function extractFileIdsFromHtml(html: string, siteUrl: string): [imgSrc: string, zentaoFileID: number][];
18
+ export {};
@@ -0,0 +1,55 @@
1
+ import { parseFragment } from 'parse5';
2
+ import { parseFileIdFromUrl } from './url.js';
3
+ function attribute(element, name) {
4
+ return element.attrs.find(item => item.name === name && !item.namespace)?.value;
5
+ }
6
+ /** @internal 按源码顺序收集图片属性和起始标签位置。 */
7
+ export function findHtmlImages(html) {
8
+ if (typeof html !== 'string' || !/<img(?=[\t\n\f\r />])/i.test(html))
9
+ return [];
10
+ const fragment = parseFragment(html, { sourceCodeLocationInfo: true });
11
+ const stack = [...fragment.childNodes].reverse();
12
+ const images = [];
13
+ const seenOffsets = new Set();
14
+ while (stack.length) {
15
+ const node = stack.pop();
16
+ if ('childNodes' in node) {
17
+ for (let i = node.childNodes.length - 1; i >= 0; i--)
18
+ stack.push(node.childNodes[i]);
19
+ }
20
+ if (!('tagName' in node))
21
+ continue;
22
+ if ('content' in node) {
23
+ const content = node.content;
24
+ for (let i = content.childNodes.length - 1; i >= 0; i--)
25
+ stack.push(content.childNodes[i]);
26
+ }
27
+ if (node.namespaceURI !== 'http://www.w3.org/1999/xhtml' || node.tagName !== 'img')
28
+ continue;
29
+ const src = attribute(node, 'src');
30
+ const attributes = node.sourceCodeLocation?.attrs;
31
+ const startTag = node.sourceCodeLocation?.startTag;
32
+ if (!src || !attributes?.src || !startTag || html[startTag.endOffset - 1] !== '>' || seenOffsets.has(startTag.startOffset))
33
+ continue;
34
+ seenOffsets.add(startTag.startOffset);
35
+ images.push({ src, alt: attribute(node, 'alt'), attributes, endOffset: startTag.endOffset });
36
+ }
37
+ // HTML error recovery can move elements ahead of earlier source tags.
38
+ return images.sort((a, b) => a.endOffset - b.endOffset);
39
+ }
40
+ /**
41
+ * 按富文本 HTML 中 `<img>` 的出现顺序提取禅道图片引用,保留重复项。
42
+ *
43
+ * @param html - 禅道富文本 HTML。
44
+ * @param siteUrl - 禅道站点根地址,含安装目录,用于匹配带域名的图片地址。
45
+ * @returns 每项为 `[imgSrc, zentaoFileID]`。src 使用 HTML 实体解码后的属性值,保留相对路径。没有匹配图片时返回空数组。
46
+ */
47
+ export function extractFileIdsFromHtml(html, siteUrl) {
48
+ const files = [];
49
+ for (const { src } of findHtmlImages(html)) {
50
+ const id = parseFileIdFromUrl(src, siteUrl, 'imageSrc');
51
+ if (id !== undefined)
52
+ files.push([src, id]);
53
+ }
54
+ return files;
55
+ }
@@ -1,5 +1,6 @@
1
1
  export { isRecord, isBlank, getNestedValue } from './object.js';
2
2
  export { asArray } from './array.js';
3
- export { normalizeSiteUrl } from './url.js';
3
+ export { extractFileIdFromUrl, normalizeSiteUrl } from './url.js';
4
+ export { extractFileIdsFromHtml } from './html.js';
4
5
  export { createLink, parseLink } from './link.js';
5
6
  export { pickFields, pickFieldsSingle, filterData, searchData, sortData, processData, } from './data.js';
@@ -1,5 +1,6 @@
1
1
  export { isRecord, isBlank, getNestedValue } from './object.js';
2
2
  export { asArray } from './array.js';
3
- export { normalizeSiteUrl } from './url.js';
3
+ export { extractFileIdFromUrl, normalizeSiteUrl } from './url.js';
4
+ export { extractFileIdsFromHtml } from './html.js';
4
5
  export { createLink, parseLink } from './link.js';
5
6
  export { pickFields, pickFieldsSingle, filterData, searchData, sortData, processData, } from './data.js';
@@ -1,2 +1,12 @@
1
1
  /** 将用户传入的站点根地址规范化,兼容误传入 `/api.php/v2` 的场景。 */
2
2
  export declare function normalizeSiteUrl(baseUrl: string): string;
3
+ /**
4
+ * 从禅道图片或附件下载链接提取文件 ID,支持路径路由和 GET 查询路由。
5
+ *
6
+ * @param url - file/read 或 file/download 的相对路径或 HTTP(S) 地址。查询参数可使用 `&amp;` 分隔。
7
+ * @param siteUrl - 禅道站点根地址,含安装目录。解析带域名的地址时必填。
8
+ * @returns 正的安全整数 ID。地址无效、站点或路由不匹配时返回 `undefined`。
9
+ */
10
+ export declare function extractFileIdFromUrl(url: string, siteUrl?: string): number | undefined;
11
+ /** @internal An HTML image src is already entity-decoded and identifies only a read route. */
12
+ export declare function parseFileIdFromUrl(url: string, siteUrl: string | undefined, source: 'fileUrl' | 'imageSrc'): number | undefined;
package/dist/utils/url.js CHANGED
@@ -17,3 +17,56 @@ export function normalizeSiteUrl(baseUrl) {
17
17
  }
18
18
  return parsed.toString().replace(/\/+$/, '');
19
19
  }
20
+ /**
21
+ * 从禅道图片或附件下载链接提取文件 ID,支持路径路由和 GET 查询路由。
22
+ *
23
+ * @param url - file/read 或 file/download 的相对路径或 HTTP(S) 地址。查询参数可使用 `&amp;` 分隔。
24
+ * @param siteUrl - 禅道站点根地址,含安装目录。解析带域名的地址时必填。
25
+ * @returns 正的安全整数 ID。地址无效、站点或路由不匹配时返回 `undefined`。
26
+ */
27
+ export function extractFileIdFromUrl(url, siteUrl) {
28
+ return parseFileIdFromUrl(url, siteUrl, 'fileUrl');
29
+ }
30
+ /** @internal An HTML image src is already entity-decoded and identifies only a read route. */
31
+ export function parseFileIdFromUrl(url, siteUrl, source) {
32
+ const src = typeof url === 'string' ? url.trim() : '';
33
+ if (!src || /[\\\u0000-\u001f]/.test(src))
34
+ return undefined;
35
+ let parsed;
36
+ try {
37
+ let base = 'https://zentao.invalid/';
38
+ if (/^(?:https?:)?\/\//i.test(src)) {
39
+ if (!siteUrl)
40
+ return undefined;
41
+ base = `${normalizeSiteUrl(siteUrl)}/`;
42
+ }
43
+ else if (/^[a-z][a-z\d+.-]*:/i.test(src)) {
44
+ return undefined;
45
+ }
46
+ parsed = new URL(src, base);
47
+ if (!parsed.href.startsWith(base))
48
+ return undefined;
49
+ }
50
+ catch {
51
+ return undefined;
52
+ }
53
+ let digits = /\/file-read-(\d+)\.[A-Za-z0-9]+$/.exec(parsed.pathname)?.[1];
54
+ if (!digits && source === 'fileUrl')
55
+ digits = /\/file-download-(\d+)(?:-left)?\.html$/.exec(parsed.pathname)?.[1];
56
+ if (!digits) {
57
+ if (!parsed.pathname.endsWith('/') && !parsed.pathname.endsWith('/index.php'))
58
+ return undefined;
59
+ const params = source === 'fileUrl' && parsed.search.includes('&amp;')
60
+ ? new URLSearchParams(parsed.search.replaceAll('&amp;', '&'))
61
+ : parsed.searchParams;
62
+ const matchesMethod = (method) => method === 'read' || (source === 'fileUrl' && method === 'download');
63
+ if (!(params.get('module') === 'file' && matchesMethod(params.get('method')))
64
+ && !(params.get('m') === 'file' && matchesMethod(params.get('f'))))
65
+ return undefined;
66
+ digits = params.get('fileID') ?? '';
67
+ if (!/^\d+$/.test(digits))
68
+ return undefined;
69
+ }
70
+ const id = Number(digits);
71
+ return Number.isSafeInteger(id) && id > 0 ? id : undefined;
72
+ }
package/dist/version.js CHANGED
@@ -1,5 +1,5 @@
1
- const fallbackBuild = "2026-11-18T10:11:03.227Z";
2
- const fallbackVersion = "0.9.0";
1
+ const fallbackBuild = "2026-11-19T07:59:08.352Z";
2
+ const fallbackVersion = "0.9.3";
3
3
  /**
4
4
  * 构建标识,由构建脚本通过 `__ZENTAO_API_BUILD__` 注入。
5
5
  *
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zentao-api",
3
- "version": "0.9.0",
3
+ "version": "0.9.3",
4
4
  "type": "module",
5
5
  "description": "Browser and Node.js SDK for ZenTao API",
6
6
  "license": "MIT",