mirror of
https://github.com/vercel/commerce.git
synced 2025-05-15 05:56:59 +00:00
feat(poc): add log for api client
This commit is contained in:
parent
47291853e3
commit
67e5c46b9b
@ -7,6 +7,10 @@ export default async function Cart() {
|
|||||||
let cartIdUpdated = true;
|
let cartIdUpdated = true;
|
||||||
const cart = await getCart();
|
const cart = await getCart();
|
||||||
|
|
||||||
|
if (!cart) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
if (cartId !== cart.id) {
|
if (cartId !== cart.id) {
|
||||||
cartIdUpdated = true;
|
cartIdUpdated = true;
|
||||||
}
|
}
|
||||||
|
@ -73,17 +73,19 @@ export async function getPage(handle: string | []): Promise<Page | undefined> {
|
|||||||
export async function getFirstSeoUrlElement(
|
export async function getFirstSeoUrlElement(
|
||||||
handle: string
|
handle: string
|
||||||
): Promise<ApiSchemas['SeoUrl'] | undefined> {
|
): Promise<ApiSchemas['SeoUrl'] | undefined> {
|
||||||
const resSeoUrl = await requestSeoUrl(handle);
|
const seoURL = await requestSeoUrl(handle);
|
||||||
if (resSeoUrl.elements && resSeoUrl.elements.length > 0 && resSeoUrl.elements[0]) {
|
if (seoURL && seoURL.elements && seoURL.elements.length > 0 && seoURL.elements[0]) {
|
||||||
return resSeoUrl.elements[0];
|
return seoURL.elements[0];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getFirstProduct(productId: string): Promise<ExtendedProduct | undefined> {
|
export async function getFirstProduct(productId: string): Promise<ExtendedProduct | undefined> {
|
||||||
const productCriteria = getDefaultProductCriteria(productId);
|
const productCriteria = getDefaultProductCriteria(productId);
|
||||||
const res: ExtendedProductListingResult = await requestProductsCollection(productCriteria);
|
const listing: ExtendedProductListingResult | undefined = await requestProductsCollection(
|
||||||
if (res.elements && res.elements.length > 0 && res.elements[0]) {
|
productCriteria
|
||||||
return res.elements[0];
|
);
|
||||||
|
if (listing && listing.elements && listing.elements.length > 0 && listing.elements[0]) {
|
||||||
|
return listing.elements[0];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -115,10 +117,12 @@ export async function getSearchCollectionProducts(params?: {
|
|||||||
const sorting = getSortingCriteria(params?.sortKey, params?.reverse);
|
const sorting = getSortingCriteria(params?.sortKey, params?.reverse);
|
||||||
const searchCriteria = { ...criteria, ...sorting };
|
const searchCriteria = { ...criteria, ...sorting };
|
||||||
|
|
||||||
const res = await requestSearchCollectionProducts(searchCriteria);
|
const search = await requestSearchCollectionProducts(searchCriteria);
|
||||||
res.elements = await changeVariantUrlToParentUrl(res);
|
if (search) {
|
||||||
|
search.elements = await changeVariantUrlToParentUrl(search);
|
||||||
|
}
|
||||||
|
|
||||||
return res ? transformProducts(res) : [];
|
return search ? transformProducts(search) : [];
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function changeVariantUrlToParentUrl(
|
export async function changeVariantUrlToParentUrl(
|
||||||
@ -151,7 +155,7 @@ export async function getCollectionProducts(params?: {
|
|||||||
categoryId?: string;
|
categoryId?: string;
|
||||||
defaultSearchCriteria?: Partial<ProductListingCriteria>;
|
defaultSearchCriteria?: Partial<ProductListingCriteria>;
|
||||||
}): Promise<{ products: Product[]; total: number; limit: number }> {
|
}): Promise<{ products: Product[]; total: number; limit: number }> {
|
||||||
let res;
|
let products;
|
||||||
let category = params?.categoryId;
|
let category = params?.categoryId;
|
||||||
const collectionName = transformHandle(params?.collection ?? '');
|
const collectionName = transformHandle(params?.collection ?? '');
|
||||||
const sorting = getSortingCriteria(params?.sortKey, params?.reverse);
|
const sorting = getSortingCriteria(params?.sortKey, params?.reverse);
|
||||||
@ -174,23 +178,27 @@ export async function getCollectionProducts(params?: {
|
|||||||
? getDefaultProductsCriteria(params?.page)
|
? getDefaultProductsCriteria(params?.page)
|
||||||
: params?.defaultSearchCriteria;
|
: params?.defaultSearchCriteria;
|
||||||
const productsCriteria = { ...criteria, ...sorting };
|
const productsCriteria = { ...criteria, ...sorting };
|
||||||
res = await requestCategoryProductsCollection(category, productsCriteria);
|
products = await requestCategoryProductsCollection(category, productsCriteria);
|
||||||
res.elements = await changeVariantUrlToParentUrl(res);
|
if (products) {
|
||||||
|
products.elements = await changeVariantUrlToParentUrl(products);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return res
|
return products
|
||||||
? { products: transformProducts(res), total: res.total ?? 0, limit: res.limit ?? 0 }
|
? {
|
||||||
|
products: transformProducts(products),
|
||||||
|
total: products.total ?? 0,
|
||||||
|
limit: products.limit ?? 0
|
||||||
|
}
|
||||||
: { products: [], total: 0, limit: 0 };
|
: { products: [], total: 0, limit: 0 };
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getCategory(
|
export async function getCategory(
|
||||||
seoUrl: ApiSchemas['SeoUrl'],
|
seoUrl: ApiSchemas['SeoUrl'],
|
||||||
cms: boolean = false
|
cms: boolean = false
|
||||||
): Promise<ExtendedCategory> {
|
): Promise<ExtendedCategory | undefined> {
|
||||||
const criteria = cms ? getDefaultCategoryWithCmsCriteria() : getDefaultCategoryCriteria();
|
const criteria = cms ? getDefaultCategoryWithCmsCriteria() : getDefaultCategoryCriteria();
|
||||||
const resCategory = await requestCategory(seoUrl.foreignKey, criteria);
|
return await requestCategory(seoUrl.foreignKey, criteria);
|
||||||
|
|
||||||
return resCategory;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// This function is only used for generateMetadata at app/search/(collection)/[...collection]/page.tsx
|
// This function is only used for generateMetadata at app/search/(collection)/[...collection]/page.tsx
|
||||||
@ -198,23 +206,25 @@ export async function getCollection(handle: string | []) {
|
|||||||
const collectionName = transformHandle(handle);
|
const collectionName = transformHandle(handle);
|
||||||
const seoUrlElement = await getFirstSeoUrlElement(collectionName);
|
const seoUrlElement = await getFirstSeoUrlElement(collectionName);
|
||||||
if (seoUrlElement) {
|
if (seoUrlElement) {
|
||||||
const resCategory = await getCategory(seoUrlElement);
|
const category = await getCategory(seoUrlElement);
|
||||||
const path = seoUrlElement.seoPathInfo ?? '';
|
const path = seoUrlElement.seoPathInfo ?? '';
|
||||||
const collection = transformCollection(seoUrlElement, resCategory);
|
if (category) {
|
||||||
|
const collection = transformCollection(seoUrlElement, category);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
...collection,
|
...collection,
|
||||||
path: `/search/${path}`
|
path: `/search/${path}`
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getProductSeoUrls() {
|
export async function getProductSeoUrls() {
|
||||||
const productSeoUrls: { path: string; updatedAt: string }[] = [];
|
const productSeoUrls: { path: string; updatedAt: string }[] = [];
|
||||||
const res = await requestSeoUrls('frontend.detail.page');
|
const seoUrls = await requestSeoUrls('frontend.detail.page');
|
||||||
|
|
||||||
if (res.elements && res.elements.length > 0) {
|
if (seoUrls && seoUrls.elements && seoUrls.elements.length > 0) {
|
||||||
res.elements.map((item) =>
|
seoUrls.elements.map((item) =>
|
||||||
productSeoUrls.push({ path: item.seoPathInfo, updatedAt: item.updatedAt ?? item.createdAt })
|
productSeoUrls.push({ path: item.seoPathInfo, updatedAt: item.updatedAt ?? item.createdAt })
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -257,9 +267,9 @@ export async function getProductRecommendations(productId: string): Promise<Prod
|
|||||||
return products ? transformProducts(products) : [];
|
return products ? transformProducts(products) : [];
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getCart(): Promise<Cart> {
|
export async function getCart(): Promise<Cart | undefined> {
|
||||||
const cartData = await requestCart();
|
const cartData = await requestCart();
|
||||||
|
if (cartData) {
|
||||||
// @ToDo: should be moved to transformCart function
|
// @ToDo: should be moved to transformCart function
|
||||||
const cart: Cart = {
|
const cart: Cart = {
|
||||||
checkoutUrl: 'https://frontends-demo.vercel.app',
|
checkoutUrl: 'https://frontends-demo.vercel.app',
|
||||||
@ -325,6 +335,6 @@ export async function getCart(): Promise<Cart> {
|
|||||||
})) || [],
|
})) || [],
|
||||||
totalQuantity: cartData.lineItems?.length || 0
|
totalQuantity: cartData.lineItems?.length || 0
|
||||||
};
|
};
|
||||||
|
|
||||||
return cart;
|
return cart;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user