mirror of
https://github.com/vercel/commerce.git
synced 2025-07-04 12:11:22 +00:00
80 lines
2.2 KiB
TypeScript
80 lines
2.2 KiB
TypeScript
import type { SpreeApiConfig, SpreeApiProvider } from '../index'
|
|
import type { GetProductOperation } from '@commerce/types/product'
|
|
import type {
|
|
OperationContext,
|
|
OperationOptions,
|
|
} from '@commerce/api/operations'
|
|
import type { IProduct } from '@spree/storefront-api-v2-sdk/types/interfaces/Product'
|
|
import type { SpreeSdkVariables } from 'framework/spree/types'
|
|
import MissingSlugVariableError from 'framework/spree/errors/MissingSlugVariableError'
|
|
import normalizeProduct from 'framework/spree/utils/normalizeProduct'
|
|
|
|
export default function getProductOperation({
|
|
commerce,
|
|
}: OperationContext<SpreeApiProvider>) {
|
|
async function getProduct<T extends GetProductOperation>(opts: {
|
|
variables: T['variables']
|
|
config?: Partial<SpreeApiConfig>
|
|
preview?: boolean
|
|
}): Promise<T['data']>
|
|
|
|
async function getProduct<T extends GetProductOperation>(
|
|
opts: {
|
|
variables: T['variables']
|
|
config?: Partial<SpreeApiConfig>
|
|
preview?: boolean
|
|
} & OperationOptions
|
|
): Promise<T['data']>
|
|
|
|
async function getProduct<T extends GetProductOperation>({
|
|
query = '',
|
|
variables: getProductVariables,
|
|
config: userConfig,
|
|
}: {
|
|
query?: string
|
|
variables?: T['variables']
|
|
config?: Partial<SpreeApiConfig>
|
|
preview?: boolean
|
|
}): Promise<T['data']> {
|
|
console.log(
|
|
'getProduct called. Configuration: ',
|
|
'getProductVariables: ',
|
|
getProductVariables,
|
|
'config: ',
|
|
userConfig
|
|
)
|
|
|
|
if (!getProductVariables?.slug) {
|
|
throw new MissingSlugVariableError()
|
|
}
|
|
|
|
const variables: SpreeSdkVariables = {
|
|
methodPath: 'products.show',
|
|
arguments: [
|
|
getProductVariables.slug,
|
|
{
|
|
include: 'variants,images,option_types,variants.option_values',
|
|
},
|
|
],
|
|
}
|
|
|
|
const config = commerce.getConfig(userConfig)
|
|
const { fetch: apiFetch } = config // TODO: Send config.locale to Spree.
|
|
|
|
const {
|
|
data: { data: spreeSuccessResponse },
|
|
} = await apiFetch<{ data: IProduct }, SpreeSdkVariables>('__UNUSED__', {
|
|
variables,
|
|
})
|
|
|
|
return {
|
|
product: normalizeProduct(
|
|
spreeSuccessResponse,
|
|
spreeSuccessResponse.data
|
|
),
|
|
}
|
|
}
|
|
|
|
return getProduct
|
|
}
|