Skip to content

Commit

Permalink
Added Custom Headers Logic to Query (#9458)
Browse files Browse the repository at this point in the history

---------

Co-authored-by: rithviknishad <[email protected]>
  • Loading branch information
srijantrpth and rithviknishad authored Dec 17, 2024
1 parent 96bc802 commit f7b8283
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/Utils/request/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ async function queryRequest<TData, TBody>(

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

Expand Down
1 change: 1 addition & 0 deletions src/Utils/request/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export interface QueryOptions<TBody = unknown> {
body?: TBody;
silent?: boolean;
signal?: AbortSignal;
headers?: HeadersInit;
}

export interface PaginatedResponse<TItem> {
Expand Down
24 changes: 7 additions & 17 deletions src/Utils/request/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,33 +50,23 @@ const ensurePathNotMissingReplacements = (path: string) => {
}
};

export function makeHeaders(noAuth: boolean) {
const headers = new Headers({
"Content-Type": "application/json",
Accept: "application/json",
});
export function makeHeaders(noAuth: boolean, additionalHeaders?: HeadersInit) {
const headers = new Headers(additionalHeaders);

headers.set("Content-Type", "application/json");
headers.append("Accept", "application/json");

if (!noAuth) {
const token = getAuthorizationHeader();
const token = localStorage.getItem(LocalStorageKeys.accessToken);

if (token) {
headers.append("Authorization", token);
headers.append("Authorization", `Bearer ${token}`);
}
}

return headers;
}

export function getAuthorizationHeader() {
const bearerToken = localStorage.getItem(LocalStorageKeys.accessToken);

if (bearerToken) {
return `Bearer ${bearerToken}`;
}

return null;
}

export function mergeRequestOptions<TData>(
options: RequestOptions<TData>,
overrides: RequestOptions<TData>,
Expand Down

0 comments on commit f7b8283

Please sign in to comment.