diff --git a/packages/nodes-base/nodes/Eventbrite/EventbriteTrigger.node.ts b/packages/nodes-base/nodes/Eventbrite/EventbriteTrigger.node.ts index 37cd3af436..ecdb2978da 100644 --- a/packages/nodes-base/nodes/Eventbrite/EventbriteTrigger.node.ts +++ b/packages/nodes-base/nodes/Eventbrite/EventbriteTrigger.node.ts @@ -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
the object data manually. If this option gets activated it
will resolve the data automatically.', + }, ], }; @@ -147,8 +155,7 @@ export class EventbriteTrigger implements INodeType { // select them easily async getOrganizations(this: ILoadOptionsFunctions): Promise { 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 { 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 { - 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 { - 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 { 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), ], }; } diff --git a/packages/nodes-base/nodes/Eventbrite/GenericFunctions.ts b/packages/nodes-base/nodes/Eventbrite/GenericFunctions.ts index dd23089e51..285c89b1f8 100644 --- a/packages/nodes-base/nodes/Eventbrite/GenericFunctions.ts +++ b/packages/nodes-base/nodes/Eventbrite/GenericFunctions.ts @@ -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 { // 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 { // 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 ( diff --git a/packages/nodes-base/nodes/Eventbrite/eventbrite.png b/packages/nodes-base/nodes/Eventbrite/eventbrite.png index d68c3789ed..11ef8cd47d 100644 Binary files a/packages/nodes-base/nodes/Eventbrite/eventbrite.png and b/packages/nodes-base/nodes/Eventbrite/eventbrite.png differ