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';
import {
IDataObject,
ILoadOptionsFunctions,
INodePropertyOptions,
INodeTypeDescription,
INodeType,
IWebhookResponseData,
ILoadOptionsFunctions,
INodePropertyOptions,
} from 'n8n-workflow';
import {
eventbriteApiRequestAllItems,
eventbriteApiRequest,
eventbriteApiRequestAllItems,
} from './GenericFunctions';
export class EventbriteTrigger implements INodeType {
@ -26,7 +27,7 @@ export class EventbriteTrigger implements INodeType {
description: 'Handle Eventbrite events via webhooks',
defaults: {
name: 'Eventbrite Trigger',
color: '#559922',
color: '#dc5237',
},
inputs: [],
outputs: ['main'],
@ -137,6 +138,13 @@ export class EventbriteTrigger implements INodeType {
default: [],
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
async getOrganizations(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const returnData: INodePropertyOptions[] = [];
let organizations;
organizations = await eventbriteApiRequestAllItems.call(this, 'organizations', 'GET', '/users/me/organizations');
const organizations = await eventbriteApiRequestAllItems.call(this, 'organizations', 'GET', '/users/me/organizations');
for (const organization of organizations) {
const organizationName = organization.name;
const organizationId = organization.id;
@ -164,8 +171,7 @@ export class EventbriteTrigger implements INodeType {
async getEvents(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const returnData: INodePropertyOptions[] = [];
const organization = this.getCurrentNodeParameter('organization');
let events;
events = await eventbriteApiRequestAllItems.call(this, 'events', 'GET', `/organizations/${organization}/events`);
const events = await eventbriteApiRequestAllItems.call(this, 'events', 'GET', `/organizations/${organization}/events`);
for (const event of events) {
const eventName = event.name.text;
const eventId = event.id;
@ -182,39 +188,32 @@ export class EventbriteTrigger implements INodeType {
webhookMethods = {
default: {
async checkExists(this: IHookFunctions): Promise<boolean> {
let webhooks;
const webhookData = this.getWorkflowStaticData('node');
if (webhookData.webhookId === undefined) {
return false;
}
const endpoint = `/webhooks/${webhookData.webhookId}/`;
try {
webhooks = await eventbriteApiRequest.call(this, 'GET', endpoint);
await eventbriteApiRequest.call(this, 'GET', endpoint);
} catch (e) {
return false;
}
return true;
},
async create(this: IHookFunctions): Promise<boolean> {
let body, responseData;
const webhookUrl = this.getNodeWebhookUrl('default');
const webhookData = this.getWorkflowStaticData('node');
const event = this.getNodeParameter('event') as string;
const actions = this.getNodeParameter('actions') as string[];
const endpoint = `/webhooks/`;
// @ts-ignore
body = {
const body: IDataObject = {
endpoint_url: webhookUrl,
actions: actions.join(','),
event_id: event,
};
try {
responseData = await eventbriteApiRequest.call(this, 'POST', endpoint, body);
} catch(error) {
console.log(error)
return false;
}
// @ts-ignore
const responseData = await eventbriteApiRequest.call(this, 'POST', endpoint, body);
webhookData.webhookId = responseData.id;
return true;
},
@ -238,9 +237,37 @@ export class EventbriteTrigger implements INodeType {
async webhook(this: IWebhookFunctions): Promise<IWebhookResponseData> {
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 {
workflowData: [
this.helpers.returnJsonArray(req.body)
this.helpers.returnJsonArray(responseData),
],
};
}

View file

@ -1,13 +1,14 @@
import { OptionsWithUri } from 'request';
import {
IExecuteFunctions,
IExecuteSingleFunctions,
IHookFunctions,
ILoadOptionsFunctions,
IExecuteSingleFunctions,
IWebhookFunctions,
} from 'n8n-core';
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');
if (credentials === undefined) {
throw new Error('No credentials got returned!');
@ -30,11 +31,11 @@ export async function eventbriteApiRequest(this: IHookFunctions | IExecuteFuncti
return await this.helpers.request!(options);
} catch (error) {
let errorMessage = error.message;
if (error.response.body) {
errorMessage = error.response.body.message || error.response.body.Message || error.message;
if (error.response.body && error.response.body.error_description) {
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 uri: string | undefined;
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;
returnData.push.apply(returnData, responseData[propertyName]);
} while (

Binary file not shown.

Before

Width:  |  Height:  |  Size: 7.1 KiB

After

Width:  |  Height:  |  Size: 2.1 KiB