Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(override): add deep merge with operations and tag options #1397

Merged
merged 1 commit into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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