Improvements to Webflow Trigger Node

This commit is contained in:
Jan Oberhauser 2021-05-07 23:32:59 -05:00
parent 8b11eb6332
commit 77da5b70bc

View file

@ -97,6 +97,18 @@ export class WebflowTrigger implements INodeType {
type: 'options', type: 'options',
required: true, required: true,
options: [ options: [
{
name: 'Collection Item Created',
value: 'collection_item_created',
},
{
name: 'Collection Item Deleted',
value: 'collection_item_deleted',
},
{
name: 'Collection Item Updated',
value: 'collection_item_changed',
},
{ {
name: 'Ecomm Inventory Changed', name: 'Ecomm Inventory Changed',
value: 'ecomm_inventory_changed', value: 'ecomm_inventory_changed',
@ -117,57 +129,47 @@ export class WebflowTrigger implements INodeType {
name: 'Site Publish', name: 'Site Publish',
value: 'site_publish', value: 'site_publish',
}, },
{
name: 'Collection Item Created',
value: 'collection_item_created',
},
{
name: 'Collection Item Updated',
value: 'collection_item_changed',
},
{
name: 'Collection Item Deleted',
value: 'collection_item_deleted',
},
], ],
default: 'form_submission', default: 'form_submission',
}, },
{ // {
displayName: 'All collections', // displayName: 'All collections',
name: 'allCollections', // name: 'allCollections',
type: 'boolean', // type: 'boolean',
required: false, // displayOptions: {
default: true, // show: {
description: 'Receive events from all collections', // event: [
}, // 'collection_item_created',
{ // 'collection_item_changed',
displayName: 'Collection', // 'collection_item_deleted',
name: 'collection', // ],
type: 'options', // },
required: false, // },
default: '', // required: false,
typeOptions: { // default: true,
loadOptionsMethod: 'getCollections', // description: 'Receive events from all collections',
loadOptionsDependsOn: [ // },
'site', // {
], // displayName: 'Collection',
}, // name: 'collection',
description: 'Collection that will trigger the events', // type: 'options',
displayOptions: { // required: false,
show: { // default: '',
event: [ // typeOptions: {
'collection_item_created', // loadOptionsMethod: 'getCollections',
'collection_item_changed', // loadOptionsDependsOn: [
'collection_item_deleted', // 'site',
], // ],
}, // },
hide: { // description: 'Collection that will trigger the events',
allCollections: [ // displayOptions: {
true, // show: {
], // allCollections: [
}, // false,
}, // ],
}, // },
// },
// },
], ],
}; };
@ -188,18 +190,18 @@ export class WebflowTrigger implements INodeType {
} }
return returnData; return returnData;
}, },
async getCollections(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> { // async getCollections(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const returnData: INodePropertyOptions[] = []; // const returnData: INodePropertyOptions[] = [];
const siteId = this.getCurrentNodeParameter('site'); // const siteId = this.getCurrentNodeParameter('site');
const collections = await webflowApiRequest.call(this, 'GET', `/sites/${siteId}/collections`); // const collections = await webflowApiRequest.call(this, 'GET', `/sites/${siteId}/collections`);
for (const collection of collections) { // for (const collection of collections) {
returnData.push({ // returnData.push({
name: collection.name, // name: collection.name,
value: collection._id, // value: collection._id,
}); // });
} // }
return returnData; // return returnData;
}, // },
}, },
}; };
@ -225,8 +227,6 @@ export class WebflowTrigger implements INodeType {
const webhookData = this.getWorkflowStaticData('node'); const webhookData = this.getWorkflowStaticData('node');
const siteId = this.getNodeParameter('site') as string; const siteId = this.getNodeParameter('site') as string;
const event = this.getNodeParameter('event') as string; const event = this.getNodeParameter('event') as string;
const collection = this.getNodeParameter('collection') as string;
const allCollections = this.getNodeParameter('allCollections') as boolean;
const endpoint = `/sites/${siteId}/webhooks`; const endpoint = `/sites/${siteId}/webhooks`;
const body: IDataObject = { const body: IDataObject = {
site_id: siteId, site_id: siteId,
@ -234,11 +234,17 @@ export class WebflowTrigger implements INodeType {
url: webhookUrl, url: webhookUrl,
}; };
if(event.startsWith('collection') && !allCollections) {
body.filter = { // if (event.startsWith('collection')) {
'_cid': collection, // const allCollections = this.getNodeParameter('allCollections') as boolean;
};
} // if (allCollections === false) {
// body.filter = {
// 'cid': this.getNodeParameter('collection') as string,
// };
// }
// }
const { _id } = await webflowApiRequest.call(this, 'POST', endpoint, body); const { _id } = await webflowApiRequest.call(this, 'POST', endpoint, body);
webhookData.webhookId = _id; webhookData.webhookId = _id;
return true; return true;
@ -250,7 +256,7 @@ export class WebflowTrigger implements INodeType {
const endpoint = `/sites/${siteId}/webhooks/${webhookData.webhookId}`; const endpoint = `/sites/${siteId}/webhooks/${webhookData.webhookId}`;
try { try {
responseData = await webflowApiRequest.call(this, 'DELETE', endpoint); responseData = await webflowApiRequest.call(this, 'DELETE', endpoint);
} catch(error) { } catch (error) {
return false; return false;
} }
if (!responseData.deleted) { if (!responseData.deleted) {