n8n-nodes-base
Version:
Base nodes of n8n
56 lines • 2.25 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.format = void 0;
exports.marketstackApiRequest = marketstackApiRequest;
exports.marketstackApiRequestAllItems = marketstackApiRequestAllItems;
exports.validateTimeOptions = validateTimeOptions;
const n8n_workflow_1 = require("n8n-workflow");
async function marketstackApiRequest(method, endpoint, body = {}, qs = {}) {
const credentials = await this.getCredentials('marketstackApi');
const protocol = credentials.useHttps ? 'https' : 'http'; // Free API does not support HTTPS
const options = {
method,
uri: `${protocol}://api.marketstack.com/v1${endpoint}`,
qs: {
access_key: credentials.apiKey,
...qs,
},
json: true,
};
if (!Object.keys(body).length) {
delete options.body;
}
try {
return await this.helpers.request(options);
}
catch (error) {
throw new n8n_workflow_1.NodeApiError(this.getNode(), error);
}
}
async function marketstackApiRequestAllItems(method, endpoint, body = {}, qs = {}) {
const returnAll = this.getNodeParameter('returnAll', 0, false);
const limit = this.getNodeParameter('limit', 0, 0);
let responseData;
const returnData = [];
qs.offset = 0;
do {
responseData = await marketstackApiRequest.call(this, method, endpoint, body, qs);
returnData.push(...responseData.data);
if (!returnAll && returnData.length > limit) {
return returnData.slice(0, limit);
}
qs.offset += responseData.count;
} while (responseData.total > returnData.length);
return returnData;
}
const format = (datetime) => datetime?.split('T')[0];
exports.format = format;
function validateTimeOptions(timeOptions) {
if (timeOptions.every((o) => !o)) {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Please filter by latest, specific date or timeframe (start and end dates).');
}
if (timeOptions.filter(Boolean).length > 1) {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Please filter by one of latest, specific date, or timeframe (start and end dates).');
}
}
//# sourceMappingURL=GenericFunctions.js.map