mirror of
https://github.com/vercel/commerce.git
synced 2025-06-19 13:41:22 +00:00
chore: setup commerce & next config fix: replace all call to bigcommerce from aquilacms provider feat add validation to input in signup
75 lines
1.6 KiB
TypeScript
75 lines
1.6 KiB
TypeScript
import type { Product } from '@commerce/types'
|
|
import { AquilacmsConfig, getConfig } from '../api'
|
|
import { normalizeProduct } from '../lib/normalize'
|
|
|
|
const FIELDS = [
|
|
'products',
|
|
'featuredProducts',
|
|
'bestSellingProducts',
|
|
'newestProducts',
|
|
]
|
|
|
|
export type ProductTypes =
|
|
| 'products'
|
|
| 'featuredProducts'
|
|
| 'bestSellingProducts'
|
|
| 'newestProducts'
|
|
|
|
export type ProductVariables = { field?: ProductTypes; first?: number } & {
|
|
locale?: string
|
|
hasLocale?: boolean
|
|
}
|
|
|
|
async function getAllProducts({
|
|
variables: { field = 'products', ...vars } = {},
|
|
config,
|
|
}: {
|
|
query?: string
|
|
variables?: ProductVariables
|
|
config?: AquilacmsConfig
|
|
preview?: boolean
|
|
} = {}): Promise<{ products: Product[] | any[] }> {
|
|
config = getConfig(config)
|
|
|
|
const locale = (vars.locale || config.locale)?.split('-')[0]
|
|
const variables = {
|
|
...vars,
|
|
locale,
|
|
hasLocale: !!locale,
|
|
}
|
|
|
|
if (!FIELDS.includes(field)) {
|
|
throw new Error(
|
|
`The field variable has to match one of ${FIELDS.join(', ')}`
|
|
)
|
|
}
|
|
|
|
// RecursivePartial forces the method to check for every prop in the data, which is
|
|
// required in case there's a custom `query`
|
|
let { datas } = await config.storeApiFetch('/v2/products', {
|
|
method: 'POST',
|
|
body: JSON.stringify({
|
|
lang: locale,
|
|
PostBody: {
|
|
structure: {
|
|
code: 1,
|
|
id: 1,
|
|
translation: 1,
|
|
attributes: 1,
|
|
pictos: 1,
|
|
canonical: 1,
|
|
images: 1,
|
|
},
|
|
page: 1,
|
|
limit: variables.first,
|
|
},
|
|
}),
|
|
})
|
|
|
|
const products = datas.map(normalizeProduct)
|
|
|
|
return { products }
|
|
}
|
|
|
|
export default getAllProducts
|