Skip to content

Commit

Permalink
feat: add revalidate and pub key to fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
VariableVic committed May 11, 2023
1 parent 2f4a6d2 commit 0d4c7e3
Showing 1 changed file with 6 additions and 22 deletions.
28 changes: 6 additions & 22 deletions lib/medusa/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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) {
Expand Down Expand Up @@ -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 = {
Expand Down

0 comments on commit 0d4c7e3

Please sign in to comment.