socg
v1.3.1
Published
A swagger openapi codegen tool that can significantly improve your coding efficiency
Downloads
12
Readme
socg
s(swagger)o(openapi)c(code)g(generate) is a swagger model and api code generator for javascript.
install
npm install -g socg
usage
socg -h
example
- just only generate typescript model file
socg model http://example.swagger.io/v2/swagger.json -o types/api.ts --locale en
- generate both typescript model file and api file
socg generate http://example.swagger.io/v1/swagger.json -d apis/ -m model.ts -l zh-CN
config
socg
is configurable, that means you can customize generation with a config file called socg.config.(c)js
in your project root directory.
here is a example configuration:
const { defineConfig } = require("socg")
module.exports = defineConfig({
generate: {
output(code) {
return [
`import http from "http";`,
code
].join('\r\n')
},
template({URL, METHOD, QUERY, BODY, RESPONSE, REQUESTCONTENTTYPE}) {
if (METHOD === "get" || METHOD === "delete") {
if (QUERY) {
return `http.${METHOD}<${RESPONSE}>(${URL}, ${QUERY})`
}
return `http.${METHOD}<${RESPONSE}>(${URL})`
} else {
// you can specify the request content type according to the `REQUESTCONTENTTYPE` value
const contentType = REQUESTCONTENTTYPE === "form_data" ? "multipart/form-data" : "application/json"
if (BODY) {
return `http.${METHOD}<${RESPONSE}>(${URL}, ${BODY}, {
"Content-Type": "${contentType}"
})`
}
return `http.${METHOD}<${RESPONSE}>(${URL})`
}
},
dir: "apis",
model: "model.ts",
locale: "zh-CN",
// fit typescript allowImportingTsExtensions option, default is true
allowImportingTsExtensions: false,
// fit typescript verbatimModuleSyntax option, default is true
verbatimModuleSyntax: false,
// support filter tags
// filterTag: ['Media'],
rewrite: (path) => path.replace(/^\/api/, ''),
// ...otherOptions
},
// support filter endpoints, but currently only support generate command.
// filterEndpoint: ['/api/media/count'],
// default is `true` so you can also ignore this.
intro: true,
// set line endings for output code.
// eol: 'auto'
})
then you can just config a npm script to generate your api code in your package.json:
{
"scripts": {
"generate": "socg generate http://example.swagger.io/v1/swagger.json"
}
}