openapi-ts-request 0.1.0 → 0.1.1
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 +6 -5
- package/dist/generator/config.d.ts +1 -1
- package/dist/generator/serviceGenarator.d.ts +1 -1
- package/dist/generator/serviceGenarator.js +10 -9
- package/dist/generator/util.js +3 -3
- package/dist/index.js +1 -1
- package/dist/type.d.ts +1 -1
- package/package.json +2 -2
- package/templates/serviceController.njk +1 -1
package/README.md
CHANGED
|
@@ -4,13 +4,14 @@
|
|
|
4
4
|
[](https://www.npmjs.com/package/openapi-ts-request)
|
|
5
5
|

|
|
6
6
|
|
|
7
|
-
根据 [Swagger2/OpenAPI3](https://swagger.io/blog/news/whats-new-in-openapi-3-0/) 文档生成 ts 类型、request client
|
|
7
|
+
根据 [Swagger2/OpenAPI3](https://swagger.io/blog/news/whats-new-in-openapi-3-0/) 文档生成 ts 类型、request client 请求代码、request mock 服务、枚举和 type 字段翻译
|
|
8
8
|
|
|
9
9
|
## Features
|
|
10
10
|
|
|
11
11
|
* supports Swagger2.0/OpenAPI 3.0,3.1 specifications
|
|
12
|
-
* generate TypeScript Interfaces, Reuquest clients, Display Babel
|
|
12
|
+
* generate TypeScript Interfaces, Reuquest clients, Request Mock Service, Enum, Display Field Babel
|
|
13
13
|
* supports Custom Request Function, Fetch、Axios、Uniapp-Request、Node.js、XHR client available
|
|
14
|
+
* supports filter specifications by tags
|
|
14
15
|
* supports JSON specifications
|
|
15
16
|
|
|
16
17
|
## Usage
|
|
@@ -104,9 +105,9 @@ npm run openapi
|
|
|
104
105
|
| serversPath | 否 | 生成的文件夹的路径 | string | './src/apis' |
|
|
105
106
|
| requestLibPath | 否 | 自定义请求方法路径 | string | - |
|
|
106
107
|
| allowedTags | 否 | 生成指定 tags 下面的 api | string[] | - |
|
|
107
|
-
| requestOptionsType | 否 | 自定义请求方法 options 参数类型 | string | '{ [key: string]:
|
|
108
|
+
| requestOptionsType | 否 | 自定义请求方法 options 参数类型 | string | '{ [key: string]: unknown }' |
|
|
108
109
|
| requestImportStatement | 否 | 自定义请求方法表达式,例如:'@/request' | string | - |
|
|
109
|
-
| apiPrefix | 否 | api 的前缀,例如:"'api'" | string | - |
|
|
110
|
+
| apiPrefix | 否 | api 的前缀,例如:'api'(动态变量), 指定字符串("'api'") | string | - |
|
|
110
111
|
| dataFields | 否 | 定义 response 中数据字段类型 | string[] | - |
|
|
111
112
|
| mockFolder | 否 | mock目录 | string | './mocks' |
|
|
112
113
|
| nullable | 否 | 使用null代替可选 | boolean | false |
|
|
@@ -117,7 +118,7 @@ npm run openapi
|
|
|
117
118
|
|
|
118
119
|
| 属性 | 类型 | 说明 |
|
|
119
120
|
| -------------- | ---- | ------------------ |
|
|
120
|
-
| afterOpenApiDataInited | (openAPIData: OpenAPIObject) => OpenAPIObject |
|
|
121
|
+
| afterOpenApiDataInited | (openAPIData: OpenAPIObject) => OpenAPIObject | 自定义OpenAPI数据 |
|
|
121
122
|
| customFunctionName | (data: APIDataType) => string | 自定义请求方法函数名称 |
|
|
122
123
|
| customTypeName | (data: APIDataType) => string | 自定义类型名称 |
|
|
123
124
|
| customClassName | (tagName: string) => string | 自定义类名 |
|
|
@@ -10,7 +10,7 @@ export declare enum TypescriptFileType {
|
|
|
10
10
|
displayEnumLabel = "displayEnumLabel"
|
|
11
11
|
}
|
|
12
12
|
export declare const DEFAULT_SCHEMA: SchemaObject;
|
|
13
|
-
export declare const DEFAULT_PATH_PARAM: ParameterObject & Dictionary<
|
|
13
|
+
export declare const DEFAULT_PATH_PARAM: ParameterObject & Dictionary<unknown>;
|
|
14
14
|
export declare enum methods {
|
|
15
15
|
get = "get",
|
|
16
16
|
put = "put",
|
|
@@ -147,7 +147,7 @@ class ServiceGenerator {
|
|
|
147
147
|
if (props.length > 0) {
|
|
148
148
|
lastTypes.push({
|
|
149
149
|
typeName: this.getTypeName(Object.assign(Object.assign({}, operationObject), { method, path: pathKey })),
|
|
150
|
-
type: 'Record<string,
|
|
150
|
+
type: 'Record<string, unknown>',
|
|
151
151
|
props: [props],
|
|
152
152
|
isEnum: false,
|
|
153
153
|
});
|
|
@@ -165,7 +165,7 @@ class ServiceGenerator {
|
|
|
165
165
|
? type_1.SchemaObjectType.number
|
|
166
166
|
: result.type;
|
|
167
167
|
}
|
|
168
|
-
return 'Record<string,
|
|
168
|
+
return 'Record<string, unknown>';
|
|
169
169
|
};
|
|
170
170
|
// 解析 props 属性中的枚举
|
|
171
171
|
if ((0, lodash_1.isArray)(result.props) && result.props.length > 0) {
|
|
@@ -223,7 +223,7 @@ class ServiceGenerator {
|
|
|
223
223
|
if (((_a = body === null || body === void 0 ? void 0 : body.mediaType) === null || _a === void 0 ? void 0 : _a.includes('form-data')) || file) {
|
|
224
224
|
formData = true;
|
|
225
225
|
}
|
|
226
|
-
let functionName = this.
|
|
226
|
+
let functionName = this.getFunctionName(newApi);
|
|
227
227
|
if (functionName && tmpFunctionRD[functionName]) {
|
|
228
228
|
functionName = `${functionName}_${(tmpFunctionRD[functionName] += 1)}`;
|
|
229
229
|
}
|
|
@@ -335,7 +335,7 @@ class ServiceGenerator {
|
|
|
335
335
|
getTemplate(type) {
|
|
336
336
|
return (0, fs_1.readFileSync)((0, path_1.join)(this.config.templatesFolder, `${type}.njk`), 'utf8');
|
|
337
337
|
}
|
|
338
|
-
|
|
338
|
+
getFunctionName(data) {
|
|
339
339
|
// 获取路径相同部分
|
|
340
340
|
const pathBasePrefix = (0, util_1.getBasePrefix)((0, lodash_1.keys)(this.openAPIData.paths));
|
|
341
341
|
return this.config.hook && this.config.hook.customFunctionName
|
|
@@ -364,10 +364,11 @@ class ServiceGenerator {
|
|
|
364
364
|
getTypeName(data) {
|
|
365
365
|
var _a, _b, _c;
|
|
366
366
|
const namespace = this.config.namespace ? `${this.config.namespace}.` : '';
|
|
367
|
-
const typeName = ((_c = (_b = (_a = this.config) === null || _a === void 0 ? void 0 : _a.hook) === null || _b === void 0 ? void 0 : _b.customTypeName) === null || _c === void 0 ? void 0 : _c.call(_b, data)) || this.
|
|
367
|
+
const typeName = ((_c = (_b = (_a = this.config) === null || _a === void 0 ? void 0 : _a.hook) === null || _b === void 0 ? void 0 : _b.customTypeName) === null || _c === void 0 ? void 0 : _c.call(_b, data)) || this.getFunctionName(data);
|
|
368
368
|
return (0, util_1.resolveTypeName)(`${namespace}${typeName !== null && typeName !== void 0 ? typeName : data.operationId}Params`);
|
|
369
369
|
}
|
|
370
370
|
getBodyTP(requestBody) {
|
|
371
|
+
var _a;
|
|
371
372
|
const reqBody = this.resolveRefObject(requestBody);
|
|
372
373
|
if ((0, lodash_1.isEmpty)(reqBody)) {
|
|
373
374
|
return null;
|
|
@@ -377,7 +378,7 @@ class ServiceGenerator {
|
|
|
377
378
|
return null;
|
|
378
379
|
}
|
|
379
380
|
let mediaType = (0, lodash_1.keys)(reqContent)[0];
|
|
380
|
-
const schema = reqContent[mediaType].schema || config_1.DEFAULT_SCHEMA;
|
|
381
|
+
const schema = ((_a = reqContent[mediaType]) === null || _a === void 0 ? void 0 : _a.schema) || config_1.DEFAULT_SCHEMA;
|
|
381
382
|
if (mediaType === '*/*') {
|
|
382
383
|
mediaType = '';
|
|
383
384
|
}
|
|
@@ -448,7 +449,7 @@ class ServiceGenerator {
|
|
|
448
449
|
this.resolveRefObject(responses.default || responses['200'] || responses['201']);
|
|
449
450
|
const defaultResponse = {
|
|
450
451
|
mediaType: '*/*',
|
|
451
|
-
type: '
|
|
452
|
+
type: 'unknown',
|
|
452
453
|
};
|
|
453
454
|
if (!response) {
|
|
454
455
|
return defaultResponse;
|
|
@@ -558,8 +559,8 @@ class ServiceGenerator {
|
|
|
558
559
|
type: `${refPaths[refPaths.length - 1]}[]`,
|
|
559
560
|
};
|
|
560
561
|
}
|
|
561
|
-
// 这里需要解析出具体属性,但由于 parser 层还不确定,所以暂时先返回
|
|
562
|
-
return { type: '
|
|
562
|
+
// 这里需要解析出具体属性,但由于 parser 层还不确定,所以暂时先返回 unknown[]
|
|
563
|
+
return { type: 'unknown[]' };
|
|
563
564
|
}
|
|
564
565
|
resolveProperties(schemaObject) {
|
|
565
566
|
return {
|
package/dist/generator/util.js
CHANGED
|
@@ -66,7 +66,7 @@ function getRefName(refObject) {
|
|
|
66
66
|
function getDefaultType(schemaObject, namespace = '', schemas) {
|
|
67
67
|
var _a, _b;
|
|
68
68
|
if ((0, lodash_1.isUndefined)(schemaObject) || (0, lodash_1.isNull)(schemaObject)) {
|
|
69
|
-
return '
|
|
69
|
+
return 'unknown';
|
|
70
70
|
}
|
|
71
71
|
if (!(0, lodash_1.isObject)(schemaObject)) {
|
|
72
72
|
return schemaObject;
|
|
@@ -146,7 +146,7 @@ function getDefaultType(schemaObject, namespace = '', schemas) {
|
|
|
146
146
|
}
|
|
147
147
|
if (schemaObject.type === 'object' || schemaObject.properties) {
|
|
148
148
|
if (!(0, lodash_1.keys)(schemaObject.properties).length) {
|
|
149
|
-
return 'Record<string,
|
|
149
|
+
return 'Record<string, unknown>';
|
|
150
150
|
}
|
|
151
151
|
return `{ ${(0, lodash_1.keys)(schemaObject.properties)
|
|
152
152
|
.map((key) => {
|
|
@@ -173,7 +173,7 @@ function getDefaultType(schemaObject, namespace = '', schemas) {
|
|
|
173
173
|
})
|
|
174
174
|
.join('')}}`;
|
|
175
175
|
}
|
|
176
|
-
return '
|
|
176
|
+
return 'unknown';
|
|
177
177
|
}
|
|
178
178
|
exports.getDefaultType = getDefaultType;
|
|
179
179
|
function getDefaultFileTag(operationObject, apiPath) {
|
package/dist/index.js
CHANGED
|
@@ -17,7 +17,7 @@ function generateService(_a) {
|
|
|
17
17
|
return;
|
|
18
18
|
}
|
|
19
19
|
const requestImportStatement = (0, util_1.getImportStatement)(requestLibPath);
|
|
20
|
-
const serviceGenerator = new serviceGenarator_1.default(Object.assign({ schemaPath, serversPath: './src/apis', requestImportStatement, requestOptionsType: '{[key: string]:
|
|
20
|
+
const serviceGenerator = new serviceGenarator_1.default(Object.assign({ schemaPath, serversPath: './src/apis', requestImportStatement, requestOptionsType: '{[key: string]: unknown}', namespace: 'API', nullable: false, isCamelCase: true, allowedTags: allowedTags
|
|
21
21
|
? (0, lodash_1.map)(allowedTags, (item) => item.toLowerCase())
|
|
22
22
|
: null }, rest), openAPI);
|
|
23
23
|
serviceGenerator.genFile();
|
package/dist/type.d.ts
CHANGED
|
@@ -36,7 +36,7 @@ export type ParameterObject = Modify<OpenAPIV3.ParameterObject, {
|
|
|
36
36
|
}>;
|
|
37
37
|
export type ResponsesObject = OpenAPIV3.ResponsesObject;
|
|
38
38
|
export type ResponseObject = OpenAPIV3.ResponseObject & {
|
|
39
|
-
example?:
|
|
39
|
+
example?: unknown;
|
|
40
40
|
};
|
|
41
41
|
export type RequestBodyObject = OpenAPIV3.RequestBodyObject;
|
|
42
42
|
export type ContentObject = {
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "openapi-ts-request",
|
|
3
|
-
"version": "0.1.
|
|
4
|
-
"description": "Swagger2/OpenAPI3 to TypeScript, Request Client, Display Label",
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "Swagger2/OpenAPI3 to TypeScript, Request Client, Request Mock Service, Enum, Display Field Label",
|
|
5
5
|
"packageManager": "pnpm@9.4.0",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
@@ -98,7 +98,7 @@ export async function {{ api.functionName }}(
|
|
|
98
98
|
{% if api.body %}
|
|
99
99
|
Object.keys(body).forEach(ele => {
|
|
100
100
|
{% if genType === "ts" %}
|
|
101
|
-
const item = (body as any)[ele];
|
|
101
|
+
const item = (body as { [key: string]: any })[ele];
|
|
102
102
|
{% else %}
|
|
103
103
|
const item = body[ele];
|
|
104
104
|
{% endif %}
|