Package Exports
- rapini
- rapini/dist/cli.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (rapini) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
🥬 Rapini - OpenAPI to React Query & Axios
Rapini is a tool that generates React Query, Axios and Typescript types, based on an OpenAPI spec file. The generated code is packaged conveniently so that it can be published as a package on any NPM registry.
Features
- 🚴 Generates axios calls for every endpoint, with typed payload.
- 🏌️ Generates custom react hooks that use React Query's useQuery and useMutation hooks for each axios call.
- Every
GET
request get's it own custom useQuery hook - Every
POST
,PUT
,PATCH
,DELETE
request get their own custom useMutation hook
- Every
- 🚣 Generates query keys for every hook.
- 🏋️ Generates strong typescript types for all inputs, outputs, and options.
Installation
Rapini is a CLI tool so you can install it globally for convenience
npm i -g rapini
Usage
rapini path/to/openapi.yaml --outdir dist
This will generate the React Query code based on an OpenAPI file at path/to/openapi.yaml
and output it in folder dist
. The outputted code will be packaged in a way to just publish it as your own NPM package and then import it in your React project.
Example Usage
Let's say you have an OpenAPI file that looks like this one.
Once you run the CLI tool to generate the React Query code, you can then run npm publish
with your own package name, then import and use it like this:
import { initialize } from "your-custom-package";
import { axiosInstance } from "./your-custom-axios-instance";
// Can even import the generated Typescript Types if needed
import type { Pet } from "your-custom-package";
const config = initialize(axiosInstance);
const { usePets } = config.queries;
const MyComponent = () => {
const { data, isLoading, isError } = usePets();
return (
<ul>
{data.pets.map((pet) => (
<li key={pet.id}>{pet.name}</li>
))}
</ul>
);
};
You must call initialize(axiosInstance)
with your custom axios instance. The return value from the initialize
will give you an object with everything you need, here is the return value with examples:
const config = initialize(axiosInstance);
config.queries; // { usePets, usePetById } ...
config.mutations; // { useUpdatePet, useDeletePet } ...
config.queryIds; // { pets: () => ['pets'] } ...
config.requests; // { pets: () => axios.get<Pet[]>(...) } ...
Important Notes
- Every request must have an
operationId
defined. TheoperationId
is used in many places in the final generated code.