diff --git a/packages/nodes-base/credentials/PipedriveOAuth2Api.credentials.ts b/packages/nodes-base/credentials/PipedriveOAuth2Api.credentials.ts new file mode 100644 index 0000000000..3902bb6212 --- /dev/null +++ b/packages/nodes-base/credentials/PipedriveOAuth2Api.credentials.ts @@ -0,0 +1,46 @@ +import { + ICredentialType, + NodePropertyTypes, +} from 'n8n-workflow'; + +export class PipedriveOAuth2Api implements ICredentialType { + name = 'pipedriveOAuth2Api'; + extends = [ + 'oAuth2Api', + ]; + displayName = 'Pipedrive OAuth2 API'; + properties = [ + { + displayName: 'Authorization URL', + name: 'authUrl', + type: 'hidden' as NodePropertyTypes, + default: 'https://oauth.pipedrive.com/oauth/authorize', + required: true, + }, + { + displayName: 'Access Token URL', + name: 'accessTokenUrl', + type: 'hidden' as NodePropertyTypes, + default: 'https://oauth.pipedrive.com/oauth/token', + required: true, + }, + { + displayName: 'Scope', + name: 'scope', + type: 'hidden' as NodePropertyTypes, + default: '', + }, + { + displayName: 'Auth URI Query Parameters', + name: 'authQueryParameters', + type: 'hidden' as NodePropertyTypes, + default: '', + }, + { + displayName: 'Authentication', + name: 'authentication', + type: 'hidden' as NodePropertyTypes, + default: 'header', + }, + ]; +} diff --git a/packages/nodes-base/nodes/Pipedrive/GenericFunctions.ts b/packages/nodes-base/nodes/Pipedrive/GenericFunctions.ts index 069ff5fe55..17033eaf76 100644 --- a/packages/nodes-base/nodes/Pipedrive/GenericFunctions.ts +++ b/packages/nodes-base/nodes/Pipedrive/GenericFunctions.ts @@ -25,7 +25,6 @@ export interface ICustomProperties { [key: string]: ICustomInterface; } - /** * Make an API request to Pipedrive * @@ -36,16 +35,7 @@ export interface ICustomProperties { * @returns {Promise} */ export async function pipedriveApiRequest(this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions, method: string, endpoint: string, body: IDataObject, query?: IDataObject, formData?: IDataObject, downloadFile?: boolean): Promise { // tslint:disable-line:no-any - const credentials = this.getCredentials('pipedriveApi'); - if (credentials === undefined) { - throw new Error('No credentials got returned!'); - } - - if (query === undefined) { - query = {}; - } - - query.api_token = credentials.apiToken; + const authenticationMethod = this.getNodeParameter('authentication', 0); const options: OptionsWithUri = { headers: { @@ -70,9 +60,28 @@ export async function pipedriveApiRequest(this: IHookFunctions | IExecuteFunctio options.formData = formData; } + if (query === undefined) { + query = {}; + } + + let responseData; + try { - //@ts-ignore - const responseData = await this.helpers.request(options); + if (authenticationMethod === 'basicAuth' || authenticationMethod === 'apiToken') { + + const credentials = this.getCredentials('pipedriveApi'); + if (credentials === undefined) { + throw new Error('No credentials got returned!'); + } + + query.api_token = credentials.apiToken; + + //@ts-ignore + responseData = await this.helpers.request(options); + + } else { + responseData = await this.helpers.requestOAuth2!.call(this, 'pipedriveOAuth2Api', options); + } if (downloadFile === true) { return { @@ -88,7 +97,7 @@ export async function pipedriveApiRequest(this: IHookFunctions | IExecuteFunctio additionalData: responseData.additional_data, data: responseData.data, }; - } catch (error) { + } catch(error) { if (error.statusCode === 401) { // Return a clear error throw new Error('The Pipedrive credentials are not valid!'); @@ -108,8 +117,6 @@ export async function pipedriveApiRequest(this: IHookFunctions | IExecuteFunctio } } - - /** * Make an API request to paginated Pipedrive endpoint * and return all results diff --git a/packages/nodes-base/nodes/Pipedrive/Pipedrive.node.ts b/packages/nodes-base/nodes/Pipedrive/Pipedrive.node.ts index 13786ba94e..a1f48305aa 100644 --- a/packages/nodes-base/nodes/Pipedrive/Pipedrive.node.ts +++ b/packages/nodes-base/nodes/Pipedrive/Pipedrive.node.ts @@ -62,9 +62,44 @@ export class Pipedrive implements INodeType { { name: 'pipedriveApi', required: true, - } + displayOptions: { + show: { + authentication: [ + 'apiToken', + ], + }, + }, + }, + { + name: 'pipedriveOAuth2Api', + required: true, + displayOptions: { + show: { + authentication: [ + 'oAuth2', + ], + }, + }, + }, ], properties: [ + { + displayName: 'Authentication', + name: 'authentication', + type: 'options', + options: [ + { + name: 'API Token', + value: 'apiToken' + }, + { + name: 'OAuth2', + value: 'oAuth2', + }, + ], + default: 'apiToken', + description: 'Method of authentication.', + }, { displayName: 'Resource', name: 'resource', @@ -2102,6 +2137,7 @@ export class Pipedrive implements INodeType { displayName: 'Term', name: 'term', type: 'string', + required: true, displayOptions: { show: { operation: [ diff --git a/packages/nodes-base/nodes/Pipedrive/PipedriveTrigger.node.ts b/packages/nodes-base/nodes/Pipedrive/PipedriveTrigger.node.ts index 2c55246a32..06aa9c9234 100644 --- a/packages/nodes-base/nodes/Pipedrive/PipedriveTrigger.node.ts +++ b/packages/nodes-base/nodes/Pipedrive/PipedriveTrigger.node.ts @@ -14,8 +14,10 @@ import { } from './GenericFunctions'; import * as basicAuth from 'basic-auth'; -import { Response } from 'express'; +import { + Response, +} from 'express'; function authorizationError(resp: Response, realm: string, responseCode: number, message?: string) { if (message === undefined) { @@ -179,7 +181,6 @@ export class PipedriveTrigger implements INodeType { description: 'Type of object to receive notifications about.', }, ], - }; // @ts-ignore (because of request) @@ -276,8 +277,6 @@ export class PipedriveTrigger implements INodeType { }, }; - - async webhook(this: IWebhookFunctions): Promise { const req = this.getRequestObject(); const resp = this.getResponseObject(); diff --git a/packages/nodes-base/package.json b/packages/nodes-base/package.json index c18c1a3df1..e9d965aa21 100644 --- a/packages/nodes-base/package.json +++ b/packages/nodes-base/package.json @@ -113,6 +113,7 @@ "dist/credentials/PagerDutyOAuth2Api.credentials.js", "dist/credentials/PayPalApi.credentials.js", "dist/credentials/PipedriveApi.credentials.js", + "dist/credentials/PipedriveOAuth2Api.credentials.js", "dist/credentials/Postgres.credentials.js", "dist/credentials/PostmarkApi.credentials.js", "dist/credentials/QuestDb.credentials.js",