n8n/packages/nodes-base/nodes/Airtable/v2/methods/loadOptions.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

104 lines
2.8 KiB
TypeScript
Raw Normal View History

2023-07-17 09:42:30 -07:00
import type { IDataObject, ILoadOptionsFunctions, INodePropertyOptions } from 'n8n-workflow';
import { NodeOperationError } from 'n8n-workflow';
import { apiRequest } from '../transport';
export async function getColumns(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
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!', {
2023-12-07 07:57:02 -08:00
level: 'warning',
});
2023-07-17 09:42:30 -07:00
}
const result: INodePropertyOptions[] = [];
for (const field of tableData.fields as IDataObject[]) {
result.push({
name: field.name as string,
value: field.name as string,
description: `Type: ${field.type}`,
});
}
return result;
}
export async function getColumnsWithRecordId(
this: ILoadOptionsFunctions,
): Promise<INodePropertyOptions[]> {
const returnData = await getColumns.call(this);
return [
{
// eslint-disable-next-line n8n-nodes-base/node-param-display-name-miscased-id, n8n-nodes-base/node-param-display-name-miscased
name: 'id',
value: 'id' as string,
description: 'Type: primaryFieldId',
},
...returnData,
];
}
export async function getColumnsWithoutColumnToMatchOn(
this: ILoadOptionsFunctions,
): Promise<INodePropertyOptions[]> {
const columnToMatchOn = this.getNodeParameter('columnToMatchOn') as string;
const returnData = await getColumns.call(this);
return returnData.filter((column) => column.value !== columnToMatchOn);
}
export async function getAttachmentColumns(
this: ILoadOptionsFunctions,
): Promise<INodePropertyOptions[]> {
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!', {
2023-12-07 07:57:02 -08:00
level: 'warning',
});
2023-07-17 09:42:30 -07:00
}
const result: INodePropertyOptions[] = [];
for (const field of tableData.fields as IDataObject[]) {
if (!(field.type as string)?.toLowerCase()?.includes('attachment')) {
continue;
}
result.push({
name: field.name as string,
value: field.name as string,
description: `Type: ${field.type}`,
});
}
return result;
}