zentao-api 0.8.5 → 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.
- package/README.md +48 -2
- package/dist/browser/zentao-api.global.js +28 -28
- package/dist/client/index.d.ts +4 -0
- package/dist/client/index.js +10 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/misc/errors.d.ts +4 -0
- package/dist/misc/errors.js +4 -0
- package/dist/misc/zentao-version.d.ts +3 -0
- package/dist/misc/zentao-version.js +9 -0
- package/dist/modules/define.d.ts +1 -0
- package/dist/modules/define.js +3 -2
- package/dist/modules/generated.d.ts +368 -0
- package/dist/modules/generated.js +421 -53
- package/dist/modules/object-props.js +212 -36
- package/dist/modules/override.js +93 -9
- package/dist/modules/query.d.ts +2 -0
- package/dist/modules/query.js +6 -0
- package/dist/modules/registry-store.d.ts +4 -4
- package/dist/modules/registry-store.js +15 -8
- package/dist/modules/result-object-type.d.ts +7 -0
- package/dist/modules/result-object-type.js +35 -0
- package/dist/request/html-file-request.d.ts +34 -0
- package/dist/request/html-file-request.js +366 -0
- package/dist/request/html-files.d.ts +28 -0
- package/dist/request/html-files.js +139 -0
- package/dist/request/index.d.ts +3 -3
- package/dist/request/index.js +78 -26
- package/dist/request/prepare-body.d.ts +2 -0
- package/dist/request/prepare-body.js +2 -2
- package/dist/types/index.d.ts +2 -0
- package/dist/types/index.js +2 -0
- package/dist/types/link.d.ts +52 -0
- package/dist/types/link.js +1 -0
- package/dist/types/module.d.ts +18 -0
- package/dist/types/options.d.ts +7 -4
- package/dist/types/response.d.ts +16 -0
- package/dist/types/upload.d.ts +64 -2
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/index.js +1 -0
- package/dist/utils/link.d.ts +30 -0
- package/dist/utils/link.js +225 -0
- package/dist/version.js +2 -2
- package/package.json +5 -2
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
import { ZentaoError } from '../misc/errors.js';
|
|
2
|
+
import { getGlobalOptions } from '../misc/global-options.js';
|
|
3
|
+
import { normalizeSiteUrl } from './url.js';
|
|
4
|
+
function requiredString(value, param) {
|
|
5
|
+
if (value === undefined || value === null || (typeof value === 'string' && !value.trim())) {
|
|
6
|
+
throw new ZentaoError('E_MISSING_PARAM', { param });
|
|
7
|
+
}
|
|
8
|
+
if (typeof value !== 'string')
|
|
9
|
+
throw new ZentaoError('E_INVALID_PARAM', { param, value: String(value) });
|
|
10
|
+
return value;
|
|
11
|
+
}
|
|
12
|
+
function configString(config, name, fallback) {
|
|
13
|
+
const value = config?.[name];
|
|
14
|
+
if (value === undefined && fallback !== undefined)
|
|
15
|
+
return fallback;
|
|
16
|
+
// defaultView 可以为空,表示路径链接不添加视图扩展名。
|
|
17
|
+
if (value === '' && (name === 'defaultView' || name === 'instCode'))
|
|
18
|
+
return value;
|
|
19
|
+
return requiredString(value, `profile.serverConfig.${name}`);
|
|
20
|
+
}
|
|
21
|
+
function requestType(value, param, allowAuto = false) {
|
|
22
|
+
const type = requiredString(value, param);
|
|
23
|
+
if (type === 'GET' || type === 'PATH_INFO' || type === 'PATH_INFO2' || (allowAuto && type === 'auto'))
|
|
24
|
+
return type;
|
|
25
|
+
throw new ZentaoError('E_INVALID_PARAM', { param, value: type });
|
|
26
|
+
}
|
|
27
|
+
function sitePath(value, base, param) {
|
|
28
|
+
let url;
|
|
29
|
+
try {
|
|
30
|
+
url = new URL(value, base);
|
|
31
|
+
}
|
|
32
|
+
catch {
|
|
33
|
+
throw new ZentaoError('E_INVALID_PARAM', { param, value });
|
|
34
|
+
}
|
|
35
|
+
if (url.protocol !== base.protocol || url.origin !== base.origin || url.search || url.hash || url.username || url.password) {
|
|
36
|
+
throw new ZentaoError('E_INVALID_PARAM', { param, value });
|
|
37
|
+
}
|
|
38
|
+
return url;
|
|
39
|
+
}
|
|
40
|
+
function linkContext(profile) {
|
|
41
|
+
const site = new URL(`${normalizeSiteUrl(requiredString(profile.server, 'profile.server'))}/`);
|
|
42
|
+
const config = profile.serverConfig;
|
|
43
|
+
const root = sitePath(configString(config, 'webRoot', site.pathname), site, 'profile.serverConfig.webRoot');
|
|
44
|
+
root.pathname = `${root.pathname.replace(/\/+$/, '')}/`;
|
|
45
|
+
const instCode = configString(config, 'instCode', '').replace(/^\/+|\/+$/g, '');
|
|
46
|
+
if (instCode) {
|
|
47
|
+
const instancePath = sitePath(`/${instCode}/`, root, 'profile.serverConfig.instCode').pathname;
|
|
48
|
+
if (!root.pathname.endsWith(instancePath))
|
|
49
|
+
root.pathname += instancePath.slice(1);
|
|
50
|
+
}
|
|
51
|
+
const router = sitePath(configString(config, 'router', 'index.php'), root, 'profile.serverConfig.router').pathname;
|
|
52
|
+
return { config, root, router, defaultView: configString(config, 'defaultView', 'html') };
|
|
53
|
+
}
|
|
54
|
+
function normalizeVars(vars) {
|
|
55
|
+
if (!vars)
|
|
56
|
+
return [];
|
|
57
|
+
return (typeof vars === 'string' ? vars.split('&') : vars).map((item) => {
|
|
58
|
+
if (typeof item !== 'string')
|
|
59
|
+
return [item[0], item[1]];
|
|
60
|
+
const equals = item.indexOf('=');
|
|
61
|
+
return equals < 0 ? [item, ''] : [item.slice(0, equals), item.slice(equals + 1)];
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
function parseParams(search) {
|
|
65
|
+
const values = new Map();
|
|
66
|
+
search.forEach((value, name) => {
|
|
67
|
+
const group = values.get(name);
|
|
68
|
+
if (group)
|
|
69
|
+
group.push(value);
|
|
70
|
+
else
|
|
71
|
+
values.set(name, [value]);
|
|
72
|
+
});
|
|
73
|
+
// 单次聚合保持线性成本;无原型字典避免自定义路由参数名命中继承属性。
|
|
74
|
+
const params = Object.create(null);
|
|
75
|
+
for (const [name, group] of values)
|
|
76
|
+
params[name] = group.join(',');
|
|
77
|
+
return params;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* 根据 profile 的服务器配置创建禅道页面链接,同步执行且不访问存储或网络。
|
|
81
|
+
*
|
|
82
|
+
* 支持 GET、PATH_INFO、PATH_INFO2。`relative` 优先级为单次选项 > 全局选项 > false(完整网址);true 返回站内路径。
|
|
83
|
+
* `vars` 和 `params` 沿用页面 helper 的约定:调用方按需编码,SDK 不二次编码;不修改输入。
|
|
84
|
+
*
|
|
85
|
+
* @param profile - 使用 server 与 serverConfig,无需账号或 Token。
|
|
86
|
+
* @param options - 页面模块、方法、参数与输出选项。
|
|
87
|
+
* @returns 完整网址或站内路径。
|
|
88
|
+
* @throws {ZentaoError} `E_MISSING_PARAM`(缺少必要路由配置或页面参数)、
|
|
89
|
+
* `E_INVALID_PARAM`(不支持的路由模式或非法配置路径)、`E_INVALID_BASE_URL`。
|
|
90
|
+
*/
|
|
91
|
+
export function createLink(profile, options) {
|
|
92
|
+
const { config, root, router, defaultView } = linkContext(profile);
|
|
93
|
+
const type = requestType(config?.requestType, 'profile.serverConfig.requestType');
|
|
94
|
+
const moduleName = requiredString(options.moduleName, 'moduleName');
|
|
95
|
+
const methodName = requiredString(options.methodName, 'methodName');
|
|
96
|
+
const vars = normalizeVars(options.vars);
|
|
97
|
+
const viewType = options.viewType || defaultView;
|
|
98
|
+
const parts = [];
|
|
99
|
+
let hasQuery = type === 'GET';
|
|
100
|
+
if (type === 'GET') {
|
|
101
|
+
const moduleVar = configString(config, 'moduleVar');
|
|
102
|
+
const methodVar = configString(config, 'methodVar');
|
|
103
|
+
const viewVar = configString(config, 'viewVar');
|
|
104
|
+
parts.push(router, '?', moduleVar, '=', moduleName, '&', methodVar, '=', methodName);
|
|
105
|
+
for (const [name, value] of vars)
|
|
106
|
+
parts.push('&', name, '=', value);
|
|
107
|
+
if (viewType && viewType !== defaultView)
|
|
108
|
+
parts.push('&', viewVar, '=', viewType);
|
|
109
|
+
}
|
|
110
|
+
else {
|
|
111
|
+
const fix = configString(config, 'requestFix');
|
|
112
|
+
parts.push(type === 'PATH_INFO2' ? `${router.replace(/\/+$/, '')}/` : root.pathname, moduleName, fix, methodName);
|
|
113
|
+
for (const [, value] of vars)
|
|
114
|
+
parts.push(fix, value);
|
|
115
|
+
if (viewType)
|
|
116
|
+
parts.push('.', viewType);
|
|
117
|
+
}
|
|
118
|
+
for (const [name, value] of Object.entries(options.params ?? {})) {
|
|
119
|
+
if (name.startsWith('$'))
|
|
120
|
+
continue;
|
|
121
|
+
parts.push(hasQuery ? '&' : '?', name, '=', value == null ? '' : String(value));
|
|
122
|
+
hasQuery = true;
|
|
123
|
+
}
|
|
124
|
+
if (typeof options.hash === 'string')
|
|
125
|
+
parts.push(options.hash.startsWith('#') ? '' : '#', options.hash);
|
|
126
|
+
const relative = options.relative ?? getGlobalOptions().relative ?? false;
|
|
127
|
+
return (relative ? '' : root.origin) + parts.join('');
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* 解析禅道页面链接,同步执行且不读取浏览器当前位置、存储或网络。
|
|
131
|
+
*
|
|
132
|
+
* 相对链接以 profile 的页面根路径为基准;同源页面目录和显式配置的入口路径属于内部链接。
|
|
133
|
+
* 其他 origin、这些路径之外及非 HTTP(S) 链接标记为外链。
|
|
134
|
+
* 默认使用 serverConfig.requestType,`requestType: 'auto'` 根据路由参数与入口路径识别。
|
|
135
|
+
* GET 查询值会解码,同名值以逗号合并;路径变量保留编码并附加到 params 的 `$1`、`$2` 等键。
|
|
136
|
+
*
|
|
137
|
+
* @param profile - 使用 server 与 serverConfig,无需账号或 Token。
|
|
138
|
+
* @param url - 完整网址或相对链接;空输入直接返回 `{ url }`。
|
|
139
|
+
* @param options - 可覆盖路由模式或启用自动识别。
|
|
140
|
+
* @returns 链接信息;外链仅包含原 url 和 external 标记。
|
|
141
|
+
* @throws {ZentaoError} `E_MISSING_PARAM`(缺少必要路由配置)、`E_INVALID_PARAM`
|
|
142
|
+
* (不支持的路由模式或非法 URL/配置路径)、`E_INVALID_BASE_URL`。
|
|
143
|
+
*/
|
|
144
|
+
export function parseLink(profile, url, options = {}) {
|
|
145
|
+
if (!url)
|
|
146
|
+
return { url };
|
|
147
|
+
const { config, root, router, defaultView } = linkContext(profile);
|
|
148
|
+
let resolved;
|
|
149
|
+
try {
|
|
150
|
+
resolved = new URL(url, root);
|
|
151
|
+
}
|
|
152
|
+
catch {
|
|
153
|
+
throw new ZentaoError('E_INVALID_PARAM', { param: 'url', value: url });
|
|
154
|
+
}
|
|
155
|
+
const routerPrefix = `${router.replace(/\/+$/, '')}/`;
|
|
156
|
+
const atRoot = resolved.pathname === root.pathname || resolved.pathname === root.pathname.slice(0, -1);
|
|
157
|
+
const atRouter = resolved.pathname === router || resolved.pathname === routerPrefix.slice(0, -1);
|
|
158
|
+
const inRouter = atRouter || resolved.pathname.startsWith(routerPrefix);
|
|
159
|
+
if (resolved.origin !== root.origin || !['http:', 'https:'].includes(resolved.protocol)
|
|
160
|
+
|| (!atRoot && !resolved.pathname.startsWith(root.pathname) && !inRouter)) {
|
|
161
|
+
return { external: true, url };
|
|
162
|
+
}
|
|
163
|
+
let type = options.requestType === undefined
|
|
164
|
+
? requestType(config?.requestType, 'profile.serverConfig.requestType')
|
|
165
|
+
: requestType(options.requestType, 'requestType', true);
|
|
166
|
+
if (type === 'auto') {
|
|
167
|
+
const moduleVar = configString(config, 'moduleVar');
|
|
168
|
+
const methodVar = configString(config, 'methodVar');
|
|
169
|
+
// 已有路径路由不受附加查询参数中的 m/f(或自定义名称)影响。
|
|
170
|
+
if (atRouter)
|
|
171
|
+
type = 'GET';
|
|
172
|
+
else if (inRouter)
|
|
173
|
+
type = 'PATH_INFO2';
|
|
174
|
+
else if (atRoot && (resolved.searchParams.has(moduleVar) || resolved.searchParams.has(methodVar)))
|
|
175
|
+
type = 'GET';
|
|
176
|
+
else
|
|
177
|
+
type = 'PATH_INFO';
|
|
178
|
+
}
|
|
179
|
+
const params = parseParams(resolved.searchParams);
|
|
180
|
+
const vars = [];
|
|
181
|
+
const link = {
|
|
182
|
+
url: /^(?:[a-z][a-z\d+.-]*:)?\/\//i.test(url) ? resolved.href.replace(/^[^:]+:\/\/[^/]+/, '') : url,
|
|
183
|
+
isOnlyBody: params.onlybody === 'yes',
|
|
184
|
+
vars,
|
|
185
|
+
hash: resolved.hash.slice(1),
|
|
186
|
+
params,
|
|
187
|
+
tid: params.tid || '',
|
|
188
|
+
requestType: type,
|
|
189
|
+
};
|
|
190
|
+
if (type === 'GET') {
|
|
191
|
+
const moduleVar = configString(config, 'moduleVar');
|
|
192
|
+
const methodVar = configString(config, 'methodVar');
|
|
193
|
+
const viewVar = configString(config, 'viewVar');
|
|
194
|
+
link.moduleName = params[moduleVar] || 'index';
|
|
195
|
+
link.methodName = params[methodVar] || 'index';
|
|
196
|
+
link.viewType = params[viewVar] || defaultView;
|
|
197
|
+
for (const [name, value] of Object.entries(params)) {
|
|
198
|
+
if (name !== moduleVar && name !== methodVar && name !== viewVar && name !== 'onlybody' && name !== 'tid') {
|
|
199
|
+
vars.push([name, value]);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
else {
|
|
204
|
+
const fix = configString(config, 'requestFix');
|
|
205
|
+
let path = resolved.pathname;
|
|
206
|
+
if (atRoot || atRouter || path === routerPrefix)
|
|
207
|
+
path = '';
|
|
208
|
+
else if (fix.includes('/'))
|
|
209
|
+
path = path.slice((inRouter ? routerPrefix : root.pathname).length);
|
|
210
|
+
else {
|
|
211
|
+
path = path.replace(/\/+$/, '');
|
|
212
|
+
path = path.slice(path.lastIndexOf('/') + 1);
|
|
213
|
+
}
|
|
214
|
+
const dot = path.lastIndexOf('.');
|
|
215
|
+
link.viewType = dot < 0 ? defaultView : path.slice(dot + 1);
|
|
216
|
+
const segments = (dot < 0 ? path : path.slice(0, dot)).split(fix);
|
|
217
|
+
link.moduleName = segments[0] || 'index';
|
|
218
|
+
link.methodName = segments[1] || 'index';
|
|
219
|
+
for (let i = 2; i < segments.length; i++) {
|
|
220
|
+
vars.push(['', segments[i]]);
|
|
221
|
+
params[`$${i - 1}`] = segments[i];
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
return link;
|
|
225
|
+
}
|
package/dist/version.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "zentao-api",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.9.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Browser and Node.js SDK for ZenTao API",
|
|
6
6
|
"license": "MIT",
|
|
@@ -74,5 +74,8 @@
|
|
|
74
74
|
"node": ">=18",
|
|
75
75
|
"bun": ">=1.0.0"
|
|
76
76
|
},
|
|
77
|
-
"packageManager": "bun@1.3.13"
|
|
77
|
+
"packageManager": "bun@1.3.13",
|
|
78
|
+
"dependencies": {
|
|
79
|
+
"parse5": "7.3.0"
|
|
80
|
+
}
|
|
78
81
|
}
|