1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58import { 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;
}