Skip to content

Commit

Permalink
feat(Airtable Node): Access token support (#6160)
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-radency authored May 4, 2023
1 parent 91fee0c commit f9fd820
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 14 deletions.
7 changes: 7 additions & 0 deletions packages/nodes-base/credentials/AirtableApi.credentials.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ export class AirtableApi implements ICredentialType {
documentationUrl = 'airtable';

properties: INodeProperties[] = [
{
displayName:
'API Keys will be deprecated by the end of January 2024, see <a href="https://support.airtable.com/docs/airtable-api-key-deprecation-notice" target="_blank">this article</a> for more details. We recommend to use Personal Access Token instead.',
name: 'deprecated',
type: 'notice',
default: '',
},
{
displayName: 'API Key',
name: 'apiKey',
Expand Down
39 changes: 39 additions & 0 deletions packages/nodes-base/credentials/AirtableTokenApi.credentials.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import type {
IAuthenticateGeneric,
ICredentialTestRequest,
ICredentialType,
INodeProperties,
} from 'n8n-workflow';

export class AirtableTokenApi implements ICredentialType {
name = 'airtableTokenApi';

displayName = 'Airtable Personal Access Token API';

documentationUrl = 'airtable';

properties: INodeProperties[] = [
{
displayName: 'Access Token',
name: 'accessToken',
type: 'string',
typeOptions: { password: true },
default: '',
},
];

authenticate: IAuthenticateGeneric = {
type: 'generic',
properties: {
headers: {
Authorization: '=Bearer {{$credentials.accessToken}}',
},
},
};

test: ICredentialTestRequest = {
request: {
baseURL: 'https://api.airtable.com/v0/meta/whoami',
},
};
}
43 changes: 38 additions & 5 deletions packages/nodes-base/nodes/Airtable/Airtable.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,39 @@ export class Airtable implements INodeType {
{
name: 'airtableApi',
required: true,
displayOptions: {
show: {
authentication: ['airtableApi'],
},
},
},
{
name: 'airtableTokenApi',
required: true,
displayOptions: {
show: {
authentication: ['airtableTokenApi'],
},
},
},
],
properties: [
{
displayName: 'Authentication',
name: 'authentication',
type: 'options',
options: [
{
name: 'API Key',
value: 'airtableApi',
},
{
name: 'Access Token',
value: 'airtableTokenApi',
},
],
default: 'airtableApi',
},
{
displayName: 'Operation',
name: 'operation',
Expand Down Expand Up @@ -546,14 +576,15 @@ export class Airtable implements INodeType {
delete (row.fields as any).id;
} else {
// Add only the specified fields
row.fields = {} as IDataObject;
const rowFields: IDataObject = {};

fields = this.getNodeParameter('fields', i, []) as string[];

for (const fieldName of fields) {
// @ts-ignore
row.fields[fieldName] = items[i].json[fieldName];
rowFields[fieldName] = items[i].json[fieldName];
}

row.fields = rowFields;
}

rows.push(row);
Expand Down Expand Up @@ -761,10 +792,12 @@ export class Airtable implements INodeType {
} else {
fields = this.getNodeParameter('fields', i, []) as string[];

const rowFields: IDataObject = {};
for (const fieldName of fields) {
// @ts-ignore
row.fields[fieldName] = items[i].json[fieldName];
rowFields[fieldName] = items[i].json[fieldName];
}

row.fields = rowFields;
}

row.id = this.getNodeParameter('id', i) as string;
Expand Down
39 changes: 32 additions & 7 deletions packages/nodes-base/nodes/Airtable/AirtableTrigger.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,42 @@ export class AirtableTrigger implements INodeType {
{
name: 'airtableApi',
required: true,
displayOptions: {
show: {
authentication: ['airtableApi'],
},
},
},
{
name: 'airtableTokenApi',
required: true,
displayOptions: {
show: {
authentication: ['airtableTokenApi'],
},
},
},
],
polling: true,
inputs: [],
outputs: ['main'],
properties: [
{
displayName: 'Authentication',
name: 'authentication',
type: 'options',
options: [
{
name: 'API Key',
value: 'airtableApi',
},
{
name: 'Access Token',
value: 'airtableTokenApi',
},
],
default: 'airtableApi',
},
{
displayName: 'Base',
name: 'baseId',
Expand Down Expand Up @@ -192,19 +222,14 @@ export class AirtableTrigger implements INodeType {

async poll(this: IPollFunctions): Promise<INodeExecutionData[][] | null> {
const downloadAttachments = this.getNodeParameter('downloadAttachments', 0) as boolean;

const webhookData = this.getWorkflowStaticData('node');

const qs: IDataObject = {};

const additionalFields = this.getNodeParameter('additionalFields') as IDataObject;

const base = this.getNodeParameter('baseId', '', { extractValue: true }) as string;

const table = this.getNodeParameter('tableId', '', { extractValue: true }) as string;

const triggerField = this.getNodeParameter('triggerField') as string;

const qs: IDataObject = {};

const endpoint = `${base}/${table}`;

const now = moment().utc().format();
Expand Down
4 changes: 2 additions & 2 deletions packages/nodes-base/nodes/Airtable/GenericFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ export async function apiRequest(
if (Object.keys(body).length === 0) {
delete options.body;
}

return this.helpers.requestWithAuthentication.call(this, 'airtableApi', options);
const authenticationMethod = this.getNodeParameter('authentication', 0) as string;
return this.helpers.requestWithAuthentication.call(this, authenticationMethod, options);
}

/**
Expand Down
1 change: 1 addition & 0 deletions packages/nodes-base/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"dist/credentials/AffinityApi.credentials.js",
"dist/credentials/AgileCrmApi.credentials.js",
"dist/credentials/AirtableApi.credentials.js",
"dist/credentials/AirtableTokenApi.credentials.js",
"dist/credentials/Amqp.credentials.js",
"dist/credentials/ApiTemplateIoApi.credentials.js",
"dist/credentials/AsanaApi.credentials.js",
Expand Down

0 comments on commit f9fd820

Please sign in to comment.