Improve some things on EventbriteTrigger-Node

This commit is contained in:
Jan Oberhauser 2020-01-04 13:45:14 -06:00
parent f4780480f4
commit 179a1d5a8b
3 changed files with 55 additions and 29 deletions

View file

@ -4,16 +4,17 @@ import {
} from 'n8n-core'; } from 'n8n-core';
import { import {
IDataObject,
ILoadOptionsFunctions,
INodePropertyOptions,
INodeTypeDescription, INodeTypeDescription,
INodeType, INodeType,
IWebhookResponseData, IWebhookResponseData,
ILoadOptionsFunctions,
INodePropertyOptions,
} from 'n8n-workflow'; } from 'n8n-workflow';
import { import {
eventbriteApiRequestAllItems,
eventbriteApiRequest, eventbriteApiRequest,
eventbriteApiRequestAllItems,
} from './GenericFunctions'; } from './GenericFunctions';
export class EventbriteTrigger implements INodeType { export class EventbriteTrigger implements INodeType {
@ -26,7 +27,7 @@ export class EventbriteTrigger implements INodeType {
description: 'Handle Eventbrite events via webhooks', description: 'Handle Eventbrite events via webhooks',
defaults: { defaults: {
name: 'Eventbrite Trigger', name: 'Eventbrite Trigger',
color: '#559922', color: '#dc5237',
}, },
inputs: [], inputs: [],
outputs: ['main'], outputs: ['main'],
@ -137,6 +138,13 @@ export class EventbriteTrigger implements INodeType {
default: [], default: [],
description: '', description: '',
}, },
{
displayName: 'Resolve Data',
name: 'resolveData',
type: 'boolean',
default: true,
description: 'By default does the webhook-data only contain the URL to receive<br />the object data manually. If this option gets activated it<br />will resolve the data automatically.',
},
], ],
}; };
@ -147,8 +155,7 @@ export class EventbriteTrigger implements INodeType {
// select them easily // select them easily
async getOrganizations(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> { async getOrganizations(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const returnData: INodePropertyOptions[] = []; const returnData: INodePropertyOptions[] = [];
let organizations; const organizations = await eventbriteApiRequestAllItems.call(this, 'organizations', 'GET', '/users/me/organizations');
organizations = await eventbriteApiRequestAllItems.call(this, 'organizations', 'GET', '/users/me/organizations');
for (const organization of organizations) { for (const organization of organizations) {
const organizationName = organization.name; const organizationName = organization.name;
const organizationId = organization.id; const organizationId = organization.id;
@ -164,8 +171,7 @@ export class EventbriteTrigger implements INodeType {
async getEvents(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> { async getEvents(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const returnData: INodePropertyOptions[] = []; const returnData: INodePropertyOptions[] = [];
const organization = this.getCurrentNodeParameter('organization'); const organization = this.getCurrentNodeParameter('organization');
let events; const events = await eventbriteApiRequestAllItems.call(this, 'events', 'GET', `/organizations/${organization}/events`);
events = await eventbriteApiRequestAllItems.call(this, 'events', 'GET', `/organizations/${organization}/events`);
for (const event of events) { for (const event of events) {
const eventName = event.name.text; const eventName = event.name.text;
const eventId = event.id; const eventId = event.id;
@ -182,39 +188,32 @@ export class EventbriteTrigger implements INodeType {
webhookMethods = { webhookMethods = {
default: { default: {
async checkExists(this: IHookFunctions): Promise<boolean> { async checkExists(this: IHookFunctions): Promise<boolean> {
let webhooks;
const webhookData = this.getWorkflowStaticData('node'); const webhookData = this.getWorkflowStaticData('node');
if (webhookData.webhookId === undefined) { if (webhookData.webhookId === undefined) {
return false; return false;
} }
const endpoint = `/webhooks/${webhookData.webhookId}/`; const endpoint = `/webhooks/${webhookData.webhookId}/`;
try { try {
webhooks = await eventbriteApiRequest.call(this, 'GET', endpoint); await eventbriteApiRequest.call(this, 'GET', endpoint);
} catch (e) { } catch (e) {
return false; return false;
} }
return true; return true;
}, },
async create(this: IHookFunctions): Promise<boolean> { async create(this: IHookFunctions): Promise<boolean> {
let body, responseData;
const webhookUrl = this.getNodeWebhookUrl('default'); const webhookUrl = this.getNodeWebhookUrl('default');
const webhookData = this.getWorkflowStaticData('node'); const webhookData = this.getWorkflowStaticData('node');
const event = this.getNodeParameter('event') as string; const event = this.getNodeParameter('event') as string;
const actions = this.getNodeParameter('actions') as string[]; const actions = this.getNodeParameter('actions') as string[];
const endpoint = `/webhooks/`; const endpoint = `/webhooks/`;
// @ts-ignore const body: IDataObject = {
body = {
endpoint_url: webhookUrl, endpoint_url: webhookUrl,
actions: actions.join(','), actions: actions.join(','),
event_id: event, event_id: event,
}; };
try {
responseData = await eventbriteApiRequest.call(this, 'POST', endpoint, body); const responseData = await eventbriteApiRequest.call(this, 'POST', endpoint, body);
} catch(error) {
console.log(error)
return false;
}
// @ts-ignore
webhookData.webhookId = responseData.id; webhookData.webhookId = responseData.id;
return true; return true;
}, },
@ -238,9 +237,37 @@ export class EventbriteTrigger implements INodeType {
async webhook(this: IWebhookFunctions): Promise<IWebhookResponseData> { async webhook(this: IWebhookFunctions): Promise<IWebhookResponseData> {
const req = this.getRequestObject(); const req = this.getRequestObject();
if (req.body.api_url === undefined) {
throw new Error('The received data does not contain required "api_url" property!');
}
const resolveData = this.getNodeParameter('resolveData', false) as boolean;
if (resolveData === false) {
// Return the data as it got received
return {
workflowData: [
this.helpers.returnJsonArray(req.body),
],
};
}
if (req.body.api_url.includes('api-endpoint-to-fetch-object-details')) {
return {
workflowData: [
this.helpers.returnJsonArray({
placeholder: 'Test received. To display actual data of object get the webhook triggered by performing the action which triggers it.',
})
],
};
}
const responseData = await eventbriteApiRequest.call(this, 'GET', '', {}, undefined, req.body.api_url);
return { return {
workflowData: [ workflowData: [
this.helpers.returnJsonArray(req.body) this.helpers.returnJsonArray(responseData),
], ],
}; };
} }

View file

@ -1,13 +1,14 @@
import { OptionsWithUri } from 'request'; import { OptionsWithUri } from 'request';
import { import {
IExecuteFunctions, IExecuteFunctions,
IExecuteSingleFunctions,
IHookFunctions, IHookFunctions,
ILoadOptionsFunctions, ILoadOptionsFunctions,
IExecuteSingleFunctions, IWebhookFunctions,
} from 'n8n-core'; } from 'n8n-core';
import { IDataObject } from 'n8n-workflow'; import { IDataObject } from 'n8n-workflow';
export async function eventbriteApiRequest(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, method: string, resource: string, body: any = {}, qs: IDataObject = {}, uri?: string, option: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any export async function eventbriteApiRequest(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions | IWebhookFunctions, method: string, resource: string, body: any = {}, qs: IDataObject = {}, uri?: string, option: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
const credentials = this.getCredentials('eventbriteApi'); const credentials = this.getCredentials('eventbriteApi');
if (credentials === undefined) { if (credentials === undefined) {
throw new Error('No credentials got returned!'); throw new Error('No credentials got returned!');
@ -30,11 +31,11 @@ export async function eventbriteApiRequest(this: IHookFunctions | IExecuteFuncti
return await this.helpers.request!(options); return await this.helpers.request!(options);
} catch (error) { } catch (error) {
let errorMessage = error.message; let errorMessage = error.message;
if (error.response.body) { if (error.response.body && error.response.body.error_description) {
errorMessage = error.response.body.message || error.response.body.Message || error.message; errorMessage = error.response.body.error_description;
} }
throw new Error(errorMessage); throw new Error('Eventbrite Error: ' + errorMessage);
} }
} }
@ -48,10 +49,8 @@ export async function eventbriteApiRequestAllItems(this: IHookFunctions | IExecu
let responseData; let responseData;
let uri: string | undefined;
do { do {
responseData = await eventbriteApiRequest.call(this, method, resource, body, query, uri); responseData = await eventbriteApiRequest.call(this, method, resource, body, query);
query.continuation = responseData.pagination.continuation; query.continuation = responseData.pagination.continuation;
returnData.push.apply(returnData, responseData[propertyName]); returnData.push.apply(returnData, responseData[propertyName]);
} while ( } while (

Binary file not shown.

Before

Width:  |  Height:  |  Size: 7.1 KiB

After

Width:  |  Height:  |  Size: 2.1 KiB