Skip to content

Commit

Permalink
feat: 支持更多HttpMethod
Browse files Browse the repository at this point in the history
  • Loading branch information
arbing committed Apr 1, 2024
1 parent 63c4348 commit 63d0200
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "swagger-to-ts",
"version": "1.4.4",
"version": "1.4.5",
"description": "Generate typescript services and models from Swagger",
"main": "./lib/index.js",
"bin": {
Expand Down
22 changes: 17 additions & 5 deletions src/CodeGen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ export const defaultConfig: GenConfig = {
pathReplace: undefined,
}

type HttpMethod = 'get' | 'post'
const httpMethods: HttpMethod[] = ['get', 'post']
type HttpMethod = 'get' | 'post' | 'put' | 'delete'
const httpMethods: HttpMethod[] = ['get', 'post', 'put', 'delete']

interface ApiDef {
name: string
Expand All @@ -92,6 +92,7 @@ interface OperationDef {
apiName: string
path: string
method: string
methodFn: string
name: string
fullName: string
summary?: string
Expand Down Expand Up @@ -287,6 +288,8 @@ export class CodeGen {
}`,
)

const fullNames = new Set<string>()

for (let path of pathKeys) {
const pathItem = this.#doc.paths[path] as OpenAPIV2.PathItemObject
for (const method of httpMethods) {
Expand Down Expand Up @@ -317,16 +320,22 @@ export class CodeGen {
const hasArgs = !!paramsTypeInfo?.type || !!dataTypeInfo?.type
const hasBody = method !== 'get'
const hasReturn = !!returnType && returnType !== 'void'
const methodFn = hasReturn ? (method === 'delete' ? 'del' : method) : 'download'

const fullPath = this.#config.baseUrl ? this.#config.baseUrl + path : path
const baseName = this.#config.baseName
? this.#config.baseName
: _.camelCase(this.#config.baseUrl.replace(/\//g, ''))
const fullName = (baseName ? baseName + '_' : '') + this.extractApiOperationFullName(path)
let fullName = (baseName ? baseName + '_' : '') + this.extractApiOperationFullName(path)
if (fullNames.has(fullName)) {
fullName = `${fullName}_${method}`
}

const operation: OperationDef = {
apiName: apiName,
path: fullPath,
method: hasReturn ? method : 'download',
method: method,
methodFn: methodFn,
name: this.extractOperationName(path),
fullName: fullName,
summary: opItem?.summary,
Expand All @@ -342,6 +351,7 @@ export class CodeGen {
this.#operations.push(operation)

this.#apis.add(apiName)
fullNames.add(fullName)
}
}
}
Expand Down Expand Up @@ -756,7 +766,9 @@ export class CodeGen {

console.log(`[INFO]: 生成 api..., apiName: ${api.name}, operations: ${operations.length}`)

const text = Mustache.render(apiTemplate, { api, operations })
const methodFnStr = Array.from(new Set(operations.map((d) => d.methodFn))).join(', ')

const text = Mustache.render(apiTemplate, { methodFnStr, api, operations })
fs.writeFileSync(path.join(apisDir, `${api.name}.ts`), text, fileOptions)
}

Expand Down
4 changes: 2 additions & 2 deletions template/api.mustache
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import type { FetchConfig } from '@/services'
import { get, post, download } from '@/services'
import { {{methodFnStr}} } from '@/services'
import type * as models from './models'

{{#operations}}
/**
* {{&summary}}
*/
export function {{fullName}}({{#paramsType}}params{{^paramsRequired}}?{{/paramsRequired}}: {{&paramsType}}{{#dataType}}, {{/dataType}}{{/paramsType}}{{#dataType}}data{{^dataRequired}}?{{/dataRequired}}: {{&dataType}}{{/dataType}}{{#hasArgs}}, {{/hasArgs}}config?: Partial<FetchConfig>) {
return {{method}}{{#hasReturn}}<{{&returnType}}>{{/hasReturn}}('{{&path}}', {{#paramsType}}params{{/paramsType}}{{^paramsType}}undefined{{/paramsType}}{{#hasBody}}, {{#dataType}}data{{/dataType}}{{^dataType}}undefined{{/dataType}}{{/hasBody}}, config)
return {{methodFn}}{{#hasReturn}}<{{&returnType}}>{{/hasReturn}}('{{&path}}', {{#paramsType}}params{{/paramsType}}{{^paramsType}}undefined{{/paramsType}}{{#hasBody}}, {{#dataType}}data{{/dataType}}{{^dataType}}undefined{{/dataType}}{{/hasBody}}, config)
}{{^-last}}

{{/-last}}
Expand Down

0 comments on commit 63d0200

Please sign in to comment.