diff --git a/packages/core/src/getters/discriminators.ts b/packages/core/src/getters/discriminators.ts index deb9a0abf..5d7593f56 100644 --- a/packages/core/src/getters/discriminators.ts +++ b/packages/core/src/getters/discriminators.ts @@ -1,4 +1,4 @@ -import { SchemasObject } from 'openapi3-ts/oas30'; +import { SchemaObject, SchemasObject } from 'openapi3-ts/oas30'; import { ContextSpecs } from '../types'; import { getRefInfo } from './ref'; import { pascal } from '../utils'; @@ -28,12 +28,17 @@ export const resolveDiscriminators = ( if (!subTypeSchema) { continue; } - + const property = subTypeSchema.properties?.[ + propertyName + ] as SchemaObject; subTypeSchema.properties = { ...subTypeSchema.properties, [propertyName]: { type: 'string', - enum: [mappingKey], + enum: [ + ...(property.enum ?? []), + mappingKey, + ], }, }; subTypeSchema.required = [ diff --git a/samples/basic/api/endpoints/petstoreFromFileSpec.ts b/samples/basic/api/endpoints/petstoreFromFileSpec.ts index 66e6bc21d..a5e33fb54 100644 --- a/samples/basic/api/endpoints/petstoreFromFileSpec.ts +++ b/samples/basic/api/endpoints/petstoreFromFileSpec.ts @@ -4,21 +4,18 @@ * 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 CreatePetsBody = { name: string; tag: string; }; 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 { @@ -54,46 +51,39 @@ export interface Pet { */ export type Pets = Pet[]; - - - - - /** +/** * @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 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 ShowPetByIdResult = AxiosResponse +export type ListPetsResult = AxiosResponse; +export type CreatePetsResult = AxiosResponse; +export type ShowPetByIdResult = AxiosResponse; diff --git a/samples/basic/api/endpoints/petstoreFromFileSpecWithConfig.ts b/samples/basic/api/endpoints/petstoreFromFileSpecWithConfig.ts index 66e6bc21d..a5e33fb54 100644 --- a/samples/basic/api/endpoints/petstoreFromFileSpecWithConfig.ts +++ b/samples/basic/api/endpoints/petstoreFromFileSpecWithConfig.ts @@ -4,21 +4,18 @@ * 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 CreatePetsBody = { name: string; tag: string; }; 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 { @@ -54,46 +51,39 @@ export interface Pet { */ export type Pets = Pet[]; - - - - - /** +/** * @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 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 ShowPetByIdResult = AxiosResponse +export type ListPetsResult = AxiosResponse; +export type CreatePetsResult = AxiosResponse; +export type ShowPetByIdResult = AxiosResponse; diff --git a/samples/basic/api/endpoints/petstoreFromFileSpecWithTransformer.ts b/samples/basic/api/endpoints/petstoreFromFileSpecWithTransformer.ts index 10c641497..13dd1e8da 100644 --- a/samples/basic/api/endpoints/petstoreFromFileSpecWithTransformer.ts +++ b/samples/basic/api/endpoints/petstoreFromFileSpecWithTransformer.ts @@ -4,135 +4,124 @@ * Swagger Petstore * OpenAPI spec version: 1.0.0 */ -import axios from 'axios' -import type { - AxiosRequestConfig, - AxiosResponse -} from 'axios' -import type { - CreatePetsBody, - ListPetsParams -} from '../model' -import { - faker -} from '@faker-js/faker' -import { - HttpResponse, - delay, - http -} from 'msw' -import type { - Pet, - Pets -} from '../model' +import axios from 'axios'; +import type { AxiosRequestConfig, AxiosResponse } from 'axios'; +import type { CreatePetsBody, ListPetsParams } from '../model'; +import { faker } from '@faker-js/faker'; +import { HttpResponse, delay, http } from 'msw'; +import type { Pet, Pets } 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 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 ShowPetByIdResult = AxiosResponse - - -export const getListPetsResponseMock = (overrideResponse: any = {}): Pets => (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]), id: faker.number.int({min: undefined, max: undefined}), name: 'jon', tag: faker.helpers.arrayElement(['jon', null]), ...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 - ]) - }))()) - +type Awaited = O extends AwaitedInput ? T : never; + +export type ListPetsResult = NonNullable>>; +export type CreatePetsResult = AxiosResponse; +export type ShowPetByIdResult = AxiosResponse; + +export const getListPetsResponseMock = (overrideResponse: any = {}): Pets => + 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, + ]), + id: faker.number.int({ min: undefined, max: undefined }), + name: 'jon', + tag: faker.helpers.arrayElement(['jon', null]), + ...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?: Pets) => { return http.get('*/v:version/pets', async () => { await delay(1000); - return new HttpResponse(JSON.stringify(overrideResponse ? overrideResponse : getListPetsResponseMock()), + return new HttpResponse( + JSON.stringify( + overrideResponse ? 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', - } - } - ) - }) -} + return new HttpResponse(null, { + status: 200, + headers: { + 'Content-Type': 'application/json', + }, + }); + }); +}; export const getShowPetByIdMockHandler = (overrideResponse?: Pet) => { return http.get('*/v:version/pets/:petId', async () => { await delay(1000); - return new HttpResponse(JSON.stringify(overrideResponse ? overrideResponse : getShowPetByIdResponseMock()), + return new HttpResponse( + JSON.stringify( + overrideResponse ? overrideResponse : getShowPetByIdResponseMock(), + ), { status: 200, headers: { 'Content-Type': 'application/json', - } - } - ) - }) -} + }, + }, + ); + }); +}; export const getSwaggerPetstoreMock = () => [ getListPetsMockHandler(), getCreatePetsMockHandler(), - getShowPetByIdMockHandler()] + getShowPetByIdMockHandler(), +]; diff --git a/samples/basic/api/model/index.ts b/samples/basic/api/model/index.ts index c0b0c68fe..f02159240 100644 --- a/samples/basic/api/model/index.ts +++ b/samples/basic/api/model/index.ts @@ -9,4 +9,4 @@ export * from './createPetsBody'; export * from './error'; export * from './listPetsParams'; export * from './pet'; -export * from './pets'; \ No newline at end of file +export * from './pets'; diff --git a/samples/basic/api/model/listPetsParams.ts b/samples/basic/api/model/listPetsParams.ts index 2beba433f..6c8e78d3b 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/react-query/basic/src/api/endpoints/petstoreFromFileSpecWithTransformer.ts b/samples/react-query/basic/src/api/endpoints/petstoreFromFileSpecWithTransformer.ts index 8a32745fe..3823715c2 100644 --- a/samples/react-query/basic/src/api/endpoints/petstoreFromFileSpecWithTransformer.ts +++ b/samples/react-query/basic/src/api/endpoints/petstoreFromFileSpecWithTransformer.ts @@ -38,34 +38,32 @@ import { customInstance } from '../mutator/custom-instance'; import type { ErrorType } from '../mutator/custom-instance'; type IsAny = 0 extends 1 & T ? true : false; -type IsUnknown = IsAny extends true - ? false - : unknown extends T - ? true - : false; +type IsUnknown = + IsAny extends true ? false : unknown extends T ? true : false; type Primitive = string | number | boolean | bigint | symbol | undefined | null; type isBuiltin = Primitive | Function | Date | Error | RegExp; -type NonReadonly = T extends Exclude - ? T - : T extends Map - ? Map, NonReadonly> - : T extends ReadonlyMap - ? Map, NonReadonly> - : T extends WeakMap - ? WeakMap, NonReadonly> - : T extends Set - ? Set> - : T extends ReadonlySet - ? Set> - : T extends WeakSet - ? WeakSet> - : T extends Promise - ? Promise> - : T extends {} - ? { -readonly [Key in keyof T]: NonReadonly } - : IsUnknown extends true - ? unknown - : T; +type NonReadonly = + T extends Exclude + ? T + : T extends Map + ? Map, NonReadonly> + : T extends ReadonlyMap + ? Map, NonReadonly> + : T extends WeakMap + ? WeakMap, NonReadonly> + : T extends Set + ? Set> + : T extends ReadonlySet + ? Set> + : T extends WeakSet + ? WeakSet> + : T extends Promise + ? Promise> + : T extends {} + ? { -readonly [Key in keyof T]: NonReadonly } + : IsUnknown extends true + ? unknown + : T; type AwaitedInput = PromiseLike | T; diff --git a/samples/react-query/form-url-encoded/endpoints.ts b/samples/react-query/form-url-encoded/endpoints.ts index 429688d37..318fc477f 100644 --- a/samples/react-query/form-url-encoded/endpoints.ts +++ b/samples/react-query/form-url-encoded/endpoints.ts @@ -4,86 +4,88 @@ * Swagger Petstore * OpenAPI spec version: 1.0.0 */ -import { - useMutation -} from 'react-query' +import { useMutation } from 'react-query'; import type { MutationFunction, UseMutationOptions, - UseMutationResult -} from 'react-query' -import type { - CreatePetsBody, - Error, - Pet -} from './models' + UseMutationResult, +} from 'react-query'; +import type { CreatePetsBody, Error, Pet } 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 Create a pet */ -export const createPets = ( - createPetsBody: CreatePetsBody, - ) => { - - const formUrlEncoded = new URLSearchParams(); -formUrlEncoded.append('name', createPetsBody.name) -formUrlEncoded.append('tag', createPetsBody.tag) - - return customInstance( - {url: `/pets`, method: 'POST', - headers: {'Content-Type': 'application/x-www-form-urlencoded', }, - data: formUrlEncoded - }, - ); - } - - - -export const getCreatePetsMutationOptions = (options?: { mutation?:UseMutationOptions>, TError,{data: CreatePetsBody}, TContext>, } -): UseMutationOptions>, TError,{data: CreatePetsBody}, TContext> => { -const {mutation: mutationOptions} = options ?? {}; - - - +export const createPets = (createPetsBody: CreatePetsBody) => { + const formUrlEncoded = new URLSearchParams(); + formUrlEncoded.append('name', createPetsBody.name); + formUrlEncoded.append('tag', createPetsBody.tag); + + return customInstance({ + url: `/pets`, + method: 'POST', + headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, + data: formUrlEncoded, + }); +}; + +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; - const mutationFn: MutationFunction>, {data: CreatePetsBody}> = (props) => { - const {data} = props ?? {}; - - return createPets(data,) - } - - - - - 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); +}; diff --git a/samples/react-query/form-url-encoded/models/index.ts b/samples/react-query/form-url-encoded/models/index.ts index 15ef9ff8a..0c7569e5a 100644 --- a/samples/react-query/form-url-encoded/models/index.ts +++ b/samples/react-query/form-url-encoded/models/index.ts @@ -9,4 +9,4 @@ export * from './createPetsBody'; export * from './error'; export * from './pet'; export * from './petCallingCode'; -export * from './petCountry'; \ No newline at end of file +export * from './petCountry'; diff --git a/samples/react-query/form-url-encoded/models/petCallingCode.ts b/samples/react-query/form-url-encoded/models/petCallingCode.ts index 2f2416e38..05921ec8e 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 db376cfa8..6dd7f170e 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;