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
npm install -D @openapi-qraft/cli
# If you have not yet installed TanStack Query:
npm install @tanstack/react-query
yarn add @openapi-qraft/react
yarn add --dev @openapi-qraft/cli
# If you have not yet installed TanStack Query:
yarn add @tanstack/react-query
pnpm add @openapi-qraft/react
pnpm add -D @openapi-qraft/cli
# 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 { QraftContext, requestFn } from '@openapi-qraft/react';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
/**
* The `createAPIClient(...)` function is generated by OpenAPI Qraft
* The `qraft` object could be created in a separate file and imported,
* or created multiple times in different components.
* It's just a typed shortcut to TanStack Query Hooks!
*/
const qraft = createAPIClient();
function ExamplePetDetails({ petId }: { petId: number }) {
const {
data: pet,
isPending,
error,
} = qraft.pet.getPetById.useQuery({ // โฌ
๏ธ Qraft's magic โจ
path: { petId },
});
if (isPending) {
return <div>Loading...</div>;
}
if (error) {
return <div>Error: {error.message}</div>;
}
return <div>Pet Name: {pet?.name}</div>;
}
const queryClient = new QueryClient();
export default function App() {
return (
<QueryClientProvider client={queryClient}>
<QraftContext.Provider
value={{
baseUrl: 'https://petstore3.swagger.io/api/v3', // the base URL of the API
requestFn, // the request function to use, could be fully customized
}}
>
<ExamplePetDetails petId={123456} />
</QraftContext.Provider>
</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(...)