From 0d4c7e33fc76278273a565b46ae62c137ceb0b30 Mon Sep 17 00:00:00 2001 From: Victor Gerbrands Date: Thu, 11 May 2023 15:39:54 +0200 Subject: [PATCH] feat: add revalidate and pub key to fetch --- lib/medusa/index.ts | 28 ++++++---------------------- 1 file changed, 6 insertions(+), 22 deletions(-) diff --git a/lib/medusa/index.ts b/lib/medusa/index.ts index 09a74966d..2d03127fb 100644 --- a/lib/medusa/index.ts +++ b/lib/medusa/index.ts @@ -20,6 +20,8 @@ import { } from './types'; const ENDPOINT = process.env.NEXT_PUBLIC_MEDUSA_BACKEND_API; +const MEDUSA_API_KEY = process.env.MEDUSA_API_KEY ?? ''; +const REVALIDATE_WINDOW = parseInt(process.env.REVALIDATE_WINDOW ?? `${60 * 15}`); // 15 minutes export default async function medusaRequest( method: string, @@ -29,8 +31,10 @@ export default async function medusaRequest( const options: RequestInit = { method, headers: { - 'Content-Type': 'application/json' - } + 'Content-Type': 'application/json', + 'x-publishable-key': MEDUSA_API_KEY + }, + next: { revalidate: REVALIDATE_WINDOW } }; if (payload) { @@ -244,26 +248,6 @@ const reshapeProductVariant = ( }; }; -const reshapeCollection = (collection: MedusaProductCollection): ProductCollection => { - const description = collection.description || collection.metadata?.description?.toString() || ''; - const seo = { - title: collection?.metadata?.seo_title?.toString() || collection.title || '', - description: collection?.metadata?.seo_description?.toString() || collection.description || '' - }; - const path = `/${collection.handle}`; - const updatedAt = collection.updated_at; - const title = collection.name; - - return { - ...collection, - description, - seo, - title, - path, - updatedAt - }; -}; - const reshapeCategory = (category: ProductCategory): ProductCollection => { const description = category.description || category.metadata?.description?.toString() || ''; const seo = {