From 43766c801612900fcea2f80e8e13ce9e4c658294 Mon Sep 17 00:00:00 2001 From: Victor Bury Date: Fri, 17 May 2024 16:29:16 +0200 Subject: [PATCH] fix(hono): correctly check response validator application/json --- .prettierignore | 1 + packages/hono/src/index.ts | 2 +- .../api/endpoints/petstoreFromFileSpec.ts | 97 ++-- .../petstoreFromFileSpecWithConfig.ts | 97 ++-- .../petstoreFromFileSpecWithTransformer.ts | 298 ++++++----- samples/basic/api/model/index.ts | 2 +- .../api/model/listPetsNestedArrayParams.ts | 8 +- samples/basic/api/model/listPetsParams.ts | 8 +- samples/basic/api/model/petCallingCode.ts | 4 +- samples/basic/api/model/petCountry.ts | 5 +- .../react-query/form-url-encoded/endpoints.ts | 470 ++++++++++-------- .../form-url-encoded/models/index.ts | 2 +- .../models/listPetsNestedArrayParams.ts | 8 +- .../form-url-encoded/models/listPetsParams.ts | 8 +- .../form-url-encoded/models/petCallingCode.ts | 4 +- .../form-url-encoded/models/petCountry.ts | 5 +- 16 files changed, 551 insertions(+), 468 deletions(-) diff --git a/.prettierignore b/.prettierignore index b55c49335..0146f793c 100644 --- a/.prettierignore +++ b/.prettierignore @@ -9,3 +9,4 @@ node_modules .husky mockServiceWorker.js yarn.lock +.svelte-kit diff --git a/packages/hono/src/index.ts b/packages/hono/src/index.ts index 4e02e65f2..c3ea06ee7 100644 --- a/packages/hono/src/index.ts +++ b/packages/hono/src/index.ts @@ -864,7 +864,7 @@ export const zValidator = if ( c.res.status !== 200 || - c.res.headers.get('Content-Type') !== 'application/json' + !c.res.headers.get('Content-Type')?.includes('application/json') ) { return; } diff --git a/samples/basic/api/endpoints/petstoreFromFileSpec.ts b/samples/basic/api/endpoints/petstoreFromFileSpec.ts index 0245223d3..31b22092d 100644 --- a/samples/basic/api/endpoints/petstoreFromFileSpec.ts +++ b/samples/basic/api/endpoints/petstoreFromFileSpec.ts @@ -4,16 +4,13 @@ * Swagger Petstore * OpenAPI spec version: 1.0.0 */ -import axios from 'axios' -import type { - AxiosRequestConfig, - AxiosResponse -} from 'axios' +import axios from 'axios'; +import type { AxiosRequestConfig, AxiosResponse } from 'axios'; export type ListPetsNestedArrayParams = { -/** - * How many items to return at one time (max 100) - */ -limit?: string; + /** + * How many items to return at one time (max 100) + */ + limit?: string; }; export type CreatePetsBody = { @@ -22,10 +19,10 @@ export type CreatePetsBody = { }; export type ListPetsParams = { -/** - * How many items to return at one time (max 100) - */ -limit?: string; + /** + * How many items to return at one time (max 100) + */ + limit?: string; }; export interface Error { @@ -43,17 +40,16 @@ export interface PetsNestedArray { data?: Pet[]; } -export type PetCountry = typeof PetCountry[keyof typeof PetCountry]; - +export type PetCountry = (typeof PetCountry)[keyof typeof PetCountry]; // eslint-disable-next-line @typescript-eslint/no-redeclare export const PetCountry = { - 'People\'s_Republic_of_China': 'People\'s Republic of China', + "People's_Republic_of_China": "People's Republic of China", Uruguay: 'Uruguay', } as const; -export type PetCallingCode = typeof PetCallingCode[keyof typeof PetCallingCode]; - +export type PetCallingCode = + (typeof PetCallingCode)[keyof typeof PetCallingCode]; // eslint-disable-next-line @typescript-eslint/no-redeclare export const PetCallingCode = { @@ -86,60 +82,53 @@ export interface Pet { tag?: string | null; } - - - - - /** +/** * @summary List all pets */ export const listPets = >( - params?: ListPetsParams, options?: AxiosRequestConfig - ): Promise => { - return axios.get( - `/pets`,{ + params?: ListPetsParams, + options?: AxiosRequestConfig, +): Promise => { + return axios.get(`/pets`, { ...options, - params: {...params, ...options?.params},} - ); - } + params: { ...params, ...options?.params }, + }); +}; /** * @summary Create a pet */ export const createPets = >( - createPetsBody: CreatePetsBody, options?: AxiosRequestConfig - ): Promise => { - return axios.post( - `/pets`, - createPetsBody,options - ); - } + createPetsBody: CreatePetsBody, + options?: AxiosRequestConfig, +): Promise => { + return axios.post(`/pets`, createPetsBody, options); +}; /** * @summary List all pets as nested array */ export const listPetsNestedArray = >( - params?: ListPetsNestedArrayParams, options?: AxiosRequestConfig - ): Promise => { - return axios.get( - `/pets-nested-array`,{ + params?: ListPetsNestedArrayParams, + options?: AxiosRequestConfig, +): Promise => { + return axios.get(`/pets-nested-array`, { ...options, - params: {...params, ...options?.params},} - ); - } + params: { ...params, ...options?.params }, + }); +}; /** * @summary Info for a specific pet */ export const showPetById = >( - petId: string, options?: AxiosRequestConfig - ): Promise => { - return axios.get( - `/pets/${petId}`,options - ); - } + petId: string, + options?: AxiosRequestConfig, +): Promise => { + return axios.get(`/pets/${petId}`, options); +}; -export type ListPetsResult = AxiosResponse -export type CreatePetsResult = AxiosResponse -export type ListPetsNestedArrayResult = AxiosResponse -export type ShowPetByIdResult = AxiosResponse +export type ListPetsResult = AxiosResponse; +export type CreatePetsResult = AxiosResponse; +export type ListPetsNestedArrayResult = AxiosResponse; +export type ShowPetByIdResult = AxiosResponse; diff --git a/samples/basic/api/endpoints/petstoreFromFileSpecWithConfig.ts b/samples/basic/api/endpoints/petstoreFromFileSpecWithConfig.ts index 0245223d3..31b22092d 100644 --- a/samples/basic/api/endpoints/petstoreFromFileSpecWithConfig.ts +++ b/samples/basic/api/endpoints/petstoreFromFileSpecWithConfig.ts @@ -4,16 +4,13 @@ * Swagger Petstore * OpenAPI spec version: 1.0.0 */ -import axios from 'axios' -import type { - AxiosRequestConfig, - AxiosResponse -} from 'axios' +import axios from 'axios'; +import type { AxiosRequestConfig, AxiosResponse } from 'axios'; export type ListPetsNestedArrayParams = { -/** - * How many items to return at one time (max 100) - */ -limit?: string; + /** + * How many items to return at one time (max 100) + */ + limit?: string; }; export type CreatePetsBody = { @@ -22,10 +19,10 @@ export type CreatePetsBody = { }; export type ListPetsParams = { -/** - * How many items to return at one time (max 100) - */ -limit?: string; + /** + * How many items to return at one time (max 100) + */ + limit?: string; }; export interface Error { @@ -43,17 +40,16 @@ export interface PetsNestedArray { data?: Pet[]; } -export type PetCountry = typeof PetCountry[keyof typeof PetCountry]; - +export type PetCountry = (typeof PetCountry)[keyof typeof PetCountry]; // eslint-disable-next-line @typescript-eslint/no-redeclare export const PetCountry = { - 'People\'s_Republic_of_China': 'People\'s Republic of China', + "People's_Republic_of_China": "People's Republic of China", Uruguay: 'Uruguay', } as const; -export type PetCallingCode = typeof PetCallingCode[keyof typeof PetCallingCode]; - +export type PetCallingCode = + (typeof PetCallingCode)[keyof typeof PetCallingCode]; // eslint-disable-next-line @typescript-eslint/no-redeclare export const PetCallingCode = { @@ -86,60 +82,53 @@ export interface Pet { tag?: string | null; } - - - - - /** +/** * @summary List all pets */ export const listPets = >( - params?: ListPetsParams, options?: AxiosRequestConfig - ): Promise => { - return axios.get( - `/pets`,{ + params?: ListPetsParams, + options?: AxiosRequestConfig, +): Promise => { + return axios.get(`/pets`, { ...options, - params: {...params, ...options?.params},} - ); - } + params: { ...params, ...options?.params }, + }); +}; /** * @summary Create a pet */ export const createPets = >( - createPetsBody: CreatePetsBody, options?: AxiosRequestConfig - ): Promise => { - return axios.post( - `/pets`, - createPetsBody,options - ); - } + createPetsBody: CreatePetsBody, + options?: AxiosRequestConfig, +): Promise => { + return axios.post(`/pets`, createPetsBody, options); +}; /** * @summary List all pets as nested array */ export const listPetsNestedArray = >( - params?: ListPetsNestedArrayParams, options?: AxiosRequestConfig - ): Promise => { - return axios.get( - `/pets-nested-array`,{ + params?: ListPetsNestedArrayParams, + options?: AxiosRequestConfig, +): Promise => { + return axios.get(`/pets-nested-array`, { ...options, - params: {...params, ...options?.params},} - ); - } + params: { ...params, ...options?.params }, + }); +}; /** * @summary Info for a specific pet */ export const showPetById = >( - petId: string, options?: AxiosRequestConfig - ): Promise => { - return axios.get( - `/pets/${petId}`,options - ); - } + petId: string, + options?: AxiosRequestConfig, +): Promise => { + return axios.get(`/pets/${petId}`, options); +}; -export type ListPetsResult = AxiosResponse -export type CreatePetsResult = AxiosResponse -export type ListPetsNestedArrayResult = AxiosResponse -export type ShowPetByIdResult = AxiosResponse +export type ListPetsResult = AxiosResponse; +export type CreatePetsResult = AxiosResponse; +export type ListPetsNestedArrayResult = AxiosResponse; +export type ShowPetByIdResult = AxiosResponse; diff --git a/samples/basic/api/endpoints/petstoreFromFileSpecWithTransformer.ts b/samples/basic/api/endpoints/petstoreFromFileSpecWithTransformer.ts index 48b6d2776..862de0564 100644 --- a/samples/basic/api/endpoints/petstoreFromFileSpecWithTransformer.ts +++ b/samples/basic/api/endpoints/petstoreFromFileSpecWithTransformer.ts @@ -4,175 +4,231 @@ * Swagger Petstore * OpenAPI spec version: 1.0.0 */ -import axios from 'axios' -import type { - AxiosRequestConfig, - AxiosResponse -} from 'axios' +import axios from 'axios'; +import type { AxiosRequestConfig, AxiosResponse } from 'axios'; import type { CreatePetsBody, ListPetsNestedArrayParams, - ListPetsParams -} from '../model' -import { - faker -} from '@faker-js/faker' -import { - HttpResponse, - delay, - http -} from 'msw' -import type { - Pet, - PetsArray, - PetsNestedArray -} from '../model' + ListPetsParams, +} from '../model'; +import { faker } from '@faker-js/faker'; +import { HttpResponse, delay, http } from 'msw'; +import type { Pet, PetsArray, PetsNestedArray } from '../model'; import listPetsMutator from '../mutator/response-type'; - - - - /** +/** * @summary List all pets */ -export const listPets = ( - params?: ListPetsParams, - version: number = 1, - ) => { - return listPetsMutator( - {url: `/v${version}/pets`, method: 'GET', - params - }, - ); - } - +export const listPets = (params?: ListPetsParams, version: number = 1) => { + return listPetsMutator({ + url: `/v${version}/pets`, + method: 'GET', + params, + }); +}; + /** * @summary Create a pet */ export const createPets = >( - createPetsBody: CreatePetsBody, - version: number = 1, options?: AxiosRequestConfig - ): Promise => { - return axios.post( - `/v${version}/pets`, - createPetsBody,options - ); - } + createPetsBody: CreatePetsBody, + version: number = 1, + options?: AxiosRequestConfig, +): Promise => { + return axios.post(`/v${version}/pets`, createPetsBody, options); +}; /** * @summary List all pets as nested array */ export const listPetsNestedArray = >( - params?: ListPetsNestedArrayParams, - version: number = 1, options?: AxiosRequestConfig - ): Promise => { - return axios.get( - `/v${version}/pets-nested-array`,{ + params?: ListPetsNestedArrayParams, + version: number = 1, + options?: AxiosRequestConfig, +): Promise => { + return axios.get(`/v${version}/pets-nested-array`, { ...options, - params: {...params, ...options?.params},} - ); - } + params: { ...params, ...options?.params }, + }); +}; /** * @summary Info for a specific pet */ export const showPetById = >( - petId: string, - version: number = 1, options?: AxiosRequestConfig - ): Promise => { - return axios.get( - `/v${version}/pets/${petId}`,options - ); - } - + petId: string, + version: number = 1, + options?: AxiosRequestConfig, +): Promise => { + return axios.get(`/v${version}/pets/${petId}`, options); +}; type AwaitedInput = PromiseLike | T; - type Awaited = O extends AwaitedInput ? T : never; - -export type ListPetsResult = NonNullable>> -export type CreatePetsResult = AxiosResponse -export type ListPetsNestedArrayResult = AxiosResponse -export type ShowPetByIdResult = AxiosResponse - - -export const getListPetsResponseMock = (): PetsArray => (Array.from({ length: faker.number.int({ min: 1, max: 10 }) }, (_, i) => i + 1).map(() => ({age: faker.helpers.arrayElement([faker.number.int({min: 0, max: 30}), undefined]), callingCode: faker.helpers.arrayElement([faker.helpers.arrayElement(['+33','+420','+33'] as const), undefined]), country: faker.helpers.arrayElement([faker.helpers.arrayElement(['People\'s Republic of China','Uruguay'] as const), undefined]), email: faker.helpers.arrayElement([faker.internet.email(), undefined]), id: faker.number.int({min: undefined, max: undefined}), name: 'jon', tag: faker.helpers.arrayElement(['jon', null])}))) - -export const getListPetsNestedArrayResponseMock = (overrideResponse: Partial< PetsNestedArray > = {}): PetsNestedArray => ({data: faker.helpers.arrayElement([Array.from({ length: faker.number.int({ min: 1, max: 10 }) }, (_, i) => i + 1).map(() => ({age: faker.helpers.arrayElement([faker.number.int({min: 0, max: 30}), undefined]), callingCode: faker.helpers.arrayElement([faker.helpers.arrayElement(['+33','+420','+33'] as const), undefined]), country: faker.helpers.arrayElement([faker.helpers.arrayElement(['People\'s Republic of China','Uruguay'] as const), undefined]), email: faker.helpers.arrayElement([faker.internet.email(), undefined]), id: faker.number.int({min: undefined, max: undefined}), name: 'jon', tag: faker.helpers.arrayElement(['jon', null])})), undefined]), ...overrideResponse}) - -export const getShowPetByIdResponseMock = () => ((() => ({ - id: faker.number.int({ min: 1, max: 99 }), - name: faker.person.firstName(), - tag: faker.helpers.arrayElement([ - faker.word.sample(), - void 0 - ]) - }))()) - - -export const getListPetsMockHandler = (overrideResponse?: PetsArray | ((info: Parameters[1]>[0]) => PetsArray)) => { +type Awaited = O extends AwaitedInput ? T : never; + +export type ListPetsResult = NonNullable>>; +export type CreatePetsResult = AxiosResponse; +export type ListPetsNestedArrayResult = AxiosResponse; +export type ShowPetByIdResult = AxiosResponse; + +export const getListPetsResponseMock = (): PetsArray => + Array.from( + { length: faker.number.int({ min: 1, max: 10 }) }, + (_, i) => i + 1, + ).map(() => ({ + age: faker.helpers.arrayElement([ + faker.number.int({ min: 0, max: 30 }), + undefined, + ]), + callingCode: faker.helpers.arrayElement([ + faker.helpers.arrayElement(['+33', '+420', '+33'] as const), + undefined, + ]), + country: faker.helpers.arrayElement([ + faker.helpers.arrayElement([ + "People's Republic of China", + 'Uruguay', + ] as const), + undefined, + ]), + email: faker.helpers.arrayElement([faker.internet.email(), undefined]), + id: faker.number.int({ min: undefined, max: undefined }), + name: 'jon', + tag: faker.helpers.arrayElement(['jon', null]), + })); + +export const getListPetsNestedArrayResponseMock = ( + overrideResponse: Partial = {}, +): PetsNestedArray => ({ + data: faker.helpers.arrayElement([ + Array.from( + { length: faker.number.int({ min: 1, max: 10 }) }, + (_, i) => i + 1, + ).map(() => ({ + age: faker.helpers.arrayElement([ + faker.number.int({ min: 0, max: 30 }), + undefined, + ]), + callingCode: faker.helpers.arrayElement([ + faker.helpers.arrayElement(['+33', '+420', '+33'] as const), + undefined, + ]), + country: faker.helpers.arrayElement([ + faker.helpers.arrayElement([ + "People's Republic of China", + 'Uruguay', + ] as const), + undefined, + ]), + email: faker.helpers.arrayElement([faker.internet.email(), undefined]), + id: faker.number.int({ min: undefined, max: undefined }), + name: 'jon', + tag: faker.helpers.arrayElement(['jon', null]), + })), + undefined, + ]), + ...overrideResponse, +}); + +export const getShowPetByIdResponseMock = () => + (() => ({ + id: faker.number.int({ min: 1, max: 99 }), + name: faker.person.firstName(), + tag: faker.helpers.arrayElement([faker.word.sample(), void 0]), + }))(); + +export const getListPetsMockHandler = ( + overrideResponse?: + | PetsArray + | ((info: Parameters[1]>[0]) => PetsArray), +) => { return http.get('*/v:version/pets', async (info) => { await delay(1000); - return new HttpResponse(JSON.stringify(overrideResponse !== undefined - ? (typeof overrideResponse === "function" ? overrideResponse(info) : overrideResponse) - : getListPetsResponseMock()), + return new HttpResponse( + JSON.stringify( + overrideResponse !== undefined + ? typeof overrideResponse === 'function' + ? overrideResponse(info) + : overrideResponse + : getListPetsResponseMock(), + ), { status: 200, headers: { 'Content-Type': 'application/json', - } - } - ) - }) -} + }, + }, + ); + }); +}; export const getCreatePetsMockHandler = () => { return http.post('*/v:version/pets', async () => { await delay(1000); - return new HttpResponse(null, - { - status: 200, - headers: { - 'Content-Type': 'application/json', - } - } - ) - }) -} - -export const getListPetsNestedArrayMockHandler = (overrideResponse?: PetsNestedArray | ((info: Parameters[1]>[0]) => PetsNestedArray)) => { + return new HttpResponse(null, { + status: 200, + headers: { + 'Content-Type': 'application/json', + }, + }); + }); +}; + +export const getListPetsNestedArrayMockHandler = ( + overrideResponse?: + | PetsNestedArray + | (( + info: Parameters[1]>[0], + ) => PetsNestedArray), +) => { return http.get('*/v:version/pets-nested-array', async (info) => { await delay(1000); - return new HttpResponse(JSON.stringify(overrideResponse !== undefined - ? (typeof overrideResponse === "function" ? overrideResponse(info) : overrideResponse) - : getListPetsNestedArrayResponseMock()), + return new HttpResponse( + JSON.stringify( + overrideResponse !== undefined + ? typeof overrideResponse === 'function' + ? overrideResponse(info) + : overrideResponse + : getListPetsNestedArrayResponseMock(), + ), { status: 200, headers: { 'Content-Type': 'application/json', - } - } - ) - }) -} - -export const getShowPetByIdMockHandler = (overrideResponse?: Pet | ((info: Parameters[1]>[0]) => Pet)) => { + }, + }, + ); + }); +}; + +export const getShowPetByIdMockHandler = ( + overrideResponse?: + | Pet + | ((info: Parameters[1]>[0]) => Pet), +) => { return http.get('*/v:version/pets/:petId', async (info) => { await delay(1000); - return new HttpResponse(JSON.stringify(overrideResponse !== undefined - ? (typeof overrideResponse === "function" ? overrideResponse(info) : overrideResponse) - : getShowPetByIdResponseMock()), + return new HttpResponse( + JSON.stringify( + overrideResponse !== undefined + ? typeof overrideResponse === 'function' + ? overrideResponse(info) + : overrideResponse + : getShowPetByIdResponseMock(), + ), { status: 200, headers: { 'Content-Type': 'application/json', - } - } - ) - }) -} + }, + }, + ); + }); +}; export const getSwaggerPetstoreMock = () => [ getListPetsMockHandler(), getCreatePetsMockHandler(), getListPetsNestedArrayMockHandler(), - getShowPetByIdMockHandler()] + getShowPetByIdMockHandler(), +]; diff --git a/samples/basic/api/model/index.ts b/samples/basic/api/model/index.ts index c0d4f3210..e561f06e3 100644 --- a/samples/basic/api/model/index.ts +++ b/samples/basic/api/model/index.ts @@ -14,4 +14,4 @@ export * from './petCallingCode'; export * from './petCountry'; export * from './pets'; export * from './petsArray'; -export * from './petsNestedArray'; \ No newline at end of file +export * from './petsNestedArray'; diff --git a/samples/basic/api/model/listPetsNestedArrayParams.ts b/samples/basic/api/model/listPetsNestedArrayParams.ts index a720e2c9f..382094d75 100644 --- a/samples/basic/api/model/listPetsNestedArrayParams.ts +++ b/samples/basic/api/model/listPetsNestedArrayParams.ts @@ -6,8 +6,8 @@ */ export type ListPetsNestedArrayParams = { -/** - * How many items to return at one time (max 100) - */ -limit?: string; + /** + * How many items to return at one time (max 100) + */ + limit?: string; }; diff --git a/samples/basic/api/model/listPetsParams.ts b/samples/basic/api/model/listPetsParams.ts index 064c4733f..0d2bb23e0 100644 --- a/samples/basic/api/model/listPetsParams.ts +++ b/samples/basic/api/model/listPetsParams.ts @@ -6,8 +6,8 @@ */ export type ListPetsParams = { -/** - * How many items to return at one time (max 100) - */ -limit?: string; + /** + * How many items to return at one time (max 100) + */ + limit?: string; }; diff --git a/samples/basic/api/model/petCallingCode.ts b/samples/basic/api/model/petCallingCode.ts index 654b7011f..92b906e67 100644 --- a/samples/basic/api/model/petCallingCode.ts +++ b/samples/basic/api/model/petCallingCode.ts @@ -5,8 +5,8 @@ * OpenAPI spec version: 1.0.0 */ -export type PetCallingCode = typeof PetCallingCode[keyof typeof PetCallingCode]; - +export type PetCallingCode = + (typeof PetCallingCode)[keyof typeof PetCallingCode]; // eslint-disable-next-line @typescript-eslint/no-redeclare export const PetCallingCode = { diff --git a/samples/basic/api/model/petCountry.ts b/samples/basic/api/model/petCountry.ts index b6ecfb9e0..e55dbe61d 100644 --- a/samples/basic/api/model/petCountry.ts +++ b/samples/basic/api/model/petCountry.ts @@ -5,11 +5,10 @@ * OpenAPI spec version: 1.0.0 */ -export type PetCountry = typeof PetCountry[keyof typeof PetCountry]; - +export type PetCountry = (typeof PetCountry)[keyof typeof PetCountry]; // eslint-disable-next-line @typescript-eslint/no-redeclare export const PetCountry = { - 'People\'s_Republic_of_China': 'People\'s Republic of China', + "People's_Republic_of_China": "People's Republic of China", Uruguay: 'Uruguay', } as const; diff --git a/samples/react-query/form-url-encoded/endpoints.ts b/samples/react-query/form-url-encoded/endpoints.ts index 5632336d8..9b4c76649 100644 --- a/samples/react-query/form-url-encoded/endpoints.ts +++ b/samples/react-query/form-url-encoded/endpoints.ts @@ -4,10 +4,7 @@ * Swagger Petstore * OpenAPI spec version: 1.0.0 */ -import { - useMutation, - useQuery -} from 'react-query' +import { useMutation, useQuery } from 'react-query'; import type { MutationFunction, QueryFunction, @@ -15,8 +12,8 @@ import type { UseMutationOptions, UseMutationResult, UseQueryOptions, - UseQueryResult -} from 'react-query' + UseQueryResult, +} from 'react-query'; import type { CreatePetsBody, Error, @@ -24,260 +21,313 @@ import type { ListPetsParams, Pet, PetsArray, - PetsNestedArray -} from './models' + PetsNestedArray, +} from './models'; import { customInstance } from './custom-instance'; - type AwaitedInput = PromiseLike | T; - type Awaited = O extends AwaitedInput ? T : never; - - +type Awaited = O extends AwaitedInput ? T : never; /** * @summary List all pets */ -export const listPets = ( - params?: ListPetsParams, - signal?: AbortSignal -) => { - - - return customInstance( - {url: `/pets`, method: 'GET', - params, signal - }, - ); - } - - -export const getListPetsQueryKey = (params?: ListPetsParams,) => { - return [`/pets`, ...(params ? [params]: [])] as const; - } - - -export const getListPetsQueryOptions = >, TError = Error>(params?: ListPetsParams, options?: { query?:UseQueryOptions>, TError, TData>, } +export const listPets = (params?: ListPetsParams, signal?: AbortSignal) => { + return customInstance({ + url: `/pets`, + method: 'GET', + params, + signal, + }); +}; + +export const getListPetsQueryKey = (params?: ListPetsParams) => { + return [`/pets`, ...(params ? [params] : [])] as const; +}; + +export const getListPetsQueryOptions = < + TData = Awaited>, + TError = Error, +>( + params?: ListPetsParams, + options?: { + query?: UseQueryOptions< + Awaited>, + TError, + TData + >; + }, ) => { + const { query: queryOptions } = options ?? {}; -const {query: queryOptions} = options ?? {}; - - const queryKey = queryOptions?.queryKey ?? getListPetsQueryKey(params); - - - - const queryFn: QueryFunction>> = ({ signal }) => listPets(params, signal); + const queryKey = queryOptions?.queryKey ?? getListPetsQueryKey(params); - + const queryFn: QueryFunction>> = ({ + signal, + }) => listPets(params, signal); - + return { queryKey, queryFn, ...queryOptions } as UseQueryOptions< + Awaited>, + TError, + TData + > & { queryKey: QueryKey }; +}; - return { queryKey, queryFn, ...queryOptions} as UseQueryOptions>, TError, TData> & { queryKey: QueryKey } -} - -export type ListPetsQueryResult = NonNullable>> -export type ListPetsQueryError = Error +export type ListPetsQueryResult = NonNullable< + Awaited> +>; +export type ListPetsQueryError = Error; /** * @summary List all pets */ -export const useListPets = >, TError = Error>( - params?: ListPetsParams, options?: { query?:UseQueryOptions>, TError, TData>, } - - ): UseQueryResult & { queryKey: QueryKey } => { - - const queryOptions = getListPetsQueryOptions(params,options) - - const query = useQuery(queryOptions) as UseQueryResult & { queryKey: QueryKey }; - - query.queryKey = queryOptions.queryKey ; +export const useListPets = < + TData = Awaited>, + TError = Error, +>( + params?: ListPetsParams, + options?: { + query?: UseQueryOptions< + Awaited>, + TError, + TData + >; + }, +): UseQueryResult & { queryKey: QueryKey } => { + const queryOptions = getListPetsQueryOptions(params, options); + + const query = useQuery(queryOptions) as UseQueryResult & { + queryKey: QueryKey; + }; + + query.queryKey = queryOptions.queryKey; return query; -} - - - +}; /** * @summary Create a pet */ -export const createPets = ( - createPetsBody: CreatePetsBody, - ) => { - - - return customInstance( - {url: `/pets`, method: 'POST', - headers: {'Content-Type': 'application/json', }, - data: createPetsBody - }, - ); - } - - - -export const getCreatePetsMutationOptions = (options?: { mutation?:UseMutationOptions>, TError,{data: CreatePetsBody}, TContext>, } -): UseMutationOptions>, TError,{data: CreatePetsBody}, TContext> => { -const {mutation: mutationOptions} = options ?? {}; - - - - - const mutationFn: MutationFunction>, {data: CreatePetsBody}> = (props) => { - const {data} = props ?? {}; - - return createPets(data,) - } +export const createPets = (createPetsBody: CreatePetsBody) => { + return customInstance({ + url: `/pets`, + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + data: createPetsBody, + }); +}; + +export const getCreatePetsMutationOptions = < + TError = Error, + TContext = unknown, +>(options?: { + mutation?: UseMutationOptions< + Awaited>, + TError, + { data: CreatePetsBody }, + TContext + >; +}): UseMutationOptions< + Awaited>, + TError, + { data: CreatePetsBody }, + TContext +> => { + const { mutation: mutationOptions } = options ?? {}; + + const mutationFn: MutationFunction< + Awaited>, + { data: CreatePetsBody } + > = (props) => { + const { data } = props ?? {}; + + return createPets(data); + }; + + return { mutationFn, ...mutationOptions }; +}; + +export type CreatePetsMutationResult = NonNullable< + Awaited> +>; +export type CreatePetsMutationBody = CreatePetsBody; +export type CreatePetsMutationError = Error; - - - - return { mutationFn, ...mutationOptions }} - - export type CreatePetsMutationResult = NonNullable>> - export type CreatePetsMutationBody = CreatePetsBody - export type CreatePetsMutationError = Error - - /** +/** * @summary Create a pet */ -export const useCreatePets = (options?: { mutation?:UseMutationOptions>, TError,{data: CreatePetsBody}, TContext>, } -): UseMutationResult< - Awaited>, - TError, - {data: CreatePetsBody}, - TContext - > => { - - const mutationOptions = getCreatePetsMutationOptions(options); - - return useMutation(mutationOptions); - } - +export const useCreatePets = (options?: { + mutation?: UseMutationOptions< + Awaited>, + TError, + { data: CreatePetsBody }, + TContext + >; +}): UseMutationResult< + Awaited>, + TError, + { data: CreatePetsBody }, + TContext +> => { + const mutationOptions = getCreatePetsMutationOptions(options); + + return useMutation(mutationOptions); +}; + /** * @summary List all pets as nested array */ export const listPetsNestedArray = ( - params?: ListPetsNestedArrayParams, - signal?: AbortSignal + params?: ListPetsNestedArrayParams, + signal?: AbortSignal, ) => { - - - return customInstance( - {url: `/pets-nested-array`, method: 'GET', - params, signal - }, - ); - } - - -export const getListPetsNestedArrayQueryKey = (params?: ListPetsNestedArrayParams,) => { - return [`/pets-nested-array`, ...(params ? [params]: [])] as const; - } - - -export const getListPetsNestedArrayQueryOptions = >, TError = Error>(params?: ListPetsNestedArrayParams, options?: { query?:UseQueryOptions>, TError, TData>, } + return customInstance({ + url: `/pets-nested-array`, + method: 'GET', + params, + signal, + }); +}; + +export const getListPetsNestedArrayQueryKey = ( + params?: ListPetsNestedArrayParams, ) => { + return [`/pets-nested-array`, ...(params ? [params] : [])] as const; +}; + +export const getListPetsNestedArrayQueryOptions = < + TData = Awaited>, + TError = Error, +>( + params?: ListPetsNestedArrayParams, + options?: { + query?: UseQueryOptions< + Awaited>, + TError, + TData + >; + }, +) => { + const { query: queryOptions } = options ?? {}; -const {query: queryOptions} = options ?? {}; - - const queryKey = queryOptions?.queryKey ?? getListPetsNestedArrayQueryKey(params); - - - - const queryFn: QueryFunction>> = ({ signal }) => listPetsNestedArray(params, signal); - - + const queryKey = + queryOptions?.queryKey ?? getListPetsNestedArrayQueryKey(params); - + const queryFn: QueryFunction< + Awaited> + > = ({ signal }) => listPetsNestedArray(params, signal); - return { queryKey, queryFn, ...queryOptions} as UseQueryOptions>, TError, TData> & { queryKey: QueryKey } -} + return { queryKey, queryFn, ...queryOptions } as UseQueryOptions< + Awaited>, + TError, + TData + > & { queryKey: QueryKey }; +}; -export type ListPetsNestedArrayQueryResult = NonNullable>> -export type ListPetsNestedArrayQueryError = Error +export type ListPetsNestedArrayQueryResult = NonNullable< + Awaited> +>; +export type ListPetsNestedArrayQueryError = Error; /** * @summary List all pets as nested array */ -export const useListPetsNestedArray = >, TError = Error>( - params?: ListPetsNestedArrayParams, options?: { query?:UseQueryOptions>, TError, TData>, } - - ): UseQueryResult & { queryKey: QueryKey } => { - - const queryOptions = getListPetsNestedArrayQueryOptions(params,options) - - const query = useQuery(queryOptions) as UseQueryResult & { queryKey: QueryKey }; - - query.queryKey = queryOptions.queryKey ; +export const useListPetsNestedArray = < + TData = Awaited>, + TError = Error, +>( + params?: ListPetsNestedArrayParams, + options?: { + query?: UseQueryOptions< + Awaited>, + TError, + TData + >; + }, +): UseQueryResult & { queryKey: QueryKey } => { + const queryOptions = getListPetsNestedArrayQueryOptions(params, options); + + const query = useQuery(queryOptions) as UseQueryResult & { + queryKey: QueryKey; + }; + + query.queryKey = queryOptions.queryKey; return query; -} - - - +}; /** * @summary Info for a specific pet */ -export const showPetById = ( - petId: string, - signal?: AbortSignal +export const showPetById = (petId: string, signal?: AbortSignal) => { + return customInstance({ url: `/pets/${petId}`, method: 'GET', signal }); +}; + +export const getShowPetByIdQueryKey = (petId: string) => { + return [`/pets/${petId}`] as const; +}; + +export const getShowPetByIdQueryOptions = < + TData = Awaited>, + TError = Error, +>( + petId: string, + options?: { + query?: UseQueryOptions< + Awaited>, + TError, + TData + >; + }, ) => { - - - return customInstance( - {url: `/pets/${petId}`, method: 'GET', signal - }, - ); - } - - -export const getShowPetByIdQueryKey = (petId: string,) => { - return [`/pets/${petId}`] as const; - } - - -export const getShowPetByIdQueryOptions = >, TError = Error>(petId: string, options?: { query?:UseQueryOptions>, TError, TData>, } -) => { - -const {query: queryOptions} = options ?? {}; - - const queryKey = queryOptions?.queryKey ?? getShowPetByIdQueryKey(petId); - - - - const queryFn: QueryFunction>> = ({ signal }) => showPetById(petId, signal); - - - - - - return { queryKey, queryFn, enabled: !!(petId), ...queryOptions} as UseQueryOptions>, TError, TData> & { queryKey: QueryKey } -} - -export type ShowPetByIdQueryResult = NonNullable>> -export type ShowPetByIdQueryError = Error + const { query: queryOptions } = options ?? {}; + + const queryKey = queryOptions?.queryKey ?? getShowPetByIdQueryKey(petId); + + const queryFn: QueryFunction>> = ({ + signal, + }) => showPetById(petId, signal); + + return { + queryKey, + queryFn, + enabled: !!petId, + ...queryOptions, + } as UseQueryOptions< + Awaited>, + TError, + TData + > & { queryKey: QueryKey }; +}; + +export type ShowPetByIdQueryResult = NonNullable< + Awaited> +>; +export type ShowPetByIdQueryError = Error; /** * @summary Info for a specific pet */ -export const useShowPetById = >, TError = Error>( - petId: string, options?: { query?:UseQueryOptions>, TError, TData>, } - - ): UseQueryResult & { queryKey: QueryKey } => { - - const queryOptions = getShowPetByIdQueryOptions(petId,options) - - const query = useQuery(queryOptions) as UseQueryResult & { queryKey: QueryKey }; - - query.queryKey = queryOptions.queryKey ; +export const useShowPetById = < + TData = Awaited>, + TError = Error, +>( + petId: string, + options?: { + query?: UseQueryOptions< + Awaited>, + TError, + TData + >; + }, +): UseQueryResult & { queryKey: QueryKey } => { + const queryOptions = getShowPetByIdQueryOptions(petId, options); + + const query = useQuery(queryOptions) as UseQueryResult & { + queryKey: QueryKey; + }; + + query.queryKey = queryOptions.queryKey; return query; -} - - - - +}; diff --git a/samples/react-query/form-url-encoded/models/index.ts b/samples/react-query/form-url-encoded/models/index.ts index 16459db4d..dfd65d93e 100644 --- a/samples/react-query/form-url-encoded/models/index.ts +++ b/samples/react-query/form-url-encoded/models/index.ts @@ -13,4 +13,4 @@ export * from './pet'; export * from './petCallingCode'; export * from './petCountry'; export * from './petsArray'; -export * from './petsNestedArray'; \ No newline at end of file +export * from './petsNestedArray'; diff --git a/samples/react-query/form-url-encoded/models/listPetsNestedArrayParams.ts b/samples/react-query/form-url-encoded/models/listPetsNestedArrayParams.ts index a720e2c9f..382094d75 100644 --- a/samples/react-query/form-url-encoded/models/listPetsNestedArrayParams.ts +++ b/samples/react-query/form-url-encoded/models/listPetsNestedArrayParams.ts @@ -6,8 +6,8 @@ */ export type ListPetsNestedArrayParams = { -/** - * How many items to return at one time (max 100) - */ -limit?: string; + /** + * How many items to return at one time (max 100) + */ + limit?: string; }; diff --git a/samples/react-query/form-url-encoded/models/listPetsParams.ts b/samples/react-query/form-url-encoded/models/listPetsParams.ts index 064c4733f..0d2bb23e0 100644 --- a/samples/react-query/form-url-encoded/models/listPetsParams.ts +++ b/samples/react-query/form-url-encoded/models/listPetsParams.ts @@ -6,8 +6,8 @@ */ export type ListPetsParams = { -/** - * How many items to return at one time (max 100) - */ -limit?: string; + /** + * How many items to return at one time (max 100) + */ + limit?: string; }; diff --git a/samples/react-query/form-url-encoded/models/petCallingCode.ts b/samples/react-query/form-url-encoded/models/petCallingCode.ts index 654b7011f..92b906e67 100644 --- a/samples/react-query/form-url-encoded/models/petCallingCode.ts +++ b/samples/react-query/form-url-encoded/models/petCallingCode.ts @@ -5,8 +5,8 @@ * OpenAPI spec version: 1.0.0 */ -export type PetCallingCode = typeof PetCallingCode[keyof typeof PetCallingCode]; - +export type PetCallingCode = + (typeof PetCallingCode)[keyof typeof PetCallingCode]; // eslint-disable-next-line @typescript-eslint/no-redeclare export const PetCallingCode = { diff --git a/samples/react-query/form-url-encoded/models/petCountry.ts b/samples/react-query/form-url-encoded/models/petCountry.ts index b6ecfb9e0..e55dbe61d 100644 --- a/samples/react-query/form-url-encoded/models/petCountry.ts +++ b/samples/react-query/form-url-encoded/models/petCountry.ts @@ -5,11 +5,10 @@ * OpenAPI spec version: 1.0.0 */ -export type PetCountry = typeof PetCountry[keyof typeof PetCountry]; - +export type PetCountry = (typeof PetCountry)[keyof typeof PetCountry]; // eslint-disable-next-line @typescript-eslint/no-redeclare export const PetCountry = { - 'People\'s_Republic_of_China': 'People\'s Republic of China', + "People's_Republic_of_China": "People's Republic of China", Uruguay: 'Uruguay', } as const;