feat(Webflow Node): Update to use the v2 API (#9996)

This commit is contained in:
Jon
2024-08-07 10:18:05 +01:00
committed by GitHub
parent 15f10ec325
commit 6d8323fade
18 changed files with 1514 additions and 569 deletions

View File

@@ -0,0 +1,179 @@
/* eslint-disable n8n-nodes-base/node-filename-against-convention */
import type {
IHookFunctions,
IDataObject,
INodeType,
INodeTypeDescription,
INodeTypeBaseDescription,
IWebhookFunctions,
IWebhookResponseData,
} from 'n8n-workflow';
import { getSites, webflowApiRequest } from '../GenericFunctions';
export class WebflowTriggerV2 implements INodeType {
description: INodeTypeDescription;
constructor(baseDescription: INodeTypeBaseDescription) {
this.description = {
...baseDescription,
displayName: 'Webflow Trigger',
name: 'webflowTrigger',
icon: 'file:webflow.svg',
group: ['trigger'],
version: 2,
description: 'Handle Webflow events via webhooks',
defaults: {
name: 'Webflow Trigger',
},
// eslint-disable-next-line n8n-nodes-base/node-class-description-inputs-wrong-regular-node
inputs: [],
outputs: ['main'],
credentials: [
{
name: 'webflowOAuth2Api',
required: true,
},
],
webhooks: [
{
name: 'default',
httpMethod: 'POST',
responseMode: 'onReceived',
path: 'webhook',
},
],
properties: [
{
displayName: 'Site Name or ID',
name: 'site',
type: 'options',
required: true,
default: '',
typeOptions: {
loadOptionsMethod: 'getSites',
},
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>.',
},
{
displayName: 'Event',
name: 'event',
type: 'options',
required: true,
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',
value: 'ecomm_inventory_changed',
},
{
name: 'Ecomm New Order',
value: 'ecomm_new_order',
},
{
name: 'Ecomm Order Changed',
value: 'ecomm_order_changed',
},
{
name: 'Form Submission',
value: 'form_submission',
},
{
name: 'Site Publish',
value: 'site_publish',
},
],
default: 'form_submission',
},
],
};
}
methods = {
loadOptions: {
getSites,
},
};
webhookMethods = {
default: {
async checkExists(this: IHookFunctions): Promise<boolean> {
const webhookData = this.getWorkflowStaticData('node');
const webhookUrl = this.getNodeWebhookUrl('default');
const siteId = this.getNodeParameter('site') as string;
const event = this.getNodeParameter('event') as string;
const registeredWebhooks = await webflowApiRequest.call(
this,
'GET',
`/sites/${siteId}/webhooks`,
);
const webhooks = registeredWebhooks.body?.webhooks || registeredWebhooks;
for (const webhook of webhooks) {
if (webhook.url === webhookUrl && webhook.triggerType === event) {
webhookData.webhookId = webhook._id;
return true;
}
}
return false;
},
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,
};
const response = await webflowApiRequest.call(this, 'POST', endpoint, body);
const _id = response.body?._id || response._id;
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);
} catch (error) {
return false;
}
const deleted = responseData.body?.deleted || responseData.deleted;
if (!deleted) {
return false;
}
delete webhookData.webhookId;
return true;
},
},
};
async webhook(this: IWebhookFunctions): Promise<IWebhookResponseData> {
const req = this.getRequestObject();
return {
workflowData: [this.helpers.returnJsonArray(req.body as IDataObject[])],
};
}
}

View File

@@ -0,0 +1,32 @@
import type {
IExecuteFunctions,
INodeType,
INodeTypeBaseDescription,
INodeTypeDescription,
} from 'n8n-workflow';
import { getSites, getCollections, getFields } from '../GenericFunctions';
import { versionDescription } from './actions/versionDescription';
import { router } from './actions/router';
export class WebflowV2 implements INodeType {
description: INodeTypeDescription;
constructor(baseDescription: INodeTypeBaseDescription) {
this.description = {
...baseDescription,
...versionDescription,
};
}
methods = {
loadOptions: {
getSites,
getCollections,
getFields,
},
};
async execute(this: IExecuteFunctions) {
return await router.call(this);
}
}

View File

@@ -0,0 +1,56 @@
import type { INodeProperties } from 'n8n-workflow';
import * as create from './create.operation';
import * as deleteItem from './delete.operation';
import * as get from './get.operation';
import * as getAll from './getAll.operation';
import * as update from './update.operation';
export { create, deleteItem, get, getAll, update };
export const description: INodeProperties[] = [
{
displayName: 'Operation',
name: 'operation',
type: 'options',
noDataExpression: true,
default: 'get',
options: [
{
name: 'Create',
value: 'create',
action: 'Create an item',
},
{
name: 'Delete',
value: 'deleteItem',
action: 'Delete an item',
},
{
name: 'Get',
value: 'get',
action: 'Get an item',
},
{
name: 'Get Many',
value: 'getAll',
action: 'Get many items',
},
{
name: 'Update',
value: 'update',
action: 'Update an item',
},
],
displayOptions: {
show: {
resource: ['item'],
},
},
},
...create.description,
...deleteItem.description,
...get.description,
...getAll.description,
...update.description,
];

View File

@@ -0,0 +1,139 @@
import type {
IDataObject,
INodeExecutionData,
INodeProperties,
IExecuteFunctions,
} from 'n8n-workflow';
import { updateDisplayOptions, wrapData } from '../../../../../utils/utilities';
import { webflowApiRequest } from '../../../GenericFunctions';
const properties: INodeProperties[] = [
{
displayName: 'Site Name or ID',
name: 'siteId',
type: 'options',
required: true,
typeOptions: {
loadOptionsMethod: 'getSites',
},
default: '',
description:
'ID of the site containing the collection whose items to add to. Choose from the list, or specify an ID using an <a href="https://docs.n8n.io/code-examples/expressions/">expression</a>.',
},
{
displayName: 'Collection Name or ID',
name: 'collectionId',
type: 'options',
required: true,
typeOptions: {
loadOptionsMethod: 'getCollections',
loadOptionsDependsOn: ['siteId'],
},
default: '',
description:
'ID of the collection to add an item to. Choose from the list, or specify an ID using an <a href="https://docs.n8n.io/code-examples/expressions/">expression</a>.',
},
{
displayName: 'Live',
name: 'live',
type: 'boolean',
required: true,
default: false,
description: 'Whether the item should be published on the live site',
},
{
displayName: 'Fields',
name: 'fieldsUi',
placeholder: 'Add Field',
type: 'fixedCollection',
typeOptions: {
multipleValues: true,
},
default: {},
options: [
{
displayName: 'Field',
name: 'fieldValues',
values: [
{
displayName: 'Field Name or ID',
name: 'fieldId',
type: 'options',
typeOptions: {
loadOptionsMethod: 'getFields',
loadOptionsDependsOn: ['collectionId'],
},
default: '',
description:
'Field to set for the item to create. Choose from the list, or specify an ID using an <a href="https://docs.n8n.io/code-examples/expressions/">expression</a>.',
},
{
displayName: 'Field Value',
name: 'fieldValue',
type: 'string',
default: '',
description: 'Value to set for the item to create',
},
],
},
],
},
];
const displayOptions = {
show: {
resource: ['item'],
operation: ['create'],
},
};
export const description = updateDisplayOptions(displayOptions, properties);
export async function execute(
this: IExecuteFunctions,
items: INodeExecutionData[],
): Promise<INodeExecutionData[]> {
const returnData: INodeExecutionData[] = [];
let responseData;
for (let i = 0; i < items.length; i++) {
try {
const collectionId = this.getNodeParameter('collectionId', i) as string;
const uiFields = this.getNodeParameter('fieldsUi.fieldValues', i, []) as IDataObject[];
const live = this.getNodeParameter('live', i) as boolean;
const fieldData = {} as IDataObject;
uiFields.forEach((data) => (fieldData[data.fieldId as string] = data.fieldValue));
const body: IDataObject = {
fieldData,
};
responseData = await webflowApiRequest.call(
this,
'POST',
`/collections/${collectionId}/items`,
body,
{ live },
);
const executionData = this.helpers.constructExecutionMetaData(
wrapData(responseData.body as IDataObject[]),
{ itemData: { item: i } },
);
returnData.push(...executionData);
} catch (error) {
if (this.continueOnFail()) {
returnData.push({ json: { message: error.message, error } });
continue;
}
throw error;
}
}
return returnData;
}

View File

@@ -0,0 +1,94 @@
import type {
IDataObject,
INodeExecutionData,
INodeProperties,
IExecuteFunctions,
} from 'n8n-workflow';
import { updateDisplayOptions, wrapData } from '../../../../../utils/utilities';
import { webflowApiRequest } from '../../../GenericFunctions';
const properties: INodeProperties[] = [
{
displayName: 'Site Name or ID',
name: 'siteId',
type: 'options',
required: true,
typeOptions: {
loadOptionsMethod: 'getSites',
},
default: '',
description:
'ID of the site containing the collection whose items to operate on. Choose from the list, or specify an ID using an <a href="https://docs.n8n.io/code-examples/expressions/">expression</a>.',
},
{
displayName: 'Collection Name or ID',
name: 'collectionId',
type: 'options',
required: true,
typeOptions: {
loadOptionsMethod: 'getCollections',
loadOptionsDependsOn: ['siteId'],
},
default: '',
description:
'ID of the collection whose items to operate on. Choose from the list, or specify an ID using an <a href="https://docs.n8n.io/code-examples/expressions/">expression</a>.',
},
{
displayName: 'Item ID',
name: 'itemId',
type: 'string',
required: true,
default: '',
description: 'ID of the item to operate on',
},
];
const displayOptions = {
show: {
resource: ['item'],
operation: ['deleteItem'],
},
};
export const description = updateDisplayOptions(displayOptions, properties);
export async function execute(
this: IExecuteFunctions,
items: INodeExecutionData[],
): Promise<INodeExecutionData[]> {
const returnData: INodeExecutionData[] = [];
for (let i = 0; i < items.length; i++) {
try {
const collectionId = this.getNodeParameter('collectionId', i) as string;
const itemId = this.getNodeParameter('itemId', i) as string;
let responseData = await webflowApiRequest.call(
this,
'DELETE',
`/collections/${collectionId}/items/${itemId}`,
);
if (responseData.statusCode === 204) {
responseData = { success: true };
} else {
responseData = { success: false };
}
const executionData = this.helpers.constructExecutionMetaData(
wrapData(responseData as IDataObject[]),
{ itemData: { item: i } },
);
returnData.push(...executionData);
} catch (error) {
if (this.continueOnFail()) {
returnData.push({ json: { message: error.message, error } });
continue;
}
throw error;
}
}
return returnData;
}

View File

@@ -0,0 +1,88 @@
import type {
IDataObject,
INodeExecutionData,
INodeProperties,
IExecuteFunctions,
} from 'n8n-workflow';
import { updateDisplayOptions, wrapData } from '../../../../../utils/utilities';
import { webflowApiRequest } from '../../../GenericFunctions';
const properties: INodeProperties[] = [
{
displayName: 'Site Name or ID',
name: 'siteId',
type: 'options',
required: true,
typeOptions: {
loadOptionsMethod: 'getSites',
},
default: '',
description:
'ID of the site containing the collection whose items to operate on. Choose from the list, or specify an ID using an <a href="https://docs.n8n.io/code-examples/expressions/">expression</a>.',
},
{
displayName: 'Collection Name or ID',
name: 'collectionId',
type: 'options',
required: true,
typeOptions: {
loadOptionsMethod: 'getCollections',
loadOptionsDependsOn: ['siteId'],
},
default: '',
description:
'ID of the collection whose items to operate on. Choose from the list, or specify an ID using an <a href="https://docs.n8n.io/code-examples/expressions/">expression</a>.',
},
{
displayName: 'Item ID',
name: 'itemId',
type: 'string',
required: true,
default: '',
description: 'ID of the item to operate on',
},
];
const displayOptions = {
show: {
resource: ['item'],
operation: ['get'],
},
};
export const description = updateDisplayOptions(displayOptions, properties);
export async function execute(
this: IExecuteFunctions,
items: INodeExecutionData[],
): Promise<INodeExecutionData[]> {
const returnData: INodeExecutionData[] = [];
let responseData;
for (let i = 0; i < items.length; i++) {
try {
const collectionId = this.getNodeParameter('collectionId', i) as string;
const itemId = this.getNodeParameter('itemId', i) as string;
responseData = await webflowApiRequest.call(
this,
'GET',
`/collections/${collectionId}/items/${itemId}`,
);
const executionData = this.helpers.constructExecutionMetaData(
wrapData(responseData.body as IDataObject[]),
{ itemData: { item: i } },
);
returnData.push(...executionData);
} catch (error) {
if (this.continueOnFail()) {
returnData.push({ json: { message: error.message, error } });
continue;
}
throw error;
}
}
return returnData;
}

View File

@@ -0,0 +1,118 @@
import type {
IDataObject,
INodeExecutionData,
INodeProperties,
IExecuteFunctions,
} from 'n8n-workflow';
import { updateDisplayOptions, wrapData } from '../../../../../utils/utilities';
import { webflowApiRequest, webflowApiRequestAllItems } from '../../../GenericFunctions';
const properties: INodeProperties[] = [
{
displayName: 'Site Name or ID',
name: 'siteId',
type: 'options',
required: true,
typeOptions: {
loadOptionsMethod: 'getSites',
},
default: '',
description:
'ID of the site containing the collection whose items to operate on. Choose from the list, or specify an ID using an <a href="https://docs.n8n.io/code-examples/expressions/">expression</a>.',
},
{
displayName: 'Collection Name or ID',
name: 'collectionId',
type: 'options',
required: true,
typeOptions: {
loadOptionsMethod: 'getCollections',
loadOptionsDependsOn: ['siteId'],
},
default: '',
description:
'ID of the collection whose items to operate on. Choose from the list, or specify an ID using an <a href="https://docs.n8n.io/code-examples/expressions/">expression</a>.',
},
{
displayName: 'Return All',
name: 'returnAll',
type: 'boolean',
default: false,
description: 'Whether to return all results or only up to a given limit',
},
{
displayName: 'Limit',
name: 'limit',
type: 'number',
typeOptions: {
minValue: 1,
maxValue: 100,
},
displayOptions: {
show: {
returnAll: [false],
},
},
default: 100,
description: 'Max number of results to return',
},
];
const displayOptions = {
show: {
resource: ['item'],
operation: ['getAll'],
},
};
export const description = updateDisplayOptions(displayOptions, properties);
export async function execute(
this: IExecuteFunctions,
items: INodeExecutionData[],
): Promise<INodeExecutionData[]> {
const returnData: INodeExecutionData[] = [];
let responseData;
for (let i = 0; i < items.length; i++) {
try {
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
const collectionId = this.getNodeParameter('collectionId', i) as string;
const qs: IDataObject = {};
if (returnAll) {
responseData = await webflowApiRequestAllItems.call(
this,
'GET',
`/collections/${collectionId}/items`,
{},
);
} else {
qs.limit = this.getNodeParameter('limit', i);
responseData = await webflowApiRequest.call(
this,
'GET',
`/collections/${collectionId}/items`,
{},
qs,
);
responseData = responseData.body.items;
}
const executionData = this.helpers.constructExecutionMetaData(
wrapData(responseData as IDataObject[]),
{ itemData: { item: i } },
);
returnData.push(...executionData);
} catch (error) {
if (this.continueOnFail()) {
returnData.push({ json: { message: error.message, error } });
continue;
}
throw error;
}
}
return returnData;
}

View File

@@ -0,0 +1,148 @@
import type {
IDataObject,
INodeExecutionData,
INodeProperties,
IExecuteFunctions,
} from 'n8n-workflow';
import { updateDisplayOptions, wrapData } from '../../../../../utils/utilities';
import { webflowApiRequest } from '../../../GenericFunctions';
const properties: INodeProperties[] = [
{
displayName: 'Site Name or ID',
name: 'siteId',
type: 'options',
required: true,
typeOptions: {
loadOptionsMethod: 'getSites',
},
default: '',
description:
'ID of the site containing the collection whose items to add to. Choose from the list, or specify an ID using an <a href="https://docs.n8n.io/code-examples/expressions/">expression</a>.',
},
{
displayName: 'Collection Name or ID',
name: 'collectionId',
type: 'options',
required: true,
typeOptions: {
loadOptionsMethod: 'getCollections',
loadOptionsDependsOn: ['siteId'],
},
default: '',
description:
'ID of the collection to add an item to. Choose from the list, or specify an ID using an <a href="https://docs.n8n.io/code-examples/expressions/">expression</a>.',
},
{
displayName: 'Item ID',
name: 'itemId',
type: 'string',
required: true,
default: '',
description: 'ID of the item to update',
},
{
displayName: 'Live',
name: 'live',
type: 'boolean',
required: true,
default: false,
description: 'Whether the item should be published on the live site',
},
{
displayName: 'Fields',
name: 'fieldsUi',
placeholder: 'Add Field',
type: 'fixedCollection',
typeOptions: {
multipleValues: true,
},
default: {},
options: [
{
displayName: 'Field',
name: 'fieldValues',
values: [
{
displayName: 'Field Name or ID',
name: 'fieldId',
type: 'options',
typeOptions: {
loadOptionsMethod: 'getFields',
loadOptionsDependsOn: ['collectionId'],
},
default: '',
description:
'Field to set for the item to create. Choose from the list, or specify an ID using an <a href="https://docs.n8n.io/code-examples/expressions/">expression</a>.',
},
{
displayName: 'Field Value',
name: 'fieldValue',
type: 'string',
default: '',
description: 'Value to set for the item to create',
},
],
},
],
},
];
const displayOptions = {
show: {
resource: ['item'],
operation: ['update'],
},
};
export const description = updateDisplayOptions(displayOptions, properties);
export async function execute(
this: IExecuteFunctions,
items: INodeExecutionData[],
): Promise<INodeExecutionData[]> {
const returnData: INodeExecutionData[] = [];
let responseData;
for (let i = 0; i < items.length; i++) {
try {
const collectionId = this.getNodeParameter('collectionId', i) as string;
const itemId = this.getNodeParameter('itemId', i) as string;
const uiFields = this.getNodeParameter('fieldsUi.fieldValues', i, []) as IDataObject[];
const live = this.getNodeParameter('live', i) as boolean;
const fieldData = {} as IDataObject;
uiFields.forEach((data) => (fieldData[data.fieldId as string] = data.fieldValue));
const body: IDataObject = {
fieldData,
};
responseData = await webflowApiRequest.call(
this,
'PATCH',
`/collections/${collectionId}/items/${itemId}`,
body,
{ live },
);
const executionData = this.helpers.constructExecutionMetaData(
wrapData(responseData.body as IDataObject[]),
{ itemData: { item: i } },
);
returnData.push(...executionData);
} catch (error) {
if (this.continueOnFail()) {
returnData.push({ json: { message: error.message, error } });
continue;
}
throw error;
}
}
return returnData;
}

View File

@@ -0,0 +1,7 @@
import type { AllEntities } from 'n8n-workflow';
type NodeMap = {
item: 'create' | 'deleteItem' | 'get' | 'getAll' | 'update';
};
export type WebflowType = AllEntities<NodeMap>;

View File

@@ -0,0 +1,35 @@
import type { IExecuteFunctions, INodeExecutionData } from 'n8n-workflow';
import { NodeOperationError } from 'n8n-workflow';
import type { WebflowType } from './node.type';
import * as item from './Item/Item.resource';
export async function router(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
let returnData: INodeExecutionData[] = [];
const items = this.getInputData();
const resource = this.getNodeParameter<WebflowType>('resource', 0);
const operation = this.getNodeParameter('operation', 0);
const webflowNodeData = {
resource,
operation,
} as WebflowType;
try {
switch (webflowNodeData.resource) {
case 'item':
returnData = await item[webflowNodeData.operation].execute.call(this, items);
break;
default:
throw new NodeOperationError(
this.getNode(),
`The operation "${operation}" is not supported!`,
);
}
} catch (error) {
throw error;
}
return [returnData];
}

View File

@@ -0,0 +1,41 @@
/* eslint-disable n8n-nodes-base/node-filename-against-convention */
import type { INodeTypeDescription } from 'n8n-workflow';
import * as item from './Item/Item.resource';
export const versionDescription: INodeTypeDescription = {
displayName: 'Webflow',
name: 'webflow',
icon: 'file:webflow.svg',
group: ['transform'],
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
description: 'Consume the Webflow API',
version: [2],
defaults: {
name: 'Webflow',
},
inputs: ['main'],
outputs: ['main'],
credentials: [
{
name: 'webflowOAuth2Api',
required: true,
},
],
properties: [
{
displayName: 'Resource',
name: 'resource',
type: 'options',
noDataExpression: true,
options: [
{
name: 'Item',
value: 'item',
},
],
default: 'item',
},
...item.description,
],
};