rsnext/examples/with-sfcc/sfcc.js
Hassan El Mghari af739ed8e4
Added example with salesforce commerce cloud (#41376)
Adding a Next.js example with Salesforce Commerce Cloud

Co-authored-by: JJ Kasper <22380829+ijjk@users.noreply.github.com>
2022-10-13 16:34:44 +00:00

57 lines
1.6 KiB
JavaScript

import { Customer, Product, Search } from 'commerce-sdk'
export default async function getProducts(searchQuery) {
const clientConfig = {
headers: {
authorization: ``,
},
parameters: {
clientId: process.env.SFDC_CLIENT_ID,
secret: process.env.SFDC_SECRET,
organizationId: process.env.SFDC_ORGANIZATIONID,
shortCode: process.env.SFDC_SHORTCODE,
siteId: process.env.SFDC_SITEID,
},
}
const credentials = `${clientConfig.parameters.clientId}:${clientConfig.parameters.secret}`
const base64data = Buffer.from(credentials).toString('base64')
const headers = { Authorization: `Basic ${base64data}` }
const client = new Customer.ShopperLogin(clientConfig)
const shopperToken = await client.getAccessToken({
headers,
body: {
grant_type: 'client_credentials',
},
})
const configWithAuth = {
...clientConfig,
headers: { authorization: `Bearer ${shopperToken.access_token}` },
}
const searchClient = new Search.ShopperSearch(configWithAuth)
const searchResults = await searchClient.productSearch({
parameters: { q: searchQuery },
})
const results = []
const productsClient = new Product.ShopperProducts(configWithAuth)
await Promise.all(
searchResults.hits.map(async (product) => {
const productResults = await productsClient.getProduct({
parameters: {
organizationId: clientConfig.parameters.organizationId,
siteId: clientConfig.parameters.siteId,
id: product.productId,
},
})
results.push(productResults)
})
)
return results
}