mirror of
https://github.com/vercel/commerce.git
synced 2025-07-08 05:41:22 +00:00
GetProduct Initial Commit
This commit is contained in:
parent
327cc2f055
commit
1daed6e9f7
@ -7,8 +7,7 @@ const getLoggedInCustomer: CustomerEndpoint['handlers']['getLoggedInCustomer'] =
|
||||
config,
|
||||
}) => {
|
||||
const token = req.cookies[config.customerCookie]
|
||||
|
||||
const { accessToken } = JSON.parse(token);
|
||||
const accessToken = token ? JSON.parse(token).accessToken : null;
|
||||
|
||||
if (accessToken) {
|
||||
const { data } = await config.fetch(
|
||||
|
98
framework/kibocommerce/api/fragments/product.ts
Normal file
98
framework/kibocommerce/api/fragments/product.ts
Normal file
@ -0,0 +1,98 @@
|
||||
export const productPrices = /* GraphQL */`
|
||||
fragment productPrices on Product {
|
||||
price {
|
||||
price
|
||||
salePrice
|
||||
}
|
||||
priceRange {
|
||||
lower { price, salePrice}
|
||||
upper { price, salePrice }
|
||||
}
|
||||
}
|
||||
`;
|
||||
export const productAttributes = /* GraphQL */`
|
||||
fragment productAttributes on Product {
|
||||
properties {
|
||||
attributeFQN
|
||||
attributeDetail {
|
||||
name
|
||||
}
|
||||
isHidden
|
||||
values {
|
||||
value
|
||||
stringValue
|
||||
}
|
||||
}
|
||||
}
|
||||
`;
|
||||
export const productContent = /* GraphQL */`
|
||||
fragment productContent on Product {
|
||||
content {
|
||||
productFullDescription
|
||||
productShortDescription
|
||||
seoFriendlyUrl
|
||||
productName
|
||||
productImages {
|
||||
imageUrl
|
||||
imageLabel
|
||||
mediaType
|
||||
}
|
||||
}
|
||||
}
|
||||
`;
|
||||
export const productOptions = /* GraphQL */`
|
||||
fragment productOptions on Product {
|
||||
options {
|
||||
attributeFQN
|
||||
attributeDetail {
|
||||
name
|
||||
}
|
||||
isProductImageGroupSelector
|
||||
isRequired
|
||||
isMultiValue
|
||||
values {
|
||||
value
|
||||
isSelected
|
||||
deltaPrice
|
||||
stringValue
|
||||
}
|
||||
}
|
||||
}
|
||||
`;
|
||||
export const productInfo = /* GraphQL */`
|
||||
fragment productInfo on Product {
|
||||
productCode
|
||||
productUsage
|
||||
|
||||
purchasableState {
|
||||
isPurchasable
|
||||
}
|
||||
|
||||
variations {
|
||||
productCode,
|
||||
options {
|
||||
__typename
|
||||
attributeFQN
|
||||
value
|
||||
}
|
||||
}
|
||||
|
||||
categories {
|
||||
categoryCode
|
||||
categoryId
|
||||
content {
|
||||
name
|
||||
slug
|
||||
}
|
||||
}
|
||||
|
||||
...productPrices
|
||||
...productAttributes
|
||||
...productContent
|
||||
...productOptions
|
||||
}
|
||||
${productPrices}
|
||||
${productAttributes}
|
||||
${productContent}
|
||||
${productOptions}
|
||||
`;
|
@ -2,13 +2,14 @@ import { Product } from '@commerce/types/product'
|
||||
import { GetAllProductsOperation } from '@commerce/types/product'
|
||||
import type { OperationContext } from '@commerce/api/operations'
|
||||
import type { KiboCommerceConfig } from '../index'
|
||||
import data from '../../data.json'
|
||||
import { getAllProductsQuery } from '../queries/get-all-products-query';
|
||||
import { normalizeProduct } from '../../lib/normalize'
|
||||
|
||||
export default function getAllProductsOperation({
|
||||
commerce,
|
||||
}: OperationContext<any>) {
|
||||
async function getAllProducts<T extends GetAllProductsOperation>({
|
||||
query = '',
|
||||
query = getAllProductsQuery,
|
||||
variables,
|
||||
config,
|
||||
}: {
|
||||
@ -17,8 +18,14 @@ export default function getAllProductsOperation({
|
||||
config?: Partial<KiboCommerceConfig>
|
||||
preview?: boolean
|
||||
} = {}): Promise<{ products: Product[] | any[] }> {
|
||||
|
||||
const cfg = commerce.getConfig(config)
|
||||
const { data } = await cfg.fetch(query);
|
||||
|
||||
let normalizedProducts = data.products.items ? data.products.items.map(normalizeProduct) : [];
|
||||
|
||||
return {
|
||||
products: data.products,
|
||||
products: normalizedProducts,
|
||||
}
|
||||
}
|
||||
return getAllProducts
|
||||
|
@ -1,14 +1,16 @@
|
||||
import type { KiboCommerceConfig } from '../index'
|
||||
import { Product } from '@commerce/types/product'
|
||||
import { GetProductOperation } from '@commerce/types/product'
|
||||
import data from '../../data.json'
|
||||
import type { OperationContext } from '@commerce/api/operations'
|
||||
import { getProductQuery } from '../queries/get-product-query'
|
||||
import { normalizeProduct } from '../../lib/normalize'
|
||||
|
||||
export default function getProductOperation({
|
||||
commerce,
|
||||
}: OperationContext<any>) {
|
||||
|
||||
async function getProduct<T extends GetProductOperation>({
|
||||
query = '',
|
||||
query = getProductQuery,
|
||||
variables,
|
||||
config,
|
||||
}: {
|
||||
@ -17,8 +19,18 @@ export default function getProductOperation({
|
||||
config?: Partial<KiboCommerceConfig>
|
||||
preview?: boolean
|
||||
} = {}): Promise<Product | {} | any> {
|
||||
const { products } = await commerce.getAllProducts();
|
||||
const product = products?.find((product: any)=> product.slug === variables?.slug );
|
||||
|
||||
const productVariables = { productCode: product.id}
|
||||
|
||||
const cfg = commerce.getConfig(config)
|
||||
const { data } = await cfg.fetch(query, { variables: productVariables });
|
||||
|
||||
const normalizedProduct = normalizeProduct(data.product)
|
||||
|
||||
return {
|
||||
product: data.products.find(({ slug }) => slug === variables!.slug),
|
||||
product: normalizedProduct
|
||||
}
|
||||
}
|
||||
|
||||
|
19
framework/kibocommerce/api/queries/get-all-products-query.ts
Normal file
19
framework/kibocommerce/api/queries/get-all-products-query.ts
Normal file
@ -0,0 +1,19 @@
|
||||
import { productInfo } from '../fragments/product';
|
||||
|
||||
export const getAllProductsQuery = /* GraphQL */`
|
||||
${productInfo}
|
||||
|
||||
query products(
|
||||
$filter: String
|
||||
$pageSize: Int
|
||||
) {
|
||||
products(
|
||||
filter: $filter
|
||||
pageSize: $pageSize
|
||||
) {
|
||||
items {
|
||||
...productInfo
|
||||
}
|
||||
}
|
||||
}
|
||||
`
|
15
framework/kibocommerce/api/queries/get-product-query.ts
Normal file
15
framework/kibocommerce/api/queries/get-product-query.ts
Normal file
@ -0,0 +1,15 @@
|
||||
import { productInfo } from '../fragments/product';
|
||||
|
||||
export const getProductQuery = /* GraphQL */`
|
||||
${productInfo}
|
||||
|
||||
query product(
|
||||
$productCode: String!
|
||||
) {
|
||||
product(
|
||||
productCode: $productCode
|
||||
) {
|
||||
...productInfo
|
||||
}
|
||||
}
|
||||
`
|
@ -24,54 +24,42 @@ function normalizeProductOption(productOption: any) {
|
||||
}
|
||||
|
||||
export function normalizeProduct(productNode: any): any {
|
||||
const {
|
||||
entityId: id,
|
||||
productOptions,
|
||||
prices,
|
||||
path,
|
||||
id: _,
|
||||
options: _0,
|
||||
} = productNode
|
||||
const product = {
|
||||
id: productNode.productCode,
|
||||
name: productNode.content.productName,
|
||||
vendor: "",
|
||||
path: `/${productNode.productCode}/${productNode.content.seoFriendlyUrl}`,
|
||||
// slug: `${productNode.productCode}/${productNode.content.seoFriendlyUrl}`,
|
||||
slug: productNode.content.seoFriendlyUrl,
|
||||
price: { value: productNode.price.price, "currencyCode": "USD" },
|
||||
descriptionHtml: productNode.content.productShortDescription,
|
||||
|
||||
return update(productNode, {
|
||||
id: { $set: String(id) },
|
||||
images: {
|
||||
$apply: ({ edges }: any) =>
|
||||
edges?.map(({ node: { urlOriginal, altText, ...rest } }: any) => ({
|
||||
url: urlOriginal,
|
||||
alt: altText,
|
||||
...rest,
|
||||
images: productNode.content.productImages.map((p: any)=> ({
|
||||
url: `http:${p.imageUrl}`,
|
||||
altText: p.imageLabel,
|
||||
})),
|
||||
},
|
||||
variants: {
|
||||
$apply: ({ edges }: any) =>
|
||||
edges?.map(({ node: { entityId, productOptions, ...rest } }: any) => ({
|
||||
id: entityId,
|
||||
options: productOptions?.edges
|
||||
? productOptions.edges.map(normalizeProductOption)
|
||||
: [],
|
||||
...rest,
|
||||
})),
|
||||
},
|
||||
options: {
|
||||
$set: productOptions.edges
|
||||
? productOptions?.edges.map(normalizeProductOption)
|
||||
: [],
|
||||
},
|
||||
brand: {
|
||||
$apply: (brand: any) => (brand?.entityId ? brand?.entityId : null),
|
||||
},
|
||||
slug: {
|
||||
$set: path?.replace(/^\/+|\/+$/g, ''),
|
||||
},
|
||||
price: {
|
||||
$set: {
|
||||
value: prices?.price.value,
|
||||
currencyCode: prices?.price.currencyCode,
|
||||
},
|
||||
},
|
||||
$unset: ['entityId'],
|
||||
})
|
||||
|
||||
variants: productNode.variations?.map((v:any) => ({
|
||||
id: v.productCode,
|
||||
options: v.options.map((o:any) => ({
|
||||
["__typename"]: o["__typename"],
|
||||
id: o.attributeFQN,
|
||||
displayName: o.attributeFQN.split('~')[1].toUpperCase(),
|
||||
values: [{label: o.value}]
|
||||
}))
|
||||
})) || [],
|
||||
|
||||
options:productNode.options?.map((o:any)=> ({
|
||||
id: o.attributeFQN,
|
||||
displayName: o.attributeDetail.name,
|
||||
values: o.values.map( (v:any)=> ({
|
||||
label: v.value,
|
||||
hexColors: ""
|
||||
}))
|
||||
})) || []
|
||||
}
|
||||
|
||||
return product;
|
||||
}
|
||||
|
||||
export function normalizePage(page: any): any {
|
||||
|
@ -3,6 +3,6 @@ const commerce = require('./commerce.config.json')
|
||||
module.exports = {
|
||||
commerce,
|
||||
images: {
|
||||
domains: ['localhost'],
|
||||
domains: ['d1slj7rdbjyb5l.cloudfront.net'],
|
||||
},
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user