mirror of
https://github.com/vercel/commerce.git
synced 2025-05-18 07:26:59 +00:00
feat: Add get content data from static file and get prices from CL
This commit is contained in:
parent
b9f8d0928a
commit
6565266242
@ -8,6 +8,7 @@ const fetchGraphqlApi: (
|
||||
) => GraphQLFetcher =
|
||||
(getConfig) =>
|
||||
async (query: string, { variables, preview } = {}, fetchOptions) => {
|
||||
debugger
|
||||
const config = getConfig()
|
||||
const res = await fetch(config.commerceUrl, {
|
||||
...fetchOptions,
|
||||
|
9
packages/commercelayer/src/api/utils/getContentData.ts
Normal file
9
packages/commercelayer/src/api/utils/getContentData.ts
Normal file
@ -0,0 +1,9 @@
|
||||
import { Product } from '@vercel/commerce/types/Product'
|
||||
|
||||
export type Products = (Product & { categoryId: string; brandId: string })[]
|
||||
|
||||
export default async function getContentData(id?: string): Promise<Products> {
|
||||
const url = process.env.NEXT_PUBLIC_COMMERCELAYER_CONTENT_DATA_URL as string
|
||||
const products = (await (await fetch(url)).json()).products as Products
|
||||
return !id ? products : products.filter((p) => p.id === id)
|
||||
}
|
@ -5,3 +5,23 @@ export default function getCredentials() {
|
||||
const accessToken = getCookie('CL_TOKEN') as string
|
||||
return { accessToken, endpoint }
|
||||
}
|
||||
|
||||
type ReturnObj = {
|
||||
organization: string
|
||||
domain: string
|
||||
}
|
||||
|
||||
export function getOrganizationSlug<E extends string>(endpoint: E): ReturnObj {
|
||||
const org = {
|
||||
organization: '',
|
||||
domain: 'commercelayer.io',
|
||||
}
|
||||
if (endpoint) {
|
||||
if (endpoint.search('commercelayer.io') === -1)
|
||||
org.domain = 'commercelayer.co'
|
||||
org.organization = endpoint
|
||||
.replace('https://', '')
|
||||
.replace(`.${org.domain}`, '')
|
||||
}
|
||||
return org
|
||||
}
|
||||
|
55
packages/commercelayer/src/api/utils/getPrices.ts
Normal file
55
packages/commercelayer/src/api/utils/getPrices.ts
Normal file
@ -0,0 +1,55 @@
|
||||
import { Price, CommerceLayerClient } from '@commercelayer/sdk'
|
||||
import { Product } from '@vercel/commerce/types/product'
|
||||
import { DeepPartialArray } from './contentfulApi'
|
||||
|
||||
type GetPricesArgs = {
|
||||
products?: DeepPartialArray<Product>
|
||||
sdk: CommerceLayerClient
|
||||
}
|
||||
|
||||
export default async function getPrices({ products = [], sdk }: GetPricesArgs) {
|
||||
let allPrices: Price[] = []
|
||||
const skus: string[] = []
|
||||
if (products) {
|
||||
products.forEach(async ({ variants }) => {
|
||||
if (variants && variants[0]?.options) {
|
||||
variants[0]?.options[0]?.id && skus.push(variants[0]?.options[0]?.id)
|
||||
}
|
||||
})
|
||||
const prices = await sdk.prices.list({
|
||||
filters: { sku_code_in: skus.join(',') },
|
||||
})
|
||||
allPrices = [...prices]
|
||||
for (
|
||||
let pageNumber = prices.meta.currentPage;
|
||||
pageNumber < prices.meta.pageCount;
|
||||
pageNumber++
|
||||
) {
|
||||
const pricesPage = await sdk.prices.list({
|
||||
filters: { sku_code_in: skus.join(',') },
|
||||
pageNumber,
|
||||
})
|
||||
allPrices = [...allPrices, ...pricesPage]
|
||||
}
|
||||
products.forEach((product, i) => {
|
||||
for (const price of allPrices) {
|
||||
const skuCode = price.sku_code
|
||||
if (skuCode && product?.id && skuCode.startsWith(product.id)) {
|
||||
product.price = {
|
||||
value: price.amount_float as number,
|
||||
currencyCode: price.currency_code,
|
||||
}
|
||||
break
|
||||
}
|
||||
product.price = {
|
||||
value: 0,
|
||||
currencyCode: 'USD',
|
||||
}
|
||||
}
|
||||
if (products && products[i]) {
|
||||
products[i] = product
|
||||
}
|
||||
})
|
||||
}
|
||||
return products as Product[]
|
||||
}
|
@ -1,30 +1,32 @@
|
||||
import { LineItem } from '@commercelayer/sdk'
|
||||
import data from '../../data.json'
|
||||
|
||||
export default function normalizeLineItems(lineItems: any[]) {
|
||||
export default function normalizeLineItems(lineItems: LineItem[]) {
|
||||
return lineItems
|
||||
.filter((l) => {
|
||||
return !['shipment', 'paymentMethod'].includes(l.itemType)
|
||||
return (
|
||||
l.item_type && !['shipment', 'payment_method'].includes(l.item_type)
|
||||
)
|
||||
})
|
||||
.map((lineItem) => {
|
||||
const id = lineItem.id
|
||||
const attributes = lineItem.attributes
|
||||
const products = data.products
|
||||
return {
|
||||
id,
|
||||
name: attributes.name,
|
||||
path: products.find((p) => p.id === attributes.reference)?.slug,
|
||||
productId: attributes.reference,
|
||||
variantId: attributes.reference,
|
||||
quantity: attributes.quantity,
|
||||
price: attributes.unit_amount_float,
|
||||
name: lineItem.name,
|
||||
path: products.find((p) => p.id === lineItem.reference)?.slug,
|
||||
productId: lineItem.reference,
|
||||
variantId: lineItem.reference,
|
||||
quantity: lineItem.quantity,
|
||||
price: lineItem.unit_amount_float,
|
||||
variant: {
|
||||
id,
|
||||
name: attributes.name,
|
||||
sku: attributes.sku_code,
|
||||
price: attributes.unit_amount_float,
|
||||
name: lineItem.name,
|
||||
sku: lineItem.sku_code,
|
||||
price: lineItem.unit_amount_float,
|
||||
image: {
|
||||
url: `https://data.commercelayer.app/vercel-provider/${attributes.reference}_FLAT.png`,
|
||||
altText: attributes.name,
|
||||
url: `https://data.commercelayer.app/vercel-provider/${lineItem.reference}_FLAT.png`,
|
||||
altText: lineItem.name,
|
||||
width: 1000,
|
||||
height: 1000,
|
||||
},
|
||||
|
Loading…
x
Reference in New Issue
Block a user