Built and signed on GitHub ActionsBuilt and signed on GitHub Actions
🔧 A tool for converting OpenApi/Swagger/Apifox into code.
STC
STC (Swagger Transform Code) is a tool for converting OpenApi/Swagger/Apifox into code.

Features
🚧 Encapsulate the "shared" directory.
- 🐹 Support for Swagger 2/3(OpenApi) and Apifox.
- 💡 Support plug-in development.
- 🐣 Built-in transformation languages:
- TypeScript, almost equivalent to handwriting. Depends on
axios,wx.request,fetch.
xhr/ajax、ofetchplanned- JavaScript, from TypeScript to it.
- Dart, dependency on
dio. - 🚧 Swift ...
- TypeScript, almost equivalent to handwriting. Depends on
Quick start
Download executable files
download by system:
- stc: Intel-based Mac
- stc-m: M-series Mac
- stc-linux:Linux
- stc-win.exe: Windows
NPM
1.Install the @lonu/stc npm package.
pnpm add @lonu/stc -D
2.Open the project's package.json file and add the following command to scripts:
{ "scripts": { "api": "stc --url=http://127.0.0.1:4523/export/openapi/2?version=3.1" } }
Use
⚠️ Note: deno will not parse the ~ character as the user's home directory.
stc --url=https://petstore3.swagger.io/api/v3/openapi.json --outDir=out


Existing Project
Assume a project directory is:
. ├── src │ └── apis # Copy the shared directory here. │ └── shared │ └── xxx.ts # Other files.
Axios
-
Find the directory of
outDir, copy the entireshareddirectory to the directory of theaxiosmodule you encapsulated. -
Open the
shared > axios > index.tsfile, copy therequestmethod, and add it to theaxiosmodule you encapsulated. If it is not encapsulated, copy theindex.tsfile as a new file to avoid the problem of modification being overwritten. -
Taking
Vueas an example, add the following code to themain.tsfile:
import { createApiClient } from './apis/shared/fetchRuntime'; createApiClient({ baseURL: 'https://api.xxx.com' // onError(msg) { // // 处理错误信息 // } })
-
Find the directory of
outDir, copy the entire directory ofsharedto the directory of thewechatmodule you encapsulated. -
Open the
shared > wechat > index.tsfile, copy therequestmethod, and add it to thewx.requestcode file you encapsulated. If it is not encapsulated, copy theindex.tsfile as a new file to avoid the problem of modification being overwritten. -
Add the following code to the
app.tsfile:
import { createApiClient } from './apis/shared/fetchRuntime'; // import Notify from './miniprogram_npm/@vant/weapp/notify/notify'; App<IAppOption>({ onLaunch() { createApiClient({ baseURL: 'https://api.xxx.com, onError(msg) { // Notify({ type: 'danger', message: msg, selector: '#v-notify'}) } }) } });
Options
| Option | Alias | Type | Default | Description |
|---|---|---|---|---|
| url | string | Swagger/OpenApi/Apifox document address, or local path. | ||
| outDir | o | string | ./stc_out | Output Directory. |
| client | string | axios | http request client. When lang is ts/js, the possible values are: axios, wechat, fetch. |
|
| lang | l | string | ts | Language, used for output file suffix. |
| tag | number | Specify the tag from the interface url. By default, the first tag is read for the file name. | ||
| filter | f | string[] | Filter interfaces. Interfaces that meet the filter conditions will be generated. Example: --filter "/pet*", generate an interface for /pet, and support multiple --filter. For more usage information, please refer to micromatch |
|
| conjunction | c | string | By | The method's connector, the default value is By. |
| actionIndex | number | -1 | The method name index, the default value is -1. |
|
| shared | boolean | true | Whether to generate the shared directory. [default: true]. | |
| clean | boolean | true | Whether to clean the output directory before generating. [default: true]. | |
| globalHeader | gh | string[] | Global header key configuration, multiple can be set. When a single API has the same key, it will not appear as a parameter. | |
| version | v | boolean | Output version information. | |
| help | h | boolean | Output help information. |
Plug-in development
For convenience, STC can not only develop plugins in Deno, but also provides @lonu/stc npm library, which can develop plugins in Node environment.
Deno
⚠️ Prepare the Deno environment.
Create a myPlugin.ts file:
// 引用模块 // import { start } from 'https://deno.land/x/stc@2.15.0/mod.ts' import { start } from 'jsr:@lonu/stc@^2.15.0' // Defining plugins const myPlugin: IPlugin = { name: 'stc:MyPlugin', lang: 'ts', setup(context: IPluginContext) { // type map return { } }, onTransform(def, action) { // definition const defContent: string = parserDefinition( def ) // action const actionContent: Map<string, string> = parserAction( action ) return { definition: { filename: '_types.ts', content: defContent, }, action: actionContent // Here actionContent is of type Map<string, string>, key is the file name, value is the converted code. } }, onEnd() { console.log('end') } } // use plugin start({ // ...other options plugins: [myPlugin] })
Node
-
Create a
myPlugin.tsfile. -
Add the
@lonu/stcreference and use thestartmethod:
import { start } from '@lonu/stc'
- Implement the code that converts
definitionandactioninto the target language in the plugin'sonTransformhook function.
export const myPlugin: IPlugin = { name: 'stc:MyPlugin', lang: 'ts', setup(context: IPluginContext) { // type map return { } }, onTransform(def, action) { // definition const defContent: string = parserDefinition( def ) // action const actionContent: Map<string, string> = parserAction( action ) return { definition: defContent, action: actionContent } }, onEnd() { console.log('end') } }
4.In the start method, add plugins:
start({ // ...other options plugins: [myPlugin] })
Who's Using This?
Example 1
Example 1
import { start } from "https://deno.land/x/stc@$X_VERSION/mod.ts"; // 导入解析方法 import { parserDefinition } from "npm:@lonu/stc/plugin/definition"; import { parserActions } from 'npm:@lonu/stc/plugin/action' const myPlugin: IPlugin = { name: "stc:MyPlugin", setup(context) { console.log(context); // 类型映射 return {}; }, onTransform(def, action) { // 转换 definition const defContent: string = parserDefinition(def) // 转换 action const actionContent: Map<string, string> = parserAction(action) // 返回转换后的内容 return { definition: defContent, action: actionContent } } } start({ // ...其他配置 plugins: [myPlugin] })
Add Package
deno add jsr:@lonu/stc
Import symbol
import * as stc from "@lonu/stc";
Import directly with a jsr specifier
import * as stc from "jsr:@lonu/stc";
Add Package
pnpm i jsr:@lonu/stc
pnpm dlx jsr add @lonu/stc
Import symbol
import * as stc from "@lonu/stc";
Add Package
yarn add jsr:@lonu/stc
yarn dlx jsr add @lonu/stc
Import symbol
import * as stc from "@lonu/stc";
Add Package
vlt install jsr:@lonu/stc
Import symbol
import * as stc from "@lonu/stc";
Add Package
npx jsr add @lonu/stc
Import symbol
import * as stc from "@lonu/stc";
Add Package
bunx jsr add @lonu/stc
Import symbol
import * as stc from "@lonu/stc";