1.4.1 • Published 5 months ago

socg v1.4.1

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

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',
    // you can use `customEnumMember` option to custom enum type key.
    // customEnumMember(enumValue) {
    //   return {name: 'enum_member_key', initializer: 'enum_member_value'}
    // }
})

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"
    }
}
1.4.1

5 months ago

1.4.0

5 months ago

1.3.1

10 months ago

1.3.0

10 months ago

1.2.0

11 months ago

1.1.2

11 months ago

1.1.1

11 months ago

1.1.0

11 months ago

1.0.1

12 months ago

1.0.0

12 months ago