Skip to content

Commit

Permalink
-
Browse files Browse the repository at this point in the history
  • Loading branch information
wanghao1993 committed Sep 4, 2024
1 parent 63213ce commit f055c21
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 18 deletions.
38 changes: 21 additions & 17 deletions lib/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,29 @@ interface FetcherOptions extends RequestInit {
}

async function fetcher<T>(url: string, options: FetcherOptions): Promise<T> {
const res = await fetch(`${BASE_URL}${url}`, {
...options,
headers: {
...options.headers,
},
body: options.body,
});
try {
const res = await fetch(`${BASE_URL}${url}`, {
...options,
headers: {
...options.headers,
},
body: options.body,
});

const json = await res.json();
if (!res.ok) {
const error = new Error("An error occurred while fetching the data.");
const json = await res.json();
if (!res.ok) {
const error = new Error("An error occurred while fetching the data.");

throw error;
}
if (json.code === 200) {
return json.data;
} else {
message.error(json.message);
return Promise.reject(json.message);
throw error;
}
if (json.code === 200) {
return json.data;
} else {
message.error(json.message);
return Promise.reject(json.message);
}
} catch (e) {
return Promise.reject(e);
}
}

Expand Down
1 change: 0 additions & 1 deletion lib/fetch_utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { NextResponse } from "next/server";
import prisma from "./pg";

export const StatusMsg = {
200: "Success",
Expand Down

0 comments on commit f055c21

Please sign in to comment.