n8n/packages/nodes-base/nodes/Airtable/v2/methods/resourceMapping.ts
Iván Ovejero 1d46983b24
refactor: Unify severity and level for all application errors for Sentry (no-changelog) (#7956)
## Summary
Unify `severity` and `level` for all backend application errors for
Sentry

Follow-up to:
https://github.com/n8n-io/n8n/pull/7914#issuecomment-1840433542

...

#### How to test the change:
1. ...


## Issues fixed
Include links to Github issue or Community forum post or **Linear
ticket**:
> Important in order to close automatically and provide context to
reviewers

...


## Review / Merge checklist
- [ ] PR title and summary are descriptive. **Remember, the title
automatically goes into the changelog. Use `(no-changelog)` otherwise.**
([conventions](https://github.com/n8n-io/n8n/blob/master/.github/pull_request_title_conventions.md))
- [ ] [Docs updated](https://github.com/n8n-io/n8n-docs) or follow-up
ticket created.
- [ ] Tests included.
> A bug is not considered fixed, unless a test is added to prevent it
from happening again. A feature is not complete without tests.
  >
> *(internal)* You can use Slack commands to trigger [e2e
tests](https://www.notion.so/n8n/How-to-use-Test-Instances-d65f49dfc51f441ea44367fb6f67eb0a?pvs=4#a39f9e5ba64a48b58a71d81c837e8227)
or [deploy test
instance](https://www.notion.so/n8n/How-to-use-Test-Instances-d65f49dfc51f441ea44367fb6f67eb0a?pvs=4#f6a177d32bde4b57ae2da0b8e454bfce)
or [deploy early access version on
Cloud](https://www.notion.so/n8n/Cloudbot-3dbe779836004972b7057bc989526998?pvs=4#fef2d36ab02247e1a0f65a74f6fb534e).
2023-12-07 16:57:02 +01:00

139 lines
3.1 KiB
TypeScript

import type {
FieldType,
IDataObject,
ILoadOptionsFunctions,
INodePropertyOptions,
ResourceMapperField,
ResourceMapperFields,
} from 'n8n-workflow';
import { NodeOperationError } from 'n8n-workflow';
import { apiRequest } from '../transport';
type AirtableSchema = {
id: string;
name: string;
type: string;
options?: IDataObject;
};
type TypesMap = Partial<Record<FieldType, string[]>>;
const airtableReadOnlyFields = [
'autoNumber',
'button',
'count',
'createdBy',
'createdTime',
'formula',
'lastModifiedBy',
'lastModifiedTime',
'lookup',
'rollup',
'externalSyncSource',
'multipleLookupValues',
'multipleRecordLinks',
];
const airtableTypesMap: TypesMap = {
string: ['singleLineText', 'multilineText', 'richText', 'email', 'phoneNumber', 'url'],
number: ['rating', 'percent', 'number', 'duration', 'currency'],
boolean: ['checkbox'],
dateTime: ['dateTime', 'date'],
time: [],
object: [],
options: ['singleSelect'],
array: ['multipleSelects', 'multipleAttachments'],
};
function mapForeignType(foreignType: string, typesMap: TypesMap): FieldType {
let type: FieldType = 'string';
for (const nativeType of Object.keys(typesMap)) {
const mappedForeignTypes = typesMap[nativeType as FieldType];
if (mappedForeignTypes?.includes(foreignType)) {
type = nativeType as FieldType;
break;
}
}
return type;
}
export async function getColumns(this: ILoadOptionsFunctions): Promise<ResourceMapperFields> {
const base = this.getNodeParameter('base', undefined, {
extractValue: true,
}) as string;
const tableId = encodeURI(
this.getNodeParameter('table', undefined, {
extractValue: true,
}) as string,
);
const response = await apiRequest.call(this, 'GET', `meta/bases/${base}/tables`);
const tableData = ((response.tables as IDataObject[]) || []).find((table: IDataObject) => {
return table.id === tableId;
});
if (!tableData) {
throw new NodeOperationError(this.getNode(), 'Table information could not be found!', {
level: 'warning',
});
}
const fields: ResourceMapperField[] = [];
const constructOptions = (field: AirtableSchema) => {
if (field?.options?.choices) {
return (field.options.choices as IDataObject[]).map((choice) => ({
name: choice.name,
value: choice.name,
})) as INodePropertyOptions[];
}
return undefined;
};
for (const field of tableData.fields as AirtableSchema[]) {
const type = mapForeignType(field.type, airtableTypesMap);
const isReadOnly = airtableReadOnlyFields.includes(field.type);
const options = constructOptions(field);
fields.push({
id: field.name,
displayName: field.name,
required: false,
defaultMatch: false,
canBeUsedToMatch: true,
display: true,
type,
options,
readOnly: isReadOnly,
removed: isReadOnly,
});
}
return { fields };
}
export async function getColumnsWithRecordId(
this: ILoadOptionsFunctions,
): Promise<ResourceMapperFields> {
const returnData = await getColumns.call(this);
return {
fields: [
{
id: 'id',
displayName: 'id',
required: false,
defaultMatch: true,
display: true,
type: 'string',
readOnly: true,
},
...returnData.fields,
],
};
}