Skip to content

Commit

Permalink
fix(Airtable Node): Use item at correct index in base/getSchema (#13174)
Browse files Browse the repository at this point in the history
  • Loading branch information
elsmr authored Feb 11, 2025
1 parent 7ee6ce7 commit f2e3586
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { mockDeep } from 'jest-mock-extended';
import { get } from 'lodash';
import type { IExecuteFunctions } from 'n8n-workflow';

import * as getSchema from '../../../../v2/actions/base/getSchema.operation';
import * as transport from '../../../../v2/transport';
import { createMockExecuteFunction } from '../helpers';

jest.mock('../../../../v2/transport', () => {
const originalModule = jest.requireActual('../../../../v2/transport');
return {
...originalModule,
apiRequest: jest.fn(async function () {
return {};
return { tables: [] };
}),
};
});
Expand All @@ -18,19 +21,35 @@ describe('Test AirtableV2, base => getSchema', () => {
resource: 'base',
operation: 'getSchema',
base: {
value: 'appYobase',
value: '={{$json.id}}',
},
};

const items = [
{
json: {},
json: { id: 'appYobase1' },
},
{
json: { id: 'appYobase2' },
},
];

await getSchema.execute.call(createMockExecuteFunction(nodeParameters), items);
await getSchema.execute.call(
mockDeep<IExecuteFunctions>({
getInputData: jest.fn(() => items),
getNodeParameter: jest.fn((param: string, itemIndex: number) => {
if (param === 'base') {
return items[itemIndex].json.id;
}

return get(nodeParameters, param);
}),
}),
items,
);

expect(transport.apiRequest).toBeCalledTimes(1);
expect(transport.apiRequest).toHaveBeenCalledWith('GET', 'meta/bases/appYobase/tables');
expect(transport.apiRequest).toBeCalledTimes(2);
expect(transport.apiRequest).toHaveBeenCalledWith('GET', 'meta/bases/appYobase1/tables');
expect(transport.apiRequest).toHaveBeenCalledWith('GET', 'meta/bases/appYobase2/tables');
});
});
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import type {
IDataObject,
IExecuteFunctions,
INodeExecutionData,
INodeProperties,
IExecuteFunctions,
NodeApiError,
} from 'n8n-workflow';

import { updateDisplayOptions, wrapData } from '../../../../../utils/utilities';
import { processAirtableError } from '../../helpers/utils';
import { apiRequest } from '../../transport';
import { baseRLC } from '../common.descriptions';
import type { TablesResponse } from '../types';

const properties: INodeProperties[] = [
{
Expand All @@ -31,22 +31,25 @@ export async function execute(
this: IExecuteFunctions,
items: INodeExecutionData[],
): Promise<INodeExecutionData[]> {
const returnData: INodeExecutionData[] = [];
let returnData: INodeExecutionData[] = [];

for (let i = 0; i < items.length; i++) {
try {
const baseId = this.getNodeParameter('base', 0, undefined, {
const baseId = this.getNodeParameter('base', i, undefined, {
extractValue: true,
}) as string;

const responseData = await apiRequest.call(this, 'GET', `meta/bases/${baseId}/tables`);

const executionData = this.helpers.constructExecutionMetaData(
wrapData(responseData.tables as IDataObject[]),
{ itemData: { item: i } },
const responseData: TablesResponse = await apiRequest.call(
this,
'GET',
`meta/bases/${baseId}/tables`,
);

returnData.push(...executionData);
const executionData = this.helpers.constructExecutionMetaData(wrapData(responseData.tables), {
itemData: { item: i },
});

returnData = returnData.concat(executionData);
} catch (error) {
error = processAirtableError(error as NodeApiError, undefined, i);
if (this.continueOnFail()) {
Expand Down
15 changes: 15 additions & 0 deletions packages/nodes-base/nodes/Airtable/v2/actions/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export type Table = {
id: string;
name: string;
fields: Field[];
};

export type TablesResponse = {
tables: Table[];
};

export type Field = {
id: string;
name: string;
type: string;
};

0 comments on commit f2e3586

Please sign in to comment.