/
types.ts
617 lines (571 loc) · 14.4 KB
/
types.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
import { JSONSchema4 } from 'json-schema'
import { LiteralUnion, OmitStrict } from 'vtils/types'
import { ParsedPath } from 'path'
export interface ChangeCase {
/**
* @example
* changeCase.camelCase('test string') // => 'testString'
*/
camelCase: (value: string) => string
/**
* @example
* changeCase.constantCase('test string') // => 'TEST_STRING'
*/
constantCase: (value: string) => string
/**
* @example
* changeCase.dotCase('test string') // => 'test.string'
*/
dotCase: (value: string) => string
/**
* @example
* changeCase.headerCase('test string') // => 'Test-String'
*/
headerCase: (value: string) => string
/**
* @example
* changeCase.lowerCase('TEST STRING') // => 'test string'
*/
lowerCase: (value: string) => string
/**
* @example
* changeCase.lowerCaseFirst('TEST') // => 'tEST'
*/
lowerCaseFirst: (value: string) => string
/**
* @example
* changeCase.paramCase('test string') // => 'test-string'
*/
paramCase: (value: string) => string
/**
* @example
* changeCase.pascalCase('test string') // => 'TestString'
*/
pascalCase: (value: string) => string
/**
* @example
* changeCase.pathCase('test string') // => 'test/string'
*/
pathCase: (value: string) => string
/**
* @example
* changeCase.sentenceCase('testString') // => 'Test string'
*/
sentenceCase: (value: string) => string
/**
* @example
* changeCase.snakeCase('test string') // => 'test_string'
*/
snakeCase: (value: string) => string
/**
* @example
* changeCase.swapCase('Test String') // => 'tEST sTRING'
*/
swapCase: (value: string) => string
/**
* @example
* changeCase.titleCase('a simple test') // => 'A Simple Test'
*/
titleCase: (value: string) => string
/**
* @example
* changeCase.upperCase('test string') // => 'TEST STRING'
*/
upperCase: (value: string) => string
/**
* @example
* changeCase.upperCaseFirst('test') // => 'Test'
*/
upperCaseFirst: (value: string) => string
}
/** 请求方式 */
export enum Method {
GET = 'GET',
POST = 'POST',
PUT = 'PUT',
DELETE = 'DELETE',
HEAD = 'HEAD',
OPTIONS = 'OPTIONS',
PATCH = 'PATCH',
}
/** 是否必需 */
export enum Required {
/** 不必需 */
false = '0',
/** 必需 */
true = '1',
}
/** 请求数据类型 */
export enum RequestBodyType {
/** 查询字符串 */
query = 'query',
/** 表单 */
form = 'form',
/** JSON */
json = 'json',
/** 纯文本 */
text = 'text',
/** 文件 */
file = 'file',
/** 原始数据 */
raw = 'raw',
/** 无请求数据 */
none = 'none',
}
/** 请求表单条目类型 */
export enum RequestFormItemType {
/** 纯文本 */
text = 'text',
/** 文件 */
file = 'file',
}
/** 返回数据类型 */
export enum ResponseBodyType {
/** JSON */
json = 'json',
/** 纯文本 */
text = 'text',
/** XML */
xml = 'xml',
/** 原始数据 */
raw = 'raw',
// yapi 实际上返回的是 json,有另外的字段指示其是否是 json schema
/** JSON Schema */
// jsonSchema = 'json-schema',
}
/** 接口定义 */
export interface Interface {
/** 接口 ID */
_id: number
/** 所属分类信息(由 YTT 自行实现) */
_category: OmitStrict<Category, 'list'>
/** 所属项目信息(由 YTT 自行实现) */
_project: Project
/** 接口名称 */
title: string
/** 状态 */
status: LiteralUnion<'done' | 'undone', string>
/** 接口备注 */
markdown: string
/** 请求路径 */
path: string
/** 请求方式,HEAD、OPTIONS 处理与 GET 相似,其余处理与 POST 相似 */
method: Method
/** 所属项目 id */
project_id: number
/** 所属分类 id */
catid: number
/** 标签列表 */
tag: string[]
/** 路径参数 */
req_params: Array<{
/** 名称 */
name: string
/** 备注 */
desc: string
/** 示例 */
example: string
}>
/** 仅 GET:请求串 */
req_query: Array<{
/** 名称 */
name: string
/** 备注 */
desc: string
/** 示例 */
example: string
/** 是否必需 */
required: Required
}>
/** 仅 POST:请求内容类型。为 text, file, raw 时不必特殊处理。 */
req_body_type: RequestBodyType
/** `req_body_type = json` 时是否为 json schema */
req_body_is_json_schema: boolean
/** `req_body_type = form` 时的请求内容 */
req_body_form: Array<{
/** 名称 */
name: string
/** 类型 */
type: RequestFormItemType
/** 备注 */
desc: string
/** 示例 */
example: string
/** 是否必需 */
required: Required
}>
/** `req_body_type = json` 时的请求内容 */
req_body_other: string
/** 返回数据类型 */
res_body_type: ResponseBodyType
/** `res_body_type = json` 时是否为 json schema */
res_body_is_json_schema: boolean
/** 返回数据 */
res_body: string
/** 创建时间(unix时间戳) */
add_time: number
/** 更新时间(unix时间戳) */
up_time: number
[key: string]: any
}
/** 扩展接口定义 */
export interface ExtendedInterface extends Interface {
parsedPath: ParsedPath
}
/** 接口列表 */
export type InterfaceList = Interface[]
/** 分类信息 */
export interface Category {
/** ID */
_id: number
/** 分类名称 */
name: string
/** 分类备注 */
desc: string
/** 分类接口列表 */
list: InterfaceList
/** 创建时间(unix时间戳) */
add_time: number
/** 更新时间(unix时间戳) */
up_time: number
}
/** 分类列表,对应数据导出的 json 内容 */
export type CategoryList = Category[]
/** 项目信息 */
export interface Project {
/** ID */
_id: number
/** 名称 */
name: string
/** 描述 */
desc: string
/** 基本路径 */
basepath: string
/** 标签 */
tag: string[]
/** 环境配置 */
env: Array<{
/** 环境名称 */
name: string
/** 环境域名 */
domain: string
}>
}
/** 支持生成 React Hooks 代码的相关配置 */
export interface ReactHooksConfig {
/**
* 是否开启该项功能。
*/
enabled: boolean
/**
* 请求 Hook 函数制造者文件路径。
*
* @default 与 `outputFilePath` 同级目录下的 `makeRequestHook.ts` 文件
* @example 'src/api/makeRequestHook.ts'
*/
requestHookMakerFilePath?: string
/**
* 获取请求 Hook 的名称。
*
* @default `use${changeCase.pascalCase(requestFunctionName)}`
* @param interfaceInfo 接口信息
* @param changeCase 常用的大小写转换函数集合对象
* @returns 请求 Hook 的名称
*/
getRequestHookName?(
interfaceInfo: ExtendedInterface,
changeCase: ChangeCase,
): string
}
/** 支持生成 JSON Schema 的相关配置 */
export interface JsonSchemaConfig {
/**
* 是否开启该项功能。
*/
enabled: boolean
/**
* 是否生成请求数据的 JSON Schema。
*
* @default true
*/
requestData?: boolean
/**
* 是否生成返回数据的 JSON Schema。
*
* @default true
*/
responseData?: boolean
}
/**
* 共享的配置。
*/
export interface SharedConfig {
/**
* 要生成的目标代码类型。
* 默认为 `typescript`,若设为 `javascript`,会将生成的 `.ts` 文件转换为 `.js` + `.d.ts` 文件并删除原 `.ts` 文件。
*
* @default 'typescript'
*/
target?: 'typescript' | 'javascript'
/**
* 是否只生成接口请求内容和返回内容的 TypeSript 类型,是则请求文件和请求函数都不会生成。
*
* @default false
*/
typesOnly?: boolean
/**
* 测试环境名称。
*
* **用于获取测试环境域名。**
*
* 获取方式:打开项目 --> `设置` --> `环境配置` --> 点开或新增测试环境 --> 复制测试环境名称。
*
* @example 'dev'
*/
devEnvName?: string
/**
* 生产环境名称。
*
* **用于获取生产环境域名。**
*
* 获取方式:打开项目 --> `设置` --> `环境配置` --> 点开或新增生产环境 --> 复制生产环境名称。
*
* @example 'prod'
*/
prodEnvName?: string
/**
* 输出文件路径。
*
* 可以是 `相对路径` 或 `绝对路径`。
*
* @example 'src/api/index.ts'
*/
outputFilePath?: string
/**
* 请求函数文件路径。
*
* @default 与 `outputFilePath` 同级目录下的 `request.ts` 文件
* @example 'src/api/request.ts'
*/
requestFunctionFilePath?: string
/**
* 如果接口响应的结果是 `JSON` 对象,
* 且我们想要的数据在该对象下,
* 那我们就可将 `dataKey` 设为我们想要的数据对应的键。
*
* 比如该对象为 `{ code: 0, msg: '成功', data: 100 }`,
* 我们想要的数据为 `100`,
* 则我们可将 `dataKey` 设为 `data`。
*
* @example 'data'
*/
dataKey?: string
/**
* 支持生成 React Hooks 代码的相关配置。
*/
reactHooks?: ReactHooksConfig
/**
* 支持生成 JSON Schema 的相关配置。
*/
jsonSchema?: JsonSchemaConfig
/**
* 是否不生成接口的更新时间注释。
*/
noUpdateTimeComment?: boolean
/**
* 预处理接口信息,返回新的接口信息。可返回 false 排除当前接口。
*
* 譬如你想对接口的 `path` 进行某些处理或者想排除某些接口,就可使用该方法。
*
* @example
*
* ```js
* interfaceInfo => {
* interfaceInfo.path = interfaceInfo.path.replace('v1', 'v2')
* return interfaceInfo
* }
* ```
*/
preproccessInterface?(
interfaceInfo: Interface,
changeCase: ChangeCase,
): Interface | false
/**
* 获取请求函数的名称。
*
* @default changeCase.camelCase(interfaceInfo.parsedPath.name)
* @param interfaceInfo 接口信息
* @param changeCase 常用的大小写转换函数集合对象
* @returns 请求函数的名称
*/
getRequestFunctionName?(
interfaceInfo: ExtendedInterface,
changeCase: ChangeCase,
): string
/**
* 获取请求数据类型的名称。
*
* @default changeCase.pascalCase(`${requestFunctionName}Request`)
* @param interfaceInfo 接口信息
* @param changeCase 常用的大小写转换函数集合对象
* @returns 请求数据类型的名称
*/
getRequestDataTypeName?(
interfaceInfo: ExtendedInterface,
changeCase: ChangeCase,
): string
/**
* 获取响应数据类型的名称。
*
* @default changeCase.pascalCase(`${requestFunctionName}Response`)
* @param interfaceInfo 接口信息
* @param changeCase 常用的大小写转换函数集合对象
* @returns 响应数据类型的名称
*/
getResponseDataTypeName?(
interfaceInfo: ExtendedInterface,
changeCase: ChangeCase,
): string
}
/**
* 分类的配置。
*/
export interface CategoryConfig extends SharedConfig {
/**
* 分类 ID,可以设置多个。设为 `0` 时表示全部分类。
*
* 如果需要获取全部分类,同时排除指定分类,可以这样:`[0, -20, -21]`,分类 ID 前面的负号表示排除。
*
* 获取方式:打开项目 --> 点开分类 --> 复制浏览器地址栏 `/api/cat_` 后面的数字。
*
* @example 20
*/
id: number | number[]
}
/**
* 项目的配置。
*/
export interface ProjectConfig extends SharedConfig {
/**
* 项目的唯一标识。支持多个项目。
*
* 获取方式:打开项目 --> `设置` --> `token配置` --> 复制 token。
*
* @example 'e02a47122259d0c1973a9ff81cabb30685d64abc72f39edaa1ac6b6a792a647d'
*/
token: string | string[]
/**
* 分类列表。
*/
categories: CategoryConfig[]
}
/**
* 服务器的配置。
*/
export interface ServerConfig extends SharedConfig {
/**
* 服务地址。若服务类型为 `yapi`,此处填其首页地址;若服务类型为 `swagger`,此处填其 json 地址。
*
* @example 'http://yapi.foo.bar'
*/
serverUrl: string
/**
* 服务类型。
*
* @default 'yapi'
*/
serverType?: 'yapi' | 'swagger'
/**
* 项目列表。
*/
projects: ProjectConfig[]
}
/** 混合的配置。 */
export type SyntheticalConfig = Partial<
ServerConfig &
ServerConfig['projects'][0] &
ServerConfig['projects'][0]['categories'][0] & {
mockUrl: string
devUrl: string
prodUrl: string
}
>
/** 配置。 */
export type Config = ServerConfig | ServerConfig[]
/**
* 请求配置。
*/
export interface RequestConfig<
MockUrl extends string = any,
DevUrl extends string = any,
ProdUrl extends string = any,
Path extends string = any,
DataKey extends string | undefined = any,
ParamName extends string = any,
QueryName extends string = any,
RequestDataOptional extends boolean = any
> {
/** 接口 Mock 地址,结尾无 `/` */
mockUrl: MockUrl
/** 接口测试环境地质,结尾无 `/` */
devUrl: DevUrl
/** 接口生产环境地址,结尾无 `/` */
prodUrl: ProdUrl
/** 接口路径,以 `/` 开头 */
path: Path
/** 请求方法 */
method: Method
/** 请求数据类型 */
requestBodyType: RequestBodyType
/** 返回数据类型 */
responseBodyType: ResponseBodyType
/** 数据所在键 */
dataKey: DataKey
/** 路径参数的名称列表 */
paramNames: ParamName[]
/** 查询参数的名称列表 */
queryNames: QueryName[]
/** 请求数据是否可选 */
requestDataOptional: RequestDataOptional
/** 请求数据的 JSON Schema (仅开启了 JSON Schema 生成时生效) */
requestDataJsonSchema: JSONSchema4
/** 返回数据的 JSON Schema (仅开启了 JSON Schema 生成时生效) */
responseDataJsonSchema: JSONSchema4
}
/**
* 请求参数。
*/
export interface RequestFunctionParams extends RequestConfig {
/** 原始数据 */
rawData: Record<string, any>
/** 请求数据,不含文件数据 */
data: Record<string, any>
/** 是否有文件数据 */
hasFileData: boolean
/** 请求文件数据 */
fileData: Record<string, any>
/** 所有请求数据,包括 data、fileData */
allData: Record<string, any>
/** 获取全部请求数据(包含文件)的 FormData 实例 */
getFormData: () => FormData
}
/** 请求函数的额外参数 */
export type RequestFunctionRestArgs<T extends Function> = T extends (
payload: any,
...args: infer R
) => any
? R
: never
/** 属性定义 */
export interface PropDefinition {
/** 属性名称 */
name: string
/** 是否必需 */
required: boolean
/** 类型 */
type: JSONSchema4['type']
/** 注释 */
comment: string
}
/** 属性定义列表 */
export type PropDefinitions = PropDefinition[]