From d822fee52703a1ae835c9f4b61e7895521ec69cf Mon Sep 17 00:00:00 2001 From: Katalina Okano Date: Wed, 10 Mar 2021 00:08:21 -0500 Subject: [PATCH] remove isomorphism --- src/common/utils/isomorphicFetch.ts | 53 ----------------------------- 1 file changed, 53 deletions(-) delete mode 100644 src/common/utils/isomorphicFetch.ts diff --git a/src/common/utils/isomorphicFetch.ts b/src/common/utils/isomorphicFetch.ts deleted file mode 100644 index 00d1b78..0000000 --- a/src/common/utils/isomorphicFetch.ts +++ /dev/null @@ -1,53 +0,0 @@ -import { NextPageContext } from 'next'; -import getConfig from 'next/config'; -import nookies from 'nookies'; -import useSWR from 'swr'; - -export const getPublicURI = (context?: NextPageContext) => { - if (context?.req) { - const { publicRuntimeConfig } = getConfig(); - return publicRuntimeConfig.apiPublicURI; - } else { - return typeof localStorage !== 'undefined' && localStorage.getItem('api_uri'); - } -}; - -export const getSessionKey = (context?: NextPageContext) => { - if (context?.req) { - return nookies.get(context)['rp_session_key']; - } else { - return ( - typeof sessionStorage !== 'undefined' && sessionStorage.getItem('session_key') - ); - } -}; - -export const apiFetch = async ( - path: string, - init?: RequestInit, - context?: NextPageContext -): Promise => { - const sessionKey = getSessionKey(context); - - const authorizedInit: RequestInit = { - ...(init || {}), - headers: { - ...(init?.headers || {}), - authorization: sessionKey ? `Bearer ${sessionKey}` : '', - }, - }; - - const response = await fetch(`${getPublicURI(context)}${path}`, authorizedInit); - - if (response.status >= 400) { - const reason = (await response.json())['error']; - throw new Error(`Fetch failed: ${reason}`); - } - - return response.json() as Promise; -}; - -export const swrFetch = (path: string, context?: NextPageContext) => - useSWR(path, (url: string): Promise => apiFetch(url, undefined, context), { - revalidateOnFocus: false, - });