open-api-typescript-request-generator 0.0.1-alpha.2 → 0.0.2
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 +11 -10
- package/es/Generator.js +26 -192
- package/es/cli.d.ts +3 -1
- package/es/cli.js +63 -23
- package/es/constants.d.ts +0 -1
- package/es/constants.js +1 -2
- package/es/genIndex.js +4 -6
- package/es/genRequest.js +4 -10
- package/es/helpers.d.ts +1 -1
- package/es/helpers.js +1 -7
- package/es/responseDataJsonSchemaHandler.js +1 -1
- package/es/types.d.ts +20 -212
- package/es/utils.d.ts +4 -10
- package/es/utils.js +8 -44
- package/lib/Generator.js +26 -192
- package/lib/cli.d.ts +3 -1
- package/lib/cli.js +63 -23
- package/lib/constants.d.ts +0 -1
- package/lib/constants.js +1 -2
- package/lib/genIndex.js +4 -6
- package/lib/genRequest.js +4 -10
- package/lib/helpers.d.ts +1 -1
- package/lib/helpers.js +1 -7
- package/lib/responseDataJsonSchemaHandler.js +1 -1
- package/lib/types.d.ts +20 -212
- package/lib/utils.d.ts +4 -10
- package/lib/utils.js +8 -44
- package/package.json +3 -2
- package/es/requestYapiData.d.ts +0 -153
- package/es/requestYapiData.js +0 -978
- package/lib/requestYapiData.d.ts +0 -153
- package/lib/requestYapiData.js +0 -978
package/es/genRequest.js
CHANGED
|
@@ -176,18 +176,15 @@ var utils_1 = require("./utils");
|
|
|
176
176
|
|
|
177
177
|
exports.default = function (config) {
|
|
178
178
|
return __awaiter(void 0, void 0, void 0, function () {
|
|
179
|
-
var
|
|
179
|
+
var defaultRequestLib, rawRequestFunctionFilePath, content;
|
|
180
180
|
return __generator(this, function (_a) {
|
|
181
181
|
switch (_a.label) {
|
|
182
182
|
case 0:
|
|
183
|
-
|
|
183
|
+
defaultRequestLib = config.defaultRequestLib;
|
|
184
184
|
if (defaultRequestLib === false) return [2
|
|
185
185
|
/*return*/
|
|
186
186
|
];
|
|
187
187
|
rawRequestFunctionFilePath = (0, getOutputPath_1.getOutputFilePath)(config, 'request.ts');
|
|
188
|
-
if (!!config.typesOnly) return [3
|
|
189
|
-
/*break*/
|
|
190
|
-
, 2];
|
|
191
188
|
return [4
|
|
192
189
|
/*yield*/
|
|
193
190
|
, fs_extra_1.default.pathExists(rawRequestFunctionFilePath)];
|
|
@@ -200,11 +197,8 @@ exports.default = function (config) {
|
|
|
200
197
|
];
|
|
201
198
|
}
|
|
202
199
|
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
case 2:
|
|
206
|
-
content = "\n ".concat((0, utils_1.topNotesContent)(), "\n\n import request,{ AxiosRequestConfig } from 'axios'; // axios\u7248\u672C>=0.18.1\n\n const instance = request.create({\n withCredentials: true,\n baseURL: process.env.BASE_URL,\n });\n\n // \u81EA\u5B9A\u4E49request\u62E6\u622A\u5668\n instance.interceptors.request.use((config) => {\n return {\n ...config\n }\n });\n\n // \u81EA\u5B9A\u4E49response\u62E6\u622A\u5668\uFF0C\n // \u6CE8\u610F\uFF1A\u5982\u679C\u4FEE\u6539\u63A5\u53E3\u6B63\u5E38\u8FD4\u56DE\u7684\u7ED3\u6784\uFF0C\u5BF9\u5E94\u7684response\u58F0\u660E\u9700\u8981\u4FEE\u6539\n instance.interceptors.response.use((r) => {\n const { data, status } = r;\n if (status>=200 && status<300) {\n return data;\n }\n return Promise.reject(data);\n });\n\n export default {\n get: <RQ, RP>(url: string, config?: AxiosRequestConfig) => {\n return instance.get<RP>(url, config);\n },\n post: <RQ, RP>(url: string, config?: AxiosRequestConfig) => {\n return instance.post<RP>(url, config);\n }\n };\n");
|
|
207
|
-
fs_extra_1.default.outputFile(rawRequestFunctionFilePath, (0, utils_1.formatContent)((0, vtils_1.dedent)(templateObject_1 || (templateObject_1 = __makeTemplateObject(["", ""], ["", ""])), content), prettierConfigPath));
|
|
200
|
+
content = "\n ".concat((0, utils_1.topNotesContent)(), "\n\n import request,{ AxiosRequestConfig } from 'axios'; // axios\u7248\u672C>=0.18.1\n\n const instance = request.create({\n withCredentials: true,\n baseURL: process.env.BASE_URL,\n });\n\n // \u81EA\u5B9A\u4E49request\u62E6\u622A\u5668\n instance.interceptors.request.use((config) => {\n return {\n ...config\n }\n });\n\n // \u81EA\u5B9A\u4E49response\u62E6\u622A\u5668\uFF0C\n // \u6CE8\u610F\uFF1A\u5982\u679C\u4FEE\u6539\u63A5\u53E3\u6B63\u5E38\u8FD4\u56DE\u7684\u7ED3\u6784\uFF0C\u5BF9\u5E94\u7684response\u58F0\u660E\u9700\u8981\u4FEE\u6539\n instance.interceptors.response.use((res) => {\n const { status } = res;\n if (status >= 200 && status < 300) {\n return res;\n }\n return Promise.reject(res);\n });\n\n export default {\n get: <RQ, RP>(url: string, config?: AxiosRequestConfig) => {\n return instance.get<RP>(url, config);\n },\n post: <RQ, RP>(url: string, config?: AxiosRequestConfig) => {\n return instance.post<RP>(url, config);\n },\n head: <RQ, RP>(url: string, config?: AxiosRequestConfig) => {\n return instance.head<RP>(url, config);\n },\n put: <RQ, RP>(url: string, config?: AxiosRequestConfig) => {\n return instance.put<RP>(url, config);\n },\n patch: <RQ, RP>(url: string, config?: AxiosRequestConfig) => {\n return instance.patch<RP>(url, config);\n },\n delete: <RQ, RP>(url: string, config?: AxiosRequestConfig) => {\n return instance.delete<RP>(url, config);\n },\n };\n");
|
|
201
|
+
fs_extra_1.default.outputFile(rawRequestFunctionFilePath, (0, utils_1.formatContent)((0, vtils_1.dedent)(templateObject_1 || (templateObject_1 = __makeTemplateObject(["", ""], ["", ""])), content)));
|
|
208
202
|
return [2
|
|
209
203
|
/*return*/
|
|
210
204
|
];
|
package/es/helpers.d.ts
CHANGED
|
@@ -5,7 +5,7 @@ import type { Config, RequestConfig, RequestFunctionParams } from './types';
|
|
|
5
5
|
*
|
|
6
6
|
* @param config 配置
|
|
7
7
|
*/
|
|
8
|
-
export declare function defineConfig(config:
|
|
8
|
+
export declare function defineConfig(config: Config | Config[]): Config[];
|
|
9
9
|
export declare class FileData<T = any> {
|
|
10
10
|
/**
|
|
11
11
|
* 原始文件数据。
|
package/es/helpers.js
CHANGED
|
@@ -186,13 +186,7 @@ function defineConfig(config) {
|
|
|
186
186
|
var final = configs.map(function (item) {
|
|
187
187
|
return __assign({
|
|
188
188
|
serverUrl: '',
|
|
189
|
-
|
|
190
|
-
target: 'typescript',
|
|
191
|
-
outputFilePath: 'src/api',
|
|
192
|
-
dataKey: 'data',
|
|
193
|
-
jsonSchema: {
|
|
194
|
-
enabled: false
|
|
195
|
-
}
|
|
189
|
+
outputFilePath: 'src/api'
|
|
196
190
|
}, item);
|
|
197
191
|
});
|
|
198
192
|
return final;
|
|
@@ -187,7 +187,7 @@ var runner = function runner(path, data, jsonSchema) {
|
|
|
187
187
|
exports.runner = runner;
|
|
188
188
|
|
|
189
189
|
var jsonSchemeFileHeader = function jsonSchemeFileHeader() {
|
|
190
|
-
return "\n import * as changeCase from 'change-case';\n import { JSONSchema4 } from 'api-
|
|
190
|
+
return "\n import * as changeCase from 'change-case';\n import { JSONSchema4 } from 'open-api-typescript-request-generator';\n\n /**\n * \u83B7\u53D6scheme\u7684key\n */\n export const jsonSchemeKey = function (path: string): string {\n const deeps = path.split('/');\n const names = deeps.splice(deeps.length - 4, deeps.length).join('_');\n return changeCase.camelCase(names);\n };\n\n export const typeStr = function (data: unknown): string {\n const type = typeof data;\n if (type === 'object' && data instanceof Array) {\n return 'array';\n }\n if (type === 'object' && data === null) {\n return 'null';\n }\n if (type === 'number') {\n return 'integer';\n }\n\n return type;\n };\n\n /**\n * \u68C0\u67E5\u5668\n */\n export const responseDataInspector = function (data: any, jsonScheme: JSONSchema4, key: any[] = ['data']): boolean {\n const { type, properties, items } = jsonScheme;\n\n if (type !== typeStr(data)) {\n console.warn(`->ResponseDataInspector\uFF1A${key.join('.')}\u4E0E\u7EA6\u5B9A\u7684\u7C7B\u578B\u4E0D\u7B26\uFF0C\u7C7B\u578B\uFF1A${type}\uFF0C\u8FD4\u56DE\u503C\uFF1A${data}`);\n return false;\n }\n if (type === 'object') {\n if (!properties || !Object.keys(properties).length) {\n return true;\n }\n return Object.keys(properties).every(k => responseDataInspector(data[k], properties[k], [...key, k]));\n }\n if (type === 'array') {\n if (items === undefined) {\n // undefined\u5219\u4E3A\u63A5\u53D7\u4EFB\u610F\u7C7B\u578B\n return true;\n }\n if (items instanceof Array) {\n return (data as Array<any>).some(v => items.every(item => responseDataInspector(v, item)));\n }\n if (items instanceof Object) {\n return (data as Array<any>).every((v, index) => responseDataInspector(v, items, [...key, index]));\n }\n }\n\n return true;\n };\n\n export const runner = function (path: string, data: any, jsonSchema: Record<string, any>): void {\n const key = jsonSchemeKey(path);\n responseDataInspector(data, jsonSchema[key]);\n };\n ";
|
|
191
191
|
};
|
|
192
192
|
|
|
193
193
|
exports.jsonSchemeFileHeader = jsonSchemeFileHeader;
|
package/es/types.d.ts
CHANGED
|
@@ -7,11 +7,7 @@ import { ParsedPath } from 'path';
|
|
|
7
7
|
export declare type requestFunctionTemplateType = (props: RequestFunctionTemplateProps, config?: SyntheticalConfig) => string;
|
|
8
8
|
/** 顶部依赖生成模板函数 */
|
|
9
9
|
/** 生成器参数 */
|
|
10
|
-
export declare type
|
|
11
|
-
export interface GenTemplateType {
|
|
12
|
-
requestFunctionTemplate?: requestFunctionTemplateType;
|
|
13
|
-
topImportPkgTemplate?: topImportPkgTemplateType;
|
|
14
|
-
}
|
|
10
|
+
export declare type topImportTemplateType = () => string;
|
|
15
11
|
export interface GeneratorOptions {
|
|
16
12
|
cwd: string;
|
|
17
13
|
}
|
|
@@ -314,29 +310,6 @@ export interface ExtendedInterface extends Interface {
|
|
|
314
310
|
}
|
|
315
311
|
/** 分类列表,对应数据导出的 json 内容 */
|
|
316
312
|
export declare type CategoryList = Category[];
|
|
317
|
-
/** 支持生成 React Hooks 代码的相关配置 */
|
|
318
|
-
export interface ReactHooksConfig {
|
|
319
|
-
/**
|
|
320
|
-
* 是否开启该项功能。
|
|
321
|
-
*/
|
|
322
|
-
enabled: boolean;
|
|
323
|
-
/**
|
|
324
|
-
* 请求 Hook 函数制造者文件路径。
|
|
325
|
-
*
|
|
326
|
-
* @default 与 `outputFilePath` 同级目录下的 `makeRequestHook.ts` 文件
|
|
327
|
-
* @example 'src/api/makeRequestHook.ts'
|
|
328
|
-
*/
|
|
329
|
-
requestHookMakerFilePath?: string;
|
|
330
|
-
/**
|
|
331
|
-
* 获取请求 Hook 的名称。
|
|
332
|
-
*
|
|
333
|
-
* @default `use${changeCase.pascalCase(requestFunctionName)}`
|
|
334
|
-
* @param interfaceInfo 接口信息
|
|
335
|
-
* @param changeCase 常用的大小写转换函数集合对象
|
|
336
|
-
* @returns 请求 Hook 的名称
|
|
337
|
-
*/
|
|
338
|
-
getRequestHookName?(interfaceInfo: ExtendedInterface, changeCase: ChangeCase): string;
|
|
339
|
-
}
|
|
340
313
|
/** 支持生成 JSON Schema 的相关配置 */
|
|
341
314
|
export interface JsonSchemaConfig {
|
|
342
315
|
/**
|
|
@@ -393,29 +366,6 @@ export interface CommentConfig {
|
|
|
393
366
|
* 共享的配置。
|
|
394
367
|
*/
|
|
395
368
|
export interface SharedConfig {
|
|
396
|
-
/**
|
|
397
|
-
* 要生成的目标代码类型。
|
|
398
|
-
* 默认为 `typescript`,若设为 `javascript`,会将生成的 `.ts` 文件转换为 `.js` + `.d.ts` 文件并删除原 `.ts` 文件。
|
|
399
|
-
*
|
|
400
|
-
* @default 'typescript'
|
|
401
|
-
*/
|
|
402
|
-
target?: 'typescript' | 'javascript';
|
|
403
|
-
/**
|
|
404
|
-
* 是否只生成接口请求内容和返回内容的 TypeSript 类型,是则请求文件和请求函数都不会生成。
|
|
405
|
-
*
|
|
406
|
-
* @default false
|
|
407
|
-
*/
|
|
408
|
-
typesOnly?: boolean;
|
|
409
|
-
/**
|
|
410
|
-
* 测试环境名称。
|
|
411
|
-
*
|
|
412
|
-
* **用于获取测试环境域名。**
|
|
413
|
-
*
|
|
414
|
-
* 获取方式:打开项目 --> `设置` --> `环境配置` --> 点开或新增测试环境 --> 复制测试环境名称。
|
|
415
|
-
*
|
|
416
|
-
* @example 'dev'
|
|
417
|
-
*/
|
|
418
|
-
devEnvName?: string;
|
|
419
369
|
/**
|
|
420
370
|
* 输出文件路径。
|
|
421
371
|
*
|
|
@@ -431,22 +381,6 @@ export interface SharedConfig {
|
|
|
431
381
|
* @example 'src/api/request.ts'
|
|
432
382
|
*/
|
|
433
383
|
requestFunctionFilePath?: string;
|
|
434
|
-
/**
|
|
435
|
-
* 如果接口响应的结果是 `JSON` 对象,
|
|
436
|
-
* 且我们想要的数据在该对象下,
|
|
437
|
-
* 那我们就可将 `dataKey` 设为我们想要的数据对应的键。
|
|
438
|
-
*
|
|
439
|
-
* 比如该对象为 `{ code: 0, msg: '成功', data: 100 }`,
|
|
440
|
-
* 我们想要的数据为 `100`,
|
|
441
|
-
* 则我们可将 `dataKey` 设为 `data`。
|
|
442
|
-
*
|
|
443
|
-
* @example 'data'
|
|
444
|
-
*/
|
|
445
|
-
dataKey?: string;
|
|
446
|
-
/**
|
|
447
|
-
* 支持生成 React Hooks 代码的相关配置。
|
|
448
|
-
*/
|
|
449
|
-
reactHooks?: ReactHooksConfig;
|
|
450
384
|
/**
|
|
451
385
|
* 支持生成 JSON Schema 的相关配置。
|
|
452
386
|
*/
|
|
@@ -454,21 +388,6 @@ export interface SharedConfig {
|
|
|
454
388
|
* 支持生成注释的相关配置。
|
|
455
389
|
*/
|
|
456
390
|
comment?: CommentConfig;
|
|
457
|
-
/**
|
|
458
|
-
* 预处理接口信息,返回新的接口信息。可返回 false 排除当前接口。
|
|
459
|
-
*
|
|
460
|
-
* 譬如你想对接口的 `path` 进行某些处理或者想排除某些接口,就可使用该方法。
|
|
461
|
-
*
|
|
462
|
-
* @example
|
|
463
|
-
*
|
|
464
|
-
* ```js
|
|
465
|
-
* interfaceInfo => {
|
|
466
|
-
* interfaceInfo.path = interfaceInfo.path.replace('v1', 'v2')
|
|
467
|
-
* return interfaceInfo
|
|
468
|
-
* }
|
|
469
|
-
* ```
|
|
470
|
-
*/
|
|
471
|
-
preproccessInterface?(interfaceInfo: Interface, changeCase: ChangeCase): Interface | false;
|
|
472
391
|
/**
|
|
473
392
|
* 获取请求函数的名称。
|
|
474
393
|
*
|
|
@@ -497,116 +416,26 @@ export interface SharedConfig {
|
|
|
497
416
|
*/
|
|
498
417
|
getResponseDataTypeName?(interfaceInfo: ExtendedInterface, changeCase: ChangeCase): string;
|
|
499
418
|
}
|
|
500
|
-
/**
|
|
501
|
-
* 分类的配置。
|
|
502
|
-
*/
|
|
503
|
-
export interface CategoryConfig extends SharedConfig {
|
|
504
|
-
/**
|
|
505
|
-
* 分类 ID。
|
|
506
|
-
*
|
|
507
|
-
* 获取方式:打开项目 --> 点开分类 --> 复制浏览器地址栏 `/api/cat_` 后面的数字。
|
|
508
|
-
*
|
|
509
|
-
* @example 20
|
|
510
|
-
*/
|
|
511
|
-
id: number;
|
|
512
|
-
/**
|
|
513
|
-
* 过滤接口
|
|
514
|
-
*/
|
|
515
|
-
filter?: ((path: string, id?: number) => boolean) | RegExp | string[];
|
|
516
|
-
}
|
|
517
|
-
export interface TokenNeed {
|
|
518
|
-
/**
|
|
519
|
-
* 项目id,与token等效,二选一
|
|
520
|
-
*
|
|
521
|
-
* 获取方式:打开项目 --> `设置` --> `项目配置` --> 项目ID。
|
|
522
|
-
*
|
|
523
|
-
* @example '123'
|
|
524
|
-
*/
|
|
525
|
-
projectId?: number;
|
|
526
|
-
/**
|
|
527
|
-
* 项目的token,与projectId等效,二选一
|
|
528
|
-
*
|
|
529
|
-
* 获取方式:打开项目 --> `设置` --> `token配置` --> 复制 token。
|
|
530
|
-
*
|
|
531
|
-
* @example 'e02a47122259d0c1973a9ff81cabb30685d64abc72f39edaa1ac6b6a792a647d'
|
|
532
|
-
*/
|
|
533
|
-
token: string;
|
|
534
|
-
}
|
|
535
|
-
export interface ProjectIdNeed {
|
|
536
|
-
/**
|
|
537
|
-
* 项目id,与token等效,二选一
|
|
538
|
-
*
|
|
539
|
-
* 获取方式:打开项目 --> `设置` --> `项目配置` --> 项目ID。
|
|
540
|
-
*
|
|
541
|
-
* @example '123'
|
|
542
|
-
*/
|
|
543
|
-
projectId: number;
|
|
544
|
-
/**
|
|
545
|
-
* 项目的token,与projectId等效,二选一
|
|
546
|
-
*
|
|
547
|
-
* 获取方式:打开项目 --> `设置` --> `token配置` --> 复制 token。
|
|
548
|
-
*
|
|
549
|
-
* @example 'e02a47122259d0c1973a9ff81cabb30685d64abc72f39edaa1ac6b6a792a647d'
|
|
550
|
-
*/
|
|
551
|
-
token?: string;
|
|
552
|
-
}
|
|
553
|
-
/**
|
|
554
|
-
* 项目的配置。
|
|
555
|
-
*/
|
|
556
|
-
export declare type ProjectConfig = SharedConfig & (ProjectIdNeed | TokenNeed) & {
|
|
557
|
-
/**
|
|
558
|
-
* 设置接口的baseURL
|
|
559
|
-
*
|
|
560
|
-
* @description 若要配置使用运行时代码,则增加`[code]:`前缀
|
|
561
|
-
```
|
|
562
|
-
例:
|
|
563
|
-
baseURL: "[code]:process.env.BASE_URL" => baseURL:process.env.BASE_URL
|
|
564
|
-
|
|
565
|
-
baseURL: "process.env.BASE_URL" => baseURL:"process.env.BASE_URL"
|
|
566
|
-
```
|
|
567
|
-
*/
|
|
568
|
-
baseURL?: ((path: string) => string | undefined) | string;
|
|
569
|
-
/**
|
|
570
|
-
* 分类列表。
|
|
571
|
-
*/
|
|
572
|
-
categories?: CategoryConfig[];
|
|
573
|
-
};
|
|
574
419
|
/**
|
|
575
420
|
* 服务器的配置。
|
|
576
421
|
*/
|
|
577
|
-
export interface
|
|
578
|
-
name
|
|
422
|
+
export interface ApiConfig {
|
|
423
|
+
name: string;
|
|
579
424
|
configIndex?: number;
|
|
580
425
|
/**
|
|
581
|
-
*
|
|
426
|
+
* 服务地址。此处填其 swagger json 地址。
|
|
427
|
+
* 比如nestjs项目一般为http://localhost:3041/api-json
|
|
582
428
|
*
|
|
583
429
|
*/
|
|
584
430
|
serverUrl: string;
|
|
585
431
|
/**
|
|
586
|
-
*
|
|
587
|
-
*/
|
|
588
|
-
project?: ProjectConfig;
|
|
589
|
-
/**
|
|
590
|
-
* prettier代码格式化配置文件的路径
|
|
432
|
+
* 输出文件路径。
|
|
591
433
|
*
|
|
592
|
-
*
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
/**
|
|
596
|
-
* 是否使用默认请求库,关闭后不再生成request.ts
|
|
597
|
-
*/
|
|
598
|
-
defaultRequestLib?: boolean;
|
|
599
|
-
/**
|
|
600
|
-
* 代理请求模式,所有请求均请求到指定接口
|
|
434
|
+
* 可以是 `相对路径` 或 `绝对路径`。
|
|
435
|
+
*
|
|
436
|
+
* @example 'src/api/index.ts'
|
|
601
437
|
*/
|
|
602
|
-
|
|
603
|
-
/**
|
|
604
|
-
* 代理接口
|
|
605
|
-
*
|
|
606
|
-
* @default /admin-interface/proxy/v0/proxy
|
|
607
|
-
*/
|
|
608
|
-
path?: string;
|
|
609
|
-
};
|
|
438
|
+
outputFilePath?: string;
|
|
610
439
|
/**
|
|
611
440
|
* 设置接口的baseURL
|
|
612
441
|
*
|
|
@@ -615,50 +444,31 @@ export interface ServerConfig extends SharedConfig, GenTemplateType {
|
|
|
615
444
|
例:
|
|
616
445
|
baseURL: "[code]:process.env.BASE_URL" => baseURL:process.env.BASE_URL
|
|
617
446
|
|
|
618
|
-
baseURL: "
|
|
447
|
+
baseURL: "http://localhost:3000" => baseURL:"http://localhost:3000"
|
|
619
448
|
```
|
|
620
449
|
*/
|
|
621
450
|
baseURL?: ((path: string) => string | undefined) | string;
|
|
622
451
|
/**
|
|
623
|
-
*
|
|
452
|
+
* 在每个生成的api文件顶部定义一段代码
|
|
453
|
+
* 例如:引入自定义request函数
|
|
454
|
+
* default: import request from './request'
|
|
624
455
|
*/
|
|
625
|
-
|
|
456
|
+
topImportTemplate?: topImportTemplateType;
|
|
626
457
|
/**
|
|
627
|
-
*
|
|
628
|
-
* 通过url分析出项目id,接口id,分类id
|
|
629
|
-
*
|
|
630
|
-
@example
|
|
631
|
-
```
|
|
632
|
-
`/project/9/interface/api/43 => 项目id:9、接口id: 43`
|
|
633
|
-
```
|
|
634
|
-
*/
|
|
635
|
-
yapiUrlList?: string | string[];
|
|
636
|
-
/**
|
|
637
|
-
* 请求函数是否需要extra入参
|
|
638
|
-
*
|
|
639
|
-
* @default false
|
|
458
|
+
* 是否使用默认请求库,关闭后不再生成request.ts
|
|
640
459
|
*/
|
|
641
|
-
|
|
460
|
+
defaultRequestLib?: boolean;
|
|
642
461
|
}
|
|
643
462
|
/** 混合的配置。 */
|
|
644
|
-
export declare type SyntheticalConfig = Partial<
|
|
645
|
-
mockUrl: string;
|
|
646
|
-
devUrl: string;
|
|
647
|
-
prodUrl: string;
|
|
463
|
+
export declare type SyntheticalConfig = Partial<ApiConfig & {
|
|
648
464
|
components: OpenAPIV3.Document['components'];
|
|
649
465
|
}>;
|
|
650
466
|
/** 配置。 */
|
|
651
|
-
export declare type Config =
|
|
467
|
+
export declare type Config = ApiConfig;
|
|
652
468
|
/**
|
|
653
469
|
* 请求配置。
|
|
654
470
|
*/
|
|
655
|
-
export interface RequestConfig<
|
|
656
|
-
/** 接口 Mock 地址,结尾无 `/` */
|
|
657
|
-
mockUrl: MockUrl;
|
|
658
|
-
/** 接口测试环境地址,结尾无 `/` */
|
|
659
|
-
devUrl: DevUrl;
|
|
660
|
-
/** 接口生产环境地址,结尾无 `/` */
|
|
661
|
-
prodUrl: ProdUrl;
|
|
471
|
+
export interface RequestConfig<Path extends string = string, ParamName extends string = string, QueryName extends string = string, RequestDataOptional extends boolean = boolean> {
|
|
662
472
|
/** 接口路径,以 `/` 开头 */
|
|
663
473
|
path: Path;
|
|
664
474
|
/** 请求方法 */
|
|
@@ -669,8 +479,6 @@ export interface RequestConfig<MockUrl extends string = string, DevUrl extends s
|
|
|
669
479
|
requestBodyType: RequestBodyType;
|
|
670
480
|
/** 返回数据类型 */
|
|
671
481
|
responseBodyType: ResponseBodyType;
|
|
672
|
-
/** 数据所在键 */
|
|
673
|
-
dataKey: DataKey;
|
|
674
482
|
/** 路径参数的名称列表 */
|
|
675
483
|
paramNames: ParamName[];
|
|
676
484
|
/** 查询参数的名称列表 */
|
package/es/utils.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import prettier from 'prettier';
|
|
2
|
-
import { Interface, PropDefinitions
|
|
2
|
+
import { Interface, PropDefinitions } from './types';
|
|
3
3
|
import { JSONSchema4 } from 'json-schema';
|
|
4
4
|
/**
|
|
5
5
|
* 抛出错误。
|
|
@@ -61,7 +61,7 @@ export declare function propDefinitionsToJsonSchema(propDefinitions: PropDefinit
|
|
|
61
61
|
* 获取prettier配置
|
|
62
62
|
* @returns
|
|
63
63
|
*/
|
|
64
|
-
export declare function getPrettier(
|
|
64
|
+
export declare function getPrettier(): prettier.Options;
|
|
65
65
|
export declare function preprocessSchema(schema: JSONSchema4): JSONSchema4;
|
|
66
66
|
/**
|
|
67
67
|
* 根据 JSONSchema 对象生产 TypeScript 类型定义。
|
|
@@ -72,7 +72,7 @@ export declare function preprocessSchema(schema: JSONSchema4): JSONSchema4;
|
|
|
72
72
|
*/
|
|
73
73
|
export declare function jsonSchemaToTsCode(jsonSchema: JSONSchema4, typeName: string): Promise<string>;
|
|
74
74
|
export declare function getRequestDataJsonSchema(interfaceInfo: Interface): JSONSchema4;
|
|
75
|
-
export declare function getResponseDataJsonSchema(interfaceInfo: Interface
|
|
75
|
+
export declare function getResponseDataJsonSchema(interfaceInfo: Interface): JSONSchema4;
|
|
76
76
|
export declare function sortByWeights<T extends {
|
|
77
77
|
weights: number[];
|
|
78
78
|
}>(list: T[]): T[];
|
|
@@ -83,15 +83,9 @@ export declare function sortByWeights<T extends {
|
|
|
83
83
|
* @returns
|
|
84
84
|
* https://prettier.io/docs/en/options.html
|
|
85
85
|
*/
|
|
86
|
-
export declare function formatContent(content: string
|
|
86
|
+
export declare function formatContent(content: string): string;
|
|
87
87
|
/**
|
|
88
88
|
* 通用生成文件顶部注释
|
|
89
89
|
* @returns
|
|
90
90
|
*/
|
|
91
91
|
export declare function topNotesContent(): string;
|
|
92
|
-
/**
|
|
93
|
-
* filter处理函数
|
|
94
|
-
* @param interfaceFilter
|
|
95
|
-
* @returns
|
|
96
|
-
*/
|
|
97
|
-
export declare const filterHandler: (interfaceFilter?: Config['filter']) => (path: string, id?: number) => boolean;
|
package/es/utils.js
CHANGED
|
@@ -180,7 +180,7 @@ var __importDefault = this && this.__importDefault || function (mod) {
|
|
|
180
180
|
Object.defineProperty(exports, "__esModule", {
|
|
181
181
|
value: true
|
|
182
182
|
});
|
|
183
|
-
exports.
|
|
183
|
+
exports.topNotesContent = exports.formatContent = exports.sortByWeights = exports.getResponseDataJsonSchema = exports.getRequestDataJsonSchema = exports.jsonSchemaToTsCode = exports.preprocessSchema = exports.getPrettier = exports.propDefinitionsToJsonSchema = exports.mockjsTemplateToJsonSchema = exports.jsonToJsonSchema = exports.jsonSchemaStringToJsonSchema = exports.processJsonSchema = exports.getNormalizedRelativePath = exports.toUnixPath = exports.throwError = void 0;
|
|
184
184
|
|
|
185
185
|
var json5_1 = __importDefault(require("json5"));
|
|
186
186
|
|
|
@@ -427,13 +427,8 @@ exports.propDefinitionsToJsonSchema = propDefinitionsToJsonSchema;
|
|
|
427
427
|
* @returns
|
|
428
428
|
*/
|
|
429
429
|
|
|
430
|
-
function getPrettier(
|
|
431
|
-
|
|
432
|
-
var configPath = prettier_1.default.resolveConfigFile.sync(filePath && path_1.default.resolve(filePath) || process.cwd());
|
|
433
|
-
var config = configPath && prettier_1.default.resolveConfig.sync(configPath) || {};
|
|
434
|
-
return config ? __assign({
|
|
435
|
-
parser: 'babel-ts'
|
|
436
|
-
}, config) : {
|
|
430
|
+
function getPrettier() {
|
|
431
|
+
return {
|
|
437
432
|
printWidth: 120,
|
|
438
433
|
tabWidth: 2,
|
|
439
434
|
singleQuote: true,
|
|
@@ -678,7 +673,7 @@ function getRequestDataJsonSchema(interfaceInfo) {
|
|
|
678
673
|
|
|
679
674
|
exports.getRequestDataJsonSchema = getRequestDataJsonSchema;
|
|
680
675
|
|
|
681
|
-
function getResponseDataJsonSchema(interfaceInfo
|
|
676
|
+
function getResponseDataJsonSchema(interfaceInfo) {
|
|
682
677
|
var jsonSchema = {};
|
|
683
678
|
|
|
684
679
|
switch (interfaceInfo.res_body_type) {
|
|
@@ -695,12 +690,6 @@ function getResponseDataJsonSchema(interfaceInfo, dataKey) {
|
|
|
695
690
|
};
|
|
696
691
|
break;
|
|
697
692
|
}
|
|
698
|
-
/* istanbul ignore if */
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
if (dataKey && jsonSchema && jsonSchema.properties && jsonSchema.properties[dataKey]) {
|
|
702
|
-
jsonSchema = jsonSchema.properties[dataKey];
|
|
703
|
-
}
|
|
704
693
|
|
|
705
694
|
return jsonSchema;
|
|
706
695
|
}
|
|
@@ -738,9 +727,9 @@ exports.sortByWeights = sortByWeights;
|
|
|
738
727
|
* https://prettier.io/docs/en/options.html
|
|
739
728
|
*/
|
|
740
729
|
|
|
741
|
-
function formatContent(content
|
|
730
|
+
function formatContent(content) {
|
|
742
731
|
// 从项目中获取prettier配置文件
|
|
743
|
-
var config = getPrettier(
|
|
732
|
+
var config = getPrettier();
|
|
744
733
|
var prettyOutputContent = prettier_1.default.format(content, config);
|
|
745
734
|
return prettyOutputContent;
|
|
746
735
|
}
|
|
@@ -752,32 +741,7 @@ exports.formatContent = formatContent;
|
|
|
752
741
|
*/
|
|
753
742
|
|
|
754
743
|
function topNotesContent() {
|
|
755
|
-
return "\n /**\n * Created By api-
|
|
744
|
+
return "\n /**\n * Created By open-api-typescript-request-generator\n */\n\n ";
|
|
756
745
|
}
|
|
757
746
|
|
|
758
|
-
exports.topNotesContent = topNotesContent;
|
|
759
|
-
/**
|
|
760
|
-
* filter处理函数
|
|
761
|
-
* @param interfaceFilter
|
|
762
|
-
* @returns
|
|
763
|
-
*/
|
|
764
|
-
|
|
765
|
-
var filterHandler = function filterHandler(interfaceFilter) {
|
|
766
|
-
return function (path, id) {
|
|
767
|
-
if (interfaceFilter instanceof RegExp && !interfaceFilter.test(path)) {
|
|
768
|
-
return false;
|
|
769
|
-
}
|
|
770
|
-
|
|
771
|
-
if (interfaceFilter instanceof Array && !interfaceFilter.includes(path)) {
|
|
772
|
-
return false;
|
|
773
|
-
}
|
|
774
|
-
|
|
775
|
-
if (interfaceFilter instanceof Function && !interfaceFilter(path, id)) {
|
|
776
|
-
return false;
|
|
777
|
-
}
|
|
778
|
-
|
|
779
|
-
return true;
|
|
780
|
-
};
|
|
781
|
-
};
|
|
782
|
-
|
|
783
|
-
exports.filterHandler = filterHandler;
|
|
747
|
+
exports.topNotesContent = topNotesContent;
|