open-api-typescript-request-generator 0.0.2 → 0.0.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/README.md CHANGED
@@ -1,21 +1,21 @@
1
1
  # open-api-typescript-request-generator
2
2
 
3
- 基于 openapi 规范,生成接口的 requestresponse 类型声明和请求方法体
3
+ Based on the OpenAPI specification, generate request and response type declarations and request method bodies for APIs
4
4
 
5
- ## 安装
5
+ ## Installation
6
6
 
7
7
  `npm install open-api-typescript-request-generator`
8
8
 
9
- ## 初始化生成配置文件
9
+ ## Initialize Configuration File
10
10
 
11
11
  `apits-gener init`
12
12
 
13
13
 
14
- ## 生成接口
14
+ ## Generate APIs
15
15
 
16
16
  `apits-gener gen`
17
17
 
18
- ## 只生成指定 name 的配置
18
+ ## Generate Only Specified Name Configuration
19
19
 
20
20
  ```bash
21
21
  apits-gener gen --name=test
@@ -23,5 +23,5 @@ apits-gener gen --name=test
23
23
  apits-gener gen -n test
24
24
  ```
25
25
 
26
- # tips
27
- 1. 如果有代码格式要求,应该在配置中忽略/api中生成的代码
26
+ # Tips
27
+ 1. If you have code formatting requirements, you should ignore the generated code in /api in your configuration
package/es/Generator.js CHANGED
@@ -676,9 +676,9 @@ function () {
676
676
  }
677
677
 
678
678
  code = (0, vtils_1.dedent)(templateObject_6 || (templateObject_6 = __makeTemplateObject(["\n ", "\n ", "\n\n ", "\n ", "\n\n ", "\n "], ["\n ", "\n ", "\n\n ", "\n ", "\n\n ", "\n "])), genComment(function (title) {
679
- return "".concat(title, " \u8BF7\u6C42\u53C2\u6570");
679
+ return "".concat(title, " request parameters");
680
680
  }), requestDataType.trim(), genComment(function (title) {
681
- return "".concat(title, " \u54CD\u5E94\u6570\u636E");
681
+ return "".concat(title, " response data");
682
682
  }), responseDataType.trim(), (0, vtils_1.dedent)(templateObject_5 || (templateObject_5 = __makeTemplateObject(["\n ", "\n ", "\n "], ["\n ", "\n ", "\n "])), genComment(function (title) {
683
683
  return "".concat(title);
684
684
  }), requestFunctionTemplate({
package/es/types.d.ts CHANGED
@@ -5,130 +5,130 @@ import { OpenAPIV3 } from 'openapi-types';
5
5
  import { LiteralUnion, OmitStrict } from 'vtils/types';
6
6
  import { ParsedPath } from 'path';
7
7
  export declare type requestFunctionTemplateType = (props: RequestFunctionTemplateProps, config?: SyntheticalConfig) => string;
8
- /** 顶部依赖生成模板函数 */
9
- /** 生成器参数 */
8
+ /** Top dependency generation template function */
9
+ /** Generator parameters */
10
10
  export declare type topImportTemplateType = () => string;
11
11
  export interface GeneratorOptions {
12
12
  cwd: string;
13
13
  }
14
- /** 项目信息 */
14
+ /** Project information */
15
15
  export interface Project {
16
16
  /** ID */
17
17
  _id: number;
18
- /** 名称 */
18
+ /** Name */
19
19
  name: string;
20
- /** 描述 */
20
+ /** Description */
21
21
  desc: string;
22
- /** 基本路径 */
22
+ /** Base path */
23
23
  basepath: string;
24
- /** 标签 */
24
+ /** Tags */
25
25
  tag: string[];
26
- /** 环境配置 */
26
+ /** Environment configuration */
27
27
  env: Array<{
28
- /** 环境名称 */
28
+ /** Environment name */
29
29
  name: string;
30
- /** 环境域名 */
30
+ /** Environment domain */
31
31
  domain: string;
32
32
  }>;
33
- /** 项目token */
33
+ /** Project token */
34
34
  token?: string;
35
- /** 项目下分类列表 */
35
+ /** Category list under the project */
36
36
  cat: Category[];
37
37
  components?: object[];
38
38
  }
39
- /** 接口定义 */
39
+ /** Interface definition */
40
40
  export interface Interface {
41
- /** 接口 ID */
41
+ /** Interface ID */
42
42
  _id: number;
43
- /** 所属分类信息(由 YTT 自行实现) */
43
+ /** Category information (implemented by YTT) */
44
44
  _category: OmitStrict<Category, 'list'>;
45
- /** 所属项目信息(由 YTT 自行实现) */
45
+ /** Project information (implemented by YTT) */
46
46
  _project: Project;
47
- /** 接口名称 */
47
+ /** Interface name */
48
48
  title: string;
49
- /** 状态 */
49
+ /** Status */
50
50
  status: LiteralUnion<'done' | 'undone', string>;
51
- /** 接口备注 */
51
+ /** Interface remarks */
52
52
  markdown: string;
53
- /** 请求路径 */
53
+ /** Request path */
54
54
  path: string;
55
- /** 请求方式,HEADOPTIONS 处理与 GET 相似,其余处理与 POST 相似 */
55
+ /** Request method, HEAD and OPTIONS are handled like GET, others like POST */
56
56
  method: Method;
57
- /** 所属项目 id */
57
+ /** Project ID */
58
58
  project_id: number;
59
- /** 所属分类 id */
59
+ /** Category ID */
60
60
  catid: number;
61
- /** 标签列表 */
61
+ /** Tag list */
62
62
  tag: string[];
63
- /** 请求头 */
63
+ /** Request headers */
64
64
  req_headers: Array<{
65
- /** 名称 */
65
+ /** Name */
66
66
  name: string;
67
- /** */
67
+ /** Value */
68
68
  value: string;
69
- /** 备注 */
69
+ /** Description */
70
70
  desc: string;
71
- /** 示例 */
71
+ /** Example */
72
72
  example: string;
73
- /** 是否必需 */
73
+ /** Required */
74
74
  required: Required;
75
75
  }>;
76
- /** 路径参数 */
76
+ /** Path parameters */
77
77
  req_params: Array<{
78
- /** 名称 */
78
+ /** Name */
79
79
  name: string;
80
- /** 备注 */
80
+ /** Description */
81
81
  desc: string;
82
- /** 示例 */
82
+ /** Example */
83
83
  example: string;
84
- /** 类型(YApi-X */
84
+ /** Type (YApi-X) */
85
85
  type?: RequestParamType;
86
86
  }>;
87
- /** GET:请求串 */
87
+ /** GET only: query string */
88
88
  req_query: Array<{
89
- /** 名称 */
89
+ /** Name */
90
90
  name: string;
91
- /** 备注 */
91
+ /** Description */
92
92
  desc: string;
93
- /** 示例 */
93
+ /** Example */
94
94
  example: string;
95
- /** 是否必需 */
95
+ /** Required */
96
96
  required: Required;
97
- /** 类型(YApi-X */
97
+ /** Type (YApi-X) */
98
98
  type?: RequestQueryType;
99
99
  }>;
100
- /** POST:请求内容类型。为 text, file, raw 时不必特殊处理。 */
100
+ /** POST only: request content type. No special handling needed for text, file, raw. */
101
101
  req_body_type: RequestBodyType;
102
- /** `req_body_type = json` 时是否为 json schema */
102
+ /** Whether it is json schema when `req_body_type = json` */
103
103
  req_body_is_json_schema: boolean;
104
- /** `req_body_type = form` 时的请求内容 */
104
+ /** Request content when `req_body_type = form` */
105
105
  req_body_form: Array<{
106
- /** 名称 */
106
+ /** Name */
107
107
  name: string;
108
- /** 类型 */
108
+ /** Type */
109
109
  type: RequestFormItemType;
110
- /** 备注 */
110
+ /** Description */
111
111
  desc: string;
112
- /** 示例 */
112
+ /** Example */
113
113
  example: string;
114
- /** 是否必需 */
114
+ /** Required */
115
115
  required: Required;
116
116
  }>;
117
- /** `req_body_type = json` 时的请求内容 */
117
+ /** Request content when `req_body_type = json` */
118
118
  req_body_other: string;
119
- /** 返回数据类型 */
119
+ /** Response data type */
120
120
  res_body_type: ResponseBodyType;
121
- /** `res_body_type = json` 时是否为 json schema */
121
+ /** Whether it is json schema when `res_body_type = json` */
122
122
  res_body_is_json_schema: boolean;
123
- /** 返回数据 */
123
+ /** Response data */
124
124
  res_body: string;
125
- /** 创建时间(unix时间戳) */
125
+ /** Creation time (unix timestamp) */
126
126
  add_time: number;
127
- /** 更新时间(unix时间戳) */
127
+ /** Update time (unix timestamp) */
128
128
  up_time: number;
129
129
  [key: string]: any;
130
130
  }
131
- /** 接口基本信息 */
131
+ /** Interface basic information */
132
132
  export interface BaseInterfaceInfo {
133
133
  edit_uid: number;
134
134
  status: string;
@@ -143,21 +143,21 @@ export interface BaseInterfaceInfo {
143
143
  uid: number;
144
144
  add_time: number;
145
145
  }
146
- /** 接口列表 */
146
+ /** Interface list */
147
147
  export declare type InterfaceList = Interface[];
148
- /** 分类信息 */
148
+ /** Category information */
149
149
  export interface Category {
150
150
  /** ID */
151
151
  _id: number;
152
- /** 分类名称 */
152
+ /** Category name */
153
153
  name: string;
154
- /** 分类备注 */
154
+ /** Category description */
155
155
  desc: string;
156
- /** 分类接口列表 */
156
+ /** Interface list in this category */
157
157
  list: InterfaceList;
158
- /** 创建时间(unix时间戳) */
158
+ /** Creation time (unix timestamp) */
159
159
  add_time: number;
160
- /** 更新时间(unix时间戳) */
160
+ /** Update time (unix timestamp) */
161
161
  up_time: number;
162
162
  }
163
163
  export interface ChangeCase {
@@ -237,7 +237,7 @@ export interface ChangeCase {
237
237
  */
238
238
  upperCaseFirst: (value: string) => string;
239
239
  }
240
- /** 请求方式 */
240
+ /** Request method */
241
241
  export declare enum Method {
242
242
  GET = "GET",
243
243
  POST = "POST",
@@ -247,201 +247,201 @@ export declare enum Method {
247
247
  OPTIONS = "OPTIONS",
248
248
  PATCH = "PATCH"
249
249
  }
250
- /** 是否必需 */
250
+ /** Required */
251
251
  export declare enum Required {
252
- /** 不必需 */
252
+ /** Not required */
253
253
  false = "0",
254
- /** 必需 */
254
+ /** Required */
255
255
  true = "1"
256
256
  }
257
- /** 请求数据类型 */
257
+ /** Request body type */
258
258
  export declare enum RequestBodyType {
259
- /** 查询字符串 */
259
+ /** Query string */
260
260
  query = "query",
261
- /** 表单 */
261
+ /** Form */
262
262
  form = "form",
263
263
  /** JSON */
264
264
  json = "json",
265
- /** 纯文本 */
265
+ /** Plain text */
266
266
  text = "text",
267
- /** 文件 */
267
+ /** File */
268
268
  file = "file",
269
- /** 原始数据 */
269
+ /** Raw data */
270
270
  raw = "raw",
271
- /** 无请求数据 */
271
+ /** No request data */
272
272
  none = "none"
273
273
  }
274
- /** 请求路径参数类型 */
274
+ /** Request path parameter type */
275
275
  export declare enum RequestParamType {
276
- /** 字符串 */
276
+ /** String */
277
277
  string = "string",
278
- /** 数字 */
278
+ /** Number */
279
279
  number = "number"
280
280
  }
281
- /** 请求查询参数类型 */
281
+ /** Request query parameter type */
282
282
  export declare enum RequestQueryType {
283
- /** 字符串 */
283
+ /** String */
284
284
  string = "string",
285
- /** 数字 */
285
+ /** Number */
286
286
  number = "number"
287
287
  }
288
- /** 请求表单条目类型 */
288
+ /** Request form item type */
289
289
  export declare enum RequestFormItemType {
290
- /** 纯文本 */
290
+ /** Plain text */
291
291
  text = "text",
292
- /** 文件 */
292
+ /** File */
293
293
  file = "file"
294
294
  }
295
- /** 返回数据类型 */
295
+ /** Response body type */
296
296
  export declare enum ResponseBodyType {
297
297
  /** JSON */
298
298
  json = "json",
299
- /** 纯文本 */
299
+ /** Plain text */
300
300
  text = "text",
301
301
  /** XML */
302
302
  xml = "xml",
303
- /** 原始数据 */
303
+ /** Raw data */
304
304
  raw = "raw"
305
305
  /** JSON Schema */
306
306
  }
307
- /** 扩展接口定义 */
307
+ /** Extended interface definition */
308
308
  export interface ExtendedInterface extends Interface {
309
309
  parsedPath: ParsedPath;
310
310
  }
311
- /** 分类列表,对应数据导出的 json 内容 */
311
+ /** Category list, corresponding to exported json content */
312
312
  export declare type CategoryList = Category[];
313
- /** 支持生成 JSON Schema 的相关配置 */
313
+ /** Configuration for generating JSON Schema */
314
314
  export interface JsonSchemaConfig {
315
315
  /**
316
- * 是否开启该项功能。
316
+ * Whether to enable this feature.
317
317
  */
318
318
  enabled: boolean;
319
319
  }
320
- /** 支持生成注释的相关配置 */
320
+ /** Configuration for generating comments */
321
321
  export interface CommentConfig {
322
322
  /**
323
- * 是否开启该项功能。
323
+ * Whether to enable this feature.
324
324
  *
325
325
  * @default true
326
326
  */
327
327
  enabled?: boolean;
328
328
  /**
329
- * 是否有标题。
329
+ * Whether to include title.
330
330
  *
331
331
  * @default true
332
332
  */
333
333
  title?: boolean;
334
334
  /**
335
- * 是否有分类名称。
335
+ * Whether to include category name.
336
336
  *
337
337
  * @default true
338
338
  */
339
339
  category?: boolean;
340
340
  /**
341
- * 是否有标签。
341
+ * Whether to include tags.
342
342
  *
343
343
  * @default true
344
344
  */
345
345
  tag?: boolean;
346
346
  /**
347
- * 是否有请求头。
347
+ * Whether to include request headers.
348
348
  *
349
349
  * @default true
350
350
  */
351
351
  requestHeader?: boolean;
352
352
  /**
353
- * 是否有更新时间。
353
+ * Whether to include update time.
354
354
  *
355
355
  * @default true
356
356
  */
357
357
  updateTime?: boolean;
358
358
  /**
359
- * 是否为标题、分类名称添加链接。
359
+ * Whether to add links to title and category name.
360
360
  *
361
361
  * @default true
362
362
  */
363
363
  link?: boolean;
364
364
  }
365
365
  /**
366
- * 共享的配置。
366
+ * Shared configuration.
367
367
  */
368
368
  export interface SharedConfig {
369
369
  /**
370
- * 输出文件路径。
370
+ * Output file path.
371
371
  *
372
- * 可以是 `相对路径` `绝对路径`。
372
+ * Can be `relative path` or `absolute path`.
373
373
  *
374
374
  * @example 'src/api/index.ts'
375
375
  */
376
376
  outputFilePath?: string;
377
377
  /**
378
- * 请求函数文件路径。
378
+ * Request function file path.
379
379
  *
380
- * @default `outputFilePath` 同级目录下的 `request.ts` 文件
380
+ * @default `request.ts` file in the same directory as `outputFilePath`
381
381
  * @example 'src/api/request.ts'
382
382
  */
383
383
  requestFunctionFilePath?: string;
384
384
  /**
385
- * 支持生成 JSON Schema 的相关配置。
385
+ * Configuration for generating JSON Schema.
386
386
  */
387
387
  /**
388
- * 支持生成注释的相关配置。
388
+ * Configuration for generating comments.
389
389
  */
390
390
  comment?: CommentConfig;
391
391
  /**
392
- * 获取请求函数的名称。
392
+ * Get the name of the request function.
393
393
  *
394
394
  * @default changeCase.camelCase(interfaceInfo.parsedPath.name)
395
- * @param interfaceInfo 接口信息
396
- * @param changeCase 常用的大小写转换函数集合对象
397
- * @returns 请求函数的名称
395
+ * @param interfaceInfo Interface information
396
+ * @param changeCase Collection of common case conversion functions
397
+ * @returns Name of the request function
398
398
  */
399
399
  getRequestFunctionName?(interfaceInfo: ExtendedInterface, changeCase: ChangeCase): string;
400
400
  /**
401
- * 获取请求数据类型的名称。
401
+ * Get the name of the request data type.
402
402
  *
403
403
  * @default changeCase.pascalCase(`${requestFunctionName}Request`)
404
- * @param interfaceInfo 接口信息
405
- * @param changeCase 常用的大小写转换函数集合对象
406
- * @returns 请求数据类型的名称
404
+ * @param interfaceInfo Interface information
405
+ * @param changeCase Collection of common case conversion functions
406
+ * @returns Name of the request data type
407
407
  */
408
408
  getRequestDataTypeName?(interfaceInfo: ExtendedInterface, changeCase: ChangeCase): string;
409
409
  /**
410
- * 获取响应数据类型的名称。
410
+ * Get the name of the response data type.
411
411
  *
412
412
  * @default changeCase.pascalCase(`${requestFunctionName}Response`)
413
- * @param interfaceInfo 接口信息
414
- * @param changeCase 常用的大小写转换函数集合对象
415
- * @returns 响应数据类型的名称
413
+ * @param interfaceInfo Interface information
414
+ * @param changeCase Collection of common case conversion functions
415
+ * @returns Name of the response data type
416
416
  */
417
417
  getResponseDataTypeName?(interfaceInfo: ExtendedInterface, changeCase: ChangeCase): string;
418
418
  }
419
419
  /**
420
- * 服务器的配置。
420
+ * Server configuration.
421
421
  */
422
422
  export interface ApiConfig {
423
423
  name: string;
424
424
  configIndex?: number;
425
425
  /**
426
- * 服务地址。此处填其 swagger json 地址。
427
- * 比如nestjs项目一般为http://localhost:3041/api-json
426
+ * Server URL. Enter the swagger json address here.
427
+ * For example, nestjs projects usually use http://localhost:3041/api-json
428
428
  *
429
429
  */
430
430
  serverUrl: string;
431
431
  /**
432
- * 输出文件路径。
432
+ * Output file path.
433
433
  *
434
- * 可以是 `相对路径` `绝对路径`。
434
+ * Can be `relative path` or `absolute path`.
435
435
  *
436
436
  * @example 'src/api/index.ts'
437
437
  */
438
438
  outputFilePath?: string;
439
439
  /**
440
- * 设置接口的baseURL
440
+ * Set the baseURL for the interface
441
441
  *
442
- * @description 若要配置使用运行时代码,则增加`[code]:`前缀
442
+ * @description To configure runtime code, add the `[code]:` prefix
443
443
  ```
444
- 例:
444
+ Example:
445
445
  baseURL: "[code]:process.env.BASE_URL" => baseURL:process.env.BASE_URL
446
446
 
447
447
  baseURL: "http://localhost:3000" => baseURL:"http://localhost:3000"
@@ -449,82 +449,82 @@ export interface ApiConfig {
449
449
  */
450
450
  baseURL?: ((path: string) => string | undefined) | string;
451
451
  /**
452
- * 在每个生成的api文件顶部定义一段代码
453
- * 例如:引入自定义request函数
452
+ * Define a code snippet at the top of each generated api file
453
+ * For example: import custom request function
454
454
  * default: import request from './request'
455
455
  */
456
456
  topImportTemplate?: topImportTemplateType;
457
457
  /**
458
- * 是否使用默认请求库,关闭后不再生成request.ts
458
+ * Whether to use the default request library, request.ts will not be generated after disabling, default: true
459
459
  */
460
460
  defaultRequestLib?: boolean;
461
461
  }
462
- /** 混合的配置。 */
462
+ /** Combined configuration. */
463
463
  export declare type SyntheticalConfig = Partial<ApiConfig & {
464
464
  components: OpenAPIV3.Document['components'];
465
465
  }>;
466
- /** 配置。 */
466
+ /** Configuration. */
467
467
  export declare type Config = ApiConfig;
468
468
  /**
469
- * 请求配置。
469
+ * Request configuration.
470
470
  */
471
471
  export interface RequestConfig<Path extends string = string, ParamName extends string = string, QueryName extends string = string, RequestDataOptional extends boolean = boolean> {
472
- /** 接口路径,以 `/` 开头 */
472
+ /** Interface path, starting with `/` */
473
473
  path: Path;
474
- /** 请求方法 */
474
+ /** Request method */
475
475
  method: Method;
476
- /** 请求头,除了 Content-Type 的所有头 */
476
+ /** Request headers, all headers except Content-Type */
477
477
  requestHeaders: Record<string, string>;
478
- /** 请求数据类型 */
478
+ /** Request body type */
479
479
  requestBodyType: RequestBodyType;
480
- /** 返回数据类型 */
480
+ /** Response body type */
481
481
  responseBodyType: ResponseBodyType;
482
- /** 路径参数的名称列表 */
482
+ /** List of path parameter names */
483
483
  paramNames: ParamName[];
484
- /** 查询参数的名称列表 */
484
+ /** List of query parameter names */
485
485
  queryNames: QueryName[];
486
- /** 请求数据是否可选 */
486
+ /** Whether request data is optional */
487
487
  requestDataOptional: RequestDataOptional;
488
- /** 请求数据的 JSON Schema (仅开启了 JSON Schema 生成时生效) */
488
+ /** JSON Schema for request data (only effective when JSON Schema generation is enabled) */
489
489
  requestDataJsonSchema: JSONSchema4;
490
- /** 返回数据的 JSON Schema (仅开启了 JSON Schema 生成时生效) */
490
+ /** JSON Schema for response data (only effective when JSON Schema generation is enabled) */
491
491
  responseDataJsonSchema: JSONSchema4;
492
- /** 请求函数名称 */
492
+ /** Request function name */
493
493
  requestFunctionName: string;
494
494
  }
495
495
  /**
496
- * 请求参数。
496
+ * Request parameters.
497
497
  */
498
498
  export interface RequestFunctionParams extends RequestConfig {
499
- /** 原始数据 */
499
+ /** Raw data */
500
500
  rawData: Record<string, any>;
501
- /** 请求数据,不含文件数据 */
501
+ /** Request data, excluding file data */
502
502
  data: Record<string, any>;
503
- /** 是否有文件数据 */
503
+ /** Whether there is file data */
504
504
  hasFileData: boolean;
505
- /** 请求文件数据 */
505
+ /** Request file data */
506
506
  fileData: Record<string, any>;
507
- /** 所有请求数据,包括 datafileData */
507
+ /** All request data, including data and fileData */
508
508
  allData: Record<string, any>;
509
- /** 获取全部请求数据(包含文件)的 FormData 实例 */
509
+ /** Get FormData instance for all request data (including files) */
510
510
  getFormData: () => FormData;
511
511
  }
512
- /** 请求函数的额外参数 */
512
+ /** Additional parameters of the request function */
513
513
  export declare type RequestFunctionRestArgs<T extends Function> = T extends (payload: any, ...args: infer R) => any ? R : never;
514
- /** 属性定义 */
514
+ /** Property definition */
515
515
  export interface PropDefinition {
516
- /** 属性名称 */
516
+ /** Property name */
517
517
  name: string;
518
- /** 是否必需 */
518
+ /** Required */
519
519
  required: boolean;
520
- /** 类型 */
520
+ /** Type */
521
521
  type: JSONSchema4['type'];
522
- /** 注释 */
522
+ /** Comment */
523
523
  comment: string;
524
524
  }
525
- /** 属性定义列表 */
525
+ /** Property definition list */
526
526
  export declare type PropDefinitions = PropDefinition[];
527
- /** 请求函数体生成模板函数 */
527
+ /** Request function body generation template function */
528
528
  export interface RequestFunctionTemplateProps {
529
529
  baseURL?: string;
530
530
  requestFunctionName: string;