Skip to content

Commit

Permalink
Added Custom Headers Logic to Query
Browse files Browse the repository at this point in the history
  • Loading branch information
srijantrpth committed Dec 16, 2024
1 parent 96bc802 commit f6c40d2
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/Utils/request/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,26 @@ import { getResponseBody } from "@/Utils/request/request";
import { QueryOptions, Route } from "@/Utils/request/types";
import { makeHeaders, makeUrl } from "@/Utils/request/utils";

// Extend the QueryOptions interface to include customHeaders
export interface ExtendedQueryOptions<TBody> extends QueryOptions<TBody> {
customHeaders?: Record<string, string>;
}

async function queryRequest<TData, TBody>(
{ path, method, noAuth }: Route<TData, TBody>,
options?: QueryOptions<TBody>,
options?: ExtendedQueryOptions<TBody>,
): Promise<TData> {
const url = `${careConfig.apiUrl}${makeUrl(path, options?.queryParams, options?.pathParams)}`;

// Merge customHeaders with default headers
const headers = {
...makeHeaders(noAuth ?? false),
...(options?.customHeaders || {}),
};

const fetchOptions: RequestInit = {
method,
headers: makeHeaders(noAuth ?? false),
headers,
signal: options?.signal,
};

Expand Down Expand Up @@ -48,7 +59,7 @@ async function queryRequest<TData, TBody>(
*/
export default function query<TData, TBody>(
route: Route<TData, TBody>,
options?: QueryOptions<TBody>,
options?: ExtendedQueryOptions<TBody>,
) {
return ({ signal }: { signal: AbortSignal }) => {
return queryRequest(route, { ...options, signal });
Expand Down

0 comments on commit f6c40d2

Please sign in to comment.