@apify/n8n-nodes-apify
Version:
n8n nodes for Apify
71 lines • 2.45 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.apiRequest = apiRequest;
exports.apiRequestAllItems = apiRequestAllItems;
const n8n_workflow_1 = require("n8n-workflow");
async function apiRequest(method, endpoint, body, query) {
var _a;
query = query || {};
const options = {
method,
body,
qs: query,
uri: `https://api.apify.com${endpoint}`,
json: true,
};
if (method === 'GET') {
delete options.body;
}
try {
const result = await this.helpers.requestWithAuthentication.call(this, 'apifyApi', options);
return result;
}
catch (error) {
if (((_a = error.constructor) === null || _a === void 0 ? void 0 : _a.name) === 'NodeApiError') {
throw error;
}
if (error.response && error.response.body) {
throw new n8n_workflow_1.NodeApiError(this.getNode(), error, {
message: error.response.body,
description: error.message,
});
}
throw new n8n_workflow_1.NodeApiError(this.getNode(), error);
}
}
async function apiRequestAllItems(method, endpoint, body, query = {}) {
const returnData = [];
query.limit = query.limit || 999;
let responseData;
do {
responseData = await apiRequest.call(this, method, endpoint, body, query);
returnData.push(responseData);
} while (query.limit <= responseData.length);
const combinedData = {
data: {
total: 0,
count: 0,
offset: 0,
limit: 0,
desc: false,
items: [],
},
};
for (const result of returnData) {
combinedData.data.total += typeof result.total === 'number' ? result.total : 0;
combinedData.data.count += typeof result.count === 'number' ? result.count : 0;
combinedData.data.offset += typeof result.offset === 'number' ? result.offset : 0;
combinedData.data.limit += typeof result.limit === 'number' ? result.limit : 0;
if (result.data &&
typeof result.data === 'object' &&
'items' in result.data &&
Array.isArray(result.data.items)) {
combinedData.data.items = [
...combinedData.data.items,
...result.data.items,
];
}
}
return combinedData;
}
//# sourceMappingURL=genericFunctions.js.map