From 8b4eb72259a51d6b8d7b7bc10f2b2e76e2534f0e Mon Sep 17 00:00:00 2001 From: Najeong-Kim Date: Wed, 21 Aug 2024 00:55:31 +0900 Subject: [PATCH] fix: Add weski proxy to fix build error --- next.config.mjs | 8 -------- src/pages/api/weski/[...path].ts | 19 +++++++++++++++++++ src/shared/api/base.ts | 6 +++--- 3 files changed, 22 insertions(+), 11 deletions(-) create mode 100644 src/pages/api/weski/[...path].ts diff --git a/next.config.mjs b/next.config.mjs index ddecb5c..ee26586 100644 --- a/next.config.mjs +++ b/next.config.mjs @@ -3,14 +3,6 @@ const nextConfig = { images: { domains: ['via.placeholder.com'], }, - async rewrites() { - return [ - { - source: '/weski/api/:path*', - destination: process.env.NEXT_PUBLIC_API_URL + '/:path*', - }, - ]; - }, }; export default nextConfig; diff --git a/src/pages/api/weski/[...path].ts b/src/pages/api/weski/[...path].ts new file mode 100644 index 0000000..f07d458 --- /dev/null +++ b/src/pages/api/weski/[...path].ts @@ -0,0 +1,19 @@ +import type { NextApiRequest, NextApiResponse } from 'next'; +import { API_URL } from '@/shared/config'; + +export default async function handler(req: NextApiRequest, res: NextApiResponse) { + const { path } = req.query; + const url = `${API_URL}/${(path as string[]).join('/')}`; + + const response = await fetch(url, { + method: req.method, + headers: { + ...(req.headers as Record), + }, + body: req.method !== 'GET' ? JSON.stringify(req.body) : undefined, + }); + + const data = await response.json(); + + res.status(response.status).json(data); +} diff --git a/src/shared/api/base.ts b/src/shared/api/base.ts index c14c271..834b7a7 100644 --- a/src/shared/api/base.ts +++ b/src/shared/api/base.ts @@ -22,7 +22,7 @@ export class ApiClient { endpoint: string, queryParams?: Record ): Promise { - const url = new URL('/weski/api' + endpoint, this.baseUrl); + const url = new URL('/api/weski' + endpoint, window.location.origin); if (queryParams) { Object.entries(queryParams).forEach(([key, value]) => { @@ -44,7 +44,7 @@ export class ApiClient { endpoint: string, body: TData ): Promise { - const url = new URL('/weski/api' + endpoint, this.baseUrl); + const url = new URL('/api/weski' + endpoint, window.location.origin); const response = await fetch(url.toString(), { method: 'POST', @@ -58,4 +58,4 @@ export class ApiClient { } } -export const apiClient = new ApiClient(window.location.origin); +export const apiClient = new ApiClient('');