yapi-to-typescript2 1.0.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/LICENSE +21 -0
- package/README.md +22 -0
- package/lib/cjs/Generator.js +1135 -0
- package/lib/cjs/SwaggerToYApiServer.js +336 -0
- package/lib/cjs/cli.js +285 -0
- package/lib/cjs/helpers.js +230 -0
- package/lib/cjs/index.js +19 -0
- package/lib/cjs/swaggerJsonToYApiData.js +496 -0
- package/lib/cjs/types.js +100 -0
- package/lib/cjs/utils.js +766 -0
- package/lib/esm/Generator.js +1111 -0
- package/lib/esm/SwaggerToYApiServer.js +317 -0
- package/lib/esm/cli.js +257 -0
- package/lib/esm/helpers.js +216 -0
- package/lib/esm/index.d.ts +845 -0
- package/lib/esm/index.js +2 -0
- package/lib/esm/swaggerJsonToYApiData.js +483 -0
- package/lib/esm/types.js +79 -0
- package/lib/esm/utils.js +707 -0
- package/package.json +114 -0
|
@@ -0,0 +1,845 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
|
|
3
|
+
import type { AppendOptions } from 'form-data';
|
|
4
|
+
import { JSONSchema4 } from 'json-schema';
|
|
5
|
+
import { JSONSchema4TypeName } from 'json-schema';
|
|
6
|
+
import { ParsedPath } from 'path';
|
|
7
|
+
|
|
8
|
+
/** Useful as a return type in interfaces or abstract classes with missing implementation */
|
|
9
|
+
declare type AsyncOrSync<T> = PromiseLike<T> | T;
|
|
10
|
+
|
|
11
|
+
/** 分类信息 */
|
|
12
|
+
export declare interface Category {
|
|
13
|
+
/** ID */
|
|
14
|
+
_id: number;
|
|
15
|
+
/** 分类在 YApi 上的地址(由 YTT 自行实现) */
|
|
16
|
+
_url: string;
|
|
17
|
+
/** 分类名称 */
|
|
18
|
+
name: string;
|
|
19
|
+
/** 分类备注 */
|
|
20
|
+
desc: string;
|
|
21
|
+
/** 分类接口列表 */
|
|
22
|
+
list: InterfaceList;
|
|
23
|
+
/** 创建时间(unix时间戳) */
|
|
24
|
+
add_time: number;
|
|
25
|
+
/** 更新时间(unix时间戳) */
|
|
26
|
+
up_time: number;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* 分类的配置。
|
|
31
|
+
*/
|
|
32
|
+
export declare interface CategoryConfig extends SharedConfig {
|
|
33
|
+
/**
|
|
34
|
+
* 分类 ID,可以设置多个。设为 `0` 时表示全部分类。
|
|
35
|
+
*
|
|
36
|
+
* 如果需要获取全部分类,同时排除指定分类,可以这样:`[0, -20, -21]`,分类 ID 前面的负号表示排除。
|
|
37
|
+
*
|
|
38
|
+
* 获取方式:打开项目 --> 点开分类 --> 复制浏览器地址栏 `/api/cat_` 后面的数字。
|
|
39
|
+
*
|
|
40
|
+
* @example 20
|
|
41
|
+
*/
|
|
42
|
+
id: number | number[];
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** 分类列表,对应数据导出的 json 内容 */
|
|
46
|
+
export declare type CategoryList = Category[];
|
|
47
|
+
|
|
48
|
+
export declare interface ChangeCase {
|
|
49
|
+
/**
|
|
50
|
+
* @example
|
|
51
|
+
* changeCase.camelCase('test string') // => 'testString'
|
|
52
|
+
*/
|
|
53
|
+
camelCase: (value: string) => string;
|
|
54
|
+
/**
|
|
55
|
+
* @example
|
|
56
|
+
* changeCase.constantCase('test string') // => 'TEST_STRING'
|
|
57
|
+
*/
|
|
58
|
+
constantCase: (value: string) => string;
|
|
59
|
+
/**
|
|
60
|
+
* @example
|
|
61
|
+
* changeCase.dotCase('test string') // => 'test.string'
|
|
62
|
+
*/
|
|
63
|
+
dotCase: (value: string) => string;
|
|
64
|
+
/**
|
|
65
|
+
* @example
|
|
66
|
+
* changeCase.headerCase('test string') // => 'Test-String'
|
|
67
|
+
*/
|
|
68
|
+
headerCase: (value: string) => string;
|
|
69
|
+
/**
|
|
70
|
+
* @example
|
|
71
|
+
* changeCase.lowerCase('TEST STRING') // => 'test string'
|
|
72
|
+
*/
|
|
73
|
+
lowerCase: (value: string) => string;
|
|
74
|
+
/**
|
|
75
|
+
* @example
|
|
76
|
+
* changeCase.lowerCaseFirst('TEST') // => 'tEST'
|
|
77
|
+
*/
|
|
78
|
+
lowerCaseFirst: (value: string) => string;
|
|
79
|
+
/**
|
|
80
|
+
* @example
|
|
81
|
+
* changeCase.paramCase('test string') // => 'test-string'
|
|
82
|
+
*/
|
|
83
|
+
paramCase: (value: string) => string;
|
|
84
|
+
/**
|
|
85
|
+
* @example
|
|
86
|
+
* changeCase.pascalCase('test string') // => 'TestString'
|
|
87
|
+
*/
|
|
88
|
+
pascalCase: (value: string) => string;
|
|
89
|
+
/**
|
|
90
|
+
* @example
|
|
91
|
+
* changeCase.pathCase('test string') // => 'test/string'
|
|
92
|
+
*/
|
|
93
|
+
pathCase: (value: string) => string;
|
|
94
|
+
/**
|
|
95
|
+
* @example
|
|
96
|
+
* changeCase.sentenceCase('testString') // => 'Test string'
|
|
97
|
+
*/
|
|
98
|
+
sentenceCase: (value: string) => string;
|
|
99
|
+
/**
|
|
100
|
+
* @example
|
|
101
|
+
* changeCase.snakeCase('test string') // => 'test_string'
|
|
102
|
+
*/
|
|
103
|
+
snakeCase: (value: string) => string;
|
|
104
|
+
/**
|
|
105
|
+
* @example
|
|
106
|
+
* changeCase.swapCase('Test String') // => 'tEST sTRING'
|
|
107
|
+
*/
|
|
108
|
+
swapCase: (value: string) => string;
|
|
109
|
+
/**
|
|
110
|
+
* @example
|
|
111
|
+
* changeCase.titleCase('a simple test') // => 'A Simple Test'
|
|
112
|
+
*/
|
|
113
|
+
titleCase: (value: string) => string;
|
|
114
|
+
/**
|
|
115
|
+
* @example
|
|
116
|
+
* changeCase.upperCase('test string') // => 'TEST STRING'
|
|
117
|
+
*/
|
|
118
|
+
upperCase: (value: string) => string;
|
|
119
|
+
/**
|
|
120
|
+
* @example
|
|
121
|
+
* changeCase.upperCaseFirst('test') // => 'Test'
|
|
122
|
+
*/
|
|
123
|
+
upperCaseFirst: (value: string) => string;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** 命令行钩子 */
|
|
127
|
+
export declare interface CliHooks {
|
|
128
|
+
/** 生成成功时触发 */
|
|
129
|
+
success?: () => AsyncOrSync<void>;
|
|
130
|
+
/** 生成失败时触发 */
|
|
131
|
+
fail?: () => AsyncOrSync<void>;
|
|
132
|
+
/** 生成完毕时触发(无论成功、失败) */
|
|
133
|
+
complete?: () => AsyncOrSync<void>;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** 支持生成注释的相关配置 */
|
|
137
|
+
export declare interface CommentConfig {
|
|
138
|
+
/**
|
|
139
|
+
* 是否开启该项功能。
|
|
140
|
+
*
|
|
141
|
+
* @default true
|
|
142
|
+
*/
|
|
143
|
+
enabled?: boolean;
|
|
144
|
+
/**
|
|
145
|
+
* 是否有标题。
|
|
146
|
+
*
|
|
147
|
+
* @default true
|
|
148
|
+
*/
|
|
149
|
+
title?: boolean;
|
|
150
|
+
/**
|
|
151
|
+
* 是否有分类名称。
|
|
152
|
+
*
|
|
153
|
+
* @default true
|
|
154
|
+
*/
|
|
155
|
+
category?: boolean;
|
|
156
|
+
/**
|
|
157
|
+
* 是否有标签。
|
|
158
|
+
*
|
|
159
|
+
* @default true
|
|
160
|
+
*/
|
|
161
|
+
tag?: boolean;
|
|
162
|
+
/**
|
|
163
|
+
* 是否有请求头。
|
|
164
|
+
*
|
|
165
|
+
* @default true
|
|
166
|
+
*/
|
|
167
|
+
requestHeader?: boolean;
|
|
168
|
+
/**
|
|
169
|
+
* 是否有更新时间。
|
|
170
|
+
*
|
|
171
|
+
* @default true
|
|
172
|
+
*/
|
|
173
|
+
updateTime?: boolean;
|
|
174
|
+
/**
|
|
175
|
+
* 是否为标题、分类名称添加链接。
|
|
176
|
+
*
|
|
177
|
+
* @default true
|
|
178
|
+
*/
|
|
179
|
+
link?: boolean;
|
|
180
|
+
/**
|
|
181
|
+
* 额外的注释标签。生成的内容形如:`@{name} {value}`。
|
|
182
|
+
*/
|
|
183
|
+
extraTags?: (interfaceInfo: ExtendedInterface) => Array<{
|
|
184
|
+
/**
|
|
185
|
+
* 标签名。
|
|
186
|
+
*/
|
|
187
|
+
name: string;
|
|
188
|
+
/**
|
|
189
|
+
* 标签值。
|
|
190
|
+
*/
|
|
191
|
+
value: string;
|
|
192
|
+
/**
|
|
193
|
+
* 标签位置,即将新标签插在标签列表的开头还是末尾。
|
|
194
|
+
*
|
|
195
|
+
* @default 'end'
|
|
196
|
+
*/
|
|
197
|
+
position?: 'start' | 'end';
|
|
198
|
+
}>;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/** 配置。 */
|
|
202
|
+
export declare type Config = ServerConfig | ServerConfig[];
|
|
203
|
+
|
|
204
|
+
export declare type ConfigWithHooks = Config & {
|
|
205
|
+
hooks?: CliHooks;
|
|
206
|
+
};
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* 定义配置。
|
|
210
|
+
*
|
|
211
|
+
* @param config 配置
|
|
212
|
+
*/
|
|
213
|
+
export declare function defineConfig(config: Config, hooks?: CliHooks): ConfigWithHooks;
|
|
214
|
+
|
|
215
|
+
/** 扩展接口定义 */
|
|
216
|
+
export declare interface ExtendedInterface extends Interface {
|
|
217
|
+
parsedPath: ParsedPath;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
export declare class FileData<T = any> {
|
|
221
|
+
/**
|
|
222
|
+
* 原始文件数据。
|
|
223
|
+
*/
|
|
224
|
+
private originalFileData;
|
|
225
|
+
/**
|
|
226
|
+
* 选项。
|
|
227
|
+
*/
|
|
228
|
+
private options;
|
|
229
|
+
/**
|
|
230
|
+
* 文件数据辅助类,统一网页、小程序等平台的文件上传。
|
|
231
|
+
*
|
|
232
|
+
* @param originalFileData 原始文件数据
|
|
233
|
+
* @param options 若使用内部的 getFormData,则选项会被其使用
|
|
234
|
+
*/
|
|
235
|
+
constructor(originalFileData: T, options?: AppendOptions);
|
|
236
|
+
/**
|
|
237
|
+
* 获取原始文件数据。
|
|
238
|
+
*
|
|
239
|
+
* @returns 原始文件数据
|
|
240
|
+
*/
|
|
241
|
+
getOriginalFileData(): T;
|
|
242
|
+
/**
|
|
243
|
+
* 获取选项。
|
|
244
|
+
*/
|
|
245
|
+
getOptions(): AppendOptions | undefined;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/** 接口定义 */
|
|
249
|
+
export declare interface Interface {
|
|
250
|
+
/** 接口 ID */
|
|
251
|
+
_id: number;
|
|
252
|
+
/** 所属分类信息(由 YTT 自行实现) */
|
|
253
|
+
_category: OmitStrict<Category, 'list'>;
|
|
254
|
+
/** 所属项目信息(由 YTT 自行实现) */
|
|
255
|
+
_project: Project;
|
|
256
|
+
/** 接口在 YApi 上的地址(由 YTT 自行实现) */
|
|
257
|
+
_url: string;
|
|
258
|
+
/** 接口名称 */
|
|
259
|
+
title: string;
|
|
260
|
+
/** 状态 */
|
|
261
|
+
status: LiteralUnion<'done' | 'undone', string>;
|
|
262
|
+
/** 接口备注 */
|
|
263
|
+
markdown: string;
|
|
264
|
+
/** 请求路径 */
|
|
265
|
+
path: string;
|
|
266
|
+
/** 请求方式,HEAD、OPTIONS 处理与 GET 相似,其余处理与 POST 相似 */
|
|
267
|
+
method: Method;
|
|
268
|
+
/** 所属项目 id */
|
|
269
|
+
project_id: number;
|
|
270
|
+
/** 所属分类 id */
|
|
271
|
+
catid: number;
|
|
272
|
+
/** 标签列表 */
|
|
273
|
+
tag: string[];
|
|
274
|
+
/** 请求头 */
|
|
275
|
+
req_headers: Array<{
|
|
276
|
+
/** 名称 */
|
|
277
|
+
name: string;
|
|
278
|
+
/** 值 */
|
|
279
|
+
value: string;
|
|
280
|
+
/** 备注 */
|
|
281
|
+
desc: string;
|
|
282
|
+
/** 示例 */
|
|
283
|
+
example: string;
|
|
284
|
+
/** 是否必需 */
|
|
285
|
+
required: Required_2;
|
|
286
|
+
}>;
|
|
287
|
+
/** 路径参数 */
|
|
288
|
+
req_params: Array<{
|
|
289
|
+
/** 名称 */
|
|
290
|
+
name: string;
|
|
291
|
+
/** 备注 */
|
|
292
|
+
desc: string;
|
|
293
|
+
/** 示例 */
|
|
294
|
+
example: string;
|
|
295
|
+
/** 类型(YApi-X) */
|
|
296
|
+
type?: RequestParamType;
|
|
297
|
+
}>;
|
|
298
|
+
/** 仅 GET:请求串 */
|
|
299
|
+
req_query: Array<{
|
|
300
|
+
/** 名称 */
|
|
301
|
+
name: string;
|
|
302
|
+
/** 备注 */
|
|
303
|
+
desc: string;
|
|
304
|
+
/** 示例 */
|
|
305
|
+
example: string;
|
|
306
|
+
/** 是否必需 */
|
|
307
|
+
required: Required_2;
|
|
308
|
+
/** 类型(YApi-X) */
|
|
309
|
+
type?: RequestQueryType;
|
|
310
|
+
}>;
|
|
311
|
+
/** 仅 POST:请求内容类型。为 text, file, raw 时不必特殊处理。 */
|
|
312
|
+
req_body_type: RequestBodyType;
|
|
313
|
+
/** `req_body_type = json` 时是否为 json schema */
|
|
314
|
+
req_body_is_json_schema: boolean;
|
|
315
|
+
/** `req_body_type = form` 时的请求内容 */
|
|
316
|
+
req_body_form: Array<{
|
|
317
|
+
/** 名称 */
|
|
318
|
+
name: string;
|
|
319
|
+
/** 类型 */
|
|
320
|
+
type: RequestFormItemType;
|
|
321
|
+
/** 备注 */
|
|
322
|
+
desc: string;
|
|
323
|
+
/** 示例 */
|
|
324
|
+
example: string;
|
|
325
|
+
/** 是否必需 */
|
|
326
|
+
required: Required_2;
|
|
327
|
+
}>;
|
|
328
|
+
/** `req_body_type = json` 时的请求内容 */
|
|
329
|
+
req_body_other: string;
|
|
330
|
+
/** 返回数据类型 */
|
|
331
|
+
res_body_type: ResponseBodyType;
|
|
332
|
+
/** `res_body_type = json` 时是否为 json schema */
|
|
333
|
+
res_body_is_json_schema: boolean;
|
|
334
|
+
/** 返回数据 */
|
|
335
|
+
res_body: string;
|
|
336
|
+
/** 创建时间(unix时间戳) */
|
|
337
|
+
add_time: number;
|
|
338
|
+
/** 更新时间(unix时间戳) */
|
|
339
|
+
up_time: number;
|
|
340
|
+
/** 创建人 ID */
|
|
341
|
+
uid: number;
|
|
342
|
+
[key: string]: any;
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
/** 接口列表 */
|
|
346
|
+
export declare type InterfaceList = Interface[];
|
|
347
|
+
|
|
348
|
+
/** 支持生成 JSON Schema 的相关配置 */
|
|
349
|
+
export declare interface JsonSchemaConfig {
|
|
350
|
+
/**
|
|
351
|
+
* 是否开启该项功能。
|
|
352
|
+
*/
|
|
353
|
+
enabled: boolean;
|
|
354
|
+
/**
|
|
355
|
+
* 是否生成请求数据的 JSON Schema。
|
|
356
|
+
*
|
|
357
|
+
* @default true
|
|
358
|
+
*/
|
|
359
|
+
requestData?: boolean;
|
|
360
|
+
/**
|
|
361
|
+
* 是否生成返回数据的 JSON Schema。
|
|
362
|
+
*
|
|
363
|
+
* @default true
|
|
364
|
+
*/
|
|
365
|
+
responseData?: boolean;
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
/**
|
|
369
|
+
Allows creating a union type by combining primitive types and literal types without sacrificing auto-completion in IDEs for the literal type part of the union.
|
|
370
|
+
|
|
371
|
+
Currently, when a union type of a primitive type is combined with literal types, TypeScript loses all information about the combined literals. Thus, when such type is used in an IDE with autocompletion, no suggestions are made for the declared literals.
|
|
372
|
+
|
|
373
|
+
This type is a workaround for [Microsoft/TypeScript#29729](https://github.com/Microsoft/TypeScript/issues/29729). It will be removed as soon as it's not needed anymore.
|
|
374
|
+
|
|
375
|
+
@example
|
|
376
|
+
```
|
|
377
|
+
import {LiteralUnion} from 'type-fest';
|
|
378
|
+
|
|
379
|
+
// Before
|
|
380
|
+
|
|
381
|
+
type Pet = 'dog' | 'cat' | string;
|
|
382
|
+
|
|
383
|
+
const pet: Pet = '';
|
|
384
|
+
// Start typing in your TypeScript-enabled IDE.
|
|
385
|
+
// You **will not** get auto-completion for `dog` and `cat` literals.
|
|
386
|
+
|
|
387
|
+
// After
|
|
388
|
+
|
|
389
|
+
type Pet2 = LiteralUnion<'dog' | 'cat', string>;
|
|
390
|
+
|
|
391
|
+
const pet: Pet2 = '';
|
|
392
|
+
// You **will** get auto-completion for `dog` and `cat` literals.
|
|
393
|
+
```
|
|
394
|
+
|
|
395
|
+
@category Utilities
|
|
396
|
+
*/
|
|
397
|
+
declare type LiteralUnion<
|
|
398
|
+
LiteralType,
|
|
399
|
+
BaseType extends Primitive,
|
|
400
|
+
> = LiteralType | (BaseType & {_?: never});
|
|
401
|
+
|
|
402
|
+
/** 请求方式 */
|
|
403
|
+
export declare enum Method {
|
|
404
|
+
GET = "GET",
|
|
405
|
+
POST = "POST",
|
|
406
|
+
PUT = "PUT",
|
|
407
|
+
DELETE = "DELETE",
|
|
408
|
+
HEAD = "HEAD",
|
|
409
|
+
OPTIONS = "OPTIONS",
|
|
410
|
+
PATCH = "PATCH"
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
/** Similar to the builtin Omit, but checks the filter strictly. */
|
|
414
|
+
declare type OmitStrict<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
|
|
415
|
+
|
|
416
|
+
/**
|
|
417
|
+
* 同 `T | T[]`。
|
|
418
|
+
*
|
|
419
|
+
* @public
|
|
420
|
+
* @example
|
|
421
|
+
* ```typescript
|
|
422
|
+
* type X = OneOrMore<number> // => number | number[]
|
|
423
|
+
* ```
|
|
424
|
+
*/
|
|
425
|
+
declare type OneOrMore<T> = T | T[];
|
|
426
|
+
|
|
427
|
+
/**
|
|
428
|
+
* 解析请求数据,从请求数据中分离出普通数据和文件数据。
|
|
429
|
+
*
|
|
430
|
+
* @param requestData 要解析的请求数据
|
|
431
|
+
* @returns 包含普通数据(data)和文件数据(fileData)的对象,data、fileData 为空对象时,表示没有此类数据
|
|
432
|
+
*/
|
|
433
|
+
export declare function parseRequestData(requestData?: any): {
|
|
434
|
+
data: any;
|
|
435
|
+
fileData: any;
|
|
436
|
+
};
|
|
437
|
+
|
|
438
|
+
/**
|
|
439
|
+
* 准备要传给请求函数的参数。
|
|
440
|
+
*/
|
|
441
|
+
export declare function prepare(requestConfig: RequestConfig, requestData: any): RequestFunctionParams;
|
|
442
|
+
|
|
443
|
+
/**
|
|
444
|
+
Matches any [primitive value](https://developer.mozilla.org/en-US/docs/Glossary/Primitive).
|
|
445
|
+
|
|
446
|
+
@category Basic
|
|
447
|
+
*/
|
|
448
|
+
declare type Primitive =
|
|
449
|
+
| null
|
|
450
|
+
| undefined
|
|
451
|
+
| string
|
|
452
|
+
| number
|
|
453
|
+
| boolean
|
|
454
|
+
| symbol
|
|
455
|
+
| bigint;
|
|
456
|
+
|
|
457
|
+
/** 项目信息 */
|
|
458
|
+
export declare interface Project {
|
|
459
|
+
/** ID */
|
|
460
|
+
_id: number;
|
|
461
|
+
/** 项目在 YApi 上的地址(由 YTT 自行实现) */
|
|
462
|
+
_url: string;
|
|
463
|
+
/** 名称 */
|
|
464
|
+
name: string;
|
|
465
|
+
/** 描述 */
|
|
466
|
+
desc: string;
|
|
467
|
+
/** 基本路径 */
|
|
468
|
+
basepath: string;
|
|
469
|
+
/** 标签 */
|
|
470
|
+
tag: string[];
|
|
471
|
+
/** 环境配置 */
|
|
472
|
+
env: Array<{
|
|
473
|
+
/** 环境名称 */
|
|
474
|
+
name: string;
|
|
475
|
+
/** 环境域名 */
|
|
476
|
+
domain: string;
|
|
477
|
+
}>;
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
/**
|
|
481
|
+
* 项目的配置。
|
|
482
|
+
*/
|
|
483
|
+
export declare interface ProjectConfig extends SharedConfig {
|
|
484
|
+
/**
|
|
485
|
+
* 项目的唯一标识。支持多个项目。
|
|
486
|
+
*
|
|
487
|
+
* 获取方式:打开项目 --> `设置` --> `token配置` --> 复制 token。
|
|
488
|
+
*
|
|
489
|
+
* @example 'e02a47122259d0c1973a9ff81cabb30685d64abc72f39edaa1ac6b6a792a647d'
|
|
490
|
+
*/
|
|
491
|
+
token: string | string[];
|
|
492
|
+
/**
|
|
493
|
+
* 分类列表。
|
|
494
|
+
*/
|
|
495
|
+
categories: CategoryConfig[];
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
/** 属性定义 */
|
|
499
|
+
export declare interface PropDefinition {
|
|
500
|
+
/** 属性名称 */
|
|
501
|
+
name: string;
|
|
502
|
+
/** 是否必需 */
|
|
503
|
+
required: boolean;
|
|
504
|
+
/** 类型 */
|
|
505
|
+
type: JSONSchema4['type'];
|
|
506
|
+
/** 注释 */
|
|
507
|
+
comment: string;
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
/** 属性定义列表 */
|
|
511
|
+
export declare type PropDefinitions = PropDefinition[];
|
|
512
|
+
|
|
513
|
+
/** 查询字符串数组格式化方式 */
|
|
514
|
+
export declare enum QueryStringArrayFormat {
|
|
515
|
+
/** 示例: `a[]=b&a[]=c` */
|
|
516
|
+
'brackets' = "brackets",
|
|
517
|
+
/** 示例: `a[0]=b&a[1]=c` */
|
|
518
|
+
'indices' = "indices",
|
|
519
|
+
/** 示例: `a=b&a=c` */
|
|
520
|
+
'repeat' = "repeat",
|
|
521
|
+
/** 示例: `a=b,c` */
|
|
522
|
+
'comma' = "comma",
|
|
523
|
+
/** 示例: `a=["b","c"]` */
|
|
524
|
+
'json' = "json"
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
/** 支持生成 React Hooks 代码的相关配置 */
|
|
528
|
+
export declare interface ReactHooksConfig {
|
|
529
|
+
/**
|
|
530
|
+
* 是否开启该项功能。
|
|
531
|
+
*/
|
|
532
|
+
enabled: boolean;
|
|
533
|
+
/**
|
|
534
|
+
* 请求 Hook 函数制造者文件路径。
|
|
535
|
+
*
|
|
536
|
+
* @default 与 `outputFilePath` 同级目录下的 `makeRequestHook.ts` 文件
|
|
537
|
+
* @example 'src/api/makeRequestHook.ts'
|
|
538
|
+
*/
|
|
539
|
+
requestHookMakerFilePath?: string;
|
|
540
|
+
/**
|
|
541
|
+
* 获取请求 Hook 的名称。
|
|
542
|
+
*
|
|
543
|
+
* @default `use${changeCase.pascalCase(requestFunctionName)}`
|
|
544
|
+
* @param interfaceInfo 接口信息
|
|
545
|
+
* @param changeCase 常用的大小写转换函数集合对象
|
|
546
|
+
* @returns 请求 Hook 的名称
|
|
547
|
+
*/
|
|
548
|
+
getRequestHookName?(interfaceInfo: ExtendedInterface, changeCase: ChangeCase): string;
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
/** 请求数据类型 */
|
|
552
|
+
export declare enum RequestBodyType {
|
|
553
|
+
/** 查询字符串 */
|
|
554
|
+
query = "query",
|
|
555
|
+
/** 表单 */
|
|
556
|
+
form = "form",
|
|
557
|
+
/** JSON */
|
|
558
|
+
json = "json",
|
|
559
|
+
/** 纯文本 */
|
|
560
|
+
text = "text",
|
|
561
|
+
/** 文件 */
|
|
562
|
+
file = "file",
|
|
563
|
+
/** 原始数据 */
|
|
564
|
+
raw = "raw",
|
|
565
|
+
/** 无请求数据 */
|
|
566
|
+
none = "none"
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
/**
|
|
570
|
+
* 请求配置。
|
|
571
|
+
*/
|
|
572
|
+
export declare interface RequestConfig<MockUrl extends string = string, DevUrl extends string = string, ProdUrl extends string = string, Path extends string = string, DataKey extends OneOrMore<string> | undefined = OneOrMore<string> | undefined, ParamName extends string = string, QueryName extends string = string, RequestDataOptional extends boolean = boolean> {
|
|
573
|
+
/** 接口 Mock 地址,结尾无 `/` */
|
|
574
|
+
mockUrl: MockUrl;
|
|
575
|
+
/** 接口测试环境地址,结尾无 `/` */
|
|
576
|
+
devUrl: DevUrl;
|
|
577
|
+
/** 接口生产环境地址,结尾无 `/` */
|
|
578
|
+
prodUrl: ProdUrl;
|
|
579
|
+
/** 接口路径,以 `/` 开头 */
|
|
580
|
+
path: Path;
|
|
581
|
+
/** 请求方法 */
|
|
582
|
+
method: Method;
|
|
583
|
+
/** 请求头,除了 Content-Type 的所有头 */
|
|
584
|
+
requestHeaders: Record<string, string>;
|
|
585
|
+
/** 请求数据类型 */
|
|
586
|
+
requestBodyType: RequestBodyType;
|
|
587
|
+
/** 返回数据类型 */
|
|
588
|
+
responseBodyType: ResponseBodyType;
|
|
589
|
+
/** 数据所在键 */
|
|
590
|
+
dataKey: DataKey;
|
|
591
|
+
/** 路径参数的名称列表 */
|
|
592
|
+
paramNames: ParamName[];
|
|
593
|
+
/** 查询参数的名称列表 */
|
|
594
|
+
queryNames: QueryName[];
|
|
595
|
+
/** 请求数据是否可选 */
|
|
596
|
+
requestDataOptional: RequestDataOptional;
|
|
597
|
+
/** 请求数据的 JSON Schema (仅开启了 JSON Schema 生成时生效) */
|
|
598
|
+
requestDataJsonSchema: JSONSchema4;
|
|
599
|
+
/** 返回数据的 JSON Schema (仅开启了 JSON Schema 生成时生效) */
|
|
600
|
+
responseDataJsonSchema: JSONSchema4;
|
|
601
|
+
/** 请求函数名称 */
|
|
602
|
+
requestFunctionName: string;
|
|
603
|
+
/** 如何格式化查询字符串中的数组值 */
|
|
604
|
+
queryStringArrayFormat: QueryStringArrayFormat;
|
|
605
|
+
/** 额外信息 */
|
|
606
|
+
extraInfo: Record<string, any>;
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
/** 请求表单条目类型 */
|
|
610
|
+
export declare enum RequestFormItemType {
|
|
611
|
+
/** 纯文本 */
|
|
612
|
+
text = "text",
|
|
613
|
+
/** 文件 */
|
|
614
|
+
file = "file"
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
/**
|
|
618
|
+
* 请求参数。
|
|
619
|
+
*/
|
|
620
|
+
export declare interface RequestFunctionParams extends RequestConfig {
|
|
621
|
+
/** 原始数据 */
|
|
622
|
+
rawData: Record<string, any>;
|
|
623
|
+
/** 请求数据,不含文件数据 */
|
|
624
|
+
data: Record<string, any>;
|
|
625
|
+
/** 是否有文件数据 */
|
|
626
|
+
hasFileData: boolean;
|
|
627
|
+
/** 请求文件数据 */
|
|
628
|
+
fileData: Record<string, any>;
|
|
629
|
+
/** 所有请求数据,包括 data、fileData */
|
|
630
|
+
allData: Record<string, any>;
|
|
631
|
+
/** 获取全部请求数据(包含文件)的 FormData 实例 */
|
|
632
|
+
getFormData: () => FormData;
|
|
633
|
+
}
|
|
634
|
+
|
|
635
|
+
/** 请求函数的额外参数 */
|
|
636
|
+
export declare type RequestFunctionRestArgs<T extends Function> = T extends (payload: any, ...args: infer R) => any ? R : never;
|
|
637
|
+
|
|
638
|
+
/** 请求路径参数类型 */
|
|
639
|
+
export declare enum RequestParamType {
|
|
640
|
+
/** 字符串 */
|
|
641
|
+
string = "string",
|
|
642
|
+
/** 数字 */
|
|
643
|
+
number = "number"
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
/** 请求查询参数类型 */
|
|
647
|
+
export declare enum RequestQueryType {
|
|
648
|
+
/** 字符串 */
|
|
649
|
+
string = "string",
|
|
650
|
+
/** 数字 */
|
|
651
|
+
number = "number"
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
/** 是否必需 */
|
|
655
|
+
declare enum Required_2 {
|
|
656
|
+
/** 不必需 */
|
|
657
|
+
false = "0",
|
|
658
|
+
/** 必需 */
|
|
659
|
+
true = "1"
|
|
660
|
+
}
|
|
661
|
+
export { Required_2 as Required }
|
|
662
|
+
|
|
663
|
+
/** 返回数据类型 */
|
|
664
|
+
export declare enum ResponseBodyType {
|
|
665
|
+
/** JSON */
|
|
666
|
+
json = "json",
|
|
667
|
+
/** 纯文本 */
|
|
668
|
+
text = "text",
|
|
669
|
+
/** XML */
|
|
670
|
+
xml = "xml",
|
|
671
|
+
/** 原始数据 */
|
|
672
|
+
raw = "raw"
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
/**
|
|
676
|
+
* 服务器的配置。
|
|
677
|
+
*/
|
|
678
|
+
export declare interface ServerConfig extends SharedConfig {
|
|
679
|
+
/**
|
|
680
|
+
* 服务地址。若服务类型为 `yapi`,此处填其首页地址;若服务类型为 `swagger`,此处填其 json 地址。
|
|
681
|
+
*
|
|
682
|
+
* @example 'http://yapi.foo.bar'
|
|
683
|
+
*/
|
|
684
|
+
serverUrl: string;
|
|
685
|
+
/**
|
|
686
|
+
* 服务类型。
|
|
687
|
+
*
|
|
688
|
+
* @default 'yapi'
|
|
689
|
+
*/
|
|
690
|
+
serverType?: 'yapi' | 'swagger';
|
|
691
|
+
/**
|
|
692
|
+
* 项目列表。
|
|
693
|
+
*/
|
|
694
|
+
projects: ProjectConfig[];
|
|
695
|
+
}
|
|
696
|
+
|
|
697
|
+
/**
|
|
698
|
+
* 共享的配置。
|
|
699
|
+
*/
|
|
700
|
+
export declare interface SharedConfig {
|
|
701
|
+
/**
|
|
702
|
+
* 要生成的目标代码类型。
|
|
703
|
+
* 默认为 `typescript`,若设为 `javascript`,会将生成的 `.ts` 文件转换为 `.js` + `.d.ts` 文件并删除原 `.ts` 文件。
|
|
704
|
+
*
|
|
705
|
+
* @default 'typescript'
|
|
706
|
+
*/
|
|
707
|
+
target?: 'typescript' | 'javascript';
|
|
708
|
+
/**
|
|
709
|
+
* 是否只生成接口请求内容和返回内容的 TypeSript 类型,是则请求文件和请求函数都不会生成。
|
|
710
|
+
*
|
|
711
|
+
* @default false
|
|
712
|
+
*/
|
|
713
|
+
typesOnly?: boolean;
|
|
714
|
+
/**
|
|
715
|
+
* 测试环境名称。
|
|
716
|
+
*
|
|
717
|
+
* **用于获取测试环境域名。**
|
|
718
|
+
*
|
|
719
|
+
* 获取方式:打开项目 --> `设置` --> `环境配置` --> 点开或新增测试环境 --> 复制测试环境名称。
|
|
720
|
+
*
|
|
721
|
+
* @example 'dev'
|
|
722
|
+
*/
|
|
723
|
+
devEnvName?: string;
|
|
724
|
+
/**
|
|
725
|
+
* 生产环境名称。
|
|
726
|
+
*
|
|
727
|
+
* **用于获取生产环境域名。**
|
|
728
|
+
*
|
|
729
|
+
* 获取方式:打开项目 --> `设置` --> `环境配置` --> 点开或新增生产环境 --> 复制生产环境名称。
|
|
730
|
+
*
|
|
731
|
+
* @example 'prod'
|
|
732
|
+
*/
|
|
733
|
+
prodEnvName?: string;
|
|
734
|
+
/**
|
|
735
|
+
* 输出文件路径。
|
|
736
|
+
*
|
|
737
|
+
* 可以是 `相对路径` 或 `绝对路径`。
|
|
738
|
+
*
|
|
739
|
+
* @example 'src/api/index.ts'
|
|
740
|
+
*/
|
|
741
|
+
outputFilePath?: string | ((interfaceInfo: Interface, changeCase: ChangeCase) => string);
|
|
742
|
+
/**
|
|
743
|
+
* 请求函数文件路径。
|
|
744
|
+
*
|
|
745
|
+
* @default 与 `outputFilePath` 同级目录下的 `request.ts` 文件
|
|
746
|
+
* @example 'src/api/request.ts'
|
|
747
|
+
*/
|
|
748
|
+
requestFunctionFilePath?: string;
|
|
749
|
+
/**
|
|
750
|
+
* 如果接口响应的结果是 `JSON` 对象,
|
|
751
|
+
* 且我们想要的数据在该对象下,
|
|
752
|
+
* 那我们就可将 `dataKey` 设为我们想要的数据对应的键。
|
|
753
|
+
*
|
|
754
|
+
* 比如该对象为 `{ code: 0, msg: '成功', data: 100 }`,
|
|
755
|
+
* 我们想要的数据为 `100`,
|
|
756
|
+
* 则我们可将 `dataKey` 设为 `data`。
|
|
757
|
+
*
|
|
758
|
+
* @example 'data'
|
|
759
|
+
*/
|
|
760
|
+
dataKey?: OneOrMore<string>;
|
|
761
|
+
/**
|
|
762
|
+
* 支持生成 React Hooks 代码的相关配置。
|
|
763
|
+
*/
|
|
764
|
+
reactHooks?: ReactHooksConfig;
|
|
765
|
+
/**
|
|
766
|
+
* 支持生成 JSON Schema 的相关配置。
|
|
767
|
+
*/
|
|
768
|
+
jsonSchema?: JsonSchemaConfig;
|
|
769
|
+
/**
|
|
770
|
+
* 支持生成注释的相关配置。
|
|
771
|
+
*/
|
|
772
|
+
comment?: CommentConfig;
|
|
773
|
+
/**
|
|
774
|
+
* 将自定义类型转为 JSONSchema 类型的映射表,自定义类型名称大小写不敏感。
|
|
775
|
+
*/
|
|
776
|
+
customTypeMapping?: Record<string, JSONSchema4TypeName>;
|
|
777
|
+
/**
|
|
778
|
+
* 如何格式化查询字符串中的数组值。
|
|
779
|
+
*
|
|
780
|
+
* @default QueryStringArrayFormat.brackets
|
|
781
|
+
*/
|
|
782
|
+
queryStringArrayFormat?: QueryStringArrayFormat;
|
|
783
|
+
/**
|
|
784
|
+
* 设置传给请求函数的参数中的 extraInfo 的值。
|
|
785
|
+
*
|
|
786
|
+
* @param interfaceInfo 接口信息
|
|
787
|
+
* @param changeCase 常用的大小写转换函数集合对象
|
|
788
|
+
* @returns 返回要赋给 extraInfo 的值
|
|
789
|
+
*/
|
|
790
|
+
setRequestFunctionExtraInfo?(interfaceInfo: Interface, changeCase: ChangeCase): Record<string, any>;
|
|
791
|
+
/**
|
|
792
|
+
* 预处理接口信息,返回新的接口信息。可返回 false 排除当前接口。
|
|
793
|
+
*
|
|
794
|
+
* 譬如你想对接口的 `path` 进行某些处理或者想排除某些接口,就可使用该方法。
|
|
795
|
+
*
|
|
796
|
+
* @param interfaceInfo 接口信息
|
|
797
|
+
* @param changeCase 常用的大小写转换函数集合对象
|
|
798
|
+
* @param syntheticalConfig 作用到当前接口的最终配置
|
|
799
|
+
* @example
|
|
800
|
+
*
|
|
801
|
+
* ```js
|
|
802
|
+
* interfaceInfo => {
|
|
803
|
+
* interfaceInfo.path = interfaceInfo.path.replace('v1', 'v2')
|
|
804
|
+
* return interfaceInfo
|
|
805
|
+
* }
|
|
806
|
+
* ```
|
|
807
|
+
*/
|
|
808
|
+
preproccessInterface?(interfaceInfo: Interface, changeCase: ChangeCase, syntheticalConfig: SyntheticalConfig): Interface | false;
|
|
809
|
+
/**
|
|
810
|
+
* 获取请求函数的名称。
|
|
811
|
+
*
|
|
812
|
+
* @default changeCase.camelCase(interfaceInfo.parsedPath.name)
|
|
813
|
+
* @param interfaceInfo 接口信息
|
|
814
|
+
* @param changeCase 常用的大小写转换函数集合对象
|
|
815
|
+
* @returns 请求函数的名称
|
|
816
|
+
*/
|
|
817
|
+
getRequestFunctionName?(interfaceInfo: ExtendedInterface, changeCase: ChangeCase): string;
|
|
818
|
+
/**
|
|
819
|
+
* 获取请求数据类型的名称。
|
|
820
|
+
*
|
|
821
|
+
* @default changeCase.pascalCase(`${requestFunctionName}Request`)
|
|
822
|
+
* @param interfaceInfo 接口信息
|
|
823
|
+
* @param changeCase 常用的大小写转换函数集合对象
|
|
824
|
+
* @returns 请求数据类型的名称
|
|
825
|
+
*/
|
|
826
|
+
getRequestDataTypeName?(interfaceInfo: ExtendedInterface, changeCase: ChangeCase): string;
|
|
827
|
+
/**
|
|
828
|
+
* 获取响应数据类型的名称。
|
|
829
|
+
*
|
|
830
|
+
* @default changeCase.pascalCase(`${requestFunctionName}Response`)
|
|
831
|
+
* @param interfaceInfo 接口信息
|
|
832
|
+
* @param changeCase 常用的大小写转换函数集合对象
|
|
833
|
+
* @returns 响应数据类型的名称
|
|
834
|
+
*/
|
|
835
|
+
getResponseDataTypeName?(interfaceInfo: ExtendedInterface, changeCase: ChangeCase): string;
|
|
836
|
+
}
|
|
837
|
+
|
|
838
|
+
/** 混合的配置。 */
|
|
839
|
+
export declare type SyntheticalConfig = Partial<ServerConfig & ServerConfig['projects'][0] & ServerConfig['projects'][0]['categories'][0] & {
|
|
840
|
+
mockUrl: string;
|
|
841
|
+
devUrl: string;
|
|
842
|
+
prodUrl: string;
|
|
843
|
+
}>;
|
|
844
|
+
|
|
845
|
+
export { }
|