|
@@ -1,3 +1,4 @@
|
|
|
+import { setupConfig } from "@/api/setup";
|
|
|
import { defaultLocale, redirect } from "@/i18n/routing";
|
|
|
import { Response } from "@/types";
|
|
|
import { cookies } from "next/headers";
|
|
@@ -43,20 +44,15 @@ const responseInterceptor = <T = any, R = unknown>(
|
|
|
};
|
|
|
class Server {
|
|
|
private static instance: Server;
|
|
|
- private BASE_URL: string;
|
|
|
- constructor() {
|
|
|
- console.log(`🚀🚀🚀🚀🚀-> in index.ts on 48`, globalThis.config);
|
|
|
- if (globalThis.config) {
|
|
|
- this.BASE_URL = globalThis.config.NEXT_PUBLIC_BASE_URL as string;
|
|
|
- }
|
|
|
- }
|
|
|
+ constructor() {}
|
|
|
|
|
|
async request<T, R = unknown>(options: ServerOptions): Promise<Response<T> & R> {
|
|
|
+ const config = await setupConfig();
|
|
|
options = requestInterceptor(options);
|
|
|
const { method = "GET", url, headers = {}, data = {}, body, ...other } = options;
|
|
|
const params = JSON.stringify(data);
|
|
|
try {
|
|
|
- const response = await fetch(`${this.BASE_URL}${url}`, {
|
|
|
+ const response = await fetch(`${config.NEXT_PUBLIC_BASE_URL}${url}`, {
|
|
|
method: method,
|
|
|
headers: {
|
|
|
Token: cookies().get("Token")?.value || "",
|
|
@@ -72,12 +68,12 @@ class Server {
|
|
|
console.log(`🚀🚀🚀🚀🚀-> in index.ts on 68`, error);
|
|
|
return {};
|
|
|
});
|
|
|
- return responseInterceptor<T, R>(response, options, this.BASE_URL);
|
|
|
+ return responseInterceptor<T, R>(response, options, config.NEXT_PUBLIC_BASE_URL);
|
|
|
} catch (error) {
|
|
|
console.log(
|
|
|
`🚀🚀🚀🚀🚀-> in index.ts on 67`,
|
|
|
error,
|
|
|
- `${this.BASE_URL}${options.url}`,
|
|
|
+ `${config.NEXT_PUBLIC_BASE_URL}${options.url}`,
|
|
|
JSON.stringify(options)
|
|
|
);
|
|
|
return Promise.reject(error);
|