diff --git a/framework/commercelayer/api/operations/get-all-products.ts b/framework/commercelayer/api/operations/get-all-products.ts index 21a04559d..c12975630 100644 --- a/framework/commercelayer/api/operations/get-all-products.ts +++ b/framework/commercelayer/api/operations/get-all-products.ts @@ -1,8 +1,10 @@ import { Product } from '@commerce/types/product' import { GetAllProductsOperation } from '@commerce/types/product' import type { OperationContext } from '@commerce/api/operations' -import type { LocalConfig, Provider } from '../index' +import type { LocalConfig } from '../index' import data from '../../data.json' +import { Price } from '@commercelayer/js-sdk' +import { getSalesChannelToken } from '@commercelayer/js-auth' export default function getAllProductsOperation({ commerce, @@ -17,6 +19,35 @@ export default function getAllProductsOperation({ config?: Partial preview?: boolean } = {}): Promise<{ products: Product[] | any[] }> { + const endpoint = process.env.NEXT_PUBLIC_COMMERCELAYER_ENDPOINT as string + const credentials = await getSalesChannelToken({ + endpoint, + clientId: process.env.NEXT_PUBLIC_COMMERCELAYER_CLIENT_ID as string, + scope: process.env.NEXT_PUBLIC_COMMERCELAYER_MARKET_SCOPE as string, + }) + if (credentials.accessToken) { + const skus: string[] = [] + const config = { + accessToken: credentials.accessToken, + endpoint, + } + data.products.map(({ variants }) => skus.push(variants[0].options[0].id)) + const prices = ( + await Price.withCredentials(config) + .where({ skuCodeIn: skus.join(',') }) + .all({ rawResponse: true }) + ).data + data.products.map((product) => { + prices.map((price) => { + const skuCode = price.attributes.sku_code + if (skuCode.startsWith(product.id)) { + product.price.value = price.attributes.amount_float + product.price.currencyCode = price.attributes.currency_code + } + }) + return product + }) + } return { products: data.products, } diff --git a/framework/commercelayer/auth/use-token.tsx b/framework/commercelayer/auth/use-token.tsx new file mode 100644 index 000000000..44aeef009 --- /dev/null +++ b/framework/commercelayer/auth/use-token.tsx @@ -0,0 +1,24 @@ +import Cookies from 'js-cookie' +import { getSalesChannelToken } from '@commercelayer/js-auth' +import { useEffect, useState } from 'react' + +export default function useToken() { + const [token, setToken] = useState('') + useEffect(() => { + const cookieToken = Cookies.get('CL_TOKEN') + const getToken = async () => { + const credentials = await getSalesChannelToken({ + endpoint: process.env.NEXT_PUBLIC_COMMERCELAYER_ENDPOINT as string, + clientId: process.env.NEXT_PUBLIC_COMMERCELAYER_CLIENT_ID as string, + scope: process.env.NEXT_PUBLIC_COMMERCELAYER_MARKET_SCOPE as string, + }) + Cookies.set('CL_TOKEN', credentials.accessToken, { + expires: credentials.expires, + }) + setToken(credentials.accessToken) + } + if (!cookieToken) getToken() + else setToken(cookieToken) + }, [token]) + return token +} diff --git a/framework/commercelayer/index.tsx b/framework/commercelayer/index.tsx index 2ec304f63..62421223f 100644 --- a/framework/commercelayer/index.tsx +++ b/framework/commercelayer/index.tsx @@ -6,6 +6,7 @@ import { CommerceProvider as CoreCommerceProvider, useCommerce as useCoreCommerce, } from '@commerce' +import useToken from './auth/use-token' export const localConfig: CommerceConfig = { locale: 'en-us', @@ -19,6 +20,8 @@ export function CommerceProvider({ children?: ReactNode locale: string } & Partial) { + const token = useToken() + if (token) config.cartCookie = token return (