Skip to content

Commit

Permalink
suggestions + improve error objs. type safety
Browse files Browse the repository at this point in the history
  • Loading branch information
rithviknishad committed Dec 16, 2024
1 parent f6c40d2 commit 9ca80e0
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 23 deletions.
17 changes: 7 additions & 10 deletions src/Utils/request/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,19 @@ 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>;
declare module "@tanstack/react-query" {
interface Register {
defaultError: QueryError;
}
}

async function queryRequest<TData, TBody>(
{ path, method, noAuth }: Route<TData, TBody>,
options?: ExtendedQueryOptions<TBody>,
options?: QueryOptions<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 headers = makeHeaders(noAuth ?? false, options?.headers);

const fetchOptions: RequestInit = {
method,
Expand Down Expand Up @@ -59,7 +56,7 @@ async function queryRequest<TData, TBody>(
*/
export default function query<TData, TBody>(
route: Route<TData, TBody>,
options?: ExtendedQueryOptions<TBody>,
options?: QueryOptions<TBody>,
) {
return ({ signal }: { signal: AbortSignal }) => {
return queryRequest(route, { ...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
17 changes: 4 additions & 13 deletions src/Utils/request/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,33 +50,24 @@ const ensurePathNotMissingReplacements = (path: string) => {
}
};

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

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 9ca80e0

Please sign in to comment.