npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@emperorschildren/swr-request-generator-win

v4.20.69

Published

A tool for generating TypeScript code and interface from swagger by using SWR and axios as client. Forked from https://github.com/teobler/swr-request-generator

Downloads

109

Readme

build cov license

This package is a fork from the original SWR Request Generator that adds windows support to config path resolving.


This tool can generate SWR request and related request params and response interface from swagger.

Because SWR will support mutation request like POST/UPDATE... since version 2.x.

Then SWR request generator will generate these requests through SWR as well since version 1.0.

You can get more info about the change between 0.x and 1.0 via changelog.

❗ 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

❓ You Are Using SWR Already, Why Still With Axios?

Personally I think axios is a good request client library which can provide better development experience.

Just like you can easily add your own interceptor, etc.

And SWR mutation request need a fetcher to be request client, I think axios could be a good choice.

But on the other side, axios do increase the size of the packaged code, this could be a cons for using axios.

If many requirements in the future for a fetch version, maybe I can extend it with any client.

And if anyone want to make it happen, it should be super cool 🥳

🧰 How to Use

🛠 Install

pnpm install -D @emperorschildren/swr-request-generator-win

or

yarn add -D @emperorschildren/swr-request-generator-win

or

npm install -D @emperorschildren/swr-request-generator-win

🔧 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 { SWRConfig, useGetRequest } from \"./useGetRequest\"",
    "import { ResponseError } from \"../types\"",
    "import { AxiosRequestConfig, AxiosResponse } from \"axios\"",
    "import { SWRMutationConfig, useMutationRequest } from \"src/request/useMutationRequest\";"
  ],
  "clients": ["https://app.swaggerhub.com/apiproxy/registry/teobler/integration-example/1.0.0"],
  "fileName": "api",
  "data": ["./examples/openAPI.json"],
  "needRequestHook": true,
  "needClient": true
}

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/yaml file path
  • needRequestHook(boolean): if need to generate default request hook(useGetRequest and useMutationRequest)
  • needClient(boolean): if need to generate default request axios client

🤩 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" if you're running on windows, just run npm run codegen -- -w

👀 Example

all the details can be found in example folder.

clone this repo.

cd example && pnpm install

after installing all dependencies you can run

pnpm run codegen

then swr request generator will generate api file in src/request/api.ts.

and it's source is swagger/opanAPI.json, you can find more info in ts-codegen.config.json file.

how to use this file can be found in src/APP.tsx.

💁‍ Tip

Wrapper functions in example folder like useGetRequest and useMutationRequest just one of the implementation.

You 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.

💻 Local Development

Clone this repo, run pnpm install to install all dependencies.

Copy this config file to your root dir:

{
  "output": "example/src/request",
  "fileHeaders": [
    "/* eslint-disable @typescript-eslint/explicit-module-boundary-types */",
    "/* eslint-disable @typescript-eslint/no-explicit-any */",
    "import { SWRConfig, useGetRequest } from \"./useGetRequest\"",
    "import { ResponseError } from \"../types\"",
    "import { AxiosRequestConfig, AxiosResponse } from \"axios\"",
    "import { SWRMutationConfig, useMutationRequest } from \"src/request/useMutationRequest\";"
  ],
  "clients": [],
  "data": ["./example/swagger/openAPI.yaml"],
  "fileName": "api",
  "needRequestHook": true,
  "needClient": true
}

Then run pnpm run start to generate request file to example folder, modify code you want and see the results.

And there are some test cases in __tests__ folder, you can run pnpm run test to see the results and help you modify the code.

🗂 Changelog

changelog

🤗 Appreciation

I would be very grateful if you could give this project a star!