From 1cd708309d0f4621e2d142a1932fd3118d473d08 Mon Sep 17 00:00:00 2001 From: Victor Gerbrands Date: Tue, 2 May 2023 16:52:39 +0200 Subject: [PATCH] fix: type fixes --- lib/medusa/index.ts | 63 +++++++++++++++++++++++++++++++-------------- lib/medusa/types.ts | 15 +++++++++-- 2 files changed, 57 insertions(+), 21 deletions(-) diff --git a/lib/medusa/index.ts b/lib/medusa/index.ts index 4b981a629..01deb669a 100644 --- a/lib/medusa/index.ts +++ b/lib/medusa/index.ts @@ -1,5 +1,12 @@ import { isMedusaError } from 'lib/type-guards'; -import { Cart, MedusaCart, MedusaProduct, Product, ProductCollection } from './types'; +import { + Cart, + MedusaCart, + MedusaProduct, + MedusaProductCollection, + Product, + ProductCollection +} from './types'; // const endpoint = `${process.env.MEDUSA_BACKEND_API!}`; const endpoint = `http://localhost:9000/store`; @@ -68,11 +75,31 @@ const reshapeProduct = (product: MedusaProduct): Product => { currencyCode: product.variants?.[0]?.prices?.[0]?.currency_code ?? '' } }; + const updatedAt = product.updated_at; return { ...product, featuredImage, - priceRange + priceRange, + updatedAt + }; +}; + +const reshapeCollection = (collection: MedusaProductCollection): ProductCollection => { + const description = collection.metadata?.description?.toString() ?? ''; + const seo = { + title: collection?.metadata?.seo_title?.toString() ?? '', + description: collection?.metadata?.seo_description?.toString() ?? '' + }; + const path = `/${collection.handle}`; + const updatedAt = collection.updated_at; + + return { + ...collection, + description, + seo, + path, + updatedAt }; }; @@ -141,26 +168,21 @@ export async function getCollectionProducts(handle: string): Promise return []; } - return res.body.products.map((product: Product) => reshapeProduct(product)); + const products: Product[] = res.body.products.map((product: MedusaProduct) => + reshapeProduct(product) + ); + + return products; } export async function getCollections(): Promise { - const collections = [ - { - handle: '', - title: 'All', - description: 'All products', - seo: { - title: 'All', - description: 'All products' - }, - path: '/search', - updatedAt: new Date().toISOString() - } - ]; + const res = await medusaRequest('GET', '/collections'); - // return collections; - return []; + const collections: ProductCollection[] = res.body.collections.map( + (collection: MedusaProductCollection) => reshapeCollection(collection) + ); + + return collections; } export async function getProduct(handle: string): Promise { @@ -176,5 +198,8 @@ export async function getProducts({ sortKey?: string; }): Promise { const res = await medusaRequest('get', `/products?q=${query}&limit=20`); - return res.body.products; + const products: Product[] = res.body.products.map((product: MedusaProduct) => + reshapeProduct(product) + ); + return products; } diff --git a/lib/medusa/types.ts b/lib/medusa/types.ts index 123166959..afd3d20b7 100644 --- a/lib/medusa/types.ts +++ b/lib/medusa/types.ts @@ -1,4 +1,4 @@ -export type ProductCollection = { +export type MedusaProductCollection = { id: string; title: string; handle: string | null; @@ -9,6 +9,16 @@ export type ProductCollection = { metadata?: Record | null; }; +export type ProductCollection = MedusaProductCollection & { + description?: string; + seo?: { + title?: string; + description?: string; + }; + path: string; + updatedAt: string; +}; + export type MedusaProduct = { id: string; title: string; @@ -51,6 +61,7 @@ export type Product = MedusaProduct & { currencyCode: string; }; }; + updatedAt: Date; }; export type Image = { @@ -246,7 +257,7 @@ export type MedusaCart = { items: []; }; -export type Cart = Omit & { +export type Cart = Partial & { lines: []; totalQuantity: number; };