@a993576769/openapi-codegen 中文文档教程
OpenAPI Typescript Codegen
可自定义token字段名的openapi-typescript-codegen
Install
npm install @a993576769/openapi-codegen --save-dev
or
yarn add @a993576769/openapi-codegen --dev
Usage
$ openapi --help
Usage: openapi [options]
Options:
-V, --version output the version number
-i, --input <value> OpenAPI specification, can be a path, url or string content (required)
-o, --output <value> Output directory (required)
-c, --client <value> HTTP client to generate [fetch, xhr, node] (default: "fetch")
--useOptions Use options instead of arguments
--useUnionTypes Use union types instead of enums
--exportCore <value> Write core files to disk (default: true)
--exportServices <value> Write services to disk (default: true)
--exportModels <value> Write models to disk (default: true)
--exportSchemas <value> Write schemas to disk (default: false)
Examples
$ openapi --input ./spec.json
$ openapi --input ./spec.json --output ./dist
$ openapi --input ./spec.json --output ./dist --client xhr
Example
package.json
{
"scripts": {
"generate": "openapi --input ./spec.json --output ./dist"
}
}
Authorization
The OpenAPI generator supports Bearer Token authorization. In order to enable the sending of tokens in each request you can set the token using the global OpenAPI configuration:
import { OpenAPI } from './generated';
OpenAPI.TOKENFIELD = 'token-field';
OpenAPI.TOKEN = 'some-bearer-token';
Alternatively, we also support an async method that provides the token for each request. You can simply assign this method to the same TOKEN
property in the global OpenAPI object.
import { OpenAPI } from './generated';
const getToken = async () => {
// Some code that requests a token...
return 'SOME_TOKEN';
}
OpenAPI.TOKEN = getToken;