Fetch Client

The Fetch API is the default HTTP client for swr and TanStack Query clients.

import { defineConfig } from 'orval';
export default defineConfig({
petstore: {
output: {
...
client: 'swr',
...
},
},
});
/**
* @summary List all pets
*/
export type listPetsResponse = {
data: Pets | BadRequest;
status: number;
};
export const getListPetsUrl = (params?: ListPetsParams) => {
const normalizedParams = new URLSearchParams();
Object.entries(params || {}).forEach(([key, value]) => {
if (value === null) {
normalizedParams.append(key, 'null');
} else if (value !== undefined) {
normalizedParams.append(key, value.toString());
}
});
return normalizedParams.size
? `http://localhost:8000/pets?${normalizedParams.toString()}`
: `http://localhost:8000/pets`;
};
export const listPets = async (
params?: ListPetsParams,
options?: RequestInit,
): Promise<listPetsResponse> => {
const res = await fetch(getListPetsUrl(params), {
...options,
method: 'GET',
});
const data: Pets =
[204, 205, 304].includes(res.status) || !res.body ? {} : await res.json();
return { status: res.status, data };
};
export const getListPetsKey = (params?: ListPetsParams) =>
[`http://localhost:8000/pets`, ...(params ? [params] : [])] as const;
export type ListPetsQueryResult = NonNullable<
Awaited<ReturnType<typeof listPets>>
>;
export type ListPetsQueryError = Promise<Pets | Error>;
/**
* @summary List all pets
*/
export const useListPets = <TError = Promise<Pets | Error>>(
params?: ListPetsParams,
options?: {
swr?: SWRConfiguration<Awaited<ReturnType<typeof listPets>>, TError> & {
swrKey?: Key;
enabled?: boolean;
};
fetch?: RequestInit;
},
) => {
const { swr: swrOptions, fetch: fetchOptions } = options ?? {};
const isEnabled = swrOptions?.enabled !== false;
const swrKey =
swrOptions?.swrKey ?? (() => (isEnabled ? getListPetsKey(params) : null));
const swrFn = () => listPets(params, fetchOptions);
const query = useSwr<Awaited<ReturnType<typeof swrFn>>, TError>(
swrKey,
swrFn,
swrOptions,
);
return {
swrKey,
...query,
};
};

Returning the Originally Defined Return Type

When using fetch as the httpClient, by default the fetch response type includes HTTP status. When using swr or queries, you will be accessing the returned data through data.data, which may become tedious. To return a defined return type instead of an automatically generated return type, set override.fetch.includeHttpResponseReturnType value to false.

import { defineConfig } from 'orval';
export default defineConfig({
petstore: {
output: {
...
override: {
fetch: {
includeHttpResponseReturnType: false,
},
},
},
...
},
});
/**
* @summary List all pets
*/
- export type listPetsResponse = {
- data: Pets | BadRequest;
- status: number;
- };
export const listPets = async (
params?: ListPetsParams,
options?: RequestInit,
- ): Promise<listPetsResponse> => {
+ ): Promise<Pet> => {
const res = await fetch(getListPetsUrl(params), {
...options,
method: 'GET',
});
const data: Pets =
[204, 205, 304].includes(res.status) || !res.body ? {} : await res.json();
- return { status: res.status, data };
+ return data;
};

Custom Fetch Client

To use a custom Fetch client, provide a mutator in the override option.

import { defineConfig } from 'orval';
export default defineConfig({
petstore: {
output: {
...
client: 'swr',
override: {
mutator: {
path: './src/mutator.ts',
name: 'customFetch',
},
},
},
},
});
Was this page helpful?