{"id":18311896,"url":"https://github.com/teobler/swr-request-generator","last_synced_at":"2025-10-24T01:39:54.725Z","repository":{"id":40247090,"uuid":"340594693","full_name":"teobler/swr-request-generator","owner":"teobler","description":"A tool for generating TypeScript code and interface from swagger by using SWR and axios as client.","archived":false,"fork":false,"pushed_at":"2024-04-18T13:40:19.000Z","size":748,"stargazers_count":48,"open_issues_count":1,"forks_count":12,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-18T14:09:19.473Z","etag":null,"topics":["api-integration","code-generator","swagger-api","swagger-codegen","swr","typescript-generator"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/@openapi-integration/swr-request-generator","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/teobler.png","metadata":{"files":{"readme":"README.md","changelog":"changelog.md","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-02-20T07:46:58.000Z","updated_at":"2025-01-26T00:45:46.000Z","dependencies_parsed_at":"2022-09-03T22:51:24.862Z","dependency_job_id":null,"html_url":"https://github.com/teobler/swr-request-generator","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/teobler%2Fswr-request-generator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/teobler%2Fswr-request-generator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/teobler%2Fswr-request-generator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/teobler%2Fswr-request-generator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/teobler","download_url":"https://codeload.github.com/teobler/swr-request-generator/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247383952,"owners_count":20930382,"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":["api-integration","code-generator","swagger-api","swagger-codegen","swr","typescript-generator"],"created_at":"2024-11-05T16:21:10.261Z","updated_at":"2025-10-24T01:39:49.676Z","avatar_url":"https://github.com/teobler.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cdiv align=\"center\"\u003e\n\n\u003ch1 align=\"center\"\u003eSWR Request Generator\u003c/h1\u003e\n\n\u003ca href=\"https://github.com/teobler/swr-request-generator/actions\"\u003e![build](https://img.shields.io/github/actions/workflow/status/teobler/swr-request-generator/build.yaml)\u003c/a\u003e\n\u003ca href=\"https://github.com/teobler/swr-request-generator/actions\"\u003e![cov](https://teobler.github.io/swr-request-generator/badges/coverage.svg)\u003c/a\u003e\n\u003ca href=\"https://github.com/teobler/swr-request-generator/blob/main/LICENSE\"\u003e![license](https://img.shields.io/github/license/teobler/swr-request-generator)\u003c/a\u003e\n\n\u003c/div\u003e\n\nThis tool can generate [SWR](https://swr.vercel.app/) request and related request params and response interface from swagger.\n\nBecause SWR will support mutation request like POST/UPDATE... since [version 2.x](https://github.com/vercel/swr/discussions/1919).\n\nThen SWR request generator will generate these requests through SWR as well since version 1.0.\n\nYou can get more info about the change between 0.x and 1.0 via [changelog](changelog.md).\n\n## ❗ Dependencies\n\nif you want to use this tool, your project should be:\n\n1. your back end API should use swagger and OpenAPI 3.0 standard\n2. your front end client should be [axios](https://github.com/axios/axios)\n3. use SWR as data fetching lib for your front end web app\n\n## ❓ You Are Using SWR Already, Why Still With Axios?\n\nPersonally I think axios is a good request client library which can provide better development experience.\n\nJust like you can easily add your own interceptor, etc.\n\nAnd SWR mutation request need a `fetcher` to be request client, I think axios could be a good choice.\n\nBut on the other side, axios do increase the size of the packaged code, this could be a cons for using axios.\n\nIf many requirements in the future for a `fetch` version, maybe I can extend it with any client.\n\nAnd if anyone want to make it happen, it should be super cool 🥳\n\n## 🧰 How to Use\n### 🛠 Install\n\n```bash\npnpm install -D @openapi-integration/swr-request-generator\n```\n\nor\n\n```bash\nyarn add -D @openapi-integration/swr-request-generator\n```\n\nor\n\n```bash\nnpm install -D @openapi-integration/swr-request-generator\n```\n\n### 🔧 Configuration\n#### ☄️ Script\n\nadd a npm script to your package.json file:\n\n```json\n{\n  \"scripts\": {\n    \"codegen\": \"ts-codegen\"\n  }\n}\n```\n\n#### 📁 Config File\n\ncreate a new json file named `ts-codegen.config.json` in your project root directory like this\n\n```json\n{\n  \"output\": \"src/request\",\n  \"fileHeaders\": [\n    \"/* eslint-disable @typescript-eslint/explicit-module-boundary-types */\",\n    \"/* eslint-disable @typescript-eslint/no-explicit-any */\",\n    \"import { SWRConfig, useGetRequest } from \\\"./useGetRequest\\\"\",\n    \"import { ResponseError } from \\\"../types\\\"\",\n    \"import { AxiosRequestConfig, AxiosResponse } from \\\"axios\\\"\",\n    \"import { SWRMutationConfig, useMutationRequest } from \\\"src/request/useMutationRequest\\\";\"\n  ],\n  \"clients\": [\"https://app.swaggerhub.com/apiproxy/registry/teobler/integration-example/1.0.0\"],\n  \"fileName\": \"api\",\n  \"data\": [\"./examples/openAPI.json\"],\n  \"needRequestHook\": true,\n  \"needClient\": true\n}\n```\n\nfields meaning:\n - output(string): output file dir\n - fileName(string): output filename\n - fileHeaders(string[]): strings in this array will be placed in output file beginning\n - clients(string[]): your swagger urls\n - data(string[]): your local swagger json/yaml file path\n - needRequestHook(boolean): if need to generate default request hook(useGetRequest and useMutationRequest)\n - needClient(boolean): if need to generate default request axios client\n\n#### 🤩 Run It!\n\n1. Find an address where you can get your back-end API swagger json file (either online or local), it should be an url can get swagger json response\n2. Fill this address into the `clients array` of the above configuration file. If you have multiple addresses, fill in multiple string addresses.\n3. If you can only download the swagger json file, that's fine, just put the json file into your project and fill in your file path in the data field of the config file.\n4. Run `npm run codegen` then you can find output file in your output dir\n\n\u003e if your swagger url need basic auth, just run `npm run codegen -- -a \"Basic #basicAuthHeader\"`\n\n### 👀 Example\n\nall the details can be found in example folder.\n\nclone this repo.\n\n```bash\ncd example \u0026\u0026 pnpm install\n```\n\nafter installing all dependencies you can run\n\n```bash\npnpm run codegen\n```\n\nthen swr request generator will generate api file in `src/request/api.ts`.\n\nand it's source is `swagger/opanAPI.json`, you can find more info in `ts-codegen.config.json` file.\n\nhow to use this file can be found in `src/APP.tsx`.\n\n### 💁‍ Tip\nWrapper functions in example folder like `useGetRequest` and `useMutationRequest` just one of the implementation.\n\nYou can use your own wrapper functions and error types as well, just ensure your function name and error type are aligned with demo, since generated function will use these names.\n\n## 💻 Local Development\n\nClone this repo, run `pnpm install` to install all dependencies.\n\nCopy this config file to your root dir:\n```json\n{\n  \"output\": \"example/src/request\",\n  \"fileHeaders\": [\n    \"/* eslint-disable @typescript-eslint/explicit-module-boundary-types */\",\n    \"/* eslint-disable @typescript-eslint/no-explicit-any */\",\n    \"import { SWRConfig, useGetRequest } from \\\"./useGetRequest\\\"\",\n    \"import { ResponseError } from \\\"../types\\\"\",\n    \"import { AxiosRequestConfig, AxiosResponse } from \\\"axios\\\"\",\n    \"import { SWRMutationConfig, useMutationRequest } from \\\"src/request/useMutationRequest\\\";\"\n  ],\n  \"clients\": [],\n  \"data\": [\"./example/swagger/openAPI.yaml\"],\n  \"fileName\": \"api\",\n  \"needRequestHook\": true,\n  \"needClient\": true\n}\n```\n\nThen run `pnpm run start` to generate request file to example folder, modify code you want and see the results.\n\nAnd there are some test cases in `__tests__` folder, you can run `pnpm run test` to see the results and help you modify the code.\n\n## 🗂 Changelog\n[changelog](changelog.md)\n\n## 🤗 Appreciation\n\nI would be very grateful if you could give this project a star!\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fteobler%2Fswr-request-generator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fteobler%2Fswr-request-generator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fteobler%2Fswr-request-generator/lists"}