Skip to content

Commit

Permalink
fix(override): add deep merge with operations and tag options (#1397)
Browse files Browse the repository at this point in the history
  • Loading branch information
anymaniax authored May 23, 2024
1 parent 64162f5 commit ce27a26
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 21 deletions.
9 changes: 4 additions & 5 deletions packages/core/src/generators/verbs-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
12 changes: 6 additions & 6 deletions packages/core/src/utils/merge-deep.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const isObject = (obj: unknown) => obj && typeof obj === 'object';

export function mergeDeep<T extends Record<string, any>>(
source: T,
target: T,
): T {
export function mergeDeep<
T extends Record<string, any>,
U extends Record<string, any>,
>(source: T, target: U): T & U {
if (!isObject(target) || !isObject(source)) {
return source;
return source as T & U;
}

return Object.entries(target).reduce(
Expand All @@ -23,5 +23,5 @@ export function mergeDeep<T extends Record<string, any>>(
return acc;
},
Object.assign({}, source),
);
) as T & U;
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,23 @@ type Awaited<O> = O extends AwaitedInput<infer T> ? 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<PetsArray>({
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;
};

Expand Down Expand Up @@ -89,8 +97,12 @@ export const getListPetsInfiniteQueryOptions = <
Awaited<ReturnType<typeof listPets>>,
QueryKey,
ListPetsParams['limit']
> = ({ pageParam }) =>
listPets({ ...params, limit: pageParam || params?.['limit'] }, version);
> = ({ signal, pageParam }) =>
listPets(
{ ...params, limit: pageParam || params?.['limit'] },
version,
signal,
);

return {
queryKey,
Expand Down Expand Up @@ -170,8 +182,9 @@ export const getListPetsQueryOptions = <
const queryKey =
queryOptions?.queryKey ?? getListPetsQueryKey(params, version);

const queryFn: QueryFunction<Awaited<ReturnType<typeof listPets>>> = () =>
listPets(params, version);
const queryFn: QueryFunction<Awaited<ReturnType<typeof listPets>>> = ({
signal,
}) => listPets(params, version, signal);

return {
queryKey,
Expand Down Expand Up @@ -235,8 +248,9 @@ export const getListPetsSuspenseQueryOptions = <
const queryKey =
queryOptions?.queryKey ?? getListPetsQueryKey(params, version);

const queryFn: QueryFunction<Awaited<ReturnType<typeof listPets>>> = () =>
listPets(params, version);
const queryFn: QueryFunction<Awaited<ReturnType<typeof listPets>>> = ({
signal,
}) => listPets(params, version, signal);

return {
queryKey,
Expand Down Expand Up @@ -321,8 +335,12 @@ export const getListPetsSuspenseInfiniteQueryOptions = <
Awaited<ReturnType<typeof listPets>>,
QueryKey,
ListPetsParams['limit']
> = ({ pageParam }) =>
listPets({ ...params, limit: pageParam || params?.['limit'] }, version);
> = ({ signal, pageParam }) =>
listPets(
{ ...params, limit: pageParam || params?.['limit'] },
version,
signal,
);

return {
queryKey,
Expand Down

0 comments on commit ce27a26

Please sign in to comment.