Overview
Meet OpenAPI Qraft: your go-to library for crafting type-safe API Hooks for React apps from OpenAPI Documents. By leveraging TanStack Query v5, it introduces a smart Proxy-based API client design to seamlessly generate custom hooks with strictly-typed parameters.
Featuresโ
- Type-safe API Requests: Utilize TypeScript for type-safe API requests, reducing runtime errors and improving developer experience.
- Modular Design: Customize the utility with a set of callbacks to handle API calls according to your project's needs.
- Integration with TanStack Query v5: Seamlessly integrate with TanStack Query for handling server state, caching, and data synchronization.
- Dynamic Proxy-Based Hooks: Automatically generate React Query hooks for your API endpoints without manual boilerplate.
- SSR Support: OpenAPI Qraft fully supports Server-Side Rendering (SSR)
at the same level
as TanStack Query, including support for Next.js
/app
directory.
Fast Runโ
- 1. OpenAPI Document
- 2. Setup
- 3. React
openapi: 3.1.0
paths:
'/pet/{petId}':
get:
tags:
- pet
summary: Find pet by ID
description: Returns a single pet
operationId: getPetById
parameters:
- name: petId
in: path
description: ID of pet to return
required: true
schema:
type: integer
format: int64
responses:
'200':
description: successful operation
content:
application/xml:
schema:
$ref: '#/components/schemas/Pet'
application/json:
schema:
$ref: '#/components/schemas/Pet'
'default':
description: Default Error
content:
application/json:
schema:
type: object
properties:
code:
type: integer
message:
type: string
'404':
description: Pet not found
components:
schemas:
Pet:
x-swagger-router-model: io.swagger.petstore.model.Pet
required:
- name
- photoUrls
properties:
id:
type: integer
name:
type: string
# ...
Installation:
- npm
- yarn
- pnpm
npm install @openapi-qraft/react@next
npm install -D @openapi-qraft/cli@next
# If you have not yet installed TanStack Query:
npm install @tanstack/react-query
yarn add @openapi-qraft/react@next
yarn add --dev @openapi-qraft/cli@next
# If you have not yet installed TanStack Query:
yarn add @tanstack/react-query
pnpm add @openapi-qraft/react@next
pnpm add -D @openapi-qraft/cli@next
# If you have not yet installed TanStack Query:
pnpm add @tanstack/react-query
API Client Generation:
- npm
- yarn
- pnpm
npx openapi-qraft --plugin tanstack-query-react --plugin openapi-typescript https://raw.githubusercontent.com/swagger-api/swagger-petstore/master/src/main/resources/openapi.yaml \
--output-dir src/api
yarn exec openapi-qraft --plugin tanstack-query-react --plugin openapi-typescript https://raw.githubusercontent.com/swagger-api/swagger-petstore/master/src/main/resources/openapi.yaml \
--output-dir src/api
pnpm exec openapi-qraft --plugin tanstack-query-react --plugin openapi-typescript https://raw.githubusercontent.com/swagger-api/swagger-petstore/master/src/main/resources/openapi.yaml \
--output-dir src/api
import { createAPIClient } from './api'; // generated by OpenAPI Qraft
import { requestFn } from '@openapi-qraft/react';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
const queryClient = new QueryClient();
// Use `createAPIClient(...)` to initialize the API client as needed.
// It's a lightweight ๐ชถ shortcut for working with TanStack Query ๐ด
const api = createAPIClient({
requestFn,
queryClient,
baseUrl: 'https://petstore3.swagger.io/api/v3',
});
function ExamplePetDetails({ petId }: { petId: number }) {
// Executes a GET request to retrieve the pet's details:
// GET /pet/{petId}
const {
data: pet,
isPending,
error,
} = qraft.pet.getPetById.useQuery({
path: { petId }, // โฌ
๏ธ All parameters are type-safe โจ
});
if (isPending) {
return <div>Loading...</div>;
}
if (error) {
return <div>Error: {error.message}</div>;
}
return <div>Pet Name: {pet?.name}</div>;
}
export default function App() {
return (
<QueryClientProvider client={queryClient}>
<ExamplePetDetails petId={123456} />
</QueryClientProvider>
);
}
Supported TanStack Query Featuresโ
Hooksโ
-
useQuery(...)
-
useMutation(...)
-
useInfiniteQuery(...)
-
useQueries(...)
-
useSuspenseQuery(...)
-
useSuspenseInfiniteQuery(...)
-
useSuspenseQueries(...)
-
useIsFetching(...)
-
useMutationState(...)
-
useIsMutating(...)
QueryClient
methodsโ
-
fetchQuery(...)
-
fetchInfiniteQuery(...)
-
prefetchQuery(...)
-
prefetchInfiniteQuery(...)
-
getQueryData(...)
-
getQueriesData(...)
-
setQueryData(...)
-
getQueryState(...)
-
setQueriesData(...)
-
invalidateQueries(...)
-
refetchQueries(...)
-
cancelQueries(...)
-
removeQueries(...)
-
resetQueries(...)
-
isFetching(...)
-
isMutating(...)