mirror of
https://github.com/vercel/commerce.git
synced 2025-05-18 07:26:59 +00:00
Products intigrated with normalize method
This commit is contained in:
parent
7510cb03a8
commit
fade459a75
@ -4,6 +4,9 @@ import type { OperationContext } from '@commerce/api/operations'
|
|||||||
import type { ElasticpathConfig, Provider } from '../index'
|
import type { ElasticpathConfig, Provider } from '../index'
|
||||||
import { gateway as MoltinGateway } from '@moltin/sdk'
|
import { gateway as MoltinGateway } from '@moltin/sdk'
|
||||||
import data from '../../data.json'
|
import data from '../../data.json'
|
||||||
|
import normalizeProduct from '../../utils/normalize'
|
||||||
|
import { debug } from 'console'
|
||||||
|
import { connect } from 'http2'
|
||||||
|
|
||||||
const Moltin = MoltinGateway({
|
const Moltin = MoltinGateway({
|
||||||
client_id: process.env.NEXT_PUBLIC_ELASTICPATH_CLIENTID
|
client_id: process.env.NEXT_PUBLIC_ELASTICPATH_CLIENTID
|
||||||
@ -22,11 +25,11 @@ export default function getAllProductsOperation({
|
|||||||
config?: Partial<ElasticpathConfig>
|
config?: Partial<ElasticpathConfig>
|
||||||
preview?: boolean
|
preview?: boolean
|
||||||
} = {}): Promise<{ products: Product[] | any[] }> {
|
} = {}): Promise<{ products: Product[] | any[] }> {
|
||||||
//elastic path get all products
|
// elastic path get all products
|
||||||
let products = await Moltin.Products.Limit(200).All();
|
let products = await Moltin.Products.Limit(200).All();
|
||||||
console.log("All products", products);
|
let normalizeProducts = await normalizeProduct(products.data)
|
||||||
return {
|
return {
|
||||||
products: data.products,
|
products: normalizeProducts,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return getAllProducts
|
return getAllProducts
|
||||||
|
@ -3,7 +3,7 @@ const commerce = require('./commerce.config.json')
|
|||||||
module.exports = {
|
module.exports = {
|
||||||
commerce,
|
commerce,
|
||||||
images: {
|
images: {
|
||||||
domains: ['localhost'],
|
domains: ['s3-eu-west-1.amazonaws.com'],
|
||||||
},
|
},
|
||||||
webpack: (config, { isServer }) => {
|
webpack: (config, { isServer }) => {
|
||||||
if (!isServer) {
|
if (!isServer) {
|
||||||
|
85
framework/elasticpath/utils/normalize.js
Normal file
85
framework/elasticpath/utils/normalize.js
Normal file
@ -0,0 +1,85 @@
|
|||||||
|
import {
|
||||||
|
gateway as MoltinGateway
|
||||||
|
} from '@moltin/sdk'
|
||||||
|
const Moltin = MoltinGateway({
|
||||||
|
client_id: process.env.NEXT_PUBLIC_ELASTICPATH_CLIENTID
|
||||||
|
})
|
||||||
|
|
||||||
|
const normalizeProduct = async(products) => {
|
||||||
|
let normalizeProducts = []
|
||||||
|
|
||||||
|
const productImageGet = async(fileId) => {
|
||||||
|
try {
|
||||||
|
let apiImage = await Moltin.Files.Get(fileId);
|
||||||
|
return apiImage;
|
||||||
|
} catch (error) {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const normalizeProductImages = async(productId) => {
|
||||||
|
let fileId;
|
||||||
|
if (productId.relationships.hasOwnProperty("main_image")) {
|
||||||
|
fileId = productId.relationships.main_image.data.id;
|
||||||
|
let productImageObject = await productImageGet(fileId);
|
||||||
|
return productImageObject.data.link.href;
|
||||||
|
}
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
|
const normalizeProductVariants = (productVariants) => {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
|
for (let index in products) {
|
||||||
|
let product = products[index];
|
||||||
|
|
||||||
|
normalizeProducts.push({
|
||||||
|
"id": product.hasOwnProperty("id") ? product.id : null,
|
||||||
|
"name": product.hasOwnProperty("name") ? product.name : null,
|
||||||
|
"vendor": "trika",
|
||||||
|
"path": product.hasOwnProperty("name") ? "/" + product.name : null,
|
||||||
|
"slug": `${product.hasOwnProperty("slug") ? product.slug:null}`,
|
||||||
|
"price": {
|
||||||
|
"value": product.hasOwnProperty("price") ? product.price[0].hasOwnProperty("amount") ? product.price[0].amount : null : null,
|
||||||
|
"currencyCode": product.hasOwnProperty("price") ? product.price[0].hasOwnProperty("currency") ? product.price[0].currency : null : null
|
||||||
|
},
|
||||||
|
"descriptionHtml": product.hasOwnProperty("description") ? product.description : null,
|
||||||
|
"images": [{
|
||||||
|
"url": await normalizeProductImages(product),
|
||||||
|
"altText": "Shirt",
|
||||||
|
"width": 1000,
|
||||||
|
"height": 1000
|
||||||
|
}],
|
||||||
|
"variants": normalizeProductVariants(product),
|
||||||
|
"options": [{
|
||||||
|
"id": "option-color",
|
||||||
|
"displayName": "Color",
|
||||||
|
"values": [{
|
||||||
|
"label": "color",
|
||||||
|
"hexColors": [
|
||||||
|
"#222"
|
||||||
|
]
|
||||||
|
}]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "option-size",
|
||||||
|
"displayName": "Size",
|
||||||
|
"values": [{
|
||||||
|
"label": "S"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"label": "M"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"label": "L"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return normalizeProducts;
|
||||||
|
}
|
||||||
|
|
||||||
|
export default normalizeProduct;
|
Loading…
x
Reference in New Issue
Block a user