From 7d05f584346a6e9aa15fae7785ba9253eda5e113 Mon Sep 17 00:00:00 2001 From: Grant Date: Mon, 4 Nov 2024 19:10:32 -0700 Subject: [PATCH] [dev] vite dev server proxies /api routes (simplify development) --- packages/client/src/lib/network.ts | 13 +++++-------- packages/client/src/lib/utils.ts | 4 +--- packages/client/vite.config.js | 9 +++++++++ 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/packages/client/src/lib/network.ts b/packages/client/src/lib/network.ts index 4143fef..aef93e8 100644 --- a/packages/client/src/lib/network.ts +++ b/packages/client/src/lib/network.ts @@ -46,14 +46,11 @@ type SentEventValue = EventEmitter.ArgumentMap< >[Extract]; class Network extends EventEmitter { - socket: Socket = io( - import.meta.env.VITE_API_HOST, - { - autoConnect: false, - withCredentials: true, - reconnection: true, - } - ); + socket: Socket = io("", { + autoConnect: false, + withCredentials: true, + reconnection: true, + }); private online_count = 0; private stateEvents: { [key in keyof INetworkEvents]?: SentEventValue; diff --git a/packages/client/src/lib/utils.ts b/packages/client/src/lib/utils.ts index 6f7776d..1eed9f6 100644 --- a/packages/client/src/lib/utils.ts +++ b/packages/client/src/lib/utils.ts @@ -40,9 +40,7 @@ export const api = async ( status: number; data: ({ success: true } & T) | { success: false; error: Error }; }> => { - const API_HOST = import.meta.env.VITE_API_HOST || ""; - - const req = await fetch(API_HOST + endpoint, { + const req = await fetch(endpoint, { method, credentials: "include", headers: { diff --git a/packages/client/vite.config.js b/packages/client/vite.config.js index a793441..3af1ac0 100644 --- a/packages/client/vite.config.js +++ b/packages/client/vite.config.js @@ -23,4 +23,13 @@ export default defineConfig({ define: { __COMMIT_HASH__: JSON.stringify(commitHash), }, + server: { + proxy: { + "/api": "http://localhost:3000", + "/socket.io": { + target: "ws://localhost:3000", + ws: true, + }, + }, + }, }); -- GitLab