-
Notifications
You must be signed in to change notification settings - Fork 475
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into issues/9109/image-upload-test-suit
- Loading branch information
Showing
7 changed files
with
251 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { navigate } from "raviger"; | ||
|
||
import * as Notifications from "@/Utils/Notifications"; | ||
import { QueryError } from "@/Utils/request/queryError"; | ||
|
||
export function handleQueryError(error: Error) { | ||
if (error.name === "AbortError") { | ||
return; | ||
} | ||
|
||
if (!(error instanceof QueryError)) { | ||
Notifications.Error({ msg: error.message || "Something went wrong!" }); | ||
return; | ||
} | ||
|
||
if (error.silent) { | ||
return; | ||
} | ||
|
||
const cause = error.cause; | ||
|
||
if (isSessionExpired(cause)) { | ||
handleSessionExpired(); | ||
return; | ||
} | ||
|
||
if (isBadRequest(error)) { | ||
Notifications.BadRequest({ errs: cause }); | ||
return; | ||
} | ||
|
||
Notifications.Error({ | ||
msg: cause?.detail || "Something went wrong...!", | ||
}); | ||
} | ||
|
||
function isSessionExpired(error: QueryError["cause"]) { | ||
return ( | ||
// If Authorization header is not valid | ||
error?.code === "token_not_valid" || | ||
// If Authorization header is not provided | ||
error?.detail === "Authentication credentials were not provided." | ||
); | ||
} | ||
|
||
function handleSessionExpired() { | ||
if (!location.pathname.startsWith("/session-expired")) { | ||
navigate(`/session-expired?redirect=${window.location.href}`); | ||
} | ||
} | ||
|
||
function isBadRequest(error: QueryError) { | ||
return error.status === 400 || error.status === 406; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import careConfig from "@careConfig"; | ||
|
||
import { QueryError } from "@/Utils/request/queryError"; | ||
import { getResponseBody } from "@/Utils/request/request"; | ||
import { QueryOptions, Route } from "@/Utils/request/types"; | ||
import { makeHeaders, makeUrl } from "@/Utils/request/utils"; | ||
|
||
async function queryRequest<TData, TBody>( | ||
{ path, method, noAuth }: Route<TData, TBody>, | ||
options?: QueryOptions<TBody>, | ||
): Promise<TData> { | ||
const url = `${careConfig.apiUrl}${makeUrl(path, options?.queryParams, options?.pathParams)}`; | ||
|
||
const fetchOptions: RequestInit = { | ||
method, | ||
headers: makeHeaders(noAuth ?? false), | ||
signal: options?.signal, | ||
}; | ||
|
||
if (options?.body) { | ||
fetchOptions.body = JSON.stringify(options.body); | ||
} | ||
|
||
let res: Response; | ||
|
||
try { | ||
res = await fetch(url, fetchOptions); | ||
} catch { | ||
throw new Error("Network Error"); | ||
} | ||
|
||
const data = await getResponseBody<TData>(res); | ||
|
||
if (!res.ok) { | ||
throw new QueryError({ | ||
message: "Request Failed", | ||
status: res.status, | ||
silent: options?.silent ?? false, | ||
cause: data as unknown as Record<string, unknown>, | ||
}); | ||
} | ||
|
||
return data; | ||
} | ||
|
||
/** | ||
* Creates a TanStack Query compatible request function | ||
*/ | ||
export default function query<TData, TBody>( | ||
route: Route<TData, TBody>, | ||
options?: QueryOptions<TBody>, | ||
) { | ||
return ({ signal }: { signal: AbortSignal }) => { | ||
return queryRequest(route, { ...options, signal }); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
type QueryErrorCause = Record<string, unknown> | undefined; | ||
|
||
export class QueryError extends Error { | ||
status: number; | ||
silent: boolean; | ||
cause?: QueryErrorCause; | ||
|
||
constructor({ | ||
message, | ||
status, | ||
silent, | ||
cause, | ||
}: { | ||
message: string; | ||
status: number; | ||
silent: boolean; | ||
cause?: Record<string, unknown>; | ||
}) { | ||
super(message, { cause }); | ||
this.status = status; | ||
this.silent = silent; | ||
this.cause = cause; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters