2023-01-27 03:22:44 -08:00
import type {
2023-03-09 09:13:15 -08:00
IHookFunctions ,
IWebhookFunctions ,
2020-01-17 09:34:36 -08:00
IDataObject ,
ILoadOptionsFunctions ,
INodePropertyOptions ,
2020-10-01 05:01:39 -07:00
INodeType ,
INodeTypeDescription ,
IWebhookResponseData ,
2020-01-17 09:34:36 -08:00
} from 'n8n-workflow' ;
2022-08-17 08:50:24 -07:00
import { webflowApiRequest } from './GenericFunctions' ;
2020-01-17 09:34:36 -08:00
export class WebflowTrigger implements INodeType {
description : INodeTypeDescription = {
displayName : 'Webflow Trigger' ,
2020-01-17 20:41:12 -08:00
name : 'webflowTrigger' ,
2021-04-17 00:25:51 -07:00
icon : 'file:webflow.svg' ,
2020-01-17 09:34:36 -08:00
group : [ 'trigger' ] ,
version : 1 ,
description : 'Handle Webflow events via webhooks' ,
defaults : {
name : 'Webflow Trigger' ,
} ,
inputs : [ ] ,
outputs : [ 'main' ] ,
credentials : [
{
name : 'webflowApi' ,
required : true ,
2020-06-08 08:50:18 -07:00
displayOptions : {
show : {
2022-08-17 08:50:24 -07:00
authentication : [ 'accessToken' ] ,
2020-06-08 08:50:18 -07:00
} ,
} ,
} ,
{
name : 'webflowOAuth2Api' ,
required : true ,
displayOptions : {
show : {
2022-08-17 08:50:24 -07:00
authentication : [ 'oAuth2' ] ,
2020-06-08 08:50:18 -07:00
} ,
} ,
} ,
2020-01-17 09:34:36 -08:00
] ,
webhooks : [
{
name : 'default' ,
httpMethod : 'POST' ,
responseMode : 'onReceived' ,
path : 'webhook' ,
} ,
] ,
properties : [
2020-06-08 08:50:18 -07:00
{
displayName : 'Authentication' ,
name : 'authentication' ,
type : 'options' ,
options : [
{
name : 'Access Token' ,
value : 'accessToken' ,
} ,
{
name : 'OAuth2' ,
value : 'oAuth2' ,
} ,
] ,
default : 'accessToken' ,
} ,
2020-01-17 09:34:36 -08:00
{
2022-06-03 10:23:49 -07:00
displayName : 'Site Name or ID' ,
2020-01-17 09:34:36 -08:00
name : 'site' ,
type : 'options' ,
required : true ,
default : '' ,
typeOptions : {
loadOptionsMethod : 'getSites' ,
} ,
2022-08-17 08:50:24 -07:00
description :
'Site that will trigger the events. Choose from the list, or specify an ID using an <a href="https://docs.n8n.io/code-examples/expressions/">expression</a>.' ,
2020-01-17 09:34:36 -08:00
} ,
{
displayName : 'Event' ,
name : 'event' ,
type : 'options' ,
required : true ,
options : [
2021-05-07 21:32:59 -07:00
{
name : 'Collection Item Created' ,
value : 'collection_item_created' ,
} ,
{
name : 'Collection Item Deleted' ,
value : 'collection_item_deleted' ,
} ,
{
name : 'Collection Item Updated' ,
value : 'collection_item_changed' ,
} ,
2020-01-17 09:34:36 -08:00
{
2020-01-17 20:36:07 -08:00
name : 'Ecomm Inventory Changed' ,
value : 'ecomm_inventory_changed' ,
2020-01-17 09:34:36 -08:00
} ,
{
name : 'Ecomm New Order' ,
value : 'ecomm_new_order' ,
} ,
{
name : 'Ecomm Order Changed' ,
value : 'ecomm_order_changed' ,
} ,
2021-04-17 00:25:51 -07:00
{
name : 'Form Submission' ,
value : 'form_submission' ,
} ,
2020-01-17 09:34:36 -08:00
{
2020-01-17 20:36:07 -08:00
name : 'Site Publish' ,
value : 'site_publish' ,
2020-01-17 09:34:36 -08:00
} ,
] ,
default : 'form_submission' ,
} ,
2021-05-07 21:32:59 -07:00
// {
// displayName: 'All collections',
// name: 'allCollections',
// type: 'boolean',
// displayOptions: {
// show: {
// event: [
// 'collection_item_created',
// 'collection_item_changed',
// 'collection_item_deleted',
// ],
// },
// },
// required: false,
// default: true,
// description: 'Receive events from all collections',
// },
// {
// displayName: 'Collection',
// name: 'collection',
// type: 'options',
// required: false,
// default: '',
// typeOptions: {
// loadOptionsMethod: 'getCollections',
// loadOptionsDependsOn: [
// 'site',
// ],
// },
// description: 'Collection that will trigger the events',
// displayOptions: {
// show: {
// allCollections: [
// false,
// ],
// },
// },
// },
2020-01-17 09:34:36 -08:00
] ,
} ;
methods = {
loadOptions : {
2023-04-19 07:00:49 -07:00
// Get all the sites to display them to user so that they can
2020-01-17 09:34:36 -08:00
// select them easily
async getSites ( this : ILoadOptionsFunctions ) : Promise < INodePropertyOptions [ ] > {
const returnData : INodePropertyOptions [ ] = [ ] ;
const sites = await webflowApiRequest . call ( this , 'GET' , '/sites' ) ;
for ( const site of sites ) {
const siteName = site . name ;
const siteId = site . _id ;
returnData . push ( {
name : siteName ,
value : siteId ,
} ) ;
}
return returnData ;
} ,
2021-05-07 21:32:59 -07:00
// async getCollections(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
// const returnData: INodePropertyOptions[] = [];
// const siteId = this.getCurrentNodeParameter('site');
// const collections = await webflowApiRequest.call(this, 'GET', `/sites/${siteId}/collections`);
// for (const collection of collections) {
// returnData.push({
// name: collection.name,
// value: collection._id,
// });
// }
// return returnData;
// },
2020-01-17 09:34:36 -08:00
} ,
2020-01-17 20:36:07 -08:00
} ;
2020-01-17 09:34:36 -08:00
webhookMethods = {
default : {
async checkExists ( this : IHookFunctions ) : Promise < boolean > {
const webhookData = this . getWorkflowStaticData ( 'node' ) ;
2022-07-04 01:44:26 -07:00
const webhookUrl = this . getNodeWebhookUrl ( 'default' ) ;
2020-01-17 09:34:36 -08:00
const siteId = this . getNodeParameter ( 'site' ) as string ;
2022-07-04 01:44:26 -07:00
const event = this . getNodeParameter ( 'event' ) as string ;
2022-08-17 08:50:24 -07:00
const registeredWebhooks = ( await webflowApiRequest . call (
this ,
'GET' ,
` /sites/ ${ siteId } /webhooks ` ,
) ) as IDataObject [ ] ;
2022-07-04 01:44:26 -07:00
for ( const webhook of registeredWebhooks ) {
if ( webhook . url === webhookUrl && webhook . triggerType === event ) {
webhookData . webhookId = webhook . _id ;
return true ;
}
2020-01-17 09:34:36 -08:00
}
2022-07-04 01:44:26 -07:00
return false ;
2020-01-17 09:34:36 -08:00
} ,
2022-07-04 01:44:26 -07:00
2020-01-17 09:34:36 -08:00
async create ( this : IHookFunctions ) : Promise < boolean > {
const webhookUrl = this . getNodeWebhookUrl ( 'default' ) ;
const webhookData = this . getWorkflowStaticData ( 'node' ) ;
const siteId = this . getNodeParameter ( 'site' ) as string ;
const event = this . getNodeParameter ( 'event' ) as string ;
const endpoint = ` /sites/ ${ siteId } /webhooks ` ;
const body : IDataObject = {
site_id : siteId ,
triggerType : event ,
url : webhookUrl ,
} ;
2021-05-07 21:32:59 -07:00
// if (event.startsWith('collection')) {
// const allCollections = this.getNodeParameter('allCollections') as boolean;
// if (allCollections === false) {
// body.filter = {
// 'cid': this.getNodeParameter('collection') as string,
// };
// }
// }
2020-01-17 09:34:36 -08:00
const { _id } = await webflowApiRequest . call ( this , 'POST' , endpoint , body ) ;
webhookData . webhookId = _id ;
return true ;
} ,
async delete ( this : IHookFunctions ) : Promise < boolean > {
let responseData ;
const webhookData = this . getWorkflowStaticData ( 'node' ) ;
const siteId = this . getNodeParameter ( 'site' ) as string ;
const endpoint = ` /sites/ ${ siteId } /webhooks/ ${ webhookData . webhookId } ` ;
try {
responseData = await webflowApiRequest . call ( this , 'DELETE' , endpoint ) ;
2021-05-07 21:32:59 -07:00
} catch ( error ) {
2020-01-17 09:34:36 -08:00
return false ;
}
if ( ! responseData . deleted ) {
return false ;
}
delete webhookData . webhookId ;
return true ;
} ,
} ,
} ;
async webhook ( this : IWebhookFunctions ) : Promise < IWebhookResponseData > {
const req = this . getRequestObject ( ) ;
return {
2023-02-27 19:39:43 -08:00
workflowData : [ this . helpers . returnJsonArray ( req . body as IDataObject [ ] ) ] ,
2020-01-17 09:34:36 -08:00
} ;
}
}