From 09366bc84263b4f87cfdc9011f57d5b9612a61e1 Mon Sep 17 00:00:00 2001 From: spencerwooo Date: Mon, 30 Aug 2021 12:45:01 +0100 Subject: [PATCH] no header if token not present --- utils/tools.ts | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/utils/tools.ts b/utils/tools.ts index 394b716..aaf69d8 100644 --- a/utils/tools.ts +++ b/utils/tools.ts @@ -15,12 +15,15 @@ export const getBaseUrl = () => { } // Common axios fetch function -const fetcher = (url: string, token: string) => - axios - .get(url, { - headers: { 'od-protected-token': token }, - }) - .then(res => res.data) +const fetcher = (url: string, token?: string) => { + return token + ? axios + .get(url, { + headers: { 'od-protected-token': token }, + }) + .then(res => res.data) + : axios.get(url).then(res => res.data) +} /** * Use stale SWR instead of revalidating on each request. Not ideal for this scenario but have to do * if fetching serverside props from component instead of pages.