mirror of
https://github.com/n8n-io/n8n.git
synced 2024-12-26 05:04:05 -08:00
61e26804ba
* ⚡ enabled array-type * ⚡ await-thenable on * ⚡ ban-types on * ⚡ default-param-last on * ⚡ dot-notation on * ⚡ member-delimiter-style on * ⚡ no-duplicate-imports on * ⚡ no-empty-interface on * ⚡ no-floating-promises on * ⚡ no-for-in-array on * ⚡ no-invalid-void-type on * ⚡ no-loop-func on * ⚡ no-shadow on * ⚡ ban-ts-comment re enabled * ⚡ @typescript-eslint/lines-between-class-members on * address my own comment * @typescript-eslint/return-await on * @typescript-eslint/promise-function-async on * @typescript-eslint/no-unnecessary-boolean-literal-compare on * @typescript-eslint/no-unnecessary-type-assertion on * prefer-const on * @typescript-eslint/prefer-optional-chain on Co-authored-by: कारतोफ्फेलस्क्रिप्ट™ <aditya@netroy.in>
84 lines
2 KiB
TypeScript
84 lines
2 KiB
TypeScript
import { OptionsWithUri } from 'request';
|
|
|
|
import { IExecuteFunctions } from 'n8n-core';
|
|
|
|
import { IDataObject, NodeApiError, NodeOperationError } from 'n8n-workflow';
|
|
|
|
export async function marketstackApiRequest(
|
|
this: IExecuteFunctions,
|
|
method: string,
|
|
endpoint: string,
|
|
body: IDataObject = {},
|
|
qs: IDataObject = {},
|
|
) {
|
|
const credentials = await this.getCredentials('marketstackApi');
|
|
const protocol = credentials.useHttps ? 'https' : 'http'; // Free API does not support HTTPS
|
|
|
|
const options: OptionsWithUri = {
|
|
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 NodeApiError(this.getNode(), error);
|
|
}
|
|
}
|
|
|
|
export async function marketstackApiRequestAllItems(
|
|
this: IExecuteFunctions,
|
|
method: string,
|
|
endpoint: string,
|
|
body: IDataObject = {},
|
|
qs: IDataObject = {},
|
|
) {
|
|
const returnAll = this.getNodeParameter('returnAll', 0, false);
|
|
const limit = this.getNodeParameter('limit', 0, 0);
|
|
|
|
let responseData;
|
|
const returnData: IDataObject[] = [];
|
|
|
|
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;
|
|
}
|
|
|
|
export const format = (datetime?: string) => datetime?.split('T')[0];
|
|
|
|
export function validateTimeOptions(this: IExecuteFunctions, timeOptions: boolean[]) {
|
|
if (timeOptions.every((o) => !o)) {
|
|
throw new NodeOperationError(
|
|
this.getNode(),
|
|
'Please filter by latest, specific date or timeframe (start and end dates).',
|
|
);
|
|
}
|
|
|
|
if (timeOptions.filter(Boolean).length > 1) {
|
|
throw new NodeOperationError(
|
|
this.getNode(),
|
|
'Please filter by one of latest, specific date, or timeframe (start and end dates).',
|
|
);
|
|
}
|
|
}
|