mirror of
https://github.com/vercel/commerce.git
synced 2025-05-15 22:16:58 +00:00
fix: type fixes
This commit is contained in:
parent
ff8098128d
commit
1cd708309d
@ -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<Product[]>
|
||||
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<ProductCollection[]> {
|
||||
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<Product | undefined> {
|
||||
@ -176,5 +198,8 @@ export async function getProducts({
|
||||
sortKey?: string;
|
||||
}): Promise<Product[]> {
|
||||
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;
|
||||
}
|
||||
|
@ -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<string, unknown> | 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<MedusaCart, 'items'> & {
|
||||
export type Cart = Partial<MedusaCart> & {
|
||||
lines: [];
|
||||
totalQuantity: number;
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user