2023-01-27 03:22:44 -08:00
|
|
|
import type {
|
2023-03-09 09:13:15 -08:00
|
|
|
IExecuteFunctions,
|
2021-08-01 04:49:24 -07:00
|
|
|
IDataObject,
|
|
|
|
INodeExecutionData,
|
|
|
|
INodeType,
|
|
|
|
INodeTypeDescription,
|
|
|
|
} from 'n8n-workflow';
|
2023-01-27 03:22:44 -08:00
|
|
|
import { NodeOperationError } from 'n8n-workflow';
|
2021-08-01 04:49:24 -07:00
|
|
|
|
|
|
|
import {
|
|
|
|
endOfDayDataFields,
|
|
|
|
endOfDayDataOperations,
|
|
|
|
exchangeFields,
|
|
|
|
exchangeOperations,
|
|
|
|
tickerFields,
|
|
|
|
tickerOperations,
|
|
|
|
} from './descriptions';
|
|
|
|
|
|
|
|
import {
|
|
|
|
format,
|
|
|
|
marketstackApiRequest,
|
|
|
|
marketstackApiRequestAllItems,
|
|
|
|
validateTimeOptions,
|
|
|
|
} from './GenericFunctions';
|
|
|
|
|
2023-01-27 03:22:44 -08:00
|
|
|
import type { EndOfDayDataFilters, Operation, Resource } from './types';
|
2021-08-01 04:49:24 -07:00
|
|
|
|
|
|
|
export class Marketstack implements INodeType {
|
|
|
|
description: INodeTypeDescription = {
|
|
|
|
displayName: 'Marketstack',
|
|
|
|
name: 'marketstack',
|
|
|
|
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
|
2024-06-06 04:34:30 -07:00
|
|
|
icon: { light: 'file:marketstack.svg', dark: 'file:marketstack.dark.svg' },
|
2021-08-01 04:49:24 -07:00
|
|
|
group: ['transform'],
|
|
|
|
version: 1,
|
|
|
|
description: 'Consume Marketstack API',
|
|
|
|
defaults: {
|
|
|
|
name: 'Marketstack',
|
|
|
|
},
|
|
|
|
inputs: ['main'],
|
|
|
|
outputs: ['main'],
|
|
|
|
credentials: [
|
|
|
|
{
|
|
|
|
name: 'marketstackApi',
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
properties: [
|
|
|
|
{
|
|
|
|
displayName: 'Resource',
|
|
|
|
name: 'resource',
|
|
|
|
type: 'options',
|
2022-05-20 14:47:24 -07:00
|
|
|
noDataExpression: true,
|
2021-08-01 04:49:24 -07:00
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'End-of-Day Data',
|
|
|
|
value: 'endOfDayData',
|
|
|
|
description: 'Stock market closing data',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Exchange',
|
|
|
|
value: 'exchange',
|
|
|
|
description: 'Stock market exchange',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Ticker',
|
|
|
|
value: 'ticker',
|
|
|
|
description: 'Stock market symbol',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
default: 'endOfDayData',
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
...endOfDayDataOperations,
|
|
|
|
...endOfDayDataFields,
|
|
|
|
...exchangeOperations,
|
|
|
|
...exchangeFields,
|
|
|
|
...tickerOperations,
|
|
|
|
...tickerFields,
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
|
|
|
const items = this.getInputData();
|
|
|
|
|
|
|
|
const resource = this.getNodeParameter('resource', 0) as Resource;
|
|
|
|
const operation = this.getNodeParameter('operation', 0) as Operation;
|
|
|
|
|
2022-12-02 06:25:21 -08:00
|
|
|
let responseData: any;
|
2022-08-30 08:55:33 -07:00
|
|
|
const returnData: INodeExecutionData[] = [];
|
2021-08-01 04:49:24 -07:00
|
|
|
|
|
|
|
for (let i = 0; i < items.length; i++) {
|
|
|
|
try {
|
|
|
|
if (resource === 'endOfDayData') {
|
|
|
|
if (operation === 'getAll') {
|
|
|
|
// ----------------------------------
|
|
|
|
// endOfDayData: getAll
|
|
|
|
// ----------------------------------
|
|
|
|
|
|
|
|
const qs: IDataObject = {
|
|
|
|
symbols: this.getNodeParameter('symbols', i),
|
|
|
|
};
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
const { latest, specificDate, dateFrom, dateTo, ...rest } = this.getNodeParameter(
|
|
|
|
'filters',
|
|
|
|
i,
|
|
|
|
) as EndOfDayDataFilters;
|
2021-08-01 04:49:24 -07:00
|
|
|
|
|
|
|
validateTimeOptions.call(this, [
|
2022-12-02 12:54:28 -08:00
|
|
|
latest !== undefined && latest,
|
2021-08-01 04:49:24 -07:00
|
|
|
specificDate !== undefined,
|
|
|
|
dateFrom !== undefined && dateTo !== undefined,
|
|
|
|
]);
|
|
|
|
|
|
|
|
if (Object.keys(rest).length) {
|
|
|
|
Object.assign(qs, rest);
|
|
|
|
}
|
|
|
|
|
|
|
|
let endpoint: string;
|
|
|
|
|
|
|
|
if (latest) {
|
|
|
|
endpoint = '/eod/latest';
|
|
|
|
} else if (specificDate) {
|
|
|
|
endpoint = `/eod/${format(specificDate)}`;
|
|
|
|
} else {
|
|
|
|
if (!dateFrom || !dateTo) {
|
|
|
|
throw new NodeOperationError(
|
|
|
|
this.getNode(),
|
2022-08-17 08:50:24 -07:00
|
|
|
'Please enter a start and end date to filter by timeframe.',
|
|
|
|
{ itemIndex: i },
|
2021-08-01 04:49:24 -07:00
|
|
|
);
|
|
|
|
}
|
|
|
|
endpoint = '/eod';
|
|
|
|
qs.date_from = format(dateFrom);
|
|
|
|
qs.date_to = format(dateTo);
|
|
|
|
}
|
|
|
|
|
|
|
|
responseData = await marketstackApiRequestAllItems.call(this, 'GET', endpoint, {}, qs);
|
|
|
|
}
|
|
|
|
} else if (resource === 'exchange') {
|
|
|
|
if (operation === 'get') {
|
|
|
|
// ----------------------------------
|
|
|
|
// exchange: get
|
|
|
|
// ----------------------------------
|
|
|
|
|
|
|
|
const exchange = this.getNodeParameter('exchange', i);
|
|
|
|
const endpoint = `/exchanges/${exchange}`;
|
|
|
|
|
|
|
|
responseData = await marketstackApiRequest.call(this, 'GET', endpoint);
|
|
|
|
}
|
|
|
|
} else if (resource === 'ticker') {
|
|
|
|
if (operation === 'get') {
|
|
|
|
// ----------------------------------
|
|
|
|
// ticker: get
|
|
|
|
// ----------------------------------
|
|
|
|
|
|
|
|
const symbol = this.getNodeParameter('symbol', i);
|
|
|
|
const endpoint = `/tickers/${symbol}`;
|
|
|
|
|
|
|
|
responseData = await marketstackApiRequest.call(this, 'GET', endpoint);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (error) {
|
2024-06-19 22:45:00 -07:00
|
|
|
if (this.continueOnFail(error)) {
|
2022-08-30 08:55:33 -07:00
|
|
|
const executionErrorData = this.helpers.constructExecutionMetaData(
|
|
|
|
this.helpers.returnJsonArray({ error: error.message }),
|
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
returnData.push(...executionErrorData);
|
2021-08-01 04:49:24 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
2022-08-30 08:55:33 -07:00
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
2023-02-27 19:39:43 -08:00
|
|
|
this.helpers.returnJsonArray(responseData as IDataObject[]),
|
2022-08-30 08:55:33 -07:00
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
|
|
|
|
returnData.push(...executionData);
|
2021-08-01 04:49:24 -07:00
|
|
|
}
|
|
|
|
|
2023-09-05 03:59:02 -07:00
|
|
|
return [returnData];
|
2021-08-01 04:49:24 -07:00
|
|
|
}
|
|
|
|
}
|