Skip to content

Commit

Permalink
fix(sample): update samples to use custom-client with BodyType
Browse files Browse the repository at this point in the history
  • Loading branch information
CPatchane committed Mar 28, 2022
1 parent 0f7af1f commit 3dfdd63
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ export const getListPetsKey = (params?: ListPetsParams,
version= 1,) => [`/v${version}/pets`, ...(params ? [params]: [])];


export type ListPetsQueryResult = NonNullable<AsyncReturnType<typeof listPets>>
export type ListPetsQueryError = Error
export type ListPetsQueryResult = NonNullable<AsyncReturnType<typeof listPets>>
export type ListPetsQueryError = Error

export const useListPets = <TError = Error>(
params?: ListPetsParams,
Expand Down Expand Up @@ -100,8 +100,8 @@ export const getShowPetByIdKey = (petId: string,
version= 1,) => [`/v${version}/pets/${petId}`];


export type ShowPetByIdQueryResult = NonNullable<AsyncReturnType<typeof showPetById>>
export type ShowPetByIdQueryError = Error
export type ShowPetByIdQueryResult = NonNullable<AsyncReturnType<typeof showPetById>>
export type ShowPetByIdQueryError = Error

export const useShowPetById = <TError = Error>(
petId: string,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import type {
Pet,
CreatePetsBody
} from '../model'
import { customInstance, ErrorType } from '../mutator/custom-instance'
import { customClient, ErrorType, BodyType } from '../mutator/custom-client'

// eslint-disable-next-line @typescript-eslint/no-explicit-any
type AsyncReturnType<
Expand All @@ -39,7 +39,7 @@ export const listPets = (
params?: ListPetsParams,
version= 1,
) => {
return customInstance<Pets>(
return customClient<Pets>(
{url: `/v${version}/pets`, method: 'get',
params,
},
Expand Down Expand Up @@ -109,7 +109,7 @@ export const createPets = (
createPetsBody: CreatePetsBody,
version= 1,
) => {
return customInstance<Pet>(
return customClient<Pet>(
{url: `/v${version}/pets`, method: 'post',
data: createPetsBody
},
Expand All @@ -119,25 +119,25 @@ export const createPets = (


export type CreatePetsMutationResult = NonNullable<AsyncReturnType<typeof createPets>>
export type CreatePetsMutationBody = CreatePetsBody
export type CreatePetsMutationBody = BodyType<CreatePetsBody>
export type CreatePetsMutationError = ErrorType<Error>

export const useCreatePets = <TError = ErrorType<Error>,

TContext = unknown>(options?: { mutation?:UseMutationOptions<AsyncReturnType<typeof createPets>, TError,{data: CreatePetsBody;version?: number}, TContext>, }
TContext = unknown>(options?: { mutation?:UseMutationOptions<AsyncReturnType<typeof createPets>, TError,{data: BodyType<CreatePetsBody>;version?: number}, TContext>, }
) => {
const {mutation: mutationOptions} = options || {}




const mutationFn: MutationFunction<AsyncReturnType<typeof createPets>, {data: CreatePetsBody;version?: number}> = (props) => {
const mutationFn: MutationFunction<AsyncReturnType<typeof createPets>, {data: BodyType<CreatePetsBody>;version?: number}> = (props) => {
const {data,version} = props || {};

return createPets(data,version,)
}

return useMutation<AsyncReturnType<typeof createPets>, TError, {data: CreatePetsBody;version?: number}, TContext>(mutationFn, mutationOptions)
return useMutation<AsyncReturnType<typeof createPets>, TError, {data: BodyType<CreatePetsBody>;version?: number}, TContext>(mutationFn, mutationOptions)
}

/**
Expand All @@ -147,7 +147,7 @@ export const showPetById = (
petId: string,
version= 1,
) => {
return customInstance<Pet>(
return customClient<Pet>(
{url: `/v${version}/pets/${petId}`, method: 'get'
},
);
Expand Down
26 changes: 26 additions & 0 deletions samples/react-query/custom-client/src/api/mutator/custom-client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
export const customClient = async <ResponseType>({
url,
method,
params,
data,
}: {
url: string;
method: 'get' | 'post' | 'put' | 'delete' | 'patch';
params?: URLSearchParams;
data?: BodyType<any>;
}) => {
const { headers } = data;
const response = await fetch(url + new URLSearchParams(params), {
method,
headers,
...(data ? { body: JSON.stringify(data) } : {}),
});

return (await response.json()) as ResponseType;
};

export default customClient;

export type ErrorType<ErrorData> = ErrorData;

export type BodyType<BodyData> = BodyData & { headers?: any };

This file was deleted.

25 changes: 25 additions & 0 deletions tests/configs/react-query.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,31 @@ export default defineConfig({
},
},
},
customClient: {
output: {
target: '../generated/react-query/mutator/endpoints.ts',
schemas: '../generated/react-query/mutator/model',
client: 'react-query',
mock: true,
override: {
mutator: {
path: '../mutators/custom-client.ts',
name: 'customClient',
},
query: {
useQuery: true,
useInfinite: true,
useInfiniteQueryParam: 'limit',
},
},
},
input: {
target: '../specifications/petstore.yaml',
override: {
transformer: '../transformers/add-version.js',
},
},
},
mutatorMultiArguments: {
output: {
target: '../generated/react-query/mutator-multi-arguments/endpoints.ts',
Expand Down

0 comments on commit 3dfdd63

Please sign in to comment.