Improvements to Hubspot-Node

This commit is contained in:
ricardo
2020-06-13 19:48:24 -04:00
parent e2490ad5a3
commit bc4d407c44
5 changed files with 62 additions and 414 deletions

View File

@@ -14,7 +14,12 @@ import {
} from 'n8n-workflow';
export async function hubspotApiRequest(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, method: string, endpoint: string, body: any = {}, query: IDataObject = {}, uri?: string): Promise<any> { // tslint:disable-line:no-any
const authenticationMethod = this.getNodeParameter('authentication', 0);
let authenticationMethod = this.getNodeParameter('authentication', 0);
if (this.getNode().type.includes('Trigger')) {
authenticationMethod = 'developerApi';
}
const options: OptionsWithUri = {
method,
@@ -26,26 +31,40 @@ export async function hubspotApiRequest(this: IHookFunctions | IExecuteFunctions
};
try {
if (authenticationMethod === 'accessToken') {
if (authenticationMethod === 'apiKey') {
const credentials = this.getCredentials('hubspotApi');
options.qs.hapikey = credentials!.apiKey as string;
return await this.helpers.request!(options);
} else if (authenticationMethod === 'developerApi') {
const credentials = this.getCredentials('hubspotDeveloperApi');
options.qs.hapikey = credentials!.apiKey as string;
return await this.helpers.request!(options);
} else {
// @ts-ignore
return await this.helpers.requestOAuth2!.call(this, 'hubspotOAuth2Api', options, 'Bearer');
}
} catch (error) {
if (error.response && error.response.body && error.response.body.errors) {
// Try to return the error prettier
let errorMessages = error.response.body.errors;
let errorMessages;
if (errorMessages[0].message) {
// @ts-ignore
errorMessages = errorMessages.map(errorItem => errorItem.message);
if (error.response && error.response.body) {
if (error.response.body.message) {
errorMessages = [error.response.body.message];
} else if (error.response.body.errors) {
// Try to return the error prettier
errorMessages = error.response.body.errors;
if (errorMessages[0].message) {
// @ts-ignore
errorMessages = errorMessages.map(errorItem => errorItem.message);
}
}
throw new Error(`Hubspot error response [${error.statusCode}]: ${errorMessages.join('|')}`);
}

View File

@@ -76,7 +76,7 @@ export class Hubspot implements INodeType {
displayOptions: {
show: {
authentication: [
'accessToken',
'apiKey',
],
},
},
@@ -100,15 +100,15 @@ export class Hubspot implements INodeType {
type: 'options',
options: [
{
name: 'Access Token',
value: 'accessToken',
name: 'API Key',
value: 'apiKey',
},
{
name: 'OAuth2',
value: 'oAuth2',
},
],
default: 'accessToken',
default: 'apiKey',
description: 'The method of authentication.',
},
{

View File

@@ -37,24 +37,6 @@ export class HubspotTrigger implements INodeType {
{
name: 'hubspotDeveloperApi',
required: true,
displayOptions: {
show: {
authentication: [
'developerApi',
],
},
},
},
{
name: 'hubspotOAuth2Api',
required: true,
displayOptions: {
show: {
authentication: [
'oAuth2',
],
},
},
},
],
webhooks: [
@@ -72,23 +54,6 @@ export class HubspotTrigger implements INodeType {
},
],
properties: [
{
displayName: 'Authentication',
name: 'authentication',
type: 'options',
options: [
{
name: 'Developer API',
value: 'developerApi',
},
{
name: 'OAuth2',
value: 'oAuth2',
},
],
default: 'developerApi',
description: 'The method of authentication.',
},
{
displayName: 'App ID',
name: 'appId',
@@ -282,15 +247,7 @@ export class HubspotTrigger implements INodeType {
async webhook(this: IWebhookFunctions): Promise<IWebhookResponseData> {
const authenticationMethod = this.getNodeParameter('authentication') as string;
let credentials : IDataObject;
if (authenticationMethod === 'hubspotDeveloperApi') {
credentials = this.getCredentials('hubspotDeveloperApi') as IDataObject;
} else {
credentials = this.getCredentials('hubspotOAuth2Api') as IDataObject;
}
const credentials = this.getCredentials('hubspotDeveloperApi') as IDataObject;
if (credentials === undefined) {
throw new Error('No credentials found!');
@@ -303,12 +260,18 @@ export class HubspotTrigger implements INodeType {
if (headerData['x-hubspot-signature'] === undefined) {
return {};
}
const hash = `${credentials!.clientSecret}${JSON.stringify(bodyData)}`;
const signature = createHash('sha256').update(hash).digest('hex');
//@ts-ignore
if (signature !== headerData['x-hubspot-signature']) {
return {};
// check signare if client secret is defined
if (credentials.clientSecret !== '') {
const hash = `${credentials!.clientSecret}${JSON.stringify(bodyData)}`;
const signature = createHash('sha256').update(hash).digest('hex');
//@ts-ignore
if (signature !== headerData['x-hubspot-signature']) {
return {};
}
}
for (let i = 0; i < bodyData.length; i++) {
const subscriptionType = bodyData[i].subscriptionType as string;
if (subscriptionType.includes('contact')) {