{"id":14986493,"url":"https://github.com/openapi-ui/openapi-ts-request","last_synced_at":"2026-02-02T08:13:52.466Z","repository":{"id":246737752,"uuid":"822349432","full_name":"openapi-ui/openapi-ts-request","owner":"openapi-ui","description":"Swagger2/OpenAPI3/Apifox to TS/JS, request client, mock service, enum, react-query, type field label, JSON Schemas;  根据 Swagger2/OpenAPI3/Apifox 生成 TypeScript/JavaScript, 客户端请求函数(支持任意客户端), 模拟请求响应服务, 枚举和枚举翻译, react-query, 类型的字段翻译, JSON Schemas定义, 欢迎提功能请求","archived":false,"fork":false,"pushed_at":"2025-05-14T02:18:38.000Z","size":1131,"stargazers_count":329,"open_issues_count":9,"forks_count":39,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-05-14T03:54:53.336Z","etag":null,"topics":["openapi-axios","openapi-fetch","openapi-taro","openapi-typescript-generator","openapi-typescript-request","openapi-uniapp"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/openapi-ts-request","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/openapi-ui.png","metadata":{"files":{"readme":"README-en_US.md","changelog":"CHANGELOG.md","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":"2024-07-01T01:23:04.000Z","updated_at":"2025-05-14T02:18:36.000Z","dependencies_parsed_at":"2024-07-23T18:05:49.823Z","dependency_job_id":"00abaebe-935f-48ea-8728-a8e1a9e7a666","html_url":"https://github.com/openapi-ui/openapi-ts-request","commit_stats":{"total_commits":65,"total_committers":4,"mean_commits":16.25,"dds":"0.46153846153846156","last_synced_commit":"6f124bb49253a8eec024fdce88b25eeedbb4630d"},"previous_names":["openapi-ui/openapi-ts-request"],"tags_count":50,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/openapi-ui%2Fopenapi-ts-request","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/openapi-ui%2Fopenapi-ts-request/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/openapi-ui%2Fopenapi-ts-request/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/openapi-ui%2Fopenapi-ts-request/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/openapi-ui","download_url":"https://codeload.github.com/openapi-ui/openapi-ts-request/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254337612,"owners_count":22054253,"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":["openapi-axios","openapi-fetch","openapi-taro","openapi-typescript-generator","openapi-typescript-request","openapi-uniapp"],"created_at":"2024-09-24T14:12:57.420Z","updated_at":"2026-01-17T02:56:02.578Z","avatar_url":"https://github.com/openapi-ui.png","language":"TypeScript","readme":"## Introduce\n\n[![GitHub Repo stars](https://img.shields.io/github/stars/openapi-ui/openapi-ts-request?style=social)](https://github.com/openapi-ui/openapi-ts-request) [![npm (scoped)](https://img.shields.io/npm/v/openapi-ts-request)](https://www.npmjs.com/package/openapi-ts-request) ![GitHub tag](https://img.shields.io/github/v/tag/openapi-ui/openapi-ts-request?include_prereleases)\n\nEnglish | \u003ca href=\"https://github.com/openapi-ui/openapi-ts-request/blob/master/README.md\"\u003e简体中文\u003c/a\u003e\n\nbased on [Swagger2/OpenAPI3/Apifox](https://swagger.io/blog/news/whats-new-in-openapi-3-0/) specification Generate\n\n- TypeScript/JavaScript\n- request client(support any client)\n- request mock service\n- enum and enum translation\n- react-query/vue-query\n- type field label\n- JSON Schemas\n- Apifox Config\n\ndocs：[use docs](https://github.com/openapi-ui/openapi-ts-request/issues/100)\n\n## Features\n\n- support Swagger2.0/OpenAPI/Apifox 3.0,3.1 specification\n- generate TypeScript/JavaScript, reuquest client(support any client), request mock service, enum and enum translation, react-query/vue-query, type field label, JSON Schemas\n- support work with npx, CLI, Nodejs\n- support custom request function, Fetch、Axios、[UniApp-request](https://github.com/openapi-ui/openapi-ts-request/issues/46)、Taro-Request、Node.js、XHR client available\n- support filter generate result by tags\n- support JSON/YAML specification\n- support translate chinese tag name to english tag name\n- support direct configuration of `apifox` `token` and `projectId` direct generation\n\n## Usage\n\n```bash\n# npm\nnpm i openapi-ts-request --save-dev\n\n# pnpm\npnpm i openapi-ts-request -D\n```\n\n### CosmiConfig\n\ncreate `openapi-ts-request.config.ts` file in the project root directory\n\n\u003e the config file also supports **_.openapi-ts-request.ts_**, **_openapi-ts-request.config.cjs_** format, reference [cosmiconfig](https://github.com/cosmiconfig/cosmiconfig?tab=readme-ov-file#cosmiconfig)\n\n```ts\nimport { defineConfig } from 'openapi-ts-request';\n\nexport default defineConfig({\n  schemaPath: 'http://petstore.swagger.io/v2/swagger.json',\n  serversPath: './src/apis',\n});\n```\n\nsupport passing in array config for generate\n\n```ts\nimport { defineConfig } from 'openapi-ts-request';\n\nexport default defineConfig([\n  {\n    schemaPath: 'http://app.swagger.io/v2/swagger.json',\n    serversPath: './src/apis/app',\n  },\n  {\n    schemaPath: 'http://auth.swagger.io/v2/swagger.json',\n    serversPath: './src/apis/auth',\n  },\n]);\n```\n\nadd the command in `script` of `package.json`: `\"openapi\": \"openapi-ts\",`\n\nrun:\n\n```bash\nnpm run openapi\n```\n\nrun:\n\n```bash\nsrc/apis/index.ts #interface entry file\nsrc/apis/types.ts #type definition file\nsrc/apis/app #app interface\n```\n\n```typescript\n// src/apis/pet.ts\n/* eslint-disable */\n// @ts-ignore\nimport request from 'axios';\n\nimport * as API from './types';\n\n/** Update an existing pet PUT /pet */\nexport async function updatePet({\n  body,\n  options,\n}: {\n  body: API.Pet;\n  options?: { [key: string]: unknown };\n}) {\n  return request\u003cunknown\u003e(`/pet`, {\n    method: 'PUT',\n    headers: {\n      'Content-Type': 'application/json',\n    },\n    data: body,\n    ...(options || {}),\n  });\n}\n\n// ... more interfaces\n```\n\n### JS\n\ncreate a new `openapi-ts-request.config.js` file in any directory `xxx/xxx`\n\n```ts\nconst { generateService } = require('openapi-ts-request');\n\ngenerateService({\n  schemaPath: 'http://petstore.swagger.io/v2/swagger.json',\n  serversPath: './apis',\n});\n```\n\nadd the command in `script` of `package.json`: `\"openapi\": \"node xxx/xxx/openapi-ts-request.config.js\"`\n\nrun:\n\n```bash\nnpm run openapi\n```\n\n### TS\n\ncreate a new `openapi-ts-request.config.ts` file in any directory `xxx/xxx`\n\n```ts\nconst { generateService } = require('openapi-ts-request');\n\ngenerateService({\n  schemaPath: 'http://petstore.swagger.io/v2/swagger.json',\n  serversPath: './apis',\n});\n```\n\nadd the command in `script` of `package.json`: `\"openapi\": \"ts-node xxx/xxx/openapi-ts-request.config.ts\",`\n\nrun:\n\n```bash\nnpm run openapi\n```\n\n### NPX\n\n```bash\n# npm\nnpx --package=openapi-ts-request -- openapi -i ./openapi.json -o ./apis\nnpx --package=openapi-ts-request -- openapi -i https://petstore3.swagger.io/api/v3/openapi.json -o ./apis\n\n# pnpm\npnpm --package=openapi-ts-request@latest dlx openapi -i ./openapi.json -o ./apis\npnpm --package=openapi-ts-request@latest dlx openapi -i https://petstore3.swagger.io/api/v3/openapi.json -o ./apis\n```\n\n### CLI\n\n```\nnpm i openapi-ts-request -g\n```\n\n```\n$ openapi --help\n\n  Usage: openapi [options]\n\n  Options:\n    -V, --version                       output the version number\n    -i, --input \u003cstring\u003e                OpenAPI specification, can be a path, url\n    -o, --output \u003cstring\u003e               output directory\n    -cfn, --configFileName \u003cstring\u003e     config file name\n    -cfp, --configFilePath \u003cstring\u003e     config file path\n    --requestLibPath \u003cstring\u003e           custom request lib path, for example: \"@/request\", \"node-fetch\" (default: \"axios\")\n    --isSplitTypesByModule \u003cboolean\u003e      split types by module, generates {module}.type.ts, common.type.ts, enum.ts, types.ts (default: false)\n    -f, --full \u003cboolean\u003e                full replacement (default: true)\n    --enableLogging \u003cboolean\u003e           open the log (default: false)\n    --priorityRule \u003cstring\u003e             priority rule, include/exclude/both (default: \"include\")\n    --filterCaseInsensitive \u003cboolean\u003e   whether to perform a case-insensitive match with includeTags, includePaths, excludeTags, excludePaths filters (default: false)\n    --includeTags \u003c(string|RegExp)[]\u003e   generate code from include tags\n    --includePaths \u003c(string|RegExp)[]\u003e  generate code from include paths\n    --excludeTags \u003c(string|RegExp)[]\u003e   generate code from exclude tags\n    --excludePaths \u003c(string|RegExp)[]\u003e  generate code from exclude paths\n    --requestOptionsType \u003cstring\u003e       custom request method options parameter type (default: \"{ [key: string]: unknown }\")\n    --requestImportStatement \u003cstring\u003e   custom request import statement, for example: \"const request = require('@/request')\"\n    --apiPrefix \u003cstring\u003e                custom the prefix of the api path, for example: \"api\"(variable), \"'api'\"(string)\n    --isGenReactQuery \u003cboolean\u003e         generate react-query (default: false)\n    --reactQueryMode \u003cstring\u003e           react-query mode, react/vue (default: \"react\")\n    --isGenJavaScript \u003cboolean\u003e         generate JavaScript (default: false)\n    --isDisplayTypeLabel \u003cboolean\u003e      generate label matching type field (default: false)\n    --isGenJsonSchemas \u003cboolean\u003e        generate JSON Schemas (default: false)\n    --mockFolder \u003cstring\u003e               mock file path, for example: './mocks'\n    --authorization \u003cstring\u003e            docs authorization\n    --nullable \u003cboolean\u003e                null instead of optional (default: false)\n    --isTranslateToEnglishTag \u003cboolean\u003e translate chinese tag name to english tag name (default: false)\n    --isOnlyGenTypeScriptType \u003cboolean\u003e only generate typescript type (default: false)\n    --isCamelCase \u003cboolean\u003e             camelCase naming of controller files and request client (default: true)\n    --isSupportParseEnumDesc \u003cboolean\u003e  parse enum description to generate enum label (default: false)\n    --supportParseEnumDescByReg \u003cstring\u003e custom regex for parsing enum description\n    -h, --help                          display help for command\n```\n\nrun:\n\n```bash\nopenapi -i ./spec.json -o ./apis\n```\n\n## Parameter\n\n| props | required | type | default | remark |\n| --- | --- | --- | --- | --- |\n| schemaPath | no\\* | string | - | Swagger2/OpenAPI3 URL (mutually exclusive with apifoxConfig, one of them is required) |\n| apifoxConfig | no\\* | [Apifox Config](#Apifox-Config) | - | apifox configs (mutually exclusive with schemaPath, one of them is required) |\n| serversPath | no | string | './src/apis' | the folder path for the run results |\n| requestLibPath | no | string | 'axios' | custom request lib path, for example: '@/request', 'node-fetch' |\n| isSplitTypesByModule | no | boolean | false | split types by module, generates {module}.type.ts (module types), common.type.ts (common types), enum.ts (enum types), types.ts (unified export) |\n| full | no | boolean | true | full replacement |\n| describe | no | string | - | description information, which will be used when using cli interactive operation mode |\n| enableLogging | no | boolean | false | open the log |\n| priorityRule | no | string | 'include' | priority rule, include/exclude/both |\n| filterCaseInsensitive | no | boolean | false | whether to perform a case-insensitive match with includeTags, includePaths, excludeTags, excludePaths filters |\n| includeTags | no | (string\\|RegExp)[] | - | generate code from include tags, priorityRule=include required |\n| includePaths | no | (string\\|RegExp)[] | - | generate code from include paths |\n| excludeTags | no | (string\\|RegExp)[] | - | generate code from exclude tags |\n| excludePaths | no | (string\\|RegExp)[] | - | generate code from exclude paths |\n| requestOptionsType | no | string | '{ [key: string]: unknown }' | custom request method options parameter type |\n| requestImportStatement | no | string | - | custom request import statement, for example: \"const request = require('@/request')\" |\n| apiPrefix | no | string | - | custom the prefix of the api path, for example: 'api'(variable), \"'api'\"(string) |\n| isGenReactQuery | no | boolean | false | generate react-query |\n| reactQueryMode | no | string | 'react' | react-query mode, react/vue |\n| isGenJavaScript | no | boolean | false | generate JavaScript |\n| isDisplayTypeLabel | no | boolean | false | generate label matching type field |\n| isGenJsonSchemas | no | boolean | false | generate JSON Schemas |\n| mockFolder | no | string | - | mock file path, for example: './mocks' |\n| authorization | no | string | - | docs authorization |\n| nullable | no | boolean | false | null instead of optional |\n| isTranslateToEnglishTag | no | boolean | false | translate chinese tag name to english tag name |\n| isOnlyGenTypeScriptType | no | boolean | false | only generate typescript type |\n| isCamelCase | no | boolean | true | camelCase naming of controller files and request client |\n| isSupportParseEnumDesc | no | boolean | false | parse enum description to generate enum label, format example: `UserRole:User(Normal User)=0,Agent(Agent)=1,Admin(Administrator)=2` |\n| supportParseEnumDescByReg | no | string \\| RegExp | - | custom regex for parsing enum descriptions. If set, replaces default parseDescriptionEnum method. Example: `/([^\\s=\u003c\u003e/\u0026;]+(?:\\s+[^\\s=\u003c\u003e/\u0026;]+)*)\\s*=\\s*(\\d+)/g` matches \"Normal = 0\" or \"SampleMaker = 1\" |\n| binaryMediaTypes | no | string[] | - | custom binary media types list, default includes: ['application/octet-stream', 'application/pdf', 'image/*', 'video/*', 'audio/*'] |\n| hook | no | [Custom Hook](#Custom-Hook) | - | custom hook |\n\n## Custom Hook\n\n| props | type | remark |\n| --- | --- | --- |\n| afterOpenApiDataInited | (openAPIData: OpenAPIObject) =\u003e OpenAPIObject | custom OpenAPI data |\n| customFunctionName | (data: APIDataType) =\u003e string | custom request client function name |\n| customTypeName | (data: APIDataType) =\u003e string | custom type name |\n| customClassName | (tagName: string) =\u003e string | custom tag name |\n| customType | ({\u003cbr\u003eschemaObject: SchemaObject \\| ReferenceObject,\u003cbr\u003enamespace: string,\u003cbr\u003eoriginGetType:(schemaObject: SchemaObject \\| ReferenceObject, namespace: string, schemas?: ComponentsObject['schemas']) =\u003e string,\u003cbr\u003eschemas?: ComponentsObject['schemas'],\u003cbr\u003e}) =\u003e string | custom type \u003cbr\u003e _returning a non-string will use the default method to get the type_ |\n| customFileNames | (\u003cbr\u003eoperationObject: OperationObject,\u003cbr\u003eapiPath: string,\u003cbr\u003eapiMethod: string,\u003cbr\u003e) =\u003e string[] | custom generate request client controller file name, can return multiple: generate multiple files. \u003cbr\u003e _if the return value is empty, the default getFileNames is used_ |\n| customTemplates | {\u003cbr\u003e[TypescriptFileType.serviceController]?: \u003cT, U\u003e(item: T, context: U) =\u003e string;\u003cbr\u003e} | custom template, details see source code |\n| customRenderTemplateData | {\u003cbr\u003e[TypescriptFileType]?: (list: any[], context: {fileName: string, params: Record\u003cstring, unknown\u003e}) =\u003e any[]\u003cbr\u003e} | custom list parameter processing during file generation, provides fine-grained control for different file types |\n\n## Apifox-Config\n\n| attribute | type | description | required |\n| --- | --- | --- | --- |\n| projectId | string | project id | true |\n| apifoxToken | string | [get](https://docs.apifox.com/doc-5723694) | true |\n| local | string | language(default:zh-CN) | false |\n| apifoxVersion | string | default: 2024-03-28, [current apifox version](https://api.apifox.com/v1/versions) | false |\n| selectedTags | \\* or string[] | default: \\* | false |\n| excludedByTags | string[] | default: [] | false |\n| oasVersion | string | specify the version of the OpenAPI specification used for export, can have values such as \"2.0\", \"3.0\" or \"3.1\" | '3.0' |\n| exportFormat | string | specify the format of the exported OpenAPI file, can have values such as 'JSON' or 'YAML' | 'JSON' |\n| includeApifoxExtensionProperties | boolean | specify whether to include the OpenAPI specification extension fields `x-apifox` | false |\n| addFoldersToTags | boolean | specify whether to include the directory name of the interface in the tag field | false |\n| branchId | number | branch id | false |\n| moduleId | number | module id | false |\n\n## JSON Schemas\n\n- default generate JSON Schemas based on [components.schemas](https://spec.openapis.org/oas/latest.html#components-object), JSON Schemas corresponding to [paths](https://spec.openapis.org/oas/latest.html#paths-object) currently need to be parsed by yourself\n- provide a schema parsing function to fill the references of `$ref` and `$allOf` into `current schema`\n\n```ts\nexport declare function patchSchema\u003cT extends object\u003e(\n  schema: ISchemaObject,\n  schemas: ComponentsObject['schemas']\n): T;\n```\n\n## Mock\n\ncurrently using [mockjs](http://mockjs.com) to generate mock data, the mocks file startup needs to rely on [@umijs/server](https://umijs.org/docs/guides/mock), we will look for other solutions later to achieve a better mock experience\n\n## Adapt to uniapp\n\nit is recommended to use a custom request function to adapt to uniapp. you can also use the `@uni-helper/axios-adapter` adapter. for details, see [【use docs 2.2】](https://github.com/openapi-ui/openapi-ts-request/issues/100)\n\n## Notes on Upgrading Older Versions\n\n- Current naming convention changes\n- Incremental changes have been made to the current version and will not affect previous versions.\n- The `openapi-ts` command can be deprecated to use `openapi`\n\n### The configuration follows the naming convention of the old version as follows\n\n```typescript\nimport type { APIDataType } from 'openapi-ts-request/dist/generator/type';\nimport {\n  genDefaultFunctionName,\n  resolveFunctionName,\n  stripDot,\n} from 'openapi-ts-request/dist/generator/util';\n\nexport default {\n  hook: {\n    customFunctionName(data: APIDataType, prefix: string) {\n      if (data.operationId)\n        return resolveFunctionName(stripDot(data.operationId), data.method);\n      return data.method + genDefaultFunctionName(data.path, prefix);\n    },\n  },\n};\n```\n\n## Contribute\n\n### Development Environment\n\n- node 18+\n- pnpm 9+\n\n### Submit Pull Request\n\n1. learn [Pull Request](\"https://help.github.com/articles/using-pull-requests\") specification\n2. fork this repository\n3. create a new branch to modify the code：`git checkout -b my-branch main`\n4. make sure your code passes all test cases (new functional test cases need to be added for new features)：`pnpm test:unit`\n5. create a changeset file using the command：`pnpm changeset`\n6. submit your changes using commit (must follow commitlint specification)\n7. If documents are involved, please update README.md, READMD-en_US.md, and agents.md simultaneousl\n8. submit Pull Request\n\n## Thanks\n\n- [openapi2typescript](https://github.com/chenshuai2144/openapi2typescript)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fopenapi-ui%2Fopenapi-ts-request","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fopenapi-ui%2Fopenapi-ts-request","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fopenapi-ui%2Fopenapi-ts-request/lists"}