zentao-api 0.1.0 → 0.2.0-beta.1
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/LICENSE +2 -2
- package/README.md +103 -147
- package/dist/browser/zentao-api.global.js +1 -0
- package/dist/browser.d.ts +1 -0
- package/dist/browser.js +1 -0
- package/dist/client/index.d.ts +37 -0
- package/dist/client/index.js +149 -0
- package/dist/index.d.ts +7 -4
- package/dist/index.js +6 -8
- package/dist/misc/browser-global.d.ts +1 -0
- package/dist/misc/browser-global.js +8 -0
- package/dist/misc/environment.d.ts +6 -0
- package/dist/misc/environment.js +30 -0
- package/dist/misc/errors.d.ts +25 -0
- package/dist/misc/errors.js +35 -0
- package/dist/misc/global-options.d.ts +5 -0
- package/dist/misc/global-options.js +9 -0
- package/dist/modules/generated.d.ts +8 -0
- package/dist/modules/generated.js +4226 -0
- package/dist/modules/registry.d.ts +22 -0
- package/dist/modules/registry.js +129 -0
- package/dist/modules/resolve.d.ts +7 -0
- package/dist/modules/resolve.js +196 -0
- package/dist/request/index.d.ts +7 -0
- package/dist/request/index.js +65 -0
- package/dist/types/index.d.ts +235 -0
- package/dist/types/index.js +1 -0
- package/dist/utils/index.d.ts +2 -0
- package/dist/utils/index.js +14 -0
- package/dist/version.d.ts +2 -0
- package/dist/version.js +4 -0
- package/package.json +43 -77
- package/dist/types.d.ts +0 -70
- package/dist/utils.d.ts +0 -93
- package/dist/zentao-api.cjs.development.js +0 -3619
- package/dist/zentao-api.cjs.development.js.map +0 -1
- package/dist/zentao-api.cjs.production.min.js +0 -2
- package/dist/zentao-api.cjs.production.min.js.map +0 -1
- package/dist/zentao-api.esm.js +0 -3611
- package/dist/zentao-api.esm.js.map +0 -1
- package/dist/zentao-config.d.ts +0 -93
- package/dist/zentao-request-builder.d.ts +0 -120
- package/dist/zentao.d.ts +0 -175
- package/dist/zentao12.d.ts +0 -676
- package/src/index.ts +0 -5
- package/src/types.ts +0 -88
- package/src/utils.ts +0 -216
- package/src/zentao-config.ts +0 -150
- package/src/zentao-request-builder.ts +0 -227
- package/src/zentao.ts +0 -596
- package/src/zentao12.ts +0 -1272
package/src/types.ts
DELETED
|
@@ -1,88 +0,0 @@
|
|
|
1
|
-
import {Method} from 'axios';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* 支持的禅道请求方式,影响 API 请求 URL 构建方式
|
|
5
|
-
*/
|
|
6
|
-
export type ZentaoRequestType = 'PATH_INFO' | 'GET';
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* 禅道请求参数键值对
|
|
10
|
-
*/
|
|
11
|
-
export type ZentaoRequestParamPair = any[]; // [name: string, value: string]
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* 禅道请求参数
|
|
15
|
-
*/
|
|
16
|
-
export type ZentaoRequestParams =
|
|
17
|
-
| Array<ZentaoRequestParamPair>
|
|
18
|
-
| string[]
|
|
19
|
-
| string
|
|
20
|
-
| Record<string, any>;
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* 禅道 API 请求方式
|
|
24
|
-
*/
|
|
25
|
-
export type ZentaoRequestMethod = Method;
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* 禅道 API 返回结果
|
|
29
|
-
*/
|
|
30
|
-
export interface ZentaoApiResult {
|
|
31
|
-
/**
|
|
32
|
-
* 状态
|
|
33
|
-
*
|
|
34
|
-
* @remarks
|
|
35
|
-
* 如果为 `0` 则表示操作请求失败,如果为 `1`,表示操作请求成功
|
|
36
|
-
*/
|
|
37
|
-
status: 0 | 1;
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* 服务器返回的描述结果的文本
|
|
41
|
-
*/
|
|
42
|
-
msg?: any;
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* 请求结果数据
|
|
46
|
-
*/
|
|
47
|
-
result?: any;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* 禅道 API 初始化选项
|
|
52
|
-
*/
|
|
53
|
-
export interface ZentaoOptions {
|
|
54
|
-
/**
|
|
55
|
-
* 禅道服务器地址
|
|
56
|
-
*/
|
|
57
|
-
readonly url: string;
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* 登录账号
|
|
61
|
-
*/
|
|
62
|
-
readonly account: string;
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* 登录密码
|
|
66
|
-
*/
|
|
67
|
-
readonly password: string;
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* 请求形式
|
|
71
|
-
*/
|
|
72
|
-
readonly accessMode?: ZentaoRequestType;
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* 是否将 token 存储到本地,如果设置为 `false`,则每次创建新的 `Zentao` 实例都会在首次调用 API 之前重新获取 Token
|
|
76
|
-
*/
|
|
77
|
-
readonly preserveToken?: boolean;
|
|
78
|
-
|
|
79
|
-
/**
|
|
80
|
-
* 当前 `Zentao` 实例名称
|
|
81
|
-
*/
|
|
82
|
-
readonly sessionName?: string;
|
|
83
|
-
|
|
84
|
-
/**
|
|
85
|
-
* 如果设置为 `true`,则会在控制台输出详细日志
|
|
86
|
-
*/
|
|
87
|
-
readonly debug?: boolean;
|
|
88
|
-
}
|
package/src/utils.ts
DELETED
|
@@ -1,216 +0,0 @@
|
|
|
1
|
-
import querystring from 'querystring';
|
|
2
|
-
import {ZentaoRequestParamPair, ZentaoRequestParams} from './types';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* 创建一个 Date 对象
|
|
6
|
-
* @param {Date|number|String} [date=null] 用于创建 Date 对象的日期时间表达值,如果留空则创建当前系统时间对象
|
|
7
|
-
* @return {Date} 日期时间对象
|
|
8
|
-
* @function
|
|
9
|
-
*/
|
|
10
|
-
export const createDate = (date: Date | number | string) => {
|
|
11
|
-
if (!date) {
|
|
12
|
-
return new Date();
|
|
13
|
-
}
|
|
14
|
-
if (!(date instanceof Date)) {
|
|
15
|
-
if (typeof date === 'string') {
|
|
16
|
-
date = date.trim();
|
|
17
|
-
if (/^\d+$/.test(date)) {
|
|
18
|
-
date = Number.parseInt(date, 10);
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
if (typeof date === 'number' && date < 10000000000) {
|
|
22
|
-
date *= 1000;
|
|
23
|
-
}
|
|
24
|
-
date = new Date(date);
|
|
25
|
-
}
|
|
26
|
-
return date;
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* 格式化日期时间值为字符串
|
|
31
|
-
* @remarks
|
|
32
|
-
* 所有可用的格式化参数有:
|
|
33
|
-
* - `yyyy`,例如:'2018',表示四位数字表示的年份
|
|
34
|
-
* - `yy`,例如:'18',表示两位数字表示的年份
|
|
35
|
-
* - `MM`,例如:'07',表示两位数字表示的月份,不足两位在起始用 0 填充
|
|
36
|
-
* - `M`,例如:'10',表示一位或两位数字表示的月份
|
|
37
|
-
* - `dd`,例如:'05',表示两位数字表示的日期,不足两位在起始用 0 填充
|
|
38
|
-
* - `d`,例如:'5',表示一位或两位数字表示的日期
|
|
39
|
-
* - `hh`,例如:'08',表示两位数字表示的小时,不足两位在起始用 0 填充
|
|
40
|
-
* - `h`,例如:'8',表示一位或两位数字表示的小时
|
|
41
|
-
* - `mm`,例如:'3',表示两位数字表示的分钟,不足两位在起始用 0 填充
|
|
42
|
-
* - `m`,例如:'03',表示一位或两位数字表示的分钟
|
|
43
|
-
* - `ss`,例如:'5',表示两位数字表示的秒数,不足两位在起始用 0 填充
|
|
44
|
-
* - `s`,例如:'05',表示一位或两位数字表示的秒数
|
|
45
|
-
* - `S`,例如:'236',表示毫秒数
|
|
46
|
-
* - `SSS`,例如:'036',表示毫秒数,不足3位在起始用 0 填充
|
|
47
|
-
* @summary 格式化日期时间值为字符串
|
|
48
|
-
* @param {Date|number|string} date 要格式化的日期时间表达值
|
|
49
|
-
* @param {string} [format='yyyy-MM-dd hh:ss'] 格式化字符串
|
|
50
|
-
* @return {string} 日期时间格式化文本
|
|
51
|
-
* @function
|
|
52
|
-
*/
|
|
53
|
-
export const formatDate = (
|
|
54
|
-
date: Date | number | string,
|
|
55
|
-
format: string = 'yyyy-MM-dd hh:mm'
|
|
56
|
-
) => {
|
|
57
|
-
date = createDate(date);
|
|
58
|
-
|
|
59
|
-
const dateInfo: Record<string, any> = {
|
|
60
|
-
'M+': date.getMonth() + 1,
|
|
61
|
-
'd+': date.getDate(),
|
|
62
|
-
'h+': date.getHours(),
|
|
63
|
-
'H+': date.getHours() % 12,
|
|
64
|
-
'm+': date.getMinutes(),
|
|
65
|
-
's+': date.getSeconds(),
|
|
66
|
-
// 'q+': Math.floor((date.getMonth() + 3) / 3),
|
|
67
|
-
'S+': date.getMilliseconds(),
|
|
68
|
-
};
|
|
69
|
-
if (/(y+)/i.test(format)) {
|
|
70
|
-
format = format.replace(
|
|
71
|
-
RegExp.$1,
|
|
72
|
-
`${date.getFullYear()}`.substr(4 - RegExp.$1.length)
|
|
73
|
-
);
|
|
74
|
-
}
|
|
75
|
-
Object.keys(dateInfo).forEach(k => {
|
|
76
|
-
if (new RegExp(`(${k})`).test(format)) {
|
|
77
|
-
const str = `${dateInfo[k]}`;
|
|
78
|
-
format = format.replace(
|
|
79
|
-
RegExp.$1,
|
|
80
|
-
RegExp.$1.length === 1
|
|
81
|
-
? str
|
|
82
|
-
: `00${str}`.substr(str.length + 2 - RegExp.$1.length)
|
|
83
|
-
);
|
|
84
|
-
}
|
|
85
|
-
});
|
|
86
|
-
return format;
|
|
87
|
-
};
|
|
88
|
-
|
|
89
|
-
/**
|
|
90
|
-
* 格式化禅道地址
|
|
91
|
-
* @param url 禅道地址
|
|
92
|
-
* @returns 格式化后的禅道地址
|
|
93
|
-
* @example
|
|
94
|
-
* // 以下例子都返回 `http://demo.zentao.net/`
|
|
95
|
-
* formatZentaoUrl('http://demo.zentao.net');
|
|
96
|
-
* formatZentaoUrl('http://demo.zentao.net');
|
|
97
|
-
* formatZentaoUrl('http://demo.zentao.net/index.php');
|
|
98
|
-
* formatZentaoUrl('demo.zentao.net');
|
|
99
|
-
*/
|
|
100
|
-
export function formatZentaoUrl(url: string) {
|
|
101
|
-
if (url.endsWith('/index.php')) {
|
|
102
|
-
url = url.substr(0, url.length - 9);
|
|
103
|
-
} else if (!url.endsWith('/')) {
|
|
104
|
-
url = `${url}/`;
|
|
105
|
-
}
|
|
106
|
-
if (!url.startsWith('https://') && !url.startsWith('http://')) {
|
|
107
|
-
url = `http://${url}`;
|
|
108
|
-
}
|
|
109
|
-
return url;
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
/**
|
|
113
|
-
* 创建一个新的对象,仅保留给定的原始对象上给定的属性
|
|
114
|
-
* @param object 给定的原始对象
|
|
115
|
-
* @param fields 要保留属性名称列表
|
|
116
|
-
* @returns 新的对象
|
|
117
|
-
* @example
|
|
118
|
-
* const obj = {a: 1, b: 2, c: 3, d: 4};
|
|
119
|
-
* slimmingObject(obj, ['b', 'c']); // 返回 `{b: 2, c: 3}`
|
|
120
|
-
*/
|
|
121
|
-
export function slimmingObject(object: any, fields: string[]): any {
|
|
122
|
-
if (!object || typeof object !== 'object' || object === null) {
|
|
123
|
-
return object;
|
|
124
|
-
}
|
|
125
|
-
let newObject: Record<string, any> = {};
|
|
126
|
-
for (const field of fields) {
|
|
127
|
-
newObject[field] = object[field];
|
|
128
|
-
}
|
|
129
|
-
return newObject;
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
/**
|
|
133
|
-
* 格式化请求参数定义
|
|
134
|
-
* @param params 要格式化的参数定义
|
|
135
|
-
* @returns 格式化后的参数列表
|
|
136
|
-
* @example
|
|
137
|
-
* // 下面返回:`[['foo', 'bar'], ['hello', 'world']]`
|
|
138
|
-
* normalizeRequestParams([['foo', 'bar'], ['hello', 'world']]);
|
|
139
|
-
*
|
|
140
|
-
* // 下面返回:`[['foo', 'bar'], ['hello', 'world']]`
|
|
141
|
-
* normalizeRequestParams('foo=bar&hello=world');
|
|
142
|
-
*
|
|
143
|
-
* // 下面返回:`[['foo', 'bar'], ['hello', 'world']]`
|
|
144
|
-
* normalizeRequestParams({foo: 'bar', hello: 'world'});
|
|
145
|
-
*
|
|
146
|
-
* // 下面返回:`[['', 'bar'], ['', 'world']]`
|
|
147
|
-
* normalizeRequestParams(['foo', 'bar']);
|
|
148
|
-
*
|
|
149
|
-
* // 下面返回:`[['foo', 'bar'], ['', 'world']]`
|
|
150
|
-
* normalizeRequestParams([['foo', 'bar'], 'bar']);
|
|
151
|
-
*/
|
|
152
|
-
export function normalizeRequestParams(
|
|
153
|
-
params?: ZentaoRequestParams
|
|
154
|
-
): ZentaoRequestParamPair[] {
|
|
155
|
-
let normalizedParams: ZentaoRequestParamPair[] = [];
|
|
156
|
-
if (typeof params === 'string') {
|
|
157
|
-
params = querystring.parse(params);
|
|
158
|
-
}
|
|
159
|
-
if (params && typeof params === 'object') {
|
|
160
|
-
if (Array.isArray(params)) {
|
|
161
|
-
for (const param of params) {
|
|
162
|
-
normalizedParams.push(
|
|
163
|
-
typeof param === 'string' ? ['', param] : param
|
|
164
|
-
);
|
|
165
|
-
}
|
|
166
|
-
} else if (params as Record<string, any>) {
|
|
167
|
-
for (const key of Object.keys(params).sort()) {
|
|
168
|
-
normalizedParams.push([key, params[key]]);
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
return normalizedParams;
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
/**
|
|
176
|
-
* 合并两个请求参数定义,并返回合并且格式化后的定义
|
|
177
|
-
* @param params 参数定义
|
|
178
|
-
* @param otherParams 要合并的其他参数定义
|
|
179
|
-
* @returns 合并后的参数列表
|
|
180
|
-
* @example
|
|
181
|
-
* const params1 = {foo: 'bar', hello: 'world'};
|
|
182
|
-
* const params2 = 'answer='42'';
|
|
183
|
-
* const params3 = [['foo', 'ter'], ['say', 'hi']];
|
|
184
|
-
*
|
|
185
|
-
* // 以下返回 `[['foo', 'bar'], ['answer', '42']]`
|
|
186
|
-
* mergeRequestParams(params1, params2);
|
|
187
|
-
*
|
|
188
|
-
* // 以下返回 `[['foo', ['bar', 'ter']], ['answer', '42'], ['hello', 'world'], ['say', 'hi']]`
|
|
189
|
-
* mergeRequestParams(params1, params2, params3);
|
|
190
|
-
*/
|
|
191
|
-
export function mergeRequestParams(
|
|
192
|
-
params: ZentaoRequestParams,
|
|
193
|
-
...otherParams: ZentaoRequestParams[]
|
|
194
|
-
): ZentaoRequestParamPair[] {
|
|
195
|
-
const normalizedParams = normalizeRequestParams(params);
|
|
196
|
-
for (const otherParam of otherParams) {
|
|
197
|
-
const otherNormalizedParams = normalizeRequestParams(otherParam);
|
|
198
|
-
for (const param of otherNormalizedParams) {
|
|
199
|
-
if (typeof param[0] === 'string' && param[0].length) {
|
|
200
|
-
const existsParam = normalizedParams.find(
|
|
201
|
-
x => x[0] === param[0]
|
|
202
|
-
);
|
|
203
|
-
if (existsParam) {
|
|
204
|
-
if (Array.isArray(existsParam[1])) {
|
|
205
|
-
existsParam[1].push(param[1]);
|
|
206
|
-
} else {
|
|
207
|
-
existsParam[1] = [existsParam[1], param[1]];
|
|
208
|
-
}
|
|
209
|
-
continue;
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
normalizedParams.push(param);
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
return normalizedParams;
|
|
216
|
-
}
|
package/src/zentao-config.ts
DELETED
|
@@ -1,150 +0,0 @@
|
|
|
1
|
-
import {ZentaoRequestType} from './types';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* 禅道配置存储对象
|
|
5
|
-
*/
|
|
6
|
-
export default class ZentaoConfig {
|
|
7
|
-
/**
|
|
8
|
-
* 禅道版本,例如:"12.4.3"
|
|
9
|
-
*/
|
|
10
|
-
readonly version: string;
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* 请求方式,包括 "GET" 和 "PATH_INFO"
|
|
14
|
-
*/
|
|
15
|
-
readonly requestType: ZentaoRequestType;
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* 当请求方式为 PATH_INFO 时的参数连接字符,例如 "-"
|
|
19
|
-
*/
|
|
20
|
-
readonly requestFix: string;
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* 当请求方式为 GET 时,模块参数名,例如 "m"
|
|
24
|
-
*/
|
|
25
|
-
readonly moduleVar: string;
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* 当请求方式为 GET 时,方法参数名,例如 "f"
|
|
29
|
-
*/
|
|
30
|
-
readonly methodVar: string;
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* 当请求方式为 GET 时,视图参数名,例如 "t"
|
|
34
|
-
*/
|
|
35
|
-
readonly viewVar: string;
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* Session 变量名称,例如 "zentaosid"
|
|
39
|
-
*/
|
|
40
|
-
readonly sessionVar: string;
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* Session 名称,例如 "zentaosid"
|
|
44
|
-
*/
|
|
45
|
-
readonly sessionName: string;
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* Session ID,例如 "inge21mp04h95m8lo64tpp6h4c"
|
|
49
|
-
*/
|
|
50
|
-
readonly sessionID: string;
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* Session 随机数,例如 7795
|
|
54
|
-
*/
|
|
55
|
-
readonly random: number;
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* Session 过期时间,例如 1440
|
|
59
|
-
*/
|
|
60
|
-
readonly expiredTime: number;
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* 服务器时间
|
|
64
|
-
*/
|
|
65
|
-
readonly serverTime: number;
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* 创建时间
|
|
69
|
-
*/
|
|
70
|
-
protected readonly _createTime: number;
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* Token 字符串
|
|
74
|
-
*/
|
|
75
|
-
protected _token?: string;
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* 上次更新 Token 的时间
|
|
79
|
-
*/
|
|
80
|
-
protected _tokenUpdateTime?: number;
|
|
81
|
-
|
|
82
|
-
/**
|
|
83
|
-
* 创建一个 ZentaoConfig 实例
|
|
84
|
-
* @param config 配置对象
|
|
85
|
-
* @example
|
|
86
|
-
* const config = new ZentaoConfig({version: 'pro9.0.3', requestType: 'PATH_INFO', requestFix: '-', moduleVar: 'm', methodVar: 'f', viewVar: 't', sessionVar: 'zentaosid', sessionName: 'zentaosid', sessionID: '6fudr187np6d82rj23srkqt1r6', random: 5337, expiredTime: '1440', serverTime: 1615098427});
|
|
87
|
-
*/
|
|
88
|
-
constructor(config: Record<string, any>) {
|
|
89
|
-
this.version = config['version'];
|
|
90
|
-
this.requestType =
|
|
91
|
-
config['requestType'] === 'GET' ? 'GET' : 'PATH_INFO';
|
|
92
|
-
this.requestFix = config['requestFix'] ?? '-';
|
|
93
|
-
this.moduleVar = config['moduleVar'] ?? 'm';
|
|
94
|
-
this.methodVar = config['methodVar'] ?? 'f';
|
|
95
|
-
this.viewVar = config['viewVar'] ?? 't';
|
|
96
|
-
this.sessionVar = config['sessionVar'] ?? 'zentaosid';
|
|
97
|
-
this.sessionName = config['sessionName'] ?? 'zentaosid"';
|
|
98
|
-
this.sessionID = config['sessionID'];
|
|
99
|
-
this.random = config['random'];
|
|
100
|
-
this.serverTime = config['serverTime'];
|
|
101
|
-
this.expiredTime =
|
|
102
|
-
typeof config['expiredTime'] === 'string'
|
|
103
|
-
? Number.parseInt(config['expiredTime'], 10)
|
|
104
|
-
: config['expiredTime'];
|
|
105
|
-
this._tokenUpdateTime = config['_tokenUpdateTime'];
|
|
106
|
-
this._token = config['_token'];
|
|
107
|
-
this._createTime = Date.now();
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
/**
|
|
111
|
-
* 获取 Token 字符串
|
|
112
|
-
*/
|
|
113
|
-
get token(): string {
|
|
114
|
-
return this._token ?? '';
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
/**
|
|
118
|
-
* Token 验证字符串
|
|
119
|
-
*/
|
|
120
|
-
get tokenAuth(): string {
|
|
121
|
-
return `${this?.sessionName ?? ''}=${this?.sessionID ?? ''}`;
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
/**
|
|
125
|
-
* 禅道主版本
|
|
126
|
-
*/
|
|
127
|
-
get mainVersion(): number {
|
|
128
|
-
return Number.parseInt(this.version.split('.')[0]);
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
/**
|
|
132
|
-
* 将 Token 验证字符串更新为当前 Token
|
|
133
|
-
*/
|
|
134
|
-
renewToken(): void {
|
|
135
|
-
this._token = this.tokenAuth;
|
|
136
|
-
this._tokenUpdateTime = Date.now();
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
/**
|
|
140
|
-
* 检查当前 Token 字符串是否过期
|
|
141
|
-
*/
|
|
142
|
-
get isTokenExpired(): boolean {
|
|
143
|
-
if (!this._tokenUpdateTime) {
|
|
144
|
-
return true;
|
|
145
|
-
}
|
|
146
|
-
return (
|
|
147
|
-
Date.now() - this._tokenUpdateTime >= (this.expiredTime - 30) * 1000
|
|
148
|
-
);
|
|
149
|
-
}
|
|
150
|
-
}
|
|
@@ -1,227 +0,0 @@
|
|
|
1
|
-
import {Method} from 'axios';
|
|
2
|
-
import {ZentaoApiResult, ZentaoRequestParams} from './types';
|
|
3
|
-
import {mergeRequestParams} from './utils';
|
|
4
|
-
import Zentao from './zentao';
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* 禅道请求构建类
|
|
8
|
-
* @internal
|
|
9
|
-
*/
|
|
10
|
-
export default class ZentaoRequestBuilder {
|
|
11
|
-
/**
|
|
12
|
-
* 禅道请求实例
|
|
13
|
-
*/
|
|
14
|
-
readonly zentao: Zentao;
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* 模块名
|
|
18
|
-
*/
|
|
19
|
-
readonly moduleName: string;
|
|
20
|
-
|
|
21
|
-
_methodName: string;
|
|
22
|
-
_params?: ZentaoRequestParams;
|
|
23
|
-
_data?: string | Record<string, any>;
|
|
24
|
-
_fields?: string[];
|
|
25
|
-
_resultConvertor?: (
|
|
26
|
-
remoteData: any,
|
|
27
|
-
result: ZentaoApiResult
|
|
28
|
-
) => ZentaoApiResult;
|
|
29
|
-
_url?: string;
|
|
30
|
-
_name?: string;
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* 创建一个禅道请求构建实例
|
|
34
|
-
* @param zentao 禅道请求实例
|
|
35
|
-
* @param moduleName 方法名
|
|
36
|
-
* @param methodName 模块名
|
|
37
|
-
* @param params 请求参数
|
|
38
|
-
*/
|
|
39
|
-
constructor(
|
|
40
|
-
zentao: Zentao,
|
|
41
|
-
moduleName: string,
|
|
42
|
-
methodName?: string,
|
|
43
|
-
params?: ZentaoRequestParams
|
|
44
|
-
) {
|
|
45
|
-
this.zentao = zentao;
|
|
46
|
-
this.moduleName = moduleName;
|
|
47
|
-
this._methodName = methodName ?? 'index';
|
|
48
|
-
this._params = params;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* 获取方法名
|
|
53
|
-
*/
|
|
54
|
-
get methodName() {
|
|
55
|
-
return this._methodName;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* 获取请求参数
|
|
60
|
-
*/
|
|
61
|
-
get params() {
|
|
62
|
-
return this._params;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* 获取请求数据
|
|
67
|
-
*/
|
|
68
|
-
get data() {
|
|
69
|
-
return this._data;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* 获取结果属性过滤列表
|
|
74
|
-
*/
|
|
75
|
-
get fields() {
|
|
76
|
-
return this._fields;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
/**
|
|
80
|
-
* 获取请求名称
|
|
81
|
-
*/
|
|
82
|
-
get name() {
|
|
83
|
-
return this._name;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* 获取自定义请求地址
|
|
88
|
-
*/
|
|
89
|
-
get url() {
|
|
90
|
-
return this._url;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
/**
|
|
94
|
-
* 获取结果转换函数
|
|
95
|
-
*/
|
|
96
|
-
get resultConvertor() {
|
|
97
|
-
return this._resultConvertor;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
/**
|
|
101
|
-
* 设置方法名
|
|
102
|
-
* @param name 方法名
|
|
103
|
-
* @returns 禅道请求构建实例自身
|
|
104
|
-
*/
|
|
105
|
-
method(name: string): ZentaoRequestBuilder {
|
|
106
|
-
this._methodName = name;
|
|
107
|
-
return this;
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
/**
|
|
111
|
-
* 设置方法名,是方法 `method(name)` 的别名
|
|
112
|
-
* @param name 方法名
|
|
113
|
-
* @returns 禅道请求构建实例自身
|
|
114
|
-
* @alias method
|
|
115
|
-
*/
|
|
116
|
-
f(name: string): ZentaoRequestBuilder {
|
|
117
|
-
return this.method(name);
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
/**
|
|
121
|
-
* 设置请求参数
|
|
122
|
-
* @param params 要设置的参数
|
|
123
|
-
* @returns 禅道请求构建实例自身
|
|
124
|
-
*/
|
|
125
|
-
withParams(params: ZentaoRequestParams): ZentaoRequestBuilder {
|
|
126
|
-
this._params = params;
|
|
127
|
-
return this;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
/**
|
|
131
|
-
* 追加请求参数
|
|
132
|
-
* @param params 要追加的参数
|
|
133
|
-
* @returns 禅道请求构建实例自身
|
|
134
|
-
*/
|
|
135
|
-
appendParams(params: ZentaoRequestParams): ZentaoRequestBuilder {
|
|
136
|
-
if (this._params) {
|
|
137
|
-
this._params = mergeRequestParams(this._params, params);
|
|
138
|
-
} else {
|
|
139
|
-
this._params = params;
|
|
140
|
-
}
|
|
141
|
-
return this;
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
/**
|
|
145
|
-
* 设置请求名称
|
|
146
|
-
* @param name 名称
|
|
147
|
-
* @returns 禅道请求构建实例自身
|
|
148
|
-
*/
|
|
149
|
-
named(name: string): ZentaoRequestBuilder {
|
|
150
|
-
this._name = name;
|
|
151
|
-
return this;
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
/**
|
|
155
|
-
* 设置结果转换函数
|
|
156
|
-
* @param converter 结果转换函数
|
|
157
|
-
* @returns 禅道请求构建实例自身
|
|
158
|
-
*/
|
|
159
|
-
useConverter(
|
|
160
|
-
converter: (remoteData: any, result: ZentaoApiResult) => ZentaoApiResult
|
|
161
|
-
): ZentaoRequestBuilder {
|
|
162
|
-
this._resultConvertor = converter;
|
|
163
|
-
return this;
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
/**
|
|
167
|
-
* 过滤需要返回的属性列表
|
|
168
|
-
* @param fields 设置仅返回的属性名称列表
|
|
169
|
-
* @returns 禅道请求构建实例自身
|
|
170
|
-
*/
|
|
171
|
-
filterFields(
|
|
172
|
-
...fields: Array<string | string[] | undefined>
|
|
173
|
-
): ZentaoRequestBuilder {
|
|
174
|
-
this._fields = fields.flat().filter(x => x !== undefined) as string[];
|
|
175
|
-
return this;
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
/**
|
|
179
|
-
* 发起 GET 请求
|
|
180
|
-
* @returns 请求结果
|
|
181
|
-
*/
|
|
182
|
-
get(): Promise<ZentaoApiResult> {
|
|
183
|
-
return this.zentao.request(this.moduleName, this._methodName, {
|
|
184
|
-
method: 'GET',
|
|
185
|
-
params: this._params,
|
|
186
|
-
data: this._data,
|
|
187
|
-
fields: this._fields,
|
|
188
|
-
resultConvertor: this._resultConvertor,
|
|
189
|
-
name: this._name,
|
|
190
|
-
url: this._url,
|
|
191
|
-
});
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
/**
|
|
195
|
-
* 发起 POST 请求
|
|
196
|
-
* @param data 要提交的数据
|
|
197
|
-
* @returns 请求结果
|
|
198
|
-
*/
|
|
199
|
-
post(data?: string | Record<string, any>): Promise<ZentaoApiResult> {
|
|
200
|
-
return this.zentao.request(this.moduleName, this._methodName, {
|
|
201
|
-
method: 'POST',
|
|
202
|
-
params: this._params,
|
|
203
|
-
data: data ?? this._data,
|
|
204
|
-
fields: this._fields,
|
|
205
|
-
resultConvertor: this._resultConvertor,
|
|
206
|
-
name: this._name,
|
|
207
|
-
url: this._url,
|
|
208
|
-
});
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
/**
|
|
212
|
-
* 发起 HTTP 请求,可以指定请求方式
|
|
213
|
-
* @param method 请求方式,默认为 GET
|
|
214
|
-
* @returns 请求结果
|
|
215
|
-
*/
|
|
216
|
-
request(method?: Method) {
|
|
217
|
-
return this.zentao.request(this.moduleName, this._methodName, {
|
|
218
|
-
method,
|
|
219
|
-
params: this._params,
|
|
220
|
-
data: this._data,
|
|
221
|
-
fields: this._fields,
|
|
222
|
-
resultConvertor: this._resultConvertor,
|
|
223
|
-
name: this._name,
|
|
224
|
-
url: this._url,
|
|
225
|
-
});
|
|
226
|
-
}
|
|
227
|
-
}
|