mirror of
https://github.com/vercel/commerce.git
synced 2025-05-18 07:26: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 = {
|
export type GetAllProductPathsResult = {
|
||||||
products: Array<{ path: string }>
|
products: Array<{ path: string }>
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function getAllProductPathsOperation() {
|
export default function getAllProductPathsOperation() {
|
||||||
function getAllProductPaths(): Promise<GetAllProductPathsResult> {
|
async function getAllProductPaths(): Promise<GetAllProductPathsResult> {
|
||||||
|
const products = await getContentData()
|
||||||
return Promise.resolve({
|
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 { GetAllProductsOperation } from '@vercel/commerce/types/product'
|
||||||
import type { OperationContext } from '@vercel/commerce/api/operations'
|
import type { OperationContext } from '@vercel/commerce/api/operations'
|
||||||
import type { CommercelayerConfig } from '../index'
|
import type { CommercelayerConfig } from '../index'
|
||||||
import data from '../../data.json'
|
|
||||||
import { Price } from '@commercelayer/js-sdk'
|
|
||||||
import { getSalesChannelToken } from '@commercelayer/js-auth'
|
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({
|
export default function getAllProductsOperation({
|
||||||
commerce,
|
commerce,
|
||||||
}: OperationContext<any>) {
|
}: OperationContext<any>) {
|
||||||
@ -20,36 +21,25 @@ export default function getAllProductsOperation({
|
|||||||
preview?: boolean
|
preview?: boolean
|
||||||
} = {}): Promise<{ products: Product[] | any[] }> {
|
} = {}): Promise<{ products: Product[] | any[] }> {
|
||||||
const endpoint = process.env.NEXT_PUBLIC_COMMERCELAYER_ENDPOINT as string
|
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({
|
const credentials = await getSalesChannelToken({
|
||||||
endpoint,
|
endpoint,
|
||||||
clientId: process.env.NEXT_PUBLIC_COMMERCELAYER_CLIENT_ID as string,
|
clientId,
|
||||||
scope: process.env.NEXT_PUBLIC_COMMERCELAYER_MARKET_SCOPE as string,
|
scope,
|
||||||
})
|
})
|
||||||
if (credentials?.accessToken) {
|
const organization = getOrganizationSlug(endpoint).organization
|
||||||
const skus: string[] = []
|
const sdk = CommerceLayerStatic.init({
|
||||||
const config = {
|
|
||||||
accessToken: credentials.accessToken,
|
accessToken: credentials.accessToken,
|
||||||
endpoint,
|
organization,
|
||||||
}
|
|
||||||
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 contentData = await getContentData()
|
||||||
})
|
const products = await getPrices({ products: contentData, sdk })
|
||||||
}
|
|
||||||
return {
|
return {
|
||||||
products: data.products,
|
products,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return getAllProducts
|
return getAllProducts
|
||||||
|
@ -2,7 +2,11 @@ import type { CommercelayerConfig } from '../index'
|
|||||||
import { Product } from '@vercel/commerce/types/product'
|
import { Product } from '@vercel/commerce/types/product'
|
||||||
import { GetProductOperation } from '@vercel/commerce/types/product'
|
import { GetProductOperation } from '@vercel/commerce/types/product'
|
||||||
import type { OperationContext } from '@vercel/commerce/api/operations'
|
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({
|
export default function getProductOperation({
|
||||||
commerce,
|
commerce,
|
||||||
@ -17,8 +21,27 @@ export default function getProductOperation({
|
|||||||
config?: Partial<CommercelayerConfig>
|
config?: Partial<CommercelayerConfig>
|
||||||
preview?: boolean
|
preview?: boolean
|
||||||
} = {}): Promise<Product | {} | any> {
|
} = {}): 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 {
|
return {
|
||||||
product: data.products.find(({ slug }) => slug === variables!.slug),
|
product,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user