Skip to content
/ stc Public
forked from long-woo/stc

🔧 Swagger 文档转换成代码的工具。 A tool for converting Swagger documents into code.

License

Notifications You must be signed in to change notification settings

frezs/stc

This branch is 185 commits behind long-woo/stc:master.

Folders and files

NameName
Last commit message
Last commit date
May 16, 2024
Jun 5, 2023
Feb 27, 2024
Apr 22, 2024
Apr 24, 2024
Mar 27, 2024
Aug 2, 2023
Feb 23, 2024
Apr 24, 2024
Apr 24, 2024
Apr 9, 2024
Apr 15, 2024
Jul 26, 2023
Mar 8, 2023

Repository files navigation

STC

logo

STC (Swagger Transform Code) is a tool for converting Swagger documents into code files.

STC(Swagger Transform Code) 是一个 Swagger 文档转换成代码文件的工具。

Publish to release Publish Package to npmjs

feature: 特性:

  • 🐹 Support for Swagger 2, 3 and Apifox.

    🐹 支持 Swagger 2、3 和 Apifox。

  • 🌐 Support Axios, Wechat request library。

    🌐 支持 Axios、Wechat 请求库。

  • 💡 Support plug-in development.

    💡 支持插件开发。

  • 🐣 Built-in transformation languages:

    🐣 内置转换语言:

    • TypeScript, almost equivalent to handwriting.

      TypeScript,几乎等同于手写。

    • JavaScript, from TypeScript to it.

      JavaScript,由 TypeScript 转换而来。

    • 🚧 ...

Quick start 快速开始

Download executable files 下载可执行文件

download by system:

按系统下载

  • stc: Intel-based Mac

    stc:Intel 系列的 Mac

  • stc-m: M-series Mac

    stc-m:M 系列的 Mac

  • stc-linux:Linux

  • stc-win.exe: Windows

NPM

1.安装 @loogwoo/stc npm 包

pnpm add @loongwoo/stc -D

2.打开项目 package.json 文件,在 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.

注意:deno 不会解析 ~字符为用户主目录。

stc --url=https://petstore3.swagger.io/api/v3/openapi.json --outDir=out

终端输出信息

输出文件

已有项目

假设一个项目目录为:

.
├── src
│   └── apis # 将 shared 目录复制到这里
│       └── shared
│       └── xxx.ts # 其他文件

Axios

1.找到 outDir 的目录,复制 shared 整个目录到你封装的 axios 模块的目录下。

2.打开 shared > axios > index.ts 文件,复制 request 方法,添加到你封装的 axios 模块中。若没有封装的话,复制 index.ts 文件为一个新文件,以免修改被覆盖的问题。

3.以 Vue 为例,在 main.ts 文件中添加以下代码:

import { createApiClient } from './apis/shared/fetchRuntime';

createApiClient({
  baseURL: 'https://api.xxx.com'
  // onError(msg) {
  //   // 处理错误信息
  // }
})

Wechat

1.找到 outDir 的目录,复制 shared 整个目录到你封装的 wechat 模块的目录下。

2.打开 shared > wechat > index.ts 文件,复制 request 方法,添加到你封装的 wx.request 代码文件中。若没有封装的话,复制 index.ts 文件为一个新文件,以免修改被覆盖的问题。

3.在 app.ts 文件中添加以下代码:

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 文档地址,或者本地路径
outDir o string ./stc_out 输出目录
platform p string axios 平台,可选值:axioswechat
lang l string ts 语言,用于输出文件的后缀名
tag number 从接口 url 指定标签,默认读取 tags 的第一个用于文件名
filter f string[] 过滤接口,符合过滤条件的接口会被生成。示例: --filter "/pet/*",生成 /pet 的接口,同时支持多个 --filter
conjunction c string By 方法的连接词,默认值为 By
version v boolean 输出版本信息
help h boolean 输出帮助信息

Plug-in development 插件开发

For convenience, STC can not only develop plugins in Deno, but also provides @loongwoo/stc npm library, which can develop plugins in Node environment.

为了方便,STC 不仅可以在 Deno 中开发插件,同时也提供了 @loongwoo/stc npm 库,可以在 Node 环境中开发插件。

查看示例代码

Deno 方式

⚠️ 准备 Deno 环境

创建一个 myPlugin.ts 文件:

// 引用模块
import { start } from 'https://deno.land/x/[email protected]/mod.ts'

// 定义插件
const myPlugin: IPlugin = {
  name: 'stc:MyPlugin',
  lang: 'ts',
  setup(options) {
    console.log(options)
  },
  onTransform(def, action) {
    // 转换 definition
    const defContent: string = parserDefinition(
      def /* 这里的 def 是 Definition 对象 */
    )
    // 转换 action
    const actionContent: Map<string, string> = parserAction(
      action /* 这里的 action 是 Action 对象 */
    )
    // 返回转换后的内容
    return {
      definition: defContent,
      action: actionContent // 这里的 actionContent 是 Map<string, string> 类型,key 是文件名称,value 是转换后的代码
    }
  },
  onEnd() {
    console.log('end')
  }
}

// 使用插件
start({
  // ...其他配置
  plugins: [myPlugin]
})

Node 方式

1.创建一个 myPlugin.ts 文件。

2.添加 @loongwoo/stc 引用,使用 start 方法:

import { start } from '@loongwoo/stc'

3.在插件的 onTransform 钩子函数中实现将 definitionaction 转换为目标语言的代码。

export const myPlugin: IPlugin = {
  name: 'stc:MyPlugin',
  lang: 'ts',
  setup(options) {
    console.log(options)
  },
  onTransform(def, action) {
    // 转换 definition
    const defContent: string = parserDefinition(
      def /* 这里的 def 是 Definition 对象 */
    )
    // 转换 action
    const actionContent: Map<string, string> = parserAction(
      action /* 这里的 action 是 Action 对象 */
    )
    // 返回转换后的内容
    return {
      definition: defContent,
      action: actionContent
    }
  },
  onEnd() {
    console.log('end')
  }
}

4.在 start 方法里,添加 plugins:

start({
  // ...其他配置
  plugins: [myPlugin]
})

About

🔧 Swagger 文档转换成代码的工具。 A tool for converting Swagger documents into code.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • TypeScript 100.0%