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 { 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 = `${process.env.MEDUSA_BACKEND_API!}`;
|
||||||
const endpoint = `http://localhost:9000/store`;
|
const endpoint = `http://localhost:9000/store`;
|
||||||
@ -68,11 +75,31 @@ const reshapeProduct = (product: MedusaProduct): Product => {
|
|||||||
currencyCode: product.variants?.[0]?.prices?.[0]?.currency_code ?? ''
|
currencyCode: product.variants?.[0]?.prices?.[0]?.currency_code ?? ''
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
const updatedAt = product.updated_at;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
...product,
|
...product,
|
||||||
featuredImage,
|
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 [];
|
||||||
}
|
}
|
||||||
|
|
||||||
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[]> {
|
export async function getCollections(): Promise<ProductCollection[]> {
|
||||||
const collections = [
|
const res = await medusaRequest('GET', '/collections');
|
||||||
{
|
|
||||||
handle: '',
|
|
||||||
title: 'All',
|
|
||||||
description: 'All products',
|
|
||||||
seo: {
|
|
||||||
title: 'All',
|
|
||||||
description: 'All products'
|
|
||||||
},
|
|
||||||
path: '/search',
|
|
||||||
updatedAt: new Date().toISOString()
|
|
||||||
}
|
|
||||||
];
|
|
||||||
|
|
||||||
// return collections;
|
const collections: ProductCollection[] = res.body.collections.map(
|
||||||
return [];
|
(collection: MedusaProductCollection) => reshapeCollection(collection)
|
||||||
|
);
|
||||||
|
|
||||||
|
return collections;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getProduct(handle: string): Promise<Product | undefined> {
|
export async function getProduct(handle: string): Promise<Product | undefined> {
|
||||||
@ -176,5 +198,8 @@ export async function getProducts({
|
|||||||
sortKey?: string;
|
sortKey?: string;
|
||||||
}): Promise<Product[]> {
|
}): Promise<Product[]> {
|
||||||
const res = await medusaRequest('get', `/products?q=${query}&limit=20`);
|
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;
|
id: string;
|
||||||
title: string;
|
title: string;
|
||||||
handle: string | null;
|
handle: string | null;
|
||||||
@ -9,6 +9,16 @@ export type ProductCollection = {
|
|||||||
metadata?: Record<string, unknown> | null;
|
metadata?: Record<string, unknown> | null;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export type ProductCollection = MedusaProductCollection & {
|
||||||
|
description?: string;
|
||||||
|
seo?: {
|
||||||
|
title?: string;
|
||||||
|
description?: string;
|
||||||
|
};
|
||||||
|
path: string;
|
||||||
|
updatedAt: string;
|
||||||
|
};
|
||||||
|
|
||||||
export type MedusaProduct = {
|
export type MedusaProduct = {
|
||||||
id: string;
|
id: string;
|
||||||
title: string;
|
title: string;
|
||||||
@ -51,6 +61,7 @@ export type Product = MedusaProduct & {
|
|||||||
currencyCode: string;
|
currencyCode: string;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
updatedAt: Date;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type Image = {
|
export type Image = {
|
||||||
@ -246,7 +257,7 @@ export type MedusaCart = {
|
|||||||
items: [];
|
items: [];
|
||||||
};
|
};
|
||||||
|
|
||||||
export type Cart = Omit<MedusaCart, 'items'> & {
|
export type Cart = Partial<MedusaCart> & {
|
||||||
lines: [];
|
lines: [];
|
||||||
totalQuantity: number;
|
totalQuantity: number;
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user