{"id":19604609,"url":"https://github.com/long-woo/stc","last_synced_at":"2025-04-27T19:32:38.938Z","repository":{"id":176517959,"uuid":"402430315","full_name":"long-woo/stc","owner":"long-woo","description":"🔧 A tool for converting OpenApi/Swagger/Apifox into code.","archived":false,"fork":false,"pushed_at":"2025-04-23T15:27:40.000Z","size":32868,"stargazers_count":36,"open_issues_count":1,"forks_count":5,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-23T16:45:12.978Z","etag":null,"topics":["aoc-2024-in-deno","apifox","code-generator","deno","openapi","swagger"],"latest_commit_sha":null,"homepage":"https://stc.deno.dev","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/long-woo.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2021-09-02T13:26:51.000Z","updated_at":"2025-04-20T19:07:08.000Z","dependencies_parsed_at":"2024-03-02T15:24:21.765Z","dependency_job_id":"c316ea16-568c-4657-a4ff-9f240f6e8015","html_url":"https://github.com/long-woo/stc","commit_stats":null,"previous_names":["long-woo/stc"],"tags_count":42,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/long-woo%2Fstc","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/long-woo%2Fstc/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/long-woo%2Fstc/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/long-woo%2Fstc/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/long-woo","download_url":"https://codeload.github.com/long-woo/stc/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251196051,"owners_count":21550893,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["aoc-2024-in-deno","apifox","code-generator","deno","openapi","swagger"],"created_at":"2024-11-11T09:37:29.564Z","updated_at":"2025-04-27T19:32:38.923Z","avatar_url":"https://github.com/long-woo.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# \u003cp align=\"center\"\u003eSTC\u003c/p\u003e\n\n\u003cp  align=\"center\"\u003e\u003cimg src=\"resources/stc.svg\" alt=\"logo\" /\u003e\u003c/p\u003e\n\nSTC (Swagger Transform Code) is a tool for converting OpenApi/Swagger/Apifox into code.\n\n![Publish to release](https://github.com/long-woo/stc/actions/workflows/deno-build.yml/badge.svg)\n[![Publish Package to npmjs](https://github.com/long-woo/stc/actions/workflows/npm.yml/badge.svg)](https://github.com/long-woo/stc/actions/workflows/npm.yml)\n\n[![JSR](https://jsr.io/badges/@lonu/stc)](https://jsr.io/@lonu/stc)\n![NPM Version](https://img.shields.io/npm/v/%40lonu%2Fstc)\n\n![stc](resources/20240422-151653.gif)\n\n## Features\n\n\u003e 🚧 Encapsulate the \"shared\" directory.\n\n- 🐹 Support for **Swagger 2/3(OpenApi)** and **Apifox**.\n- 💡 Support plug-in development.\n- 🐣 Built-in transformation languages:\n  - **TypeScript**, almost equivalent to handwriting. Depends on **`axios`, `wx.request`, `fetch`**.\n   \u003e `xhr/ajax、ofetch` planned\n  - **JavaScript**, from TypeScript to it.\n  - **Dart**, dependency on **`dio`**.\n  - 🚧 **Swift** ...\n\n## Quick start\n\n### Download executable files\n\n[download](https://github.com/long-woo/stc/releases) by system：\n\n- stc: Intel-based Mac\n- stc-m: M-series Mac\n- stc-linux：Linux\n- stc-win.exe: Windows\n\n### NPM\n\n1.Install the `@lonu/stc` npm package.\n\n```sh\npnpm add @lonu/stc -D\n```\n\n2.Open the project's `package.json` file and add the following command to `scripts`:\n\n```json\n{\n  \"scripts\": {\n    \"api\": \"stc --url=http://127.0.0.1:4523/export/openapi/2?version=3.1\"\n  }\n}\n```\n\n### Use\n\n⚠️ Note: deno will not parse the `~` character as the user's home directory.\n\n```sh\nstc --url=https://petstore3.swagger.io/api/v3/openapi.json --outDir=out\n```\n\n![终端输出信息](resources/output.png)\n\n![输出文件](resources/file.png)\n\n### Existing Project\n\nAssume a project directory is:\n\n```\n.\n├── src\n│   └── apis # Copy the shared directory here.\n│       └── shared\n│       └── xxx.ts # Other files.\n\n```\n\n#### Axios\n\n1. Find the directory of `outDir`, copy the entire `shared` directory to the directory of the `axios` module you encapsulated.\n\n2. Open the `shared \u003e axios \u003e index.ts` file, copy the `request` method, and add it to the `axios` module you encapsulated. If it is not encapsulated, copy the `index.ts` file as a new file to avoid the problem of modification being overwritten.\n\n3. Taking `Vue` as an example, add the following code to the `main.ts` file:\n\n```ts\nimport { createApiClient } from './apis/shared/fetchRuntime';\n\ncreateApiClient({\n  baseURL: 'https://api.xxx.com'\n  // onError(msg) {\n  //   // 处理错误信息\n  // }\n})\n```\n\n#### Wechat\n\n1. Find the directory of `outDir`, copy the entire directory of `shared` to the directory of the `wechat` module you encapsulated.\n\n2. Open the `shared \u003e wechat \u003e ​​index.ts` file, copy the `request` method, and add it to the `wx.request` code file you encapsulated. If it is not encapsulated, copy the `index.ts` file as a new file to avoid the problem of modification being overwritten.\n\n3. Add the following code to the `app.ts` file:\n\n```ts\nimport { createApiClient } from './apis/shared/fetchRuntime';\n// import Notify from './miniprogram_npm/@vant/weapp/notify/notify';\n\nApp\u003cIAppOption\u003e({\n  onLaunch() {\n    createApiClient({\n      baseURL: 'https://api.xxx.com,\n      onError(msg) {\n        // Notify({ type: 'danger', message: msg, selector: '#v-notify'})\n      }\n    })\n  }\n});\n```\n\n### Options\n\n| Option      | Alias | Type     | Default   | Description                                                                                                  |\n| ----------- | ----- | -------- | --------- | ------------------------------------------------------------------------------------------------------------ |\n| url         |       | string   |           | Swagger/OpenApi/Apifox document address, or local path.                                                      |\n| outDir      | o     | string   | ./stc_out | Output Directory.                                                                                            |\n| client      |       | string   | axios     | http request client. When `lang` is `ts/js`, the possible values ​​are: `axios`, `wechat`, `fetch`.            |\n| lang        | l     | string   | ts        | Language, used for output file suffix.                                                                       |\n| tag         |       | number   |           | Specify the tag from the interface url. By default, the first tag is read for the file name.                 |\n| 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](https://github.com/micromatch/micromatch) |\n| conjunction | c     | string   | By        | The method's connector, the default value is `By`.                                                           |\n| actionIndex |      | number   | -1        | The method name index, the default value is `-1`.                                                            |\n| shared      |      | boolean  | true      | Whether to generate the shared directory. [default: true].                                                   |\n| version     | v     | boolean  |           | Output version information.                                                                                  |\n| help        | h     | boolean  |           | Output help information.                                                                                     |\n\n## Plug-in development\n\nFor convenience, STC can not only develop plugins in Deno, but also provides `@lonu/stc` npm library, which can develop plugins in Node environment.\n\n[examples](https://github.com/long-woo/stc/tree/master/examples)\n\n### Deno\n\n⚠️ Prepare the [Deno](https://github.com/denoland/deno#install) environment.\n\nCreate a `myPlugin.ts` file:\n\n```ts\n// 引用模块\n// import { start } from 'https://deno.land/x/stc@2.11.0/mod.ts'\nimport { start } from 'jsr:@lonu/stc@^2.11.0'\n\n// Defining plugins\nconst myPlugin: IPlugin = {\n  name: 'stc:MyPlugin',\n  lang: 'ts',\n  setup(options) {\n    console.log(options)\n  },\n  onTransform(def, action) {\n    // definition\n    const defContent: string = parserDefinition(\n      def\n    )\n    // action\n    const actionContent: Map\u003cstring, string\u003e = parserAction(\n      action\n    )\n\n    return {\n      definition: {\n        filename: '_types.ts',\n        content: defContent,\n      },\n      action: actionContent // Here actionContent is of type Map\u003cstring, string\u003e, key is the file name, value is the converted code.\n    }\n  },\n  onEnd() {\n    console.log('end')\n  }\n}\n\n// 使用插件\nstart({\n  // ...其他配置\n  plugins: [myPlugin]\n})\n```\n\n### Node\n\n1. Create a `myPlugin.ts` file.\n\n2. Add the `@lonu/stc` reference and use the `start` method:\n\n```ts\nimport { start } from '@lonu/stc'\n```\n\n3. Implement the code that converts `definition` and `action` into the target language in the plugin's `onTransform` hook function.\n\n```ts\nexport const myPlugin: IPlugin = {\n  name: 'stc:MyPlugin',\n  lang: 'ts',\n  setup(options) {\n    console.log(options)\n  },\n  onTransform(def, action) {\n    // definition\n    const defContent: string = parserDefinition(\n      def\n    )\n    // action\n    const actionContent: Map\u003cstring, string\u003e = parserAction(\n      action\n    )\n\n    return {\n      definition: defContent,\n      action: actionContent\n    }\n  },\n  onEnd() {\n    console.log('end')\n  }\n}\n```\n\n4.In the `start` method, add `plugins`:\n\n```ts\nstart({\n  // ...other options\n  plugins: [myPlugin]\n})\n```\n\n## Who's Using This?\n\n\u003cdiv align=\"center\"\u003e\n  \u003cimg src=\"./resources/company/lyh.png\" style=\"height: 50px;\"  /\u003e\n  \u003cbr/\u003e\n  \u003cimg src=\"./resources/company/gfk.png\" style=\"height: 50px;\" /\u003e\n\u003c/div\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flong-woo%2Fstc","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flong-woo%2Fstc","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flong-woo%2Fstc/lists"}