@cgignite/ignite-sf-commerce
Version:
Salesforce Commerce connector for Ignite
78 lines • 2.82 kB
JavaScript
const { getAsync, postAsync, putAsync, patchAsync, deleteAsync, customCall } = require('./callApi');
var querystring = require('querystring');
exports.getConfigurationData = async (info) => {
try {
if (info.tokenType === 'guestToken'){
apiURL = info.shopperTokenUri;
method = 'POST'
var myHeaders = {
'Content-Type': 'application/json',
'x-dw-client-id': info.clientId,
'Connection': 'keep-alive'
}
var body = {
"type" : "guest"
}
if (apiURL.includes("{shortCode}")) {
apiURL = apiURL.replace("{shortCode}", info.shortCode);
}
if (apiURL.includes("{organizationId}")) {
apiURL = apiURL.replace("{organizationId}", info.organizationId);
}
if (apiURL.includes("{version}")) {
apiURL = apiURL.replace("{version}", info.version);
}
if (apiURL.includes("{siteId}")) {
apiURL = apiURL.replace("{siteId}", info.siteId);
}
if (apiURL.includes("{clientId}")) {
apiURL = apiURL.replace("{clientId}", info.clientId);
}
return await customCall(apiURL, myHeaders, body, method)
} else if (info.tokenType === 'custToken'){
apiURL = info.shopperTokenUri;
method = 'POST'
Authorization = 'Basic ' + Buffer.from(`${info.custUsername}:${info.custPassword}`).toString('base64');
var myHeaders = {
'x-dw-client-id': info.clientId,
'Authorization': Authorization,
'Content-Type': 'application/json',
'Connection': 'keep-alive'
}
var body = {
"type" : "credentials"
}
if (apiURL.includes("{shortCode}")) {
apiURL = apiURL.replace("{shortCode}", info.shortCode);
}
if (apiURL.includes("{organizationId}")) {
apiURL = apiURL.replace("{organizationId}", info.organizationId);
}
if (apiURL.includes("{version}")) {
apiURL = apiURL.replace("{version}", info.version);
}
if (apiURL.includes("{siteId}")) {
apiURL = apiURL.replace("{siteId}", info.siteId);
}
if (apiURL.includes("{clientId}")) {
apiURL = apiURL.replace("{clientId}", info.clientId);
}
return await customCall(apiURL, myHeaders, body, method)
} else if (info.tokenType === 'authToken'){
apiURL = info.authTokenUri;
method = 'POST'
Authorization = 'Basic ' + Buffer.from(`${info.clientId}:${info.clientSecret}`).toString('base64');
let myHeaders = {
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': Authorization
};
Body = {
'grant_type' : 'client_credentials'
}
let body = querystring.stringify(Body);
return await customCall(apiURL, myHeaders, body, method)
}
} catch (err) {
return err
}
};