From f7b8283decd380a4c5acfd596e9ffe8ef3c7d537 Mon Sep 17 00:00:00 2001 From: Srijan Tripathi <72181144+srijantrpth@users.noreply.github.com> Date: Tue, 17 Dec 2024 09:23:10 +0530 Subject: [PATCH] Added Custom Headers Logic to Query (#9458) --------- Co-authored-by: rithviknishad --- src/Utils/request/query.ts | 2 +- src/Utils/request/types.ts | 1 + src/Utils/request/utils.ts | 24 +++++++----------------- 3 files changed, 9 insertions(+), 18 deletions(-) diff --git a/src/Utils/request/query.ts b/src/Utils/request/query.ts index 3431f625728..53fe96878a2 100644 --- a/src/Utils/request/query.ts +++ b/src/Utils/request/query.ts @@ -13,7 +13,7 @@ async function queryRequest( const fetchOptions: RequestInit = { method, - headers: makeHeaders(noAuth ?? false), + headers: makeHeaders(noAuth ?? false, options?.headers), signal: options?.signal, }; diff --git a/src/Utils/request/types.ts b/src/Utils/request/types.ts index 20f095ae6fd..2b4be31d28d 100644 --- a/src/Utils/request/types.ts +++ b/src/Utils/request/types.ts @@ -41,6 +41,7 @@ export interface QueryOptions { body?: TBody; silent?: boolean; signal?: AbortSignal; + headers?: HeadersInit; } export interface PaginatedResponse { diff --git a/src/Utils/request/utils.ts b/src/Utils/request/utils.ts index 8fd7bc96bea..03427c17b44 100644 --- a/src/Utils/request/utils.ts +++ b/src/Utils/request/utils.ts @@ -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( options: RequestOptions, overrides: RequestOptions,