aboutsummaryrefslogtreecommitdiff
path: root/utils/request/index.ts
diff options
context:
space:
mode:
authorFactiven <[email protected]>2023-12-24 13:03:54 +0700
committerFactiven <[email protected]>2023-12-24 13:03:54 +0700
commit50a0f0240d7fef133eb5acc1bea2b1168b08e9db (patch)
tree307e09e505580415a58d64b5fc3580e9235869f1 /utils/request/index.ts
parentUpdate README.md (#104) (diff)
downloadmoopa-50a0f0240d7fef133eb5acc1bea2b1168b08e9db.tar.xz
moopa-50a0f0240d7fef133eb5acc1bea2b1168b08e9db.zip
migrate to typescript
Diffstat (limited to 'utils/request/index.ts')
-rw-r--r--utils/request/index.ts111
1 files changed, 111 insertions, 0 deletions
diff --git a/utils/request/index.ts b/utils/request/index.ts
new file mode 100644
index 0000000..854ef2b
--- /dev/null
+++ b/utils/request/index.ts
@@ -0,0 +1,111 @@
+import axios, { AxiosRequestConfig } from "axios";
+import { getSession } from "next-auth/react";
+import { toast } from "sonner";
+
+function isAnilist(url: string | undefined): boolean {
+ return url?.includes("anilist.co") ?? false;
+}
+
+interface RequestOption extends RequestInit {
+ headers?: {
+ "Content-Type"?: string;
+ Authorization?: string;
+ };
+}
+
+const pls = {
+ // GET request handler
+ async get(
+ url: string,
+ options?: AxiosRequestConfig,
+ ctx?: any
+ ): Promise<any> {
+ try {
+ const session: any | null = isAnilist(url) ? await getSession(ctx) : null;
+ const controller = new AbortController();
+ const signal = controller.signal;
+
+ const response = await axios.get(url, { ...options, signal });
+ return response.data;
+ } catch (error: any) {
+ handleError(error);
+ // throw error;
+ }
+ },
+
+ // POST request handler
+ async post(url: string, options: RequestOption, ctx?: any): Promise<any> {
+ try {
+ const session: any | null = await getSession(ctx);
+ const accessToken: string | undefined = session?.user?.token;
+
+ const controller = new AbortController();
+ const signal = controller.signal;
+
+ const response = await fetch(url, {
+ method: "POST",
+ headers: {
+ "Content-Type": "application/json",
+ ...(accessToken &&
+ isAnilist(url) && { Authorization: `Bearer ${accessToken}` }),
+ },
+ ...options,
+ signal,
+ });
+
+ const data = await response.json();
+ return [data, session];
+ } catch (error: any) {
+ handleError(error);
+ // throw error;
+ }
+ },
+};
+
+function handleError(error: {
+ response: { status: any; data: any };
+ message: any;
+}) {
+ console.log(error);
+ if (error.response) {
+ const { status, data } = error.response;
+ switch (status) {
+ case 400:
+ toast.error("400 Bad request", {
+ description: data?.message || error.message,
+ });
+ break;
+ case 401:
+ toast.error("401 Unauthorized", {
+ description: data?.message || error.message,
+ });
+ break;
+ case 403:
+ toast.error("403 Forbidden", {
+ description: data?.message || error.message,
+ });
+ break;
+ case 404:
+ toast.error(`Resource not found - 404`, {
+ description: data?.message || error.message,
+ });
+ break;
+ case 500:
+ toast.error("500 Internal server error", {
+ description: data?.message || error.message,
+ });
+ break;
+ default:
+ toast.error("An error occurred", {
+ description: data?.message || error.message,
+ });
+ break;
+ }
+
+ if (data && data.message) {
+ console.error("Error message:", data.message);
+ }
+ }
+}
+
+export default pls;