Skip to content

Commit f2e3586

Browse files
authored
fix(Airtable Node): Use item at correct index in base/getSchema (n8n-io#13174)
1 parent 7ee6ce7 commit f2e3586

File tree

3 files changed

+54
-17
lines changed

3 files changed

+54
-17
lines changed
Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1+
import { mockDeep } from 'jest-mock-extended';
2+
import { get } from 'lodash';
3+
import type { IExecuteFunctions } from 'n8n-workflow';
4+
15
import * as getSchema from '../../../../v2/actions/base/getSchema.operation';
26
import * as transport from '../../../../v2/transport';
3-
import { createMockExecuteFunction } from '../helpers';
47

58
jest.mock('../../../../v2/transport', () => {
69
const originalModule = jest.requireActual('../../../../v2/transport');
710
return {
811
...originalModule,
912
apiRequest: jest.fn(async function () {
10-
return {};
13+
return { tables: [] };
1114
}),
1215
};
1316
});
@@ -18,19 +21,35 @@ describe('Test AirtableV2, base => getSchema', () => {
1821
resource: 'base',
1922
operation: 'getSchema',
2023
base: {
21-
value: 'appYobase',
24+
value: '={{$json.id}}',
2225
},
2326
};
2427

2528
const items = [
2629
{
27-
json: {},
30+
json: { id: 'appYobase1' },
31+
},
32+
{
33+
json: { id: 'appYobase2' },
2834
},
2935
];
3036

31-
await getSchema.execute.call(createMockExecuteFunction(nodeParameters), items);
37+
await getSchema.execute.call(
38+
mockDeep<IExecuteFunctions>({
39+
getInputData: jest.fn(() => items),
40+
getNodeParameter: jest.fn((param: string, itemIndex: number) => {
41+
if (param === 'base') {
42+
return items[itemIndex].json.id;
43+
}
44+
45+
return get(nodeParameters, param);
46+
}),
47+
}),
48+
items,
49+
);
3250

33-
expect(transport.apiRequest).toBeCalledTimes(1);
34-
expect(transport.apiRequest).toHaveBeenCalledWith('GET', 'meta/bases/appYobase/tables');
51+
expect(transport.apiRequest).toBeCalledTimes(2);
52+
expect(transport.apiRequest).toHaveBeenCalledWith('GET', 'meta/bases/appYobase1/tables');
53+
expect(transport.apiRequest).toHaveBeenCalledWith('GET', 'meta/bases/appYobase2/tables');
3554
});
3655
});

packages/nodes-base/nodes/Airtable/v2/actions/base/getSchema.operation.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import type {
2-
IDataObject,
2+
IExecuteFunctions,
33
INodeExecutionData,
44
INodeProperties,
5-
IExecuteFunctions,
65
NodeApiError,
76
} from 'n8n-workflow';
87

98
import { updateDisplayOptions, wrapData } from '../../../../../utils/utilities';
109
import { processAirtableError } from '../../helpers/utils';
1110
import { apiRequest } from '../../transport';
1211
import { baseRLC } from '../common.descriptions';
12+
import type { TablesResponse } from '../types';
1313

1414
const properties: INodeProperties[] = [
1515
{
@@ -31,22 +31,25 @@ export async function execute(
3131
this: IExecuteFunctions,
3232
items: INodeExecutionData[],
3333
): Promise<INodeExecutionData[]> {
34-
const returnData: INodeExecutionData[] = [];
34+
let returnData: INodeExecutionData[] = [];
3535

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

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

49-
returnData.push(...executionData);
48+
const executionData = this.helpers.constructExecutionMetaData(wrapData(responseData.tables), {
49+
itemData: { item: i },
50+
});
51+
52+
returnData = returnData.concat(executionData);
5053
} catch (error) {
5154
error = processAirtableError(error as NodeApiError, undefined, i);
5255
if (this.continueOnFail()) {
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export type Table = {
2+
id: string;
3+
name: string;
4+
fields: Field[];
5+
};
6+
7+
export type TablesResponse = {
8+
tables: Table[];
9+
};
10+
11+
export type Field = {
12+
id: string;
13+
name: string;
14+
type: string;
15+
};

0 commit comments

Comments
 (0)