n8n/packages/nodes-base/nodes/TheHiveProject/helpers/utils.ts
Iván Ovejero e77fd5d286
refactor: Switch plain errors in nodes-base to ApplicationError (no-changelog) (#7914)
Ensure all errors in `nodes-base` are `ApplicationError` or children of
it and contain no variables in the message, to continue normalizing all
the backend errors we report to Sentry. Also, skip reporting to Sentry
errors from user input and from external APIs. In future we should
refine `ApplicationError` to more specific errors.

Follow-up to: [#7877](https://github.com/n8n-io/n8n/pull/7877)

- [x] Test workflows:
https://github.com/n8n-io/n8n/actions/runs/7084627970
- [x] e2e: https://github.com/n8n-io/n8n/actions/runs/7084936861

---------

Co-authored-by: Michael Kret <michael.k@radency.com>
2023-12-05 11:17:08 +01:00

103 lines
1.9 KiB
TypeScript

import { ApplicationError, type IDataObject } from 'n8n-workflow';
import get from 'lodash/get';
import set from 'lodash/set';
export function splitAndTrim(str: string | string[]) {
if (typeof str === 'string') {
return str
.split(',')
.map((tag) => tag.trim())
.filter((tag) => tag);
}
return str;
}
export function fixFieldType(fields: IDataObject) {
const returnData: IDataObject = {};
for (const key of Object.keys(fields)) {
if (
[
'date',
'lastSyncDate',
'startDate',
'endDate',
'dueDate',
'includeInTimeline',
'sightedAt',
].includes(key)
) {
returnData[key] = Date.parse(fields[key] as string);
continue;
}
if (['tags', 'addTags', 'removeTags'].includes(key)) {
returnData[key] = splitAndTrim(fields[key] as string);
continue;
}
returnData[key] = fields[key];
}
return returnData;
}
export function prepareInputItem(item: IDataObject, schema: IDataObject[], i: number) {
const returnData: IDataObject = {};
for (const entry of schema) {
const id = entry.id as string;
const value = get(item, id);
if (value !== undefined) {
set(returnData, id, value);
} else {
if (entry.required) {
throw new ApplicationError(`Required field "${id}" is missing in item ${i}`, {
level: 'warning',
});
}
}
}
return returnData;
}
export function constructFilter(entry: IDataObject) {
const { field, value } = entry;
let { operator } = entry;
if (operator === undefined) {
operator = '_eq';
}
if (operator === '_between') {
const { from, to } = entry;
return {
_between: {
_field: field,
_from: from,
_to: to,
},
};
}
if (operator === '_in') {
const { values } = entry;
return {
_in: {
_field: field,
_values: typeof values === 'string' ? splitAndTrim(values) : values,
},
};
}
return {
[operator as string]: {
_field: field,
_value: value,
},
};
}