From bc9866f7fbe324c3b62a33e286f690d38ae2ee20 Mon Sep 17 00:00:00 2001 From: Alessandro Casazza Date: Fri, 13 May 2022 10:53:58 +0200 Subject: [PATCH] fix: Remove unused code and add hosted checkout url --- .../api/endpoints/checkout/get-checkout.ts | 20 +- .../commercelayer/src/api/utils/getPrices.ts | 4 +- .../src/api/utils/normalizeLineItems.ts | 22 +- packages/commercelayer/src/cart/use-cart.tsx | 7 +- packages/commercelayer/src/data.json | 602 ------------------ 5 files changed, 33 insertions(+), 622 deletions(-) delete mode 100644 packages/commercelayer/src/data.json diff --git a/packages/commercelayer/src/api/endpoints/checkout/get-checkout.ts b/packages/commercelayer/src/api/endpoints/checkout/get-checkout.ts index ed6bde659..e6941797d 100644 --- a/packages/commercelayer/src/api/endpoints/checkout/get-checkout.ts +++ b/packages/commercelayer/src/api/endpoints/checkout/get-checkout.ts @@ -1,6 +1,6 @@ import type { CheckoutEndpoint } from '.' -import getCredentials from '../../utils/getCredentials' -import { Order } from '@commercelayer/sdk' +import getCredentials, { getOrganizationSlug } from '../../utils/getCredentials' +import CLSdk from '@commercelayer/sdk' const getCheckout: CheckoutEndpoint['handlers']['getCheckout'] = async ({ req, @@ -18,11 +18,17 @@ const getCheckout: CheckoutEndpoint['handlers']['getCheckout'] = async ({ const { ENDPOINT } = getCredentials() if (orderId && accessToken) { - const clOrder = await Order.withCredentials({ endpoint, accessToken }) - .includes('lineItems') - .find(orderId as string, { rawResponse: true }) - const checkoutUrl = clOrder.data.attributes.checkout_url - + const organization = getOrganizationSlug(ENDPOINT).organization + const sdk = CLSdk({ + accessToken, + organization, + }) + const order = await sdk.orders.retrieve(orderId as string, { + fields: ['checkout_url'], + }) + const checkoutUrl = order?.checkout_url + ? order?.checkout_url + : `https://${organization}.checkout.commercelayer.app/${order?.id}` if (checkoutUrl) { res.redirect(`${checkoutUrl}?accessToken=${accessToken}`) } else { diff --git a/packages/commercelayer/src/api/utils/getPrices.ts b/packages/commercelayer/src/api/utils/getPrices.ts index 594488f5e..b51fa4e3d 100644 --- a/packages/commercelayer/src/api/utils/getPrices.ts +++ b/packages/commercelayer/src/api/utils/getPrices.ts @@ -1,9 +1,9 @@ import { Price, CommerceLayerClient } from '@commercelayer/sdk' import { Product } from '@vercel/commerce/types/product' -import { DeepPartialArray } from './contentfulApi' +import type { Products } from './getContentData' type GetPricesArgs = { - products?: DeepPartialArray + products?: Products sdk: CommerceLayerClient } diff --git a/packages/commercelayer/src/api/utils/normalizeLineItems.ts b/packages/commercelayer/src/api/utils/normalizeLineItems.ts index b95a3bf37..0bc02e437 100644 --- a/packages/commercelayer/src/api/utils/normalizeLineItems.ts +++ b/packages/commercelayer/src/api/utils/normalizeLineItems.ts @@ -1,7 +1,12 @@ import { LineItem } from '@commercelayer/sdk' -import data from '../../data.json' +import type { Products } from './getContentData' -export default function normalizeLineItems(lineItems: LineItem[]) { +type Params = { + lineItems: LineItem[] + products: Products +} + +export default function normalizeLineItems({ lineItems, products }: Params) { return lineItems .filter((l) => { return ( @@ -10,11 +15,13 @@ export default function normalizeLineItems(lineItems: LineItem[]) { }) .map((lineItem) => { const id = lineItem.id - const products = data.products + const [product] = products.filter((p) => p.id === lineItem.reference) + const path = product?.slug + const [image] = product?.images return { id, name: lineItem.name, - path: products.find((p) => p.id === lineItem.reference)?.slug, + path, productId: lineItem.reference, variantId: lineItem.reference, quantity: lineItem.quantity, @@ -24,12 +31,7 @@ export default function normalizeLineItems(lineItems: LineItem[]) { name: lineItem.name, sku: lineItem.sku_code, price: lineItem.unit_amount_float, - image: { - url: `https://data.commercelayer.app/vercel-provider/${lineItem.reference}_FLAT.png`, - altText: lineItem.name, - width: 1000, - height: 1000, - }, + image, }, } }) diff --git a/packages/commercelayer/src/cart/use-cart.tsx b/packages/commercelayer/src/cart/use-cart.tsx index 865c9104f..c148a7934 100644 --- a/packages/commercelayer/src/cart/use-cart.tsx +++ b/packages/commercelayer/src/cart/use-cart.tsx @@ -6,6 +6,7 @@ import getCredentials, { getOrganizationSlug, } from '../api/utils/getCredentials' import normalizeLineItems from '../api/utils/normalizeLineItems' +import getContentData from '../api/utils/getContentData' export default useCart as UseCart @@ -25,8 +26,12 @@ export const handler: SWRHook = { const order = await sdk.orders.retrieve(id, { include: ['line_items'] }) const orderStatus = order.status if (orderStatus && ['pending', 'draft'].includes(orderStatus)) { + const contentData = await getContentData() const lineItems = order.line_items - ? normalizeLineItems(order.line_items) + ? normalizeLineItems({ + lineItems: order.line_items, + products: contentData, + }) : [] return { id, diff --git a/packages/commercelayer/src/data.json b/packages/commercelayer/src/data.json deleted file mode 100644 index 85179e129..000000000 --- a/packages/commercelayer/src/data.json +++ /dev/null @@ -1,602 +0,0 @@ -{ - "products": [ - { - "id": "SHIRTWLS000000FFFFFF", - "name": "Black Women Long Sleeve Shirt", - "brandId": "Commerce Layer", - "path": "/black-women-long-sleeve-shirt", - "slug": "black-women-long-sleeve-shirt", - "categoryId": "clothings", - "price": { "value": 50, "currencyCode": "USD" }, - "descriptionHtml": "

This relaxed long sleeve tee combines the best of both worlds, proving that cozy can also be chic. 65/35 polyester/viscose. Drop shoulder. Curved bottom hem.

", - "images": [ - { - "url": "https://data.commercelayer.app/vercel-provider/SHIRTWLS000000FFFFFF_FLAT.png", - "altText": " A Black Women Long Sleeve Shirt", - "width": 1000, - "height": 1000 - }, - { - "url": "https://data.commercelayer.app/vercel-provider/SHIRTWLS000000FFFFFF_01.png", - "altText": " A Black Women Long Sleeve Shirt", - "width": 1000, - "height": 1000 - }, - { - "url": "https://data.commercelayer.app/vercel-provider/SHIRTWLS000000FFFFFF_02.png", - "altText": " A Black Women Long Sleeve Shirt", - "width": 1000, - "height": 1000 - } - ], - "variants": [ - { - "id": "SHIRTWLS000000FFFFFFXSXX", - "options": [ - { - "__typename": "MultipleChoiceOption", - "id": "SHIRTWLS000000FFFFFFXSXX", - "displayName": "Size", - "values": [ - { - "label": "XS" - } - ] - } - ] - }, - { - "id": "SHIRTWLS000000FFFFFFSXXX", - "options": [ - { - "__typename": "MultipleChoiceOption", - "id": "SHIRTWLS000000FFFFFFSXXX", - "displayName": "Size", - "values": [ - { - "label": "S" - } - ] - } - ] - }, - { - "id": "SHIRTWLS000000FFFFFFMXXX", - "options": [ - { - "__typename": "MultipleChoiceOption", - "id": "SHIRTWLS000000FFFFFFMXXX", - "displayName": "Size", - "values": [ - { - "label": "M" - } - ] - } - ] - }, - { - "id": "SHIRTWLS000000FFFFFFLXXX", - "options": [ - { - "__typename": "MultipleChoiceOption", - "id": "SHIRTWLS000000FFFFFFLXXX", - "displayName": "Size", - "values": [ - { - "label": "L" - } - ] - } - ] - } - ], - "options": [ - { - "id": "option-color", - "displayName": "Color", - "values": [ - { - "label": "Black color", - "hexColors": ["#000"] - } - ] - }, - { - "id": "option-size", - "displayName": "Size", - "values": [ - { - "label": "XS" - }, - { - "label": "S" - }, - { - "label": "M" - }, - { - "label": "L" - } - ] - } - ] - }, - { - "id": "EMUG12OZFFFFFF000000", - "name": "White Enamel Mug", - "brandId": "Commerce Layer", - "path": "/white-enamel-mug", - "slug": "white-enamel-mug", - "categoryId": "household", - "price": { "value": 100, "currencyCode": "USD" }, - "descriptionHtml": "

Lightweight and durable, this mug is a must-have for every devoted camper. Use it for your favorite beverage or a hot meal, and attach it to your bag for easy access on a hike. Ready for your next camping trip?

", - "images": [ - { - "url": "https://data.commercelayer.app/vercel-provider/EMUG12OZFFFFFF000000_FLAT.png", - "altText": " A White Enamel Mug", - "width": 1000, - "height": 1000 - }, - { - "url": "https://data.commercelayer.app/vercel-provider/EMUG12OZFFFFFF000000_01.png", - "altText": " A White Enamel Mug", - "width": 1000, - "height": 1000 - }, - { - "url": "https://data.commercelayer.app/vercel-provider/EMUG12OZFFFFFF000000_02.png", - "altText": " A White Enamel Mug", - "width": 1000, - "height": 1000 - } - ], - "variants": [ - { - "id": "EMUG12OZFFFFFF000000XXXX", - "options": [ - { - "__typename": "MultipleChoiceOption", - "id": "EMUG12OZFFFFFF000000XXXX", - "displayName": "Size", - "values": [ - { - "label": "Size One" - } - ] - } - ] - } - ], - "options": [ - { - "id": "option-color", - "displayName": "Color", - "values": [ - { - "label": "White color", - "hexColors": ["#fff"] - } - ] - }, - { - "id": "option-size", - "displayName": "Size", - "values": [ - { - "label": "Size One" - } - ] - } - ] - }, - { - "id": "VFLEXFIT000000FFFFFF", - "name": "Black Visor Flexfit", - "brandId": "Commerce Layer", - "path": "/black-visor-flexfit", - "slug": "black-visor-flexfit", - "categoryId": "clothings", - "price": { "value": 39.5, "currencyCode": "USD" }, - "descriptionHtml": "

A visor is perfect for the days when you’re not in the mood for a cap, but you still want to protect your peepers from the blinding sun. Made of a quick-drying fabric, it’ll be right for sports activities or just a casual stroll around.

", - "images": [ - { - "url": "https://data.commercelayer.app/vercel-provider/VFLEXFIT000000FFFFFF_FLAT.png", - "altText": " A Black Visor Flexfit", - "width": 1000, - "height": 1000 - }, - { - "url": "https://data.commercelayer.app/vercel-provider/VFLEXFIT000000FFFFFF_01.png", - "altText": " A Black Visor Flexfit", - "width": 1000, - "height": 1000 - }, - { - "url": "https://data.commercelayer.app/vercel-provider/VFLEXFIT000000FFFFFF_02.png", - "altText": " A Black Visor Flexfit", - "width": 1000, - "height": 1000 - }, - { - "url": "https://data.commercelayer.app/vercel-provider/VFLEXFIT000000FFFFFF_03.png", - "altText": " A Black Visor Flexfit", - "width": 1000, - "height": 1000 - } - ], - "variants": [ - { - "id": "VFLEXFIT000000FFFFFFXXXX", - "options": [ - { - "__typename": "MultipleChoiceOption", - "id": "VFLEXFIT000000FFFFFFXXXX", - "displayName": "Size", - "values": [ - { - "label": "Size One" - } - ] - } - ] - } - ], - "options": [ - { - "id": "option-color", - "displayName": "Color", - "values": [ - { - "label": "Black color", - "hexColors": ["#000"] - } - ] - }, - { - "id": "option-size", - "displayName": "Size", - "values": [ - { - "label": "Size One" - } - ] - } - ] - }, - { - "id": "FACEMASK000000FFFFFF", - "name": "Black Face Mask", - "brandId": "Commerce Layer", - "path": "/black-face-mask", - "slug": "black-face-mask", - "price": { "value": 15, "currencyCode": "USD" }, - "categoryId": "clothings", - "descriptionHtml": "

Durable double-layer, washable cloth face mask. The center seam and elastic ear loops give the mask a close fit, and the treated fabric ensures that it will serve you well.

", - "images": [ - { - "url": "https://data.commercelayer.app/vercel-provider/FACEMASK000000FFFFFF_FLAT.png", - "altText": " A Black Face Mask", - "width": 1000, - "height": 1000 - } - ], - "variants": [ - { - "id": "FACEMASK000000FFFFFFXXXX", - "options": [ - { - "__typename": "MultipleChoiceOption", - "id": "FACEMASK000000FFFFFFXXXX", - "displayName": "Size", - "values": [ - { - "label": "Size One" - } - ] - } - ] - } - ], - "options": [ - { - "id": "option-color", - "displayName": "Color", - "values": [ - { - "label": "Black color", - "hexColors": ["#000"] - } - ] - }, - { - "id": "option-size", - "displayName": "Size", - "values": [ - { - "label": "Size One" - } - ] - } - ] - }, - { - "id": "SHIRTWLS9397A0000000", - "name": "Gray Women Long Sleeve Shirt", - "brandId": "Commerce Layer", - "path": "/gray-women-long-sleeve-shirt", - "slug": "gray-women-long-sleeve-shirt", - "categoryId": "clothings", - "price": { "value": 50, "currencyCode": "USD" }, - "descriptionHtml": "

This relaxed long sleeve tee combines the best of both worlds, proving that cozy can also be chic. 65/35 polyester/viscose. Drop shoulder. Curved bottom hem.

", - "images": [ - { - "url": "https://data.commercelayer.app/vercel-provider/SHIRTWLS9397A0000000_FLAT.png", - "altText": " A Gray Women Long Sleeve Shirt", - "width": 1000, - "height": 1000 - } - ], - "variants": [ - { - "id": "SHIRTWLS9397A0000000XSXX", - "options": [ - { - "__typename": "MultipleChoiceOption", - "id": "SHIRTWLS9397A0000000XSXX", - "displayName": "Size", - "values": [ - { - "label": "XS" - } - ] - } - ] - }, - { - "id": "SHIRTWLS9397A0000000SXXX", - "options": [ - { - "__typename": "MultipleChoiceOption", - "id": "SHIRTWLS9397A0000000SXXX", - "displayName": "Size", - "values": [ - { - "label": "S" - } - ] - } - ] - }, - { - "id": "SHIRTWLS9397A0000000MXXX", - "options": [ - { - "__typename": "MultipleChoiceOption", - "id": "SHIRTWLS9397A0000000MXXX", - "displayName": "Size", - "values": [ - { - "label": "M" - } - ] - } - ] - }, - { - "id": "SHIRTWLS9397A0000000LXXX", - "options": [ - { - "__typename": "MultipleChoiceOption", - "id": "SHIRTWLS9397A0000000LXXX", - "displayName": "Size", - "values": [ - { - "label": "L" - } - ] - } - ] - } - ], - "options": [ - { - "id": "option-color", - "displayName": "Color", - "values": [ - { - "label": "Gray color", - "hexColors": ["#bbbbbb"] - } - ] - }, - { - "id": "option-size", - "displayName": "Size", - "values": [ - { - "label": "XS" - }, - { - "label": "S" - }, - { - "label": "M" - }, - { - "label": "L" - } - ] - } - ] - }, - { - "id": "FACEMASKFFFFFF000000", - "name": "White Face Mask", - "brandId": "Commerce Layer", - "path": "/white-face-mask", - "slug": "white-face-mask", - "categoryId": "clothings", - "price": { "value": 15, "currencyCode": "USD" }, - "descriptionHtml": "

Durable double-layer, washable cloth face mask. The center seam and elastic ear loops give the mask a close fit, and the treated fabric ensures that it will serve you well.

", - "images": [ - { - "url": "https://data.commercelayer.app/vercel-provider/FACEMASKFFFFFF000000_FLAT.png", - "altText": " A White Face Mask", - "width": 1000, - "height": 1000 - } - ], - "variants": [ - { - "id": "FACEMASKFFFFFF000000XXXX", - "options": [ - { - "__typename": "MultipleChoiceOption", - "id": "FACEMASKFFFFFF000000XXXX", - "displayName": "Size", - "values": [ - { - "label": "Size One" - } - ] - } - ] - } - ], - "options": [ - { - "id": "option-color", - "displayName": "Color", - "values": [ - { - "label": "White color", - "hexColors": ["#fff"] - } - ] - }, - { - "id": "option-size", - "displayName": "Size", - "values": [ - { - "label": "Size One" - } - ] - } - ] - }, - { - "id": "BEANIEXXFFFFFF000000", - "name": "White Beanie", - "brandId": "Commerce Layer", - "path": "/white-beanie", - "slug": "white-beanie", - "categoryId": "clothings", - "price": { "value": 20, "currencyCode": "USD" }, - "descriptionHtml": "

Soft double-layered customizable beanie. 95% polyester, 5% spandex. Regular fit. Accurately printed, cut, and hand-sewn.

", - "images": [ - { - "url": "https://data.commercelayer.app/vercel-provider/BEANIEXXFFFFFF000000_FLAT.png", - "altText": " A White Beanie", - "width": 1000, - "height": 1000 - } - ], - "variants": [ - { - "id": "BEANIEXXFFFFFF000000XXXX", - "options": [ - { - "__typename": "MultipleChoiceOption", - "id": "BEANIEXXFFFFFF000000XXXX", - "displayName": "Size", - "values": [ - { - "label": "Size One" - } - ] - } - ] - } - ], - "options": [ - { - "id": "option-color", - "displayName": "Color", - "values": [ - { - "label": "White color", - "hexColors": ["#fff"] - } - ] - }, - { - "id": "option-size", - "displayName": "Size", - "values": [ - { - "label": "Size One" - } - ] - } - ] - }, - { - "id": "DUFFLBAGFFFFFF000000", - "name": "White Duffle Bag", - "brandId": "Commerce Layer", - "path": "/white-duffle-bag", - "slug": "white-duffle-bag", - "categoryId": "featured", - "price": { "value": 250, "currencyCode": "USD" }, - "descriptionHtml": "

The perfect spacious bag no matter the occasion. It’s great for packing exercise gear when heading to the gym, or throwing in necessities and going on an adventure.

", - "images": [ - { - "url": "https://data.commercelayer.app/vercel-provider/DUFFLBAGFFFFFF000000_FLAT.png", - "altText": " A White Face Mask", - "width": 1000, - "height": 1000 - } - ], - "variants": [ - { - "id": "DUFFLBAGFFFFFF000000XXXX", - "options": [ - { - "__typename": "MultipleChoiceOption", - "id": "DUFFLBAGFFFFFF000000XXXX", - "displayName": "Size", - "values": [ - { - "label": "Size One" - } - ] - } - ] - } - ], - "options": [ - { - "id": "option-color", - "displayName": "Color", - "values": [ - { - "label": "White color", - "hexColors": ["#fff"] - } - ] - }, - { - "id": "option-size", - "displayName": "Size", - "values": [ - { - "label": "Size One" - } - ] - } - ] - } - ] -}