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,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,
],
};