mirror of
https://github.com/vercel/commerce.git
synced 2025-05-17 23:16:59 +00:00
feat: Get products from external static file
This commit is contained in:
parent
9ccbc55151
commit
a9e2d75f14
@ -1,13 +1,14 @@
|
||||
import data from '../../data.json'
|
||||
import getContentData from '../utils/getContentData'
|
||||
|
||||
export type GetAllProductPathsResult = {
|
||||
products: Array<{ path: string }>
|
||||
}
|
||||
|
||||
export default function getAllProductPathsOperation() {
|
||||
function getAllProductPaths(): Promise<GetAllProductPathsResult> {
|
||||
async function getAllProductPaths(): Promise<GetAllProductPathsResult> {
|
||||
const products = await getContentData()
|
||||
return Promise.resolve({
|
||||
products: data.products.map(({ path }) => ({ path })),
|
||||
products: products.map(({ path }) => ({ path: path || '' })),
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -2,10 +2,11 @@ import { Product } from '@vercel/commerce/types/product'
|
||||
import { GetAllProductsOperation } from '@vercel/commerce/types/product'
|
||||
import type { OperationContext } from '@vercel/commerce/api/operations'
|
||||
import type { CommercelayerConfig } from '../index'
|
||||
import data from '../../data.json'
|
||||
import { Price } from '@commercelayer/js-sdk'
|
||||
import { getSalesChannelToken } from '@commercelayer/js-auth'
|
||||
|
||||
import { getOrganizationSlug } from '../utils/getCredentials'
|
||||
import { CommerceLayerStatic } from '@commercelayer/sdk'
|
||||
import getPrices from '../utils/getPrices'
|
||||
import getContentData from '../utils/getContentData'
|
||||
export default function getAllProductsOperation({
|
||||
commerce,
|
||||
}: OperationContext<any>) {
|
||||
@ -20,36 +21,25 @@ export default function getAllProductsOperation({
|
||||
preview?: boolean
|
||||
} = {}): Promise<{ products: Product[] | any[] }> {
|
||||
const endpoint = process.env.NEXT_PUBLIC_COMMERCELAYER_ENDPOINT as string
|
||||
const clientId = process.env.NEXT_PUBLIC_COMMERCELAYER_CLIENT_ID as string
|
||||
const scope = process.env.NEXT_PUBLIC_COMMERCELAYER_MARKET_SCOPE as string
|
||||
if ([!endpoint, !clientId, !scope].every(Boolean)) {
|
||||
throw new Error('Missing commercelayer endpoint, client ID or scope')
|
||||
}
|
||||
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,
|
||||
clientId,
|
||||
scope,
|
||||
})
|
||||
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
|
||||
})
|
||||
}
|
||||
const organization = getOrganizationSlug(endpoint).organization
|
||||
const sdk = CommerceLayerStatic.init({
|
||||
accessToken: credentials.accessToken,
|
||||
organization,
|
||||
})
|
||||
const contentData = await getContentData()
|
||||
const products = await getPrices({ products: contentData, sdk })
|
||||
return {
|
||||
products: data.products,
|
||||
products,
|
||||
}
|
||||
}
|
||||
return getAllProducts
|
||||
|
@ -2,7 +2,11 @@ import type { CommercelayerConfig } from '../index'
|
||||
import { Product } from '@vercel/commerce/types/product'
|
||||
import { GetProductOperation } from '@vercel/commerce/types/product'
|
||||
import type { OperationContext } from '@vercel/commerce/api/operations'
|
||||
import data from '../../data.json'
|
||||
import { getSalesChannelToken } from '@commercelayer/js-auth'
|
||||
import { CommerceLayerStatic } from '@commercelayer/sdk'
|
||||
import { getOrganizationSlug } from '../utils/getCredentials'
|
||||
import getPrices from '../utils/getPrices'
|
||||
import getContentData from '../utils/getContentData'
|
||||
|
||||
export default function getProductOperation({
|
||||
commerce,
|
||||
@ -17,8 +21,27 @@ export default function getProductOperation({
|
||||
config?: Partial<CommercelayerConfig>
|
||||
preview?: boolean
|
||||
} = {}): Promise<Product | {} | any> {
|
||||
const endpoint = process.env.NEXT_PUBLIC_COMMERCELAYER_ENDPOINT as string
|
||||
const clientId = process.env.NEXT_PUBLIC_COMMERCELAYER_CLIENT_ID as string
|
||||
const scope = process.env.NEXT_PUBLIC_COMMERCELAYER_MARKET_SCOPE as string
|
||||
if ([!endpoint, !clientId, !scope].every(Boolean)) {
|
||||
throw new Error('Missing commercelayer endpoint, client ID or scope')
|
||||
}
|
||||
const credentials = await getSalesChannelToken({
|
||||
endpoint,
|
||||
clientId,
|
||||
scope,
|
||||
})
|
||||
const organization = getOrganizationSlug(endpoint).organization
|
||||
const sdk = CommerceLayerStatic.init({
|
||||
accessToken: credentials.accessToken,
|
||||
organization,
|
||||
})
|
||||
const contentData = await getContentData()
|
||||
const products = await getPrices({ products: contentData, sdk })
|
||||
const product = products.find(({ slug }) => slug === variables?.slug)
|
||||
return {
|
||||
product: data.products.find(({ slug }) => slug === variables!.slug),
|
||||
product,
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user