openapi-ts-request 0.1.0 → 0.1.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 +6 -5
- package/dist/generator/config.d.ts +1 -1
- package/dist/generator/serviceGenarator.d.ts +1 -1
- package/dist/generator/serviceGenarator.js +12 -9
- package/dist/generator/util.js +3 -3
- package/dist/index.js +1 -1
- package/dist/type.d.ts +1 -1
- package/package.json +4 -3
- 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
|
});
|
|
@@ -159,13 +159,14 @@ class ServiceGenerator {
|
|
|
159
159
|
const result = this.resolveObject(schema);
|
|
160
160
|
const getDefinesType = () => {
|
|
161
161
|
if (result === null || result === void 0 ? void 0 : result.type) {
|
|
162
|
+
console.log('888', result);
|
|
162
163
|
return schema.type === 'object'
|
|
163
164
|
? type_1.SchemaObjectType.object
|
|
164
165
|
: config_1.numberEnum.includes(result.type)
|
|
165
166
|
? type_1.SchemaObjectType.number
|
|
166
167
|
: result.type;
|
|
167
168
|
}
|
|
168
|
-
return 'Record<string,
|
|
169
|
+
return 'Record<string, unknown>';
|
|
169
170
|
};
|
|
170
171
|
// 解析 props 属性中的枚举
|
|
171
172
|
if ((0, lodash_1.isArray)(result.props) && result.props.length > 0) {
|
|
@@ -187,6 +188,7 @@ class ServiceGenerator {
|
|
|
187
188
|
schema.isAllowed) {
|
|
188
189
|
const isEnum = result.isEnum;
|
|
189
190
|
const typeName = (0, util_1.resolveTypeName)(schemaKey);
|
|
191
|
+
console.log('name:', typeName, getDefinesType());
|
|
190
192
|
lastTypes.push({
|
|
191
193
|
typeName,
|
|
192
194
|
type: getDefinesType(),
|
|
@@ -223,7 +225,7 @@ class ServiceGenerator {
|
|
|
223
225
|
if (((_a = body === null || body === void 0 ? void 0 : body.mediaType) === null || _a === void 0 ? void 0 : _a.includes('form-data')) || file) {
|
|
224
226
|
formData = true;
|
|
225
227
|
}
|
|
226
|
-
let functionName = this.
|
|
228
|
+
let functionName = this.getFunctionName(newApi);
|
|
227
229
|
if (functionName && tmpFunctionRD[functionName]) {
|
|
228
230
|
functionName = `${functionName}_${(tmpFunctionRD[functionName] += 1)}`;
|
|
229
231
|
}
|
|
@@ -335,7 +337,7 @@ class ServiceGenerator {
|
|
|
335
337
|
getTemplate(type) {
|
|
336
338
|
return (0, fs_1.readFileSync)((0, path_1.join)(this.config.templatesFolder, `${type}.njk`), 'utf8');
|
|
337
339
|
}
|
|
338
|
-
|
|
340
|
+
getFunctionName(data) {
|
|
339
341
|
// 获取路径相同部分
|
|
340
342
|
const pathBasePrefix = (0, util_1.getBasePrefix)((0, lodash_1.keys)(this.openAPIData.paths));
|
|
341
343
|
return this.config.hook && this.config.hook.customFunctionName
|
|
@@ -364,10 +366,11 @@ class ServiceGenerator {
|
|
|
364
366
|
getTypeName(data) {
|
|
365
367
|
var _a, _b, _c;
|
|
366
368
|
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.
|
|
369
|
+
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
370
|
return (0, util_1.resolveTypeName)(`${namespace}${typeName !== null && typeName !== void 0 ? typeName : data.operationId}Params`);
|
|
369
371
|
}
|
|
370
372
|
getBodyTP(requestBody) {
|
|
373
|
+
var _a;
|
|
371
374
|
const reqBody = this.resolveRefObject(requestBody);
|
|
372
375
|
if ((0, lodash_1.isEmpty)(reqBody)) {
|
|
373
376
|
return null;
|
|
@@ -377,7 +380,7 @@ class ServiceGenerator {
|
|
|
377
380
|
return null;
|
|
378
381
|
}
|
|
379
382
|
let mediaType = (0, lodash_1.keys)(reqContent)[0];
|
|
380
|
-
const schema = reqContent[mediaType].schema || config_1.DEFAULT_SCHEMA;
|
|
383
|
+
const schema = ((_a = reqContent[mediaType]) === null || _a === void 0 ? void 0 : _a.schema) || config_1.DEFAULT_SCHEMA;
|
|
381
384
|
if (mediaType === '*/*') {
|
|
382
385
|
mediaType = '';
|
|
383
386
|
}
|
|
@@ -448,7 +451,7 @@ class ServiceGenerator {
|
|
|
448
451
|
this.resolveRefObject(responses.default || responses['200'] || responses['201']);
|
|
449
452
|
const defaultResponse = {
|
|
450
453
|
mediaType: '*/*',
|
|
451
|
-
type: '
|
|
454
|
+
type: 'unknown',
|
|
452
455
|
};
|
|
453
456
|
if (!response) {
|
|
454
457
|
return defaultResponse;
|
|
@@ -558,8 +561,8 @@ class ServiceGenerator {
|
|
|
558
561
|
type: `${refPaths[refPaths.length - 1]}[]`,
|
|
559
562
|
};
|
|
560
563
|
}
|
|
561
|
-
// 这里需要解析出具体属性,但由于 parser 层还不确定,所以暂时先返回
|
|
562
|
-
return { type: '
|
|
564
|
+
// 这里需要解析出具体属性,但由于 parser 层还不确定,所以暂时先返回 unknown[]
|
|
565
|
+
return { type: 'unknown[]' };
|
|
563
566
|
}
|
|
564
567
|
resolveProperties(schemaObject) {
|
|
565
568
|
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.2",
|
|
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",
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
"license": "MIT",
|
|
11
11
|
"author": "rookie-luochao",
|
|
12
12
|
"main": "dist/index.js",
|
|
13
|
-
"types": "index.d.ts",
|
|
13
|
+
"types": "dist/index.d.ts",
|
|
14
14
|
"bin": {
|
|
15
15
|
"openapi-ts-request": "dist/cli.js"
|
|
16
16
|
},
|
|
@@ -23,6 +23,7 @@
|
|
|
23
23
|
"start": "tsc -w",
|
|
24
24
|
"build": "tsc",
|
|
25
25
|
"changeset": "changeset",
|
|
26
|
+
"prepublish:test": "npm run build && np --no-cleanup --yolo --no-publish --any-branch",
|
|
26
27
|
"lint": "eslint ./src --report-unused-disable-directives --max-warnings=0",
|
|
27
28
|
"lint:fix": "eslint ./src --report-unused-disable-directives --max-warnings=0 --fix",
|
|
28
29
|
"test": "rm -rf ./test/apis/ ./test/mocks && npm run build && cd ./test && node ./test.js && cd ..",
|
|
@@ -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 %}
|