open-api-typescript-request-generator 0.0.1 → 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 +14 -13
- package/es/Generator.js +29 -195
- 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 +170 -372
- package/es/types.js +24 -24
- package/es/utils.d.ts +4 -10
- package/es/utils.js +8 -44
- package/lib/Generator.js +29 -195
- 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 +170 -372
- package/lib/types.js +24 -24
- 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;
|