mirror of
https://github.com/vercel/commerce.git
synced 2025-05-18 15:36:58 +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 data from '../../data.json'
|
||||||
import { gateway as MoltinGateway } from '@moltin/sdk'
|
|
||||||
import normalizeProduct from '../../utils/normalize'
|
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 = {
|
export type GetAllProductPathsResult = {
|
||||||
products: Array<{ path: string }>
|
products: Array<{ path: string }>
|
||||||
@ -12,11 +9,13 @@ export type GetAllProductPathsResult = {
|
|||||||
|
|
||||||
export default function getAllProductPathsOperation() {
|
export default function getAllProductPathsOperation() {
|
||||||
async function getAllProductPaths(): Promise<GetAllProductPathsResult> {
|
async function getAllProductPaths(): Promise<GetAllProductPathsResult> {
|
||||||
let products = await Moltin.Products.Limit(200).All();
|
let products = await epClient.PCM.Limit(200).All();
|
||||||
let normalizeProducts = await normalizeProduct(products.data)
|
let paths = [];
|
||||||
let productPaths = normalizeProducts.map(({ path }) => ({ path }));
|
for (let product of products.data) {
|
||||||
|
paths.push({path: "/"+product.attributes.slug});
|
||||||
|
}
|
||||||
return await Promise.resolve({
|
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 { GetAllProductsOperation } from '@commerce/types/product'
|
||||||
import type { OperationContext } from '@commerce/api/operations'
|
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 normalizeProduct from '../../utils/normalize'
|
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 getAllProductsOperation({
|
export default function getAllProductsOperation({
|
||||||
commerce,
|
commerce,
|
||||||
@ -23,7 +19,7 @@ export default function getAllProductsOperation({
|
|||||||
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 epClient.PCM.Limit(200).All();
|
||||||
let normalizeProducts = await normalizeProduct(products.data)
|
let normalizeProducts = await normalizeProduct(products.data)
|
||||||
return {
|
return {
|
||||||
products: normalizeProducts,
|
products: normalizeProducts,
|
||||||
|
@ -3,12 +3,9 @@ import { Product } from '@commerce/types/product'
|
|||||||
import { GetProductOperation } from '@commerce/types/product'
|
import { GetProductOperation } from '@commerce/types/product'
|
||||||
import data from '../../data.json'
|
import data from '../../data.json'
|
||||||
import type { OperationContext } from '@commerce/api/operations'
|
import type { OperationContext } from '@commerce/api/operations'
|
||||||
import { gateway as MoltinGateway } from '@moltin/sdk'
|
|
||||||
import normalizeProduct from '../../utils/normalize'
|
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({
|
export default function getProductOperation({
|
||||||
commerce,
|
commerce,
|
||||||
@ -29,7 +26,7 @@ export default function getProductOperation({
|
|||||||
} else {
|
} else {
|
||||||
variablesS = variables.slug;
|
variablesS = variables.slug;
|
||||||
}
|
}
|
||||||
let products = await Moltin.Products.Filter({
|
let products = await epClient.PCM.Filter({
|
||||||
eq: {
|
eq: {
|
||||||
slug: variablesS
|
slug: variablesS
|
||||||
}
|
}
|
||||||
|
@ -4,13 +4,13 @@ import { CommerceError } from '@commerce/utils/errors'
|
|||||||
import useLogin, { UseLogin } from '@commerce/auth/use-login'
|
import useLogin, { UseLogin } from '@commerce/auth/use-login'
|
||||||
import type { LoginHook } from '../types/login'
|
import type { LoginHook } from '../types/login'
|
||||||
import useCustomer from '../customer/use-customer'
|
import useCustomer from '../customer/use-customer'
|
||||||
|
import epClient from '../utils/ep-client'
|
||||||
|
|
||||||
export default useLogin as UseLogin<typeof handler>
|
export default useLogin as UseLogin<typeof handler>
|
||||||
|
|
||||||
export const handler: MutationHook<LoginHook> = {
|
export const handler: MutationHook<any> = {
|
||||||
fetchOptions: {
|
fetchOptions: {
|
||||||
url: '/api/login',
|
query: ''
|
||||||
method: 'POST',
|
|
||||||
},
|
},
|
||||||
async fetcher({ input: { email, password }, options, fetch }) {
|
async fetcher({ input: { email, password }, options, fetch }) {
|
||||||
if (!(email && password)) {
|
if (!(email && password)) {
|
||||||
@ -20,10 +20,8 @@ export const handler: MutationHook<LoginHook> = {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
return fetch({
|
let token = await epClient.Customers.TokenViaPassword(email, password);
|
||||||
...options,
|
return token || null;
|
||||||
variables: { email, password },
|
|
||||||
});
|
|
||||||
},
|
},
|
||||||
useHook: ({ fetch }) => () => {
|
useHook: ({ fetch }) => () => {
|
||||||
const { revalidate } = useCustomer()
|
const { revalidate } = useCustomer()
|
||||||
|
@ -3,13 +3,13 @@ import useCustomer from '../customer/use-customer'
|
|||||||
import { MutationHook } from '@commerce/utils/types'
|
import { MutationHook } from '@commerce/utils/types'
|
||||||
import { CommerceError } from '@commerce/utils/errors'
|
import { CommerceError } from '@commerce/utils/errors'
|
||||||
import useSignup, { UseSignup } from '@commerce/auth/use-signup'
|
import useSignup, { UseSignup } from '@commerce/auth/use-signup'
|
||||||
|
import epClient from '../utils/ep-client'
|
||||||
|
|
||||||
export default useSignup as UseSignup<typeof handler>
|
export default useSignup as UseSignup<typeof handler>
|
||||||
|
|
||||||
export const handler: MutationHook<any> = {
|
export const handler: MutationHook<any> = {
|
||||||
fetchOptions: {
|
fetchOptions: {
|
||||||
url: '/api/signup',
|
query: '',
|
||||||
method: 'POST',
|
|
||||||
},
|
},
|
||||||
async fetcher({ input: { firstName, lastName, email, password }, options, fetch }) {
|
async fetcher({ input: { firstName, lastName, email, password }, options, fetch }) {
|
||||||
console.log("input", firstName)
|
console.log("input", firstName)
|
||||||
@ -20,15 +20,12 @@ export const handler: MutationHook<any> = {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
return fetch({
|
const data = await epClient.Customers.Create({
|
||||||
...options,
|
|
||||||
variables: {
|
|
||||||
firstName,
|
|
||||||
lastName,
|
|
||||||
email,
|
email,
|
||||||
password
|
type: "customer",
|
||||||
},
|
name: `${firstName} ${lastName}`,
|
||||||
});
|
password });
|
||||||
|
return data || null;
|
||||||
},
|
},
|
||||||
useHook: ({ fetch }) => () => {
|
useHook: ({ fetch }) => () => {
|
||||||
const { revalidate } = useCustomer()
|
const { revalidate } = useCustomer()
|
||||||
|
@ -1,11 +1,7 @@
|
|||||||
import { SWRHook } from '@commerce/utils/types'
|
import { SWRHook } from '@commerce/utils/types'
|
||||||
import useCustomer, { UseCustomer } from '@commerce/customer/use-customer'
|
import useCustomer, { UseCustomer } from '@commerce/customer/use-customer'
|
||||||
import { gateway as MoltinGateway } from '@moltin/sdk'
|
|
||||||
import getCustomerCookie from '../utils/get-customer-creds'
|
import getCustomerCookie from '../utils/get-customer-creds'
|
||||||
|
import epClient from '../utils/ep-client'
|
||||||
const Moltin = MoltinGateway({
|
|
||||||
client_id: process.env.NEXT_PUBLIC_ELASTICPATH_CLIENTID
|
|
||||||
})
|
|
||||||
|
|
||||||
export default useCustomer as UseCustomer<typeof handler>
|
export default useCustomer as UseCustomer<typeof handler>
|
||||||
export const handler: SWRHook<any> = {
|
export const handler: SWRHook<any> = {
|
||||||
@ -19,8 +15,14 @@ export const handler: SWRHook<any> = {
|
|||||||
if(!creds) {
|
if(!creds) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
const data = await Moltin.Customers.Get(creds.customer_id, creds.token);
|
console.log('moltin sdk', epClient);
|
||||||
return data || null;
|
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) => {
|
useHook: ({ useData }) => (input) => {
|
||||||
return useData({
|
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
|
client_id: process.env.NEXT_PUBLIC_ELASTICPATH_CLIENTID
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
const normalizeProduct = async(products) => {
|
const normalizeProduct = async(products) => {
|
||||||
let normalizeProducts = []
|
let normalizeProducts = []
|
||||||
|
|
||||||
@ -13,86 +14,59 @@ const normalizeProduct = async(products) => {
|
|||||||
let apiImage = await Moltin.Files.Get(fileId);
|
let apiImage = await Moltin.Files.Get(fileId);
|
||||||
return apiImage;
|
return apiImage;
|
||||||
} catch (error) {
|
} 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) => {
|
const normalizeProductImages = async(productId) => {
|
||||||
let fileId;
|
let fileId = productId.relationships?.files?.data[0]?.id;
|
||||||
if (productId.relationships.hasOwnProperty("main_image")) {
|
if (fileId) {
|
||||||
fileId = productId.relationships.main_image.data.id;
|
|
||||||
let productImageObject = await productImageGet(fileId);
|
let productImageObject = await productImageGet(fileId);
|
||||||
return productImageObject.data.link.href;
|
return productImageObject?.data?.link?.href || '/assets/lightweight-jacket-0.png';
|
||||||
}
|
}
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
const normalizeProductVariants = (productVariants) => {
|
|
||||||
return [
|
|
||||||
{
|
|
||||||
"id": "Z2lkOi8vc2hvcGlmeS9Qcm9kdWN0LzU0NDczMjUwMjQ0MjAss=",
|
|
||||||
"options": [
|
|
||||||
{
|
|
||||||
"__typename": "MultipleChoiceOption",
|
|
||||||
"id": "asd",
|
|
||||||
"displayName": "Size",
|
|
||||||
"values": [
|
|
||||||
{
|
|
||||||
"label": "XL"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
for (let index in products) {
|
for (let index in products) {
|
||||||
let product = products[index];
|
let product = products[index];
|
||||||
|
|
||||||
normalizeProducts.push({
|
normalizeProducts.push({
|
||||||
"id": product.hasOwnProperty("id") ? product.id : '',
|
"id": product.id,
|
||||||
"name": product.hasOwnProperty("name") ? product.name : '',
|
"name": product.attributes?.name,
|
||||||
"vendor": "trika",
|
"vendor": "trika",
|
||||||
"path": product.hasOwnProperty("slug") ? "/" + product.slug : '',
|
"path": "/"+product.attributes?.slug,
|
||||||
"slug": `${product.hasOwnProperty("slug") ? product.slug:''}`,
|
"slug": product.attributes?.slug,
|
||||||
"price": {
|
"price": getPrices(product.attributes?.price)[0],
|
||||||
"value": product.hasOwnProperty("price") ? product.price[0].hasOwnProperty("amount") ? product.price[0].amount : '' : '',
|
"descriptionHtml": product.attributes?.description,
|
||||||
"currencyCode": product.hasOwnProperty("price") ? product.price[0].hasOwnProperty("currency") ? product.price[0].currency : '' : ''
|
|
||||||
},
|
|
||||||
"descriptionHtml": product.hasOwnProperty("description") ? product.description : null,
|
|
||||||
"images": [{
|
"images": [{
|
||||||
"url": await normalizeProductImages(product),
|
"url": await normalizeProductImages(product),
|
||||||
"altText": "Shirt",
|
"altText": "Shirt",
|
||||||
"width": 1000,
|
"width": 1000,
|
||||||
"height": 1000
|
"height": 1000
|
||||||
}],
|
}],
|
||||||
"variants": normalizeProductVariants(product),
|
"variants": [{
|
||||||
"options": [{
|
"options": []
|
||||||
"id": "option-color",
|
}],
|
||||||
"displayName": "Color",
|
"options": []
|
||||||
"values": [{
|
|
||||||
"label": "color",
|
|
||||||
"hexColors": [
|
|
||||||
"#222"
|
|
||||||
]
|
|
||||||
}]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"id": "option-size",
|
|
||||||
"displayName": "Size",
|
|
||||||
"values": [{
|
|
||||||
"label": "S"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"label": "M"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"label": "L"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
]
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
return normalizeProducts;
|
return normalizeProducts;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user