Skip to content

teobler/swr-request-generator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

b181a73 · Oct 24, 2022
Sep 19, 2019
Oct 24, 2022
Oct 24, 2022
Jun 14, 2019
Sep 19, 2019
Sep 19, 2019
Jun 14, 2019
Sep 30, 2021
Oct 24, 2022
Oct 24, 2022
Oct 24, 2022
Oct 24, 2022
Feb 3, 2021
Sep 22, 2020

Repository files navigation

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:

  1. your back end API should use swagger and OpenAPI 3.0 standard
  2. your front end client should be axios
  3. 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!

  1. 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
  2. Fill this address into the clients array of the above configuration file. If you have multiple addresses, fill in multiple string addresses.
  3. 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.
  4. 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

changelog

changelog