@frostup/swr-request-generator
v0.7.5
Published
A tool for generating TypeScript code and interface from swagger by using SWR and axios as client.
Downloads
3
Readme
SWR request generator
This tool can generate SWR request and related request params and response interface from swagger.
it will generate all GET request via SWR and others will be axios.
Dependencies
if you want to use this tool, your project should be:
- your back end API should use swagger and OpenAPI 3.0 standard
- your front end client should be axios
- use SWR as data fetching lib for your front end web app
How to use
install
npm install -D @openapi-integration/swr-request-generator
or
yarn add -D @openapi-integration/swr-request-generator
Configuration
script
add a npm script to your package.json file:
{
"scripts": {
"codegen": "ts-codegen"
}
}
config file
create a new json file named ts-codegen.config.json
in your project root directory like this
{
"output": "src/request",
"fileHeaders": [
"/* eslint-disable @typescript-eslint/explicit-module-boundary-types */",
"/* eslint-disable @typescript-eslint/no-explicit-any */",
"import { ISWRConfig, useRequest } from './useRequest';",
"import { IResponseError } from \"../../constants/error\";",
"import { client } from \"./client\";"
],
"clients": ["https://app.swaggerhub.com/apiproxy/registry/teobler/integration-example/1.0.0"],
"fileName": "api",
"data": ["./examples/openAPI.json"]
}
fields meaning:
- output(string): output file dir
- fileName(string): output filename
- fileHeaders(string[]): strings in this array will be placed in output file beginning
- clients(string[]): your swagger urls
- data(string[]): your local swagger json file dirs
Run it!
- 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
- Fill this address into the
clients array
of the above configuration file. If you have multiple addresses, fill in multiple string addresses. - 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.
- Run
npm run codegen
then you can find output file in your output dir
if your swagger url need basic auth, just run
npm run codegen -- -a "Basic #basicAuthHeader"
example
all the details can be found in example folder.
clone this repo
run npm i
to install all dependency
then run bin/ts-codegen.js
can generate api file in example/request/api.ts
how to use this file can be found in page.tsx