n8n-nodes-amocrm
Version:
n8n node for amocrm Api
52 lines • 2.27 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.apiRequestAllItems = exports.apiRequest = void 0;
const n8n_workflow_1 = require("n8n-workflow");
const async_await_mutex_lock_1 = require("async-await-mutex-lock");
const lock = new async_await_mutex_lock_1.Lock();
async function apiRequest(method, endpoint, body = {}, qs = {}) {
var _a, _b;
const authenticationMethod = this.getNodeParameter('authentication', 0);
const credentialType = authenticationMethod === 'oAuth2' ? 'amocrmOAuth2Api' : 'amocrmLongLivedApi';
const credentials = await this.getCredentials(credentialType);
const options = {
method,
body,
qs,
url: `https://${credentials.subdomain}.amocrm.ru/api/v4/${endpoint}`,
headers: {
'content-type': 'application/json; charset=utf-8',
},
};
try {
await lock.acquire();
return await this.helpers.httpRequestWithAuthentication.call(this, credentialType, options);
}
catch (e) {
const concreteErrorsDescription = (_b = (_a = e.cause) === null || _a === void 0 ? void 0 : _a.response) === null || _b === void 0 ? void 0 : _b.data['validation-errors'];
if (concreteErrorsDescription)
throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Incorrect fields', {
description: JSON.stringify(concreteErrorsDescription, null, 2),
});
throw e;
}
finally {
lock.release();
}
}
exports.apiRequest = apiRequest;
async function apiRequestAllItems(method, endpoint, body = {}, query = {}) {
var _a, _b, _c;
const returnData = [];
let responseData;
query.page = 1;
query.limit = query.limit ? query.limit : 250;
do {
responseData = await apiRequest.call(this, method, endpoint, body, query);
query.page++;
returnData.push(responseData);
} while ((_c = (_b = (_a = responseData._links) === null || _a === void 0 ? void 0 : _a.next) === null || _b === void 0 ? void 0 : _b.href) === null || _c === void 0 ? void 0 : _c.length);
return returnData;
}
exports.apiRequestAllItems = apiRequestAllItems;
//# sourceMappingURL=index.js.map