n8n-nodes-bitrix
Version:
n8n node for bitrix rest api
77 lines • 2.62 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.bitrixApiRequest = bitrixApiRequest;
exports.processParameters = processParameters;
const n8n_workflow_1 = require("n8n-workflow");
async function bitrixApiRequest(method, endpoint, body = {}, qs = {}, option = {}) {
const authenticationMethod = this.getNodeParameter('authentication', 0);
const isOAuth2 = authenticationMethod === 'bitrixOAuth2Api';
const credentials = await this.getCredentials(authenticationMethod);
const baseUrl = isOAuth2
? `https://${credentials.domain}/rest`
: credentials.webhookUrl.toString().replace(/\/$/, '');
const uri = `${baseUrl}/${endpoint}.json`;
const options = {
method,
uri,
json: true,
headers: {
Accept: 'application/json',
'Content-Type': 'application/json; charset=utf-8',
},
body,
qs,
};
if (!Object.keys(body || {}).length) {
delete options.body;
}
if (!Object.keys(qs || {}).length) {
delete options.qs;
}
if (Object.keys(option).length) {
Object.assign(options, option);
}
try {
return isOAuth2
? await this.helpers.requestWithAuthentication.call(this, authenticationMethod, options)
: await this.helpers.request.call(this, options);
}
catch (error) {
throw new n8n_workflow_1.NodeApiError(this.getNode(), error);
}
}
function processParameters(parameters) {
const result = {};
for (const param of parameters) {
if (!param.name)
continue;
const keys = param.name.split('.');
let current = result;
for (let i = 0; i < keys.length; i++) {
const key = keys[i];
const isLast = i === keys.length - 1;
if (isLast) {
const arrayMatch = key.match(/(.*)\[(\d+)]$/);
if (arrayMatch) {
const arrayKey = arrayMatch[1];
const index = parseInt(arrayMatch[2], 10);
if (!current[arrayKey]) {
current[arrayKey] = [];
}
current[arrayKey][index] = param.value;
}
else {
current[key] = param.value;
}
}
else {
if (!current[key]) {
current[key] = {};
}
current = current[key];
}
}
}
return result;
}
//# sourceMappingURL=GenericFunctions.js.map