open-api-typescript-request-generator 0.0.1 → 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 +27 -193
- 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 -8
- package/es/responseDataJsonSchemaHandler.js +1 -1
- package/es/types.d.ts +20 -222
- package/es/utils.d.ts +4 -10
- package/es/utils.js +8 -44
- package/lib/Generator.js +27 -193
- 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 -8
- package/lib/responseDataJsonSchemaHandler.js +1 -1
- package/lib/types.d.ts +20 -222
- package/lib/utils.d.ts +4 -10
- package/lib/utils.js +8 -44
- package/package.json +3 -2
- package/es/requestYapiData.d.ts +0 -157
- package/es/requestYapiData.js +0 -1010
- package/lib/requestYapiData.d.ts +0 -157
- package/lib/requestYapiData.js +0 -1010
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, config } = r;\n if (data.code === 0) {\n\n return data.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,14 +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
|
-
prodEnvName: 'local',
|
|
192
|
-
outputFilePath: 'src/api',
|
|
193
|
-
dataKey: 'data',
|
|
194
|
-
jsonSchema: {
|
|
195
|
-
enabled: false
|
|
196
|
-
}
|
|
189
|
+
outputFilePath: 'src/api'
|
|
197
190
|
}, item);
|
|
198
191
|
});
|
|
199
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,39 +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
|
-
/**
|
|
420
|
-
* 生产环境名称。
|
|
421
|
-
*
|
|
422
|
-
* **用于获取生产环境域名。**
|
|
423
|
-
*
|
|
424
|
-
* 获取方式:打开项目 --> `设置` --> `环境配置` --> 点开或新增生产环境 --> 复制生产环境名称。
|
|
425
|
-
*
|
|
426
|
-
* @example 'prod'
|
|
427
|
-
*/
|
|
428
|
-
prodEnvName?: string;
|
|
429
369
|
/**
|
|
430
370
|
* 输出文件路径。
|
|
431
371
|
*
|
|
@@ -441,22 +381,6 @@ export interface SharedConfig {
|
|
|
441
381
|
* @example 'src/api/request.ts'
|
|
442
382
|
*/
|
|
443
383
|
requestFunctionFilePath?: string;
|
|
444
|
-
/**
|
|
445
|
-
* 如果接口响应的结果是 `JSON` 对象,
|
|
446
|
-
* 且我们想要的数据在该对象下,
|
|
447
|
-
* 那我们就可将 `dataKey` 设为我们想要的数据对应的键。
|
|
448
|
-
*
|
|
449
|
-
* 比如该对象为 `{ code: 0, msg: '成功', data: 100 }`,
|
|
450
|
-
* 我们想要的数据为 `100`,
|
|
451
|
-
* 则我们可将 `dataKey` 设为 `data`。
|
|
452
|
-
*
|
|
453
|
-
* @example 'data'
|
|
454
|
-
*/
|
|
455
|
-
dataKey?: string;
|
|
456
|
-
/**
|
|
457
|
-
* 支持生成 React Hooks 代码的相关配置。
|
|
458
|
-
*/
|
|
459
|
-
reactHooks?: ReactHooksConfig;
|
|
460
384
|
/**
|
|
461
385
|
* 支持生成 JSON Schema 的相关配置。
|
|
462
386
|
*/
|
|
@@ -464,21 +388,6 @@ export interface SharedConfig {
|
|
|
464
388
|
* 支持生成注释的相关配置。
|
|
465
389
|
*/
|
|
466
390
|
comment?: CommentConfig;
|
|
467
|
-
/**
|
|
468
|
-
* 预处理接口信息,返回新的接口信息。可返回 false 排除当前接口。
|
|
469
|
-
*
|
|
470
|
-
* 譬如你想对接口的 `path` 进行某些处理或者想排除某些接口,就可使用该方法。
|
|
471
|
-
*
|
|
472
|
-
* @example
|
|
473
|
-
*
|
|
474
|
-
* ```js
|
|
475
|
-
* interfaceInfo => {
|
|
476
|
-
* interfaceInfo.path = interfaceInfo.path.replace('v1', 'v2')
|
|
477
|
-
* return interfaceInfo
|
|
478
|
-
* }
|
|
479
|
-
* ```
|
|
480
|
-
*/
|
|
481
|
-
preproccessInterface?(interfaceInfo: Interface, changeCase: ChangeCase): Interface | false;
|
|
482
391
|
/**
|
|
483
392
|
* 获取请求函数的名称。
|
|
484
393
|
*
|
|
@@ -507,116 +416,26 @@ export interface SharedConfig {
|
|
|
507
416
|
*/
|
|
508
417
|
getResponseDataTypeName?(interfaceInfo: ExtendedInterface, changeCase: ChangeCase): string;
|
|
509
418
|
}
|
|
510
|
-
/**
|
|
511
|
-
* 分类的配置。
|
|
512
|
-
*/
|
|
513
|
-
export interface CategoryConfig extends SharedConfig {
|
|
514
|
-
/**
|
|
515
|
-
* 分类 ID。
|
|
516
|
-
*
|
|
517
|
-
* 获取方式:打开项目 --> 点开分类 --> 复制浏览器地址栏 `/api/cat_` 后面的数字。
|
|
518
|
-
*
|
|
519
|
-
* @example 20
|
|
520
|
-
*/
|
|
521
|
-
id: number;
|
|
522
|
-
/**
|
|
523
|
-
* 过滤接口
|
|
524
|
-
*/
|
|
525
|
-
filter?: ((path: string, id?: number) => boolean) | RegExp | string[];
|
|
526
|
-
}
|
|
527
|
-
export interface TokenNeed {
|
|
528
|
-
/**
|
|
529
|
-
* 项目id,与token等效,二选一
|
|
530
|
-
*
|
|
531
|
-
* 获取方式:打开项目 --> `设置` --> `项目配置` --> 项目ID。
|
|
532
|
-
*
|
|
533
|
-
* @example '123'
|
|
534
|
-
*/
|
|
535
|
-
projectId?: number;
|
|
536
|
-
/**
|
|
537
|
-
* 项目的token,与projectId等效,二选一
|
|
538
|
-
*
|
|
539
|
-
* 获取方式:打开项目 --> `设置` --> `token配置` --> 复制 token。
|
|
540
|
-
*
|
|
541
|
-
* @example 'e02a47122259d0c1973a9ff81cabb30685d64abc72f39edaa1ac6b6a792a647d'
|
|
542
|
-
*/
|
|
543
|
-
token: string;
|
|
544
|
-
}
|
|
545
|
-
export interface ProjectIdNeed {
|
|
546
|
-
/**
|
|
547
|
-
* 项目id,与token等效,二选一
|
|
548
|
-
*
|
|
549
|
-
* 获取方式:打开项目 --> `设置` --> `项目配置` --> 项目ID。
|
|
550
|
-
*
|
|
551
|
-
* @example '123'
|
|
552
|
-
*/
|
|
553
|
-
projectId: number;
|
|
554
|
-
/**
|
|
555
|
-
* 项目的token,与projectId等效,二选一
|
|
556
|
-
*
|
|
557
|
-
* 获取方式:打开项目 --> `设置` --> `token配置` --> 复制 token。
|
|
558
|
-
*
|
|
559
|
-
* @example 'e02a47122259d0c1973a9ff81cabb30685d64abc72f39edaa1ac6b6a792a647d'
|
|
560
|
-
*/
|
|
561
|
-
token?: string;
|
|
562
|
-
}
|
|
563
|
-
/**
|
|
564
|
-
* 项目的配置。
|
|
565
|
-
*/
|
|
566
|
-
export declare type ProjectConfig = SharedConfig & (ProjectIdNeed | TokenNeed) & {
|
|
567
|
-
/**
|
|
568
|
-
* 设置接口的baseURL
|
|
569
|
-
*
|
|
570
|
-
* @description 若要配置使用运行时代码,则增加`[code]:`前缀
|
|
571
|
-
```
|
|
572
|
-
例:
|
|
573
|
-
baseURL: "[code]:process.env.BASE_URL" => baseURL:process.env.BASE_URL
|
|
574
|
-
|
|
575
|
-
baseURL: "process.env.BASE_URL" => baseURL:"process.env.BASE_URL"
|
|
576
|
-
```
|
|
577
|
-
*/
|
|
578
|
-
baseURL?: ((path: string) => string | undefined) | string;
|
|
579
|
-
/**
|
|
580
|
-
* 分类列表。
|
|
581
|
-
*/
|
|
582
|
-
categories?: CategoryConfig[];
|
|
583
|
-
};
|
|
584
419
|
/**
|
|
585
420
|
* 服务器的配置。
|
|
586
421
|
*/
|
|
587
|
-
export interface
|
|
588
|
-
name
|
|
422
|
+
export interface ApiConfig {
|
|
423
|
+
name: string;
|
|
589
424
|
configIndex?: number;
|
|
590
425
|
/**
|
|
591
|
-
*
|
|
426
|
+
* 服务地址。此处填其 swagger json 地址。
|
|
427
|
+
* 比如nestjs项目一般为http://localhost:3041/api-json
|
|
592
428
|
*
|
|
593
429
|
*/
|
|
594
430
|
serverUrl: string;
|
|
595
431
|
/**
|
|
596
|
-
*
|
|
597
|
-
*/
|
|
598
|
-
project?: ProjectConfig;
|
|
599
|
-
/**
|
|
600
|
-
* prettier代码格式化配置文件的路径
|
|
432
|
+
* 输出文件路径。
|
|
601
433
|
*
|
|
602
|
-
*
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
/**
|
|
606
|
-
* 是否使用默认请求库,关闭后不再生成request.ts
|
|
607
|
-
*/
|
|
608
|
-
defaultRequestLib?: boolean;
|
|
609
|
-
/**
|
|
610
|
-
* 代理请求模式,所有请求均请求到指定接口
|
|
434
|
+
* 可以是 `相对路径` 或 `绝对路径`。
|
|
435
|
+
*
|
|
436
|
+
* @example 'src/api/index.ts'
|
|
611
437
|
*/
|
|
612
|
-
|
|
613
|
-
/**
|
|
614
|
-
* 代理接口
|
|
615
|
-
*
|
|
616
|
-
* @default /admin-interface/proxy/v0/proxy
|
|
617
|
-
*/
|
|
618
|
-
path?: string;
|
|
619
|
-
};
|
|
438
|
+
outputFilePath?: string;
|
|
620
439
|
/**
|
|
621
440
|
* 设置接口的baseURL
|
|
622
441
|
*
|
|
@@ -625,50 +444,31 @@ export interface ServerConfig extends SharedConfig, GenTemplateType {
|
|
|
625
444
|
例:
|
|
626
445
|
baseURL: "[code]:process.env.BASE_URL" => baseURL:process.env.BASE_URL
|
|
627
446
|
|
|
628
|
-
baseURL: "
|
|
447
|
+
baseURL: "http://localhost:3000" => baseURL:"http://localhost:3000"
|
|
629
448
|
```
|
|
630
449
|
*/
|
|
631
450
|
baseURL?: ((path: string) => string | undefined) | string;
|
|
632
451
|
/**
|
|
633
|
-
*
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
/**
|
|
637
|
-
* yapi页面链接地址列表
|
|
638
|
-
* 通过url分析出项目id,接口id,分类id
|
|
639
|
-
*
|
|
640
|
-
@example
|
|
641
|
-
```
|
|
642
|
-
`/project/9/interface/api/43 => 项目id:9、接口id: 43`
|
|
643
|
-
```
|
|
452
|
+
* 在每个生成的api文件顶部定义一段代码
|
|
453
|
+
* 例如:引入自定义request函数
|
|
454
|
+
* default: import request from './request'
|
|
644
455
|
*/
|
|
645
|
-
|
|
456
|
+
topImportTemplate?: topImportTemplateType;
|
|
646
457
|
/**
|
|
647
|
-
*
|
|
648
|
-
*
|
|
649
|
-
* @default false
|
|
458
|
+
* 是否使用默认请求库,关闭后不再生成request.ts
|
|
650
459
|
*/
|
|
651
|
-
|
|
460
|
+
defaultRequestLib?: boolean;
|
|
652
461
|
}
|
|
653
462
|
/** 混合的配置。 */
|
|
654
|
-
export declare type SyntheticalConfig = Partial<
|
|
655
|
-
mockUrl: string;
|
|
656
|
-
devUrl: string;
|
|
657
|
-
prodUrl: string;
|
|
463
|
+
export declare type SyntheticalConfig = Partial<ApiConfig & {
|
|
658
464
|
components: OpenAPIV3.Document['components'];
|
|
659
465
|
}>;
|
|
660
466
|
/** 配置。 */
|
|
661
|
-
export declare type Config =
|
|
467
|
+
export declare type Config = ApiConfig;
|
|
662
468
|
/**
|
|
663
469
|
* 请求配置。
|
|
664
470
|
*/
|
|
665
|
-
export interface RequestConfig<
|
|
666
|
-
/** 接口 Mock 地址,结尾无 `/` */
|
|
667
|
-
mockUrl: MockUrl;
|
|
668
|
-
/** 接口测试环境地址,结尾无 `/` */
|
|
669
|
-
devUrl: DevUrl;
|
|
670
|
-
/** 接口生产环境地址,结尾无 `/` */
|
|
671
|
-
prodUrl: ProdUrl;
|
|
471
|
+
export interface RequestConfig<Path extends string = string, ParamName extends string = string, QueryName extends string = string, RequestDataOptional extends boolean = boolean> {
|
|
672
472
|
/** 接口路径,以 `/` 开头 */
|
|
673
473
|
path: Path;
|
|
674
474
|
/** 请求方法 */
|
|
@@ -679,8 +479,6 @@ export interface RequestConfig<MockUrl extends string = string, DevUrl extends s
|
|
|
679
479
|
requestBodyType: RequestBodyType;
|
|
680
480
|
/** 返回数据类型 */
|
|
681
481
|
responseBodyType: ResponseBodyType;
|
|
682
|
-
/** 数据所在键 */
|
|
683
|
-
dataKey: DataKey;
|
|
684
482
|
/** 路径参数的名称列表 */
|
|
685
483
|
paramNames: ParamName[];
|
|
686
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;
|