mirror of
https://github.com/vercel/commerce.git
synced 2025-05-18 07:26:59 +00:00
New epcc client
- Extended motlin sdk and corrected pcm ur - Using new client for below sections - Get all products - Get product - Get all products path - Login & Signup - Customers - Changing product normalizer for pcm products
This commit is contained in:
parent
60f3c68e7e
commit
d785f3c0ef
@ -1,10 +1,7 @@
|
||||
import data from '../../data.json'
|
||||
import { gateway as MoltinGateway } from '@moltin/sdk'
|
||||
import normalizeProduct from '../../utils/normalize'
|
||||
import epClient from '../../utils/ep-client'
|
||||
|
||||
const Moltin = MoltinGateway({
|
||||
client_id: process.env.NEXT_PUBLIC_ELASTICPATH_CLIENTID
|
||||
})
|
||||
|
||||
export type GetAllProductPathsResult = {
|
||||
products: Array<{ path: string }>
|
||||
@ -12,11 +9,13 @@ export type GetAllProductPathsResult = {
|
||||
|
||||
export default function getAllProductPathsOperation() {
|
||||
async function getAllProductPaths(): Promise<GetAllProductPathsResult> {
|
||||
let products = await Moltin.Products.Limit(200).All();
|
||||
let normalizeProducts = await normalizeProduct(products.data)
|
||||
let productPaths = normalizeProducts.map(({ path }) => ({ path }));
|
||||
let products = await epClient.PCM.Limit(200).All();
|
||||
let paths = [];
|
||||
for (let product of products.data) {
|
||||
paths.push({path: "/"+product.attributes.slug});
|
||||
}
|
||||
return await Promise.resolve({
|
||||
products: productPaths
|
||||
products: paths
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -2,12 +2,8 @@ import { Product } from '@commerce/types/product'
|
||||
import { GetAllProductsOperation } from '@commerce/types/product'
|
||||
import type { OperationContext } from '@commerce/api/operations'
|
||||
import type { ElasticpathConfig, Provider } from '../index'
|
||||
import { gateway as MoltinGateway } from '@moltin/sdk'
|
||||
import normalizeProduct from '../../utils/normalize'
|
||||
|
||||
const Moltin = MoltinGateway({
|
||||
client_id: process.env.NEXT_PUBLIC_ELASTICPATH_CLIENTID
|
||||
})
|
||||
import epClient from '../../utils/ep-client'
|
||||
|
||||
export default function getAllProductsOperation({
|
||||
commerce,
|
||||
@ -23,7 +19,7 @@ export default function getAllProductsOperation({
|
||||
preview?: boolean
|
||||
} = {}): Promise<{ products: Product[] | any[] }> {
|
||||
// elastic path get all products
|
||||
let products = await Moltin.Products.Limit(200).All();
|
||||
let products = await epClient.PCM.Limit(200).All();
|
||||
let normalizeProducts = await normalizeProduct(products.data)
|
||||
return {
|
||||
products: normalizeProducts,
|
||||
|
@ -3,12 +3,9 @@ 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 { gateway as MoltinGateway } from '@moltin/sdk'
|
||||
import normalizeProduct from '../../utils/normalize'
|
||||
import epClient from '../../utils/ep-client'
|
||||
|
||||
const Moltin = MoltinGateway({
|
||||
client_id: process.env.NEXT_PUBLIC_ELASTICPATH_CLIENTID
|
||||
})
|
||||
|
||||
export default function getProductOperation({
|
||||
commerce,
|
||||
@ -29,7 +26,7 @@ export default function getProductOperation({
|
||||
} else {
|
||||
variablesS = variables.slug;
|
||||
}
|
||||
let products = await Moltin.Products.Filter({
|
||||
let products = await epClient.PCM.Filter({
|
||||
eq: {
|
||||
slug: variablesS
|
||||
}
|
||||
|
@ -4,13 +4,13 @@ import { CommerceError } from '@commerce/utils/errors'
|
||||
import useLogin, { UseLogin } from '@commerce/auth/use-login'
|
||||
import type { LoginHook } from '../types/login'
|
||||
import useCustomer from '../customer/use-customer'
|
||||
import epClient from '../utils/ep-client'
|
||||
|
||||
export default useLogin as UseLogin<typeof handler>
|
||||
|
||||
export const handler: MutationHook<LoginHook> = {
|
||||
export const handler: MutationHook<any> = {
|
||||
fetchOptions: {
|
||||
url: '/api/login',
|
||||
method: 'POST',
|
||||
query: ''
|
||||
},
|
||||
async fetcher({ input: { email, password }, options, fetch }) {
|
||||
if (!(email && password)) {
|
||||
@ -20,10 +20,8 @@ export const handler: MutationHook<LoginHook> = {
|
||||
})
|
||||
}
|
||||
|
||||
return fetch({
|
||||
...options,
|
||||
variables: { email, password },
|
||||
});
|
||||
let token = await epClient.Customers.TokenViaPassword(email, password);
|
||||
return token || null;
|
||||
},
|
||||
useHook: ({ fetch }) => () => {
|
||||
const { revalidate } = useCustomer()
|
||||
|
@ -3,13 +3,13 @@ import useCustomer from '../customer/use-customer'
|
||||
import { MutationHook } from '@commerce/utils/types'
|
||||
import { CommerceError } from '@commerce/utils/errors'
|
||||
import useSignup, { UseSignup } from '@commerce/auth/use-signup'
|
||||
import epClient from '../utils/ep-client'
|
||||
|
||||
export default useSignup as UseSignup<typeof handler>
|
||||
|
||||
export const handler: MutationHook<any> = {
|
||||
fetchOptions: {
|
||||
url: '/api/signup',
|
||||
method: 'POST',
|
||||
query: '',
|
||||
},
|
||||
async fetcher({ input: { firstName, lastName, email, password }, options, fetch }) {
|
||||
console.log("input", firstName)
|
||||
@ -20,15 +20,12 @@ export const handler: MutationHook<any> = {
|
||||
})
|
||||
}
|
||||
|
||||
return fetch({
|
||||
...options,
|
||||
variables: {
|
||||
firstName,
|
||||
lastName,
|
||||
email,
|
||||
password
|
||||
},
|
||||
});
|
||||
const data = await epClient.Customers.Create({
|
||||
email,
|
||||
type: "customer",
|
||||
name: `${firstName} ${lastName}`,
|
||||
password });
|
||||
return data || null;
|
||||
},
|
||||
useHook: ({ fetch }) => () => {
|
||||
const { revalidate } = useCustomer()
|
||||
|
@ -1,11 +1,7 @@
|
||||
import { SWRHook } from '@commerce/utils/types'
|
||||
import useCustomer, { UseCustomer } from '@commerce/customer/use-customer'
|
||||
import { gateway as MoltinGateway } from '@moltin/sdk'
|
||||
import getCustomerCookie from '../utils/get-customer-creds'
|
||||
|
||||
const Moltin = MoltinGateway({
|
||||
client_id: process.env.NEXT_PUBLIC_ELASTICPATH_CLIENTID
|
||||
})
|
||||
import epClient from '../utils/ep-client'
|
||||
|
||||
export default useCustomer as UseCustomer<typeof handler>
|
||||
export const handler: SWRHook<any> = {
|
||||
@ -19,8 +15,14 @@ export const handler: SWRHook<any> = {
|
||||
if(!creds) {
|
||||
return null;
|
||||
}
|
||||
const data = await Moltin.Customers.Get(creds.customer_id, creds.token);
|
||||
return data || null;
|
||||
console.log('moltin sdk', epClient);
|
||||
const {data:customer} = await epClient.Customers.Get(creds.customer_id, creds.token);
|
||||
|
||||
return {
|
||||
...customer,
|
||||
firstName: customer.name.split(" ")[0],
|
||||
lastName: customer.name.split(" ")[1]
|
||||
}
|
||||
},
|
||||
useHook: ({ useData }) => (input) => {
|
||||
return useData({
|
||||
|
9
framework/elasticpath/utils/ep-client.ts
Normal file
9
framework/elasticpath/utils/ep-client.ts
Normal file
@ -0,0 +1,9 @@
|
||||
import { gateway as MoltinGateway, Moltin } from '@moltin/sdk';
|
||||
|
||||
let client:any = MoltinGateway({
|
||||
client_id: process.env.NEXT_PUBLIC_ELASTICPATH_CLIENTID
|
||||
});
|
||||
|
||||
client.PCM.endpoint = 'catalog/products';
|
||||
|
||||
export default client;
|
@ -5,6 +5,7 @@ const Moltin = MoltinGateway({
|
||||
client_id: process.env.NEXT_PUBLIC_ELASTICPATH_CLIENTID
|
||||
})
|
||||
|
||||
|
||||
const normalizeProduct = async(products) => {
|
||||
let normalizeProducts = []
|
||||
|
||||
@ -13,86 +14,59 @@ const normalizeProduct = async(products) => {
|
||||
let apiImage = await Moltin.Files.Get(fileId);
|
||||
return apiImage;
|
||||
} catch (error) {
|
||||
|
||||
console.error(fileId, error);
|
||||
}
|
||||
}
|
||||
|
||||
const getPrices = (prices) => {
|
||||
|
||||
if(!prices) {
|
||||
return [{
|
||||
"value": 0,
|
||||
"currencyCode": 'USD'
|
||||
}];
|
||||
}
|
||||
|
||||
let allPrices = []
|
||||
for(let key in prices) {
|
||||
allPrices.push({
|
||||
"value": prices[key].amount/100,
|
||||
"currencyCode": key
|
||||
})
|
||||
}
|
||||
return allPrices
|
||||
}
|
||||
|
||||
const normalizeProductImages = async(productId) => {
|
||||
let fileId;
|
||||
if (productId.relationships.hasOwnProperty("main_image")) {
|
||||
fileId = productId.relationships.main_image.data.id;
|
||||
let fileId = productId.relationships?.files?.data[0]?.id;
|
||||
if (fileId) {
|
||||
let productImageObject = await productImageGet(fileId);
|
||||
return productImageObject.data.link.href;
|
||||
return productImageObject?.data?.link?.href || '/assets/lightweight-jacket-0.png';
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
const normalizeProductVariants = (productVariants) => {
|
||||
return [
|
||||
{
|
||||
"id": "Z2lkOi8vc2hvcGlmeS9Qcm9kdWN0LzU0NDczMjUwMjQ0MjAss=",
|
||||
"options": [
|
||||
{
|
||||
"__typename": "MultipleChoiceOption",
|
||||
"id": "asd",
|
||||
"displayName": "Size",
|
||||
"values": [
|
||||
{
|
||||
"label": "XL"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
];
|
||||
}
|
||||
|
||||
for (let index in products) {
|
||||
let product = products[index];
|
||||
|
||||
|
||||
normalizeProducts.push({
|
||||
"id": product.hasOwnProperty("id") ? product.id : '',
|
||||
"name": product.hasOwnProperty("name") ? product.name : '',
|
||||
"id": product.id,
|
||||
"name": product.attributes?.name,
|
||||
"vendor": "trika",
|
||||
"path": product.hasOwnProperty("slug") ? "/" + product.slug : '',
|
||||
"slug": `${product.hasOwnProperty("slug") ? product.slug:''}`,
|
||||
"price": {
|
||||
"value": product.hasOwnProperty("price") ? product.price[0].hasOwnProperty("amount") ? product.price[0].amount : '' : '',
|
||||
"currencyCode": product.hasOwnProperty("price") ? product.price[0].hasOwnProperty("currency") ? product.price[0].currency : '' : ''
|
||||
},
|
||||
"descriptionHtml": product.hasOwnProperty("description") ? product.description : null,
|
||||
"path": "/"+product.attributes?.slug,
|
||||
"slug": product.attributes?.slug,
|
||||
"price": getPrices(product.attributes?.price)[0],
|
||||
"descriptionHtml": product.attributes?.description,
|
||||
"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"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
"variants": [{
|
||||
"options": []
|
||||
}],
|
||||
"options": []
|
||||
})
|
||||
}
|
||||
return normalizeProducts;
|
||||
|
Loading…
x
Reference in New Issue
Block a user