Skip to content

Commit

Permalink
Pass queryArguments as part of context
Browse files Browse the repository at this point in the history
  • Loading branch information
Anahkiasen committed Aug 11, 2023
1 parent c08fcac commit d63ebf4
Showing 1 changed file with 10 additions and 19 deletions.
29 changes: 10 additions & 19 deletions src/hooks/api/authenticated-query-v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ type GenerateQueryKeyArguments = {

type GeneratedQueryKey = readonly [QueryKey, QueryArguments];

type AuthenticatedQueryFunctionContext =
type AuthenticatedQueryFunctionContext<TQueryArguments = unknown> =
QueryFunctionContext<GeneratedQueryKey> & {
headers: HeadersInit;
queryArguments?: TQueryArguments;
};

type ServerSideOptions = {
Expand All @@ -41,8 +42,11 @@ type PrefetchAuthenticatedQueryOptions<TQueryFnData> = {
} & ServerSideOptions &
FetchQueryOptions<TQueryFnData, FetchError, TQueryFnData, QueryKey>;

type UseAuthenticatedQueryOptions<TQueryFnData> = {
queryArguments?: QueryArguments;
type UseAuthenticatedQueryOptions<
TQueryFnData,
TQueryArguments = QueryArguments,
> = {
queryArguments?: TQueryArguments;
} & Omit<
UseQueryOptions<TQueryFnData, FetchError, TQueryFnData, QueryKey>,
'queryFn'
Expand Down Expand Up @@ -75,31 +79,18 @@ const getPreparedOptions = <TQueryFnData = unknown>({
options,
isTokenPresent,
headers,
}: GetPreparedOptionsArguments<TQueryFnData>): UseQueryOptions<
TQueryFnData,
FetchError,
TQueryFnData,
GeneratedQueryKey
> => {
}: GetPreparedOptionsArguments<TQueryFnData>) => {
const { queryKey, queryArguments, queryFn, ...restOptions } = options;
const generatedQueryKey = generateQueryKey({
queryKey,
queryArguments,
});

const queryFunctionWithHeaders = async (
context: QueryFunctionContext<GeneratedQueryKey>,
) => {
return await queryFn(
context,
// @ts-expect-error
headers,
);
};
return {
...restOptions,
queryKey: generatedQueryKey,
queryFn: (context) => queryFn({ ...context, headers }),
queryArguments,
queryFn: (context) => queryFn({ ...context, queryArguments, headers }),
...('enabled' in restOptions && {
enabled: isTokenPresent && !!restOptions.enabled,
}),
Expand Down

0 comments on commit d63ebf4

Please sign in to comment.