swagger-axios-codegen
Advanced tools
Comparing version 0.7.4 to 0.8.0
## 0.8.0 | ||
- Add class-transformer to transform results and support dates [issue 37](https://github.com/Manweill/swagger-axios-codegen/issues/40) by [arkraft](https://github.com/arkraft) | ||
- Fix array in response bodys [issue 32](https://github.com/Manweill/swagger-axios-codegen/issues/32) with [LucaDe](https://github.com/LucaDe) | ||
## 0.7.4 | ||
- Make responseType、baseURL configurable [issue 41](https://github.com/Manweill/swagger-axios-codegen/issues/40) with [LucaDe](https://github.com/LucaDe) | ||
## 0.7.3 | ||
@@ -3,0 +13,0 @@ |
@@ -17,2 +17,4 @@ export interface ISwaggerOptions { | ||
modelMode?: 'class' | 'interface'; | ||
/** use class-transformer to transform the results */ | ||
useClassTransformer?: boolean; | ||
} | ||
@@ -22,3 +24,6 @@ export interface IPropDef { | ||
type: string; | ||
format?: string; | ||
desc: string; | ||
isType: boolean; | ||
isEnum: boolean; | ||
} | ||
@@ -25,0 +30,0 @@ export interface IInclude { |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
//# sourceMappingURL=baseInterfaces.js.map |
@@ -0,0 +0,0 @@ import { IDefinitionProperties } from "../swaggerInterfaces"; |
@@ -46,3 +46,3 @@ "use strict"; | ||
// propsStr += classPropsTemplate(k, propType, v.description) | ||
model.props.push({ name: k, type: propType, desc: v.description }); | ||
model.props.push({ name: k, type: propType, format: v.format, desc: v.description, isType, isEnum }); | ||
} | ||
@@ -49,0 +49,0 @@ // : classTemplate(className, propsStr, constructorStr) |
@@ -0,0 +0,0 @@ import { IEnumDef } from "../baseInterfaces"; |
@@ -0,0 +0,0 @@ "use strict"; |
@@ -0,0 +0,0 @@ import { IDefinitions } from '../swaggerInterfaces'; |
@@ -0,0 +0,0 @@ "use strict"; |
@@ -0,0 +0,0 @@ import { IDefinitionProperty } from "../swaggerInterfaces"; |
@@ -0,0 +0,0 @@ "use strict"; |
import { ISwaggerOptions } from './baseInterfaces'; | ||
export declare function codegen(params: ISwaggerOptions): Promise<void>; |
@@ -31,3 +31,4 @@ "use strict"; | ||
include: [], | ||
strictNullChecks: true | ||
strictNullChecks: true, | ||
useClassTransformer: false, | ||
}; | ||
@@ -59,4 +60,4 @@ async function codegen(params) { | ||
let apiSource = options.useCustomerRequestInstance | ||
? template_1.customerServiceHeader | ||
: template_1.serviceHeader; | ||
? template_1.customerServiceHeader(options) | ||
: template_1.serviceHeader(options); | ||
// TODO: next next next time | ||
@@ -90,3 +91,3 @@ // if (options.multipleFileMode) { | ||
? template_1.interfaceTemplate(item.value.name, item.value.props, [], params.strictNullChecks) | ||
: template_1.classTemplate(item.value.name, item.value.props, [], params.strictNullChecks); | ||
: template_1.classTemplate(item.value.name, item.value.props, [], params.strictNullChecks, options.useClassTransformer); | ||
// const fileDir = path.join(options.outputDir || '', 'definitions') | ||
@@ -149,3 +150,3 @@ // writeFile(fileDir, item.name + '.ts', format(text, options)) | ||
? template_1.interfaceTemplate(item.value.name, item.value.props, [], params.strictNullChecks) | ||
: template_1.classTemplate(item.value.name, item.value.props, [], params.strictNullChecks); | ||
: template_1.classTemplate(item.value.name, item.value.props, [], params.strictNullChecks, options.useClassTransformer); | ||
defSource += text; | ||
@@ -182,3 +183,3 @@ } | ||
? template_1.interfaceTemplate(item.value.name, item.value.props, [], params.strictNullChecks) | ||
: template_1.classTemplate(item.value.name, item.value.props, [], params.strictNullChecks); | ||
: template_1.classTemplate(item.value.name, item.value.props, [], params.strictNullChecks, options.useClassTransformer); | ||
apiSource += text; | ||
@@ -185,0 +186,0 @@ }); |
@@ -0,0 +0,0 @@ import { IParameter } from '../swaggerInterfaces'; |
@@ -67,3 +67,7 @@ "use strict"; | ||
else if (p.in === 'body') { | ||
var body = p.schema ? `...params['${paramName}']` : `'${p.name}':params['${paramName}']`; | ||
var body = p.schema | ||
? p.schema.type === 'array' | ||
? `[...params['${paramName}']]` | ||
: `...params['${paramName}']` | ||
: `'${p.name}':params['${paramName}']`; | ||
bodyParameters.push(body); | ||
@@ -70,0 +74,0 @@ } |
@@ -0,0 +0,0 @@ import { IRequestMethod } from '../swaggerInterfaces'; |
@@ -23,2 +23,3 @@ "use strict"; | ||
let checkType = resSchema.type; | ||
let format = resSchema.format; | ||
// 如果是数组 | ||
@@ -32,3 +33,3 @@ if (checkType === 'array' || resSchema.items) { | ||
else { | ||
const refType = utils_1.toBaseType(resSchema.type); | ||
const refType = utils_1.toBaseType(resSchema.items.type, resSchema.items.format); | ||
result = refType + '[]'; | ||
@@ -44,3 +45,3 @@ } | ||
result = checkType; | ||
result = utils_1.toBaseType(result); | ||
result = utils_1.toBaseType(result, format); | ||
} | ||
@@ -47,0 +48,0 @@ if (result == 'object') { |
@@ -0,0 +0,0 @@ import { IPaths } from '../swaggerInterfaces'; |
@@ -0,0 +0,0 @@ "use strict"; |
@@ -30,2 +30,3 @@ export interface ISwaggerSource { | ||
'items'?: IParameterItems; | ||
'format'?: string; | ||
}; | ||
@@ -56,2 +57,3 @@ }; | ||
type?: string; | ||
format?: string; | ||
$ref: string; | ||
@@ -58,0 +60,0 @@ items?: IParameterItems; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
//# sourceMappingURL=swaggerInterfaces.js.map |
@@ -1,8 +0,9 @@ | ||
import { IPropDef } from "./baseInterfaces"; | ||
import { IPropDef, ISwaggerOptions } from "./baseInterfaces"; | ||
/** 类模板 */ | ||
export declare function interfaceTemplate(name: string, props: IPropDef[], imports: string[], strictNullChecks?: boolean): string; | ||
/** 类模板 */ | ||
export declare function classTemplate(name: string, props: IPropDef[], imports: string[], strictNullChecks?: boolean): string; | ||
export declare function classTemplate(name: string, props: IPropDef[], imports: string[], strictNullChecks: boolean, useClassTransformer: boolean): string; | ||
/** 类属性模板 */ | ||
export declare function classPropsTemplate(filedName: string, type: string, description: string, canNull: boolean): string; | ||
export declare function classPropsTemplate(filedName: string, type: string, format: string, description: string, canNull: boolean, useClassTransformer: boolean, isType: boolean): string; | ||
export declare function classTransformTemplate(type: string, format: string, isType: boolean): string; | ||
/** 类属性模板 */ | ||
@@ -27,4 +28,4 @@ export declare function classConstructorTemplate(name: string): string; | ||
export declare function serviceTemplate(name: string, body: string): string; | ||
export declare const serviceHeader = "/** Generate by swagger-axios-codegen */\n\nimport axiosStatic, { AxiosPromise, AxiosInstance } from 'axios';\nexport interface IRequestOptions {\n headers?: any;\n baseURL?: string;\n}\n\ninterface IRequestConfig {\n method?: any;\n headers?: any;\n url?: any;\n data?: any;\n params?: any;\n}\n\n// Add options interface\nexport interface ServiceOptions {\n axios?: AxiosInstance,\n}\n\n// Add default options\nexport const serviceOptions: ServiceOptions = {\n};\n\n// Instance selector\nfunction axios(configs: IRequestConfig, resolve: (p: any) => void, reject: (p: any) => void) {\n const req = serviceOptions.axios ? serviceOptions.axios.request(configs) : axiosStatic(configs);\n\n return req.then((res) => { resolve(res.data); }).catch(err => { reject(err); });\n}\n\nfunction getConfigs(method: string, contentType: string, url: string,options: any):IRequestConfig {\n const configs: IRequestConfig = { ...options, method, url };\n configs.headers = {\n ...options.headers,\n 'Content-Type': contentType,\n };\n return configs\n}\n"; | ||
export declare const customerServiceHeader = "/** Generate by swagger-axios-codegen */\n\nexport interface IRequestOptions {\n headers?: any;\n}\n\ninterface IRequestPromise<T=any> extends Promise<IRequestResponse<T>> {}\n\ninterface IRequestResponse<T=any> {\n data: T;\n status: number;\n statusText: string;\n headers: any;\n config: any;\n request?: any;\n}\n\ninterface IRequestInstance {\n (config: any): IRequestPromise;\n (url: string, config?: any): IRequestPromise;\n request<T = any>(config: any): IRequestPromise<T>;\n}\n\ninterface IRequestConfig {\n method?: any;\n headers?: any;\n url?: any;\n data?: any;\n params?: any;\n}\n\n// Add options interface\nexport interface ServiceOptions {\n axios?: IRequestInstance,\n}\n\n// Add default options\nexport const serviceOptions: ServiceOptions = {\n};\n\n// Instance selector\nfunction axios(configs: IRequestConfig): IRequestPromise {\n return serviceOptions.axios && serviceOptions.axios.request(configs);\n}\n"; | ||
export declare const serviceHeader: (options: ISwaggerOptions) => string; | ||
export declare const customerServiceHeader: (options: ISwaggerOptions) => string; | ||
export {}; |
@@ -7,2 +7,4 @@ "use strict"; | ||
const camelcase_1 = __importDefault(require("camelcase")); | ||
const utils_1 = require("./utils"); | ||
const baseTypes = ['string', 'number', 'object', 'boolean', 'any']; | ||
/** 类模板 */ | ||
@@ -19,3 +21,3 @@ function interfaceTemplate(name, props, imports, strictNullChecks = true) { | ||
${props.map(p => classPropsTemplate(p.name, p.type, p.desc, !strictNullChecks)).join('')} | ||
${props.map(p => classPropsTemplate(p.name, p.type, null, p.desc, !strictNullChecks, false, false)).join('')} | ||
} | ||
@@ -26,7 +28,11 @@ `; | ||
/** 类模板 */ | ||
function classTemplate(name, props, imports, strictNullChecks = true) { | ||
function classTemplate(name, props, imports, strictNullChecks = true, useClassTransformer) { | ||
// 所有的引用 | ||
const importString = imports.map(imp => { | ||
const mappedImports = imports.map(imp => { | ||
return `import { ${imp} } from '../definitions/${imp}'\n`; | ||
}).join(''); | ||
}); | ||
if (useClassTransformer && imports.length > 0) { | ||
mappedImports.push(`import { Type, Transform, Expose } from 'class-transformer'\n`); | ||
} | ||
const importString = mappedImports.join(''); | ||
return ` | ||
@@ -37,3 +43,3 @@ ${importString} | ||
${props.map(p => classPropsTemplate(p.name, p.type, p.desc, !strictNullChecks)).join('')} | ||
${props.map(p => classPropsTemplate(p.name, p.type, p.format, p.desc, !strictNullChecks, useClassTransformer, p.isEnum || p.isType)).join('')} | ||
@@ -48,3 +54,3 @@ constructor(data: (undefined | any) = {}){ | ||
/** 类属性模板 */ | ||
function classPropsTemplate(filedName, type, description, canNull) { | ||
function classPropsTemplate(filedName, type, format, description, canNull, useClassTransformer, isType) { | ||
/** | ||
@@ -55,4 +61,7 @@ * eg: | ||
*/ | ||
type = utils_1.toBaseType(type, format); | ||
const decorators = useClassTransformer ? classTransformTemplate(type, format, isType) : ''; | ||
return ` | ||
/** ${description || ''} */ | ||
${decorators} | ||
${filedName}${canNull ? '?' : ''}:${type}; | ||
@@ -62,2 +71,12 @@ `; | ||
exports.classPropsTemplate = classPropsTemplate; | ||
function classTransformTemplate(type, format, isType) { | ||
const decorators = [`@Expose()`]; | ||
const nonArrayType = type.replace('[', '').replace(']', ''); | ||
/* ignore interfaces */ | ||
if (baseTypes.indexOf(nonArrayType) < 0 && !isType) { | ||
decorators.push(`@Type(() => ${nonArrayType})`); | ||
} | ||
return decorators.join('\n'); | ||
} | ||
exports.classTransformTemplate = classTransformTemplate; | ||
/** 类属性模板 */ | ||
@@ -80,3 +99,8 @@ function classConstructorTemplate(name) { | ||
let { summary = '', parameters = '', responseType = '', method = '', contentType = 'multipart/form-data', path = '', pathReplace = '', parsedParameters = {}, formData = '' } = requestSchema; | ||
const { useClassTransformer } = options; | ||
const { queryParameters = [], bodyParameters = [] } = parsedParameters; | ||
const nonArrayType = responseType.replace('[', '').replace(']', ''); | ||
const isArrayType = responseType.indexOf('[') > 0; | ||
const transform = useClassTransformer && baseTypes.indexOf(nonArrayType) < 0; | ||
const resolveString = transform ? `(response: any${isArrayType ? '[]' : ''}) => resolve(plainToClass(${nonArrayType}, response, {strategy: 'excludeAll'}))` : 'resolve'; | ||
return ` | ||
@@ -95,7 +119,7 @@ /** | ||
let data = ${parsedParameters && bodyParameters.length > 0 | ||
? '{' + bodyParameters.join(',') + '}' | ||
? bodyParameters.length === 1 && bodyParameters[0].startsWith('[') ? bodyParameters[0] : '{' + bodyParameters.join(',') + '}' | ||
: 'null'} | ||
${contentType === 'multipart/form-data' ? formData : ''} | ||
configs.data = data; | ||
axios(configs, resolve, reject) | ||
axios(configs, ${resolveString}, reject); | ||
}); | ||
@@ -114,88 +138,98 @@ }`; | ||
exports.serviceTemplate = serviceTemplate; | ||
exports.serviceHeader = `/** Generate by swagger-axios-codegen */ | ||
exports.serviceHeader = (options) => { | ||
const classTransformerImport = options.useClassTransformer | ||
? `import { Expose, Transform, Type, plainToClass } from 'class-transformer'; | ||
` : ''; | ||
return `/** Generate by swagger-axios-codegen */ | ||
import axiosStatic, { AxiosPromise, AxiosInstance } from 'axios'; | ||
import axiosStatic, { AxiosPromise, AxiosInstance } from 'axios'; | ||
export interface IRequestOptions { | ||
headers?: any; | ||
baseURL?: string; | ||
} | ||
${classTransformerImport} | ||
interface IRequestConfig { | ||
method?: any; | ||
headers?: any; | ||
url?: any; | ||
data?: any; | ||
params?: any; | ||
} | ||
export interface IRequestOptions { | ||
headers?: any; | ||
baseURL?: string; | ||
responseType?: string; | ||
} | ||
// Add options interface | ||
export interface ServiceOptions { | ||
axios?: AxiosInstance, | ||
} | ||
interface IRequestConfig { | ||
method?: any; | ||
headers?: any; | ||
url?: any; | ||
data?: any; | ||
params?: any; | ||
} | ||
// Add default options | ||
export const serviceOptions: ServiceOptions = { | ||
}; | ||
// Add options interface | ||
export interface ServiceOptions { | ||
axios?: AxiosInstance, | ||
} | ||
// Instance selector | ||
function axios(configs: IRequestConfig, resolve: (p: any) => void, reject: (p: any) => void) { | ||
const req = serviceOptions.axios ? serviceOptions.axios.request(configs) : axiosStatic(configs); | ||
// Add default options | ||
export const serviceOptions: ServiceOptions = { | ||
}; | ||
return req.then((res) => { resolve(res.data); }).catch(err => { reject(err); }); | ||
} | ||
// Instance selector | ||
function axios(configs: IRequestConfig, resolve: (p: any) => void, reject: (p: any) => void) { | ||
const req = serviceOptions.axios ? serviceOptions.axios.request(configs) : axiosStatic(configs); | ||
function getConfigs(method: string, contentType: string, url: string,options: any):IRequestConfig { | ||
const configs: IRequestConfig = { ...options, method, url }; | ||
configs.headers = { | ||
...options.headers, | ||
'Content-Type': contentType, | ||
}; | ||
return configs | ||
} | ||
`; | ||
exports.customerServiceHeader = `/** Generate by swagger-axios-codegen */ | ||
return req.then((res) => { resolve(res.data); }).catch(err => { reject(err); }); | ||
} | ||
export interface IRequestOptions { | ||
headers?: any; | ||
} | ||
function getConfigs(method: string, contentType: string, url: string,options: any):IRequestConfig { | ||
const configs: IRequestConfig = { ...options, method, url }; | ||
configs.headers = { | ||
...options.headers, | ||
'Content-Type': contentType, | ||
}; | ||
return configs | ||
} | ||
`; | ||
}; | ||
exports.customerServiceHeader = (options) => { | ||
return `/** Generate by swagger-axios-codegen */ | ||
interface IRequestPromise<T=any> extends Promise<IRequestResponse<T>> {} | ||
export interface IRequestOptions { | ||
headers?: any; | ||
} | ||
interface IRequestResponse<T=any> { | ||
data: T; | ||
status: number; | ||
statusText: string; | ||
headers: any; | ||
config: any; | ||
request?: any; | ||
} | ||
interface IRequestPromise<T=any> extends Promise<IRequestResponse<T>> {} | ||
interface IRequestInstance { | ||
(config: any): IRequestPromise; | ||
(url: string, config?: any): IRequestPromise; | ||
request<T = any>(config: any): IRequestPromise<T>; | ||
} | ||
interface IRequestResponse<T=any> { | ||
data: T; | ||
status: number; | ||
statusText: string; | ||
headers: any; | ||
config: any; | ||
request?: any; | ||
} | ||
interface IRequestConfig { | ||
method?: any; | ||
headers?: any; | ||
url?: any; | ||
data?: any; | ||
params?: any; | ||
} | ||
interface IRequestInstance { | ||
(config: any): IRequestPromise; | ||
(url: string, config?: any): IRequestPromise; | ||
request<T = any>(config: any): IRequestPromise<T>; | ||
} | ||
// Add options interface | ||
export interface ServiceOptions { | ||
axios?: IRequestInstance, | ||
} | ||
interface IRequestConfig { | ||
method?: any; | ||
headers?: any; | ||
url?: any; | ||
data?: any; | ||
params?: any; | ||
} | ||
// Add default options | ||
export const serviceOptions: ServiceOptions = { | ||
// Add options interface | ||
export interface ServiceOptions { | ||
axios?: IRequestInstance, | ||
} | ||
// Add default options | ||
export const serviceOptions: ServiceOptions = { | ||
}; | ||
// Instance selector | ||
function axios(configs: IRequestConfig): IRequestPromise { | ||
return serviceOptions.axios && serviceOptions.axios.request(configs); | ||
} | ||
`; | ||
}; | ||
// Instance selector | ||
function axios(configs: IRequestConfig): IRequestPromise { | ||
return serviceOptions.axios && serviceOptions.axios.request(configs); | ||
} | ||
`; | ||
//# sourceMappingURL=template.js.map |
@@ -17,5 +17,5 @@ import { IDefinitionClass, IDefinitionEnum } from "./baseInterfaces"; | ||
export declare function refClassName(s: string): string; | ||
export declare function toBaseType(s: string): string; | ||
export declare function toBaseType(s: string, format?: string): string; | ||
export declare function getMethodName(path: string): string; | ||
export declare function trimString(str: string, char: string, type: string): string; | ||
export declare function findDeepRefs(imports: string[], allDefinition: IDefinitionClass[], allEnums: IDefinitionEnum[]): string[]; |
@@ -35,3 +35,3 @@ "use strict"; | ||
exports.refClassName = refClassName; | ||
function toBaseType(s) { | ||
function toBaseType(s, format) { | ||
if (s === undefined || s === null || s.length === 0) { | ||
@@ -58,3 +58,10 @@ return 'any | null'; | ||
case 'string': | ||
result = 'string'; | ||
switch (format) { | ||
case 'date': | ||
case 'date-time': | ||
result = 'Date'; | ||
break; | ||
default: | ||
result = 'string'; | ||
} | ||
break; | ||
@@ -61,0 +68,0 @@ case 'file': |
{ | ||
"name": "swagger-axios-codegen", | ||
"version": "0.7.4", | ||
"version": "0.8.0", | ||
"main": "./dist/index", | ||
@@ -39,2 +39,2 @@ "typings": "./dist/", | ||
} | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
87282
47
1272