swr-openapi
TypeScript icon, indicating that this package has built-in type declarations

5.1.2 • Public • Published

swr-openapi

Generate swr hooks using OpenAPI schemas

npm license

Setup

npm install swr-openapi swr openapi-fetch

Follow openapi-typescript directions to generate TypeScript definitions for each service being used.

Here is an example of types being generated for a service via the command line:

npx openapi-typescript "https://sandwiches.example/openapi/json" --output ./types/sandwich-schema.ts

Basic Usage

Initialize an openapi-fetch client and create any desired hooks.

// sandwich-api.ts
import createClient from "openapi-fetch";
import { createQueryHook } from "swr-openapi";
import type { paths as SandwichPaths } from "./types/sandwich-schema";

const client = createClient<SandwichPaths>(/* ... */);

const useSandwiches = createQueryHook(client, "sandwich-api");

const { data, error, isLoading, isValidating, mutate } = useSandwiches(
  "/sandwich/{id}", // <- Fully typed paths!
  {
    params: {
      path: {
        id: "123", // <- Fully typed params!
      },
    },
  },
);

📓 Docs

View Docs

Package Sidebar

Install

npm i swr-openapi

Weekly Downloads

2,920

Version

5.1.2

License

MIT

Unpacked Size

34.9 kB

Total Files

31

Last publish

Collaborators

  • drewpowers
  • htunnicliff