From 749694bcfdddabfab5abfa37407bf69d1f19ce9c Mon Sep 17 00:00:00 2001 From: that-one-tom Date: Fri, 18 Mar 2022 15:09:40 +0100 Subject: [PATCH 1/4] fix(Action Network Node): Pagination --- .../nodes-base/nodes/ActionNetwork/GenericFunctions.ts | 7 +++++-- .../nodes/ActionNetwork/descriptions/PersonDescription.ts | 3 ++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/packages/nodes-base/nodes/ActionNetwork/GenericFunctions.ts b/packages/nodes-base/nodes/ActionNetwork/GenericFunctions.ts index da67bd3b76c3e..ed683fbe189d4 100644 --- a/packages/nodes-base/nodes/ActionNetwork/GenericFunctions.ts +++ b/packages/nodes-base/nodes/ActionNetwork/GenericFunctions.ts @@ -95,8 +95,11 @@ export async function handleListing( return returnData.slice(0, limit); } - qs.page = responseData.page as number; - } while (responseData.total_pages && qs.page < responseData.total_pages); + if (responseData._links && responseData._links.next && responseData._links.next.href) { + const queryString = new URLSearchParams(responseData._links.next.href.split('?')[1]); + qs.page = queryString.get('page') as string; + } + } while (responseData._links && responseData._links.next); return returnData; } diff --git a/packages/nodes-base/nodes/ActionNetwork/descriptions/PersonDescription.ts b/packages/nodes-base/nodes/ActionNetwork/descriptions/PersonDescription.ts index f8ce9dd9fe801..6ebc042e52286 100644 --- a/packages/nodes-base/nodes/ActionNetwork/descriptions/PersonDescription.ts +++ b/packages/nodes-base/nodes/ActionNetwork/descriptions/PersonDescription.ts @@ -186,10 +186,11 @@ export const personFields: INodeProperties[] = [ displayName: 'Limit', name: 'limit', type: 'number', - default: 50, + default: 25, description: 'The number of results to return.', typeOptions: { minValue: 1, + maxValue: 25 }, displayOptions: { show: { From 7995e0421bec228edaf8eac2671309b22e4b1f89 Mon Sep 17 00:00:00 2001 From: Jonathan Bennetts Date: Mon, 4 Apr 2022 16:56:26 +0100 Subject: [PATCH 2/4] Fixed lint issue --- .../nodes/ActionNetwork/descriptions/PersonDescription.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/nodes-base/nodes/ActionNetwork/descriptions/PersonDescription.ts b/packages/nodes-base/nodes/ActionNetwork/descriptions/PersonDescription.ts index 6ebc042e52286..30b90f1077672 100644 --- a/packages/nodes-base/nodes/ActionNetwork/descriptions/PersonDescription.ts +++ b/packages/nodes-base/nodes/ActionNetwork/descriptions/PersonDescription.ts @@ -190,7 +190,7 @@ export const personFields: INodeProperties[] = [ description: 'The number of results to return.', typeOptions: { minValue: 1, - maxValue: 25 + maxValue: 25, }, displayOptions: { show: { From 09a4580cf5fdd344415bb496b5f89547202817b6 Mon Sep 17 00:00:00 2001 From: Jonathan Bennetts Date: Mon, 4 Apr 2022 16:56:43 +0100 Subject: [PATCH 3/4] Added credential test --- .../nodes/ActionNetwork/ActionNetwork.node.ts | 22 ++++++++++++++++++ .../nodes/ActionNetwork/GenericFunctions.ts | 23 +++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/packages/nodes-base/nodes/ActionNetwork/ActionNetwork.node.ts b/packages/nodes-base/nodes/ActionNetwork/ActionNetwork.node.ts index 06435d9ba5901..e4fa65bd98ae2 100644 --- a/packages/nodes-base/nodes/ActionNetwork/ActionNetwork.node.ts +++ b/packages/nodes-base/nodes/ActionNetwork/ActionNetwork.node.ts @@ -3,7 +3,11 @@ import { } from 'n8n-core'; import { + ICredentialDataDecryptedObject, + ICredentialsDecrypted, + ICredentialTestFunctions, IDataObject, + INodeCredentialTestResult, INodeExecutionData, INodeType, INodeTypeDescription, @@ -19,6 +23,7 @@ import { makeOsdiLink, resourceLoaders, simplifyResponse, + validateCredentials, } from './GenericFunctions'; import { @@ -65,6 +70,7 @@ export class ActionNetwork implements INodeType { { name: 'actionNetworkApi', required: true, + testedBy: 'actionNetworkCredentialTest', }, ], properties: [ @@ -123,6 +129,22 @@ export class ActionNetwork implements INodeType { }; methods = { + credentialTest: { + async actionNetworkCredentialTest(this: ICredentialTestFunctions, credential: ICredentialsDecrypted): Promise { + try { + await validateCredentials.call(this, credential.data as ICredentialDataDecryptedObject); + return { + status: 'OK', + message: 'Authentication successful!', + }; + } catch (error) { + return { + status: 'Error', + message: 'Invalid credentials', + }; + } + }, + }, loadOptions: resourceLoaders, }; diff --git a/packages/nodes-base/nodes/ActionNetwork/GenericFunctions.ts b/packages/nodes-base/nodes/ActionNetwork/GenericFunctions.ts index ed683fbe189d4..47b3eff922624 100644 --- a/packages/nodes-base/nodes/ActionNetwork/GenericFunctions.ts +++ b/packages/nodes-base/nodes/ActionNetwork/GenericFunctions.ts @@ -3,6 +3,8 @@ import { } from 'n8n-core'; import { + ICredentialDataDecryptedObject, + ICredentialTestFunctions, IDataObject, ILoadOptionsFunctions, NodeApiError, @@ -104,6 +106,27 @@ export async function handleListing( return returnData; } +export async function validateCredentials(this: ICredentialTestFunctions, decryptedCredentials: ICredentialDataDecryptedObject): Promise { // tslint:disable-line:no-any + const credentials = decryptedCredentials; + + const { + apiKey, + } = credentials as { + apiKey: string, + }; + + const options: OptionsWithUri = { + method: 'GET', + headers: { + 'OSDI-API-Token': apiKey, + }, + uri: `https://actionnetwork.org/api/v2/events?per_page=1`, + json: true, + }; + + return await this.helpers.request(options); +} + // ---------------------------------------- // helpers From c77433f5b1e3d50a3a839ec510050c3147b7acc1 Mon Sep 17 00:00:00 2001 From: ricardo Date: Wed, 6 Apr 2022 15:20:19 -0400 Subject: [PATCH 4/4] :zap: Move credentials verification and injection to the credentials file --- .../ActionNetworkApi.credentials.ts | 13 +++++++ .../nodes/ActionNetwork/ActionNetwork.node.ts | 22 ------------ .../nodes/ActionNetwork/GenericFunctions.ts | 36 +------------------ 3 files changed, 14 insertions(+), 57 deletions(-) diff --git a/packages/nodes-base/credentials/ActionNetworkApi.credentials.ts b/packages/nodes-base/credentials/ActionNetworkApi.credentials.ts index 95e889f419f7f..394f343771fad 100644 --- a/packages/nodes-base/credentials/ActionNetworkApi.credentials.ts +++ b/packages/nodes-base/credentials/ActionNetworkApi.credentials.ts @@ -1,5 +1,8 @@ import { + ICredentialDataDecryptedObject, + ICredentialTestRequest, ICredentialType, + IHttpRequestOptions, INodeProperties, } from 'n8n-workflow'; @@ -15,4 +18,14 @@ export class ActionNetworkApi implements ICredentialType { default: '', }, ]; + test: ICredentialTestRequest = { + request: { + baseURL: 'https://actionnetwork.org/api/v2', + url: '/events?per_page=1', + }, + }; + async authenticate(credentials: ICredentialDataDecryptedObject, requestOptions: IHttpRequestOptions): Promise { + requestOptions.headers = { 'OSDI-API-Token': credentials.apiKey }; + return requestOptions; + } } diff --git a/packages/nodes-base/nodes/ActionNetwork/ActionNetwork.node.ts b/packages/nodes-base/nodes/ActionNetwork/ActionNetwork.node.ts index e4fa65bd98ae2..06435d9ba5901 100644 --- a/packages/nodes-base/nodes/ActionNetwork/ActionNetwork.node.ts +++ b/packages/nodes-base/nodes/ActionNetwork/ActionNetwork.node.ts @@ -3,11 +3,7 @@ import { } from 'n8n-core'; import { - ICredentialDataDecryptedObject, - ICredentialsDecrypted, - ICredentialTestFunctions, IDataObject, - INodeCredentialTestResult, INodeExecutionData, INodeType, INodeTypeDescription, @@ -23,7 +19,6 @@ import { makeOsdiLink, resourceLoaders, simplifyResponse, - validateCredentials, } from './GenericFunctions'; import { @@ -70,7 +65,6 @@ export class ActionNetwork implements INodeType { { name: 'actionNetworkApi', required: true, - testedBy: 'actionNetworkCredentialTest', }, ], properties: [ @@ -129,22 +123,6 @@ export class ActionNetwork implements INodeType { }; methods = { - credentialTest: { - async actionNetworkCredentialTest(this: ICredentialTestFunctions, credential: ICredentialsDecrypted): Promise { - try { - await validateCredentials.call(this, credential.data as ICredentialDataDecryptedObject); - return { - status: 'OK', - message: 'Authentication successful!', - }; - } catch (error) { - return { - status: 'Error', - message: 'Invalid credentials', - }; - } - }, - }, loadOptions: resourceLoaders, }; diff --git a/packages/nodes-base/nodes/ActionNetwork/GenericFunctions.ts b/packages/nodes-base/nodes/ActionNetwork/GenericFunctions.ts index 47b3eff922624..72cf055931674 100644 --- a/packages/nodes-base/nodes/ActionNetwork/GenericFunctions.ts +++ b/packages/nodes-base/nodes/ActionNetwork/GenericFunctions.ts @@ -3,12 +3,9 @@ import { } from 'n8n-core'; import { - ICredentialDataDecryptedObject, - ICredentialTestFunctions, IDataObject, ILoadOptionsFunctions, NodeApiError, - NodeOperationError, } from 'n8n-workflow'; import { @@ -37,16 +34,7 @@ export async function actionNetworkApiRequest( body: IDataObject = {}, qs: IDataObject = {}, ) { - const credentials = await this.getCredentials('actionNetworkApi') as { apiKey: string } | undefined; - - if (credentials === undefined) { - throw new NodeOperationError(this.getNode(), 'No credentials got returned!'); - } - const options: OptionsWithUri = { - headers: { - 'OSDI-API-Token': credentials.apiKey, - }, method, body, qs, @@ -63,7 +51,7 @@ export async function actionNetworkApiRequest( } try { - return await this.helpers.request!(options); + return await this.helpers.requestWithAuthentication.call(this, 'actionNetworkApi', options); } catch (error) { throw new NodeApiError(this.getNode(), error); } @@ -106,28 +94,6 @@ export async function handleListing( return returnData; } -export async function validateCredentials(this: ICredentialTestFunctions, decryptedCredentials: ICredentialDataDecryptedObject): Promise { // tslint:disable-line:no-any - const credentials = decryptedCredentials; - - const { - apiKey, - } = credentials as { - apiKey: string, - }; - - const options: OptionsWithUri = { - method: 'GET', - headers: { - 'OSDI-API-Token': apiKey, - }, - uri: `https://actionnetwork.org/api/v2/events?per_page=1`, - json: true, - }; - - return await this.helpers.request(options); -} - - // ---------------------------------------- // helpers // ----------------------------------------