From a561aa495c96538a1e6b273101c166ff61d3cd25 Mon Sep 17 00:00:00 2001 From: Victor Bury Date: Thu, 23 May 2024 20:26:47 +0200 Subject: [PATCH] fix(override): add deep merge with operations and tag options --- packages/core/src/generators/verbs-options.ts | 9 ++--- packages/core/src/utils/merge-deep.ts | 12 +++--- .../petstoreFromFileSpecWithTransformer.ts | 38 ++++++++++++++----- 3 files changed, 38 insertions(+), 21 deletions(-) diff --git a/packages/core/src/generators/verbs-options.ts b/packages/core/src/generators/verbs-options.ts index 225abe4a3..73c5a79a7 100644 --- a/packages/core/src/generators/verbs-options.ts +++ b/packages/core/src/generators/verbs-options.ts @@ -74,11 +74,10 @@ const generateVerbOptions = async ({ {} as NormalizedOperationOptions, ); - const override: NormalizedOverrideOutput = { - ...output.override, - ...overrideTag, - ...overrideOperation, - }; + const override: NormalizedOverrideOutput = mergeDeep( + mergeDeep(output.override, overrideTag), + overrideOperation, + ); const overrideOperationName = overrideOperation?.operationName || output.override?.operationName; diff --git a/packages/core/src/utils/merge-deep.ts b/packages/core/src/utils/merge-deep.ts index b1aa6ee12..ccebac6f5 100644 --- a/packages/core/src/utils/merge-deep.ts +++ b/packages/core/src/utils/merge-deep.ts @@ -1,11 +1,11 @@ const isObject = (obj: unknown) => obj && typeof obj === 'object'; -export function mergeDeep>( - source: T, - target: T, -): T { +export function mergeDeep< + T extends Record, + U extends Record, +>(source: T, target: U): T & U { if (!isObject(target) || !isObject(source)) { - return source; + return source as T & U; } return Object.entries(target).reduce( @@ -23,5 +23,5 @@ export function mergeDeep>( return acc; }, Object.assign({}, source), - ); + ) as T & U; } diff --git a/samples/react-query/basic/src/api/endpoints/petstoreFromFileSpecWithTransformer.ts b/samples/react-query/basic/src/api/endpoints/petstoreFromFileSpecWithTransformer.ts index a1939dd1b..010708aed 100644 --- a/samples/react-query/basic/src/api/endpoints/petstoreFromFileSpecWithTransformer.ts +++ b/samples/react-query/basic/src/api/endpoints/petstoreFromFileSpecWithTransformer.ts @@ -46,15 +46,23 @@ type Awaited = O extends AwaitedInput ? T : never; /** * @summary List all pets */ -const listPets = (params?: ListPetsParams, version: number = 1) => { +export const listPets = ( + params?: ListPetsParams, + version: number = 1, + signal?: AbortSignal, +) => { return customInstance({ url: `/v${version}/pets`, method: 'GET', params, + signal, }); }; -const getListPetsQueryKey = (params?: ListPetsParams, version: number = 1) => { +export const getListPetsQueryKey = ( + params?: ListPetsParams, + version: number = 1, +) => { return [`/v${version}/pets`, ...(params ? [params] : [])] as const; }; @@ -89,8 +97,12 @@ export const getListPetsInfiniteQueryOptions = < Awaited>, QueryKey, ListPetsParams['limit'] - > = ({ pageParam }) => - listPets({ ...params, limit: pageParam || params?.['limit'] }, version); + > = ({ signal, pageParam }) => + listPets( + { ...params, limit: pageParam || params?.['limit'] }, + version, + signal, + ); return { queryKey, @@ -170,8 +182,9 @@ export const getListPetsQueryOptions = < const queryKey = queryOptions?.queryKey ?? getListPetsQueryKey(params, version); - const queryFn: QueryFunction>> = () => - listPets(params, version); + const queryFn: QueryFunction>> = ({ + signal, + }) => listPets(params, version, signal); return { queryKey, @@ -235,8 +248,9 @@ export const getListPetsSuspenseQueryOptions = < const queryKey = queryOptions?.queryKey ?? getListPetsQueryKey(params, version); - const queryFn: QueryFunction>> = () => - listPets(params, version); + const queryFn: QueryFunction>> = ({ + signal, + }) => listPets(params, version, signal); return { queryKey, @@ -321,8 +335,12 @@ export const getListPetsSuspenseInfiniteQueryOptions = < Awaited>, QueryKey, ListPetsParams['limit'] - > = ({ pageParam }) => - listPets({ ...params, limit: pageParam || params?.['limit'] }, version); + > = ({ signal, pageParam }) => + listPets( + { ...params, limit: pageParam || params?.['limit'] }, + version, + signal, + ); return { queryKey,