mirror of
https://github.com/n8n-io/n8n.git
synced 2024-12-25 12:44:07 -08:00
Merge pull request #641 from n8n-io/Pipedrive-OAuth2-support
Pipedrive OAuth2 Support
This commit is contained in:
commit
61d7c52787
|
@ -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',
|
||||
},
|
||||
];
|
||||
}
|
|
@ -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<any>}
|
||||
*/
|
||||
export async function pipedriveApiRequest(this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions, method: string, endpoint: string, body: IDataObject, query?: IDataObject, formData?: IDataObject, downloadFile?: boolean): Promise<any> { // 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
|
||||
|
|
|
@ -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: [
|
||||
|
|
|
@ -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<IWebhookResponseData> {
|
||||
const req = this.getRequestObject();
|
||||
const resp = this.getResponseObject();
|
||||
|
|
|
@ -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",
|
||||
|
|
Loading…
Reference in a new issue