feat: add revalidate and pub key to fetch

This commit is contained in:
Victor Gerbrands 2023-05-11 15:39:54 +02:00
parent 2f4a6d2177
commit 0d4c7e33fc

View File

@ -20,6 +20,8 @@ import {
} from './types'; } from './types';
const ENDPOINT = process.env.NEXT_PUBLIC_MEDUSA_BACKEND_API; 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( export default async function medusaRequest(
method: string, method: string,
@ -29,8 +31,10 @@ export default async function medusaRequest(
const options: RequestInit = { const options: RequestInit = {
method, method,
headers: { headers: {
'Content-Type': 'application/json' 'Content-Type': 'application/json',
} 'x-publishable-key': MEDUSA_API_KEY
},
next: { revalidate: REVALIDATE_WINDOW }
}; };
if (payload) { 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 reshapeCategory = (category: ProductCategory): ProductCollection => {
const description = category.description || category.metadata?.description?.toString() || ''; const description = category.description || category.metadata?.description?.toString() || '';
const seo = { const seo = {