2023-01-27 03:22:44 -08:00
|
|
|
import type {
|
2023-03-09 09:13:15 -08:00
|
|
|
IExecuteFunctions,
|
2021-04-17 00:25:51 -07:00
|
|
|
IDataObject,
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
INodeExecutionData,
|
|
|
|
INodePropertyOptions,
|
|
|
|
INodeType,
|
|
|
|
INodeTypeDescription,
|
|
|
|
} from 'n8n-workflow';
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { webflowApiRequest, webflowApiRequestAllItems } from './GenericFunctions';
|
2021-04-17 00:25:51 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { itemFields, itemOperations } from './ItemDescription';
|
2021-04-17 00:25:51 -07:00
|
|
|
|
|
|
|
export class Webflow implements INodeType {
|
|
|
|
description: INodeTypeDescription = {
|
|
|
|
displayName: 'Webflow',
|
|
|
|
name: 'webflow',
|
|
|
|
icon: 'file:webflow.svg',
|
|
|
|
group: ['transform'],
|
|
|
|
version: 1,
|
|
|
|
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
|
|
|
|
description: 'Consume the Webflow API',
|
|
|
|
defaults: {
|
|
|
|
name: 'Webflow',
|
|
|
|
},
|
|
|
|
inputs: ['main'],
|
|
|
|
outputs: ['main'],
|
|
|
|
credentials: [
|
|
|
|
{
|
|
|
|
name: 'webflowApi',
|
|
|
|
required: true,
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
2022-08-17 08:50:24 -07:00
|
|
|
authentication: ['accessToken'],
|
2021-04-17 00:25:51 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'webflowOAuth2Api',
|
|
|
|
required: true,
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
2022-08-17 08:50:24 -07:00
|
|
|
authentication: ['oAuth2'],
|
2021-04-17 00:25:51 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
properties: [
|
|
|
|
{
|
|
|
|
displayName: 'Authentication',
|
|
|
|
name: 'authentication',
|
|
|
|
type: 'options',
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'Access Token',
|
|
|
|
value: 'accessToken',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'OAuth2',
|
|
|
|
value: 'oAuth2',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
default: 'accessToken',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Resource',
|
|
|
|
name: 'resource',
|
|
|
|
type: 'options',
|
2022-05-20 14:47:24 -07:00
|
|
|
noDataExpression: true,
|
2021-04-17 00:25:51 -07:00
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'Item',
|
|
|
|
value: 'item',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
default: 'item',
|
|
|
|
},
|
|
|
|
...itemOperations,
|
|
|
|
...itemFields,
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
methods = {
|
|
|
|
loadOptions: {
|
|
|
|
async getSites(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
|
|
const returnData: INodePropertyOptions[] = [];
|
|
|
|
const sites = await webflowApiRequest.call(this, 'GET', '/sites');
|
|
|
|
for (const site of sites) {
|
|
|
|
returnData.push({
|
|
|
|
name: site.name,
|
|
|
|
value: site._id,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return returnData;
|
|
|
|
},
|
|
|
|
async getCollections(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
|
|
const returnData: INodePropertyOptions[] = [];
|
|
|
|
const siteId = this.getCurrentNodeParameter('siteId');
|
2022-08-17 08:50:24 -07:00
|
|
|
const collections = await webflowApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
`/sites/${siteId}/collections`,
|
|
|
|
);
|
2021-04-17 00:25:51 -07:00
|
|
|
for (const collection of collections) {
|
|
|
|
returnData.push({
|
|
|
|
name: collection.name,
|
|
|
|
value: collection._id,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return returnData;
|
|
|
|
},
|
|
|
|
async getFields(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
|
|
const returnData: INodePropertyOptions[] = [];
|
|
|
|
const collectionId = this.getCurrentNodeParameter('collectionId');
|
2022-08-17 08:50:24 -07:00
|
|
|
const { fields } = await webflowApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
`/collections/${collectionId}`,
|
|
|
|
);
|
2021-04-17 00:25:51 -07:00
|
|
|
for (const field of fields) {
|
|
|
|
returnData.push({
|
2022-08-17 08:50:24 -07:00
|
|
|
name: `${field.name} (${field.type}) ${field.required ? ' (required)' : ''}`,
|
2021-04-17 00:25:51 -07:00
|
|
|
value: field.slug,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return returnData;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
|
|
|
const items = this.getInputData();
|
|
|
|
|
2022-12-02 03:53:59 -08:00
|
|
|
const resource = this.getNodeParameter('resource', 0);
|
|
|
|
const operation = this.getNodeParameter('operation', 0);
|
2021-04-17 00:25:51 -07:00
|
|
|
let responseData;
|
2022-08-30 08:55:33 -07:00
|
|
|
const returnData: INodeExecutionData[] = [];
|
2021-04-17 00:25:51 -07:00
|
|
|
|
|
|
|
for (let i = 0; i < items.length; i++) {
|
2021-07-19 23:58:54 -07:00
|
|
|
try {
|
|
|
|
if (resource === 'item') {
|
|
|
|
// *********************************************************************
|
|
|
|
// item
|
|
|
|
// *********************************************************************
|
2021-04-17 00:25:51 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
// https://developers.webflow.com/#item-model
|
2021-04-17 00:25:51 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'create') {
|
|
|
|
// ----------------------------------
|
|
|
|
// item: create
|
|
|
|
// ----------------------------------
|
2021-04-17 00:25:51 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
// https://developers.webflow.com/#create-new-collection-item
|
2021-04-17 00:25:51 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const collectionId = this.getNodeParameter('collectionId', i) as string;
|
2021-04-17 00:25:51 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
const properties = this.getNodeParameter(
|
|
|
|
'fieldsUi.fieldValues',
|
|
|
|
i,
|
|
|
|
[],
|
|
|
|
) as IDataObject[];
|
2021-04-17 00:25:51 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const live = this.getNodeParameter('live', i) as boolean;
|
2021-04-17 00:25:51 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const fields = {} as IDataObject;
|
2021-04-17 00:25:51 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
properties.forEach((data) => (fields[data.fieldId as string] = data.fieldValue));
|
2021-04-17 00:25:51 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const body: IDataObject = {
|
|
|
|
fields,
|
|
|
|
};
|
2021-04-17 00:25:51 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await webflowApiRequest.call(
|
|
|
|
this,
|
|
|
|
'POST',
|
|
|
|
`/collections/${collectionId}/items`,
|
|
|
|
body,
|
|
|
|
{ live },
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
} else if (operation === 'delete') {
|
|
|
|
// ----------------------------------
|
|
|
|
// item: delete
|
|
|
|
// ----------------------------------
|
2021-04-17 00:25:51 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
// https://developers.webflow.com/#remove-collection-item
|
2021-04-17 00:25:51 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const collectionId = this.getNodeParameter('collectionId', i) as string;
|
|
|
|
const itemId = this.getNodeParameter('itemId', i) as string;
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await webflowApiRequest.call(
|
|
|
|
this,
|
|
|
|
'DELETE',
|
|
|
|
`/collections/${collectionId}/items/${itemId}`,
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
} else if (operation === 'get') {
|
|
|
|
// ----------------------------------
|
|
|
|
// item: get
|
|
|
|
// ----------------------------------
|
2021-04-17 00:25:51 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
// https://developers.webflow.com/#get-single-item
|
2021-04-17 00:25:51 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const collectionId = this.getNodeParameter('collectionId', i) as string;
|
|
|
|
const itemId = this.getNodeParameter('itemId', i) as string;
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await webflowApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
`/collections/${collectionId}/items/${itemId}`,
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = responseData.items;
|
|
|
|
} else if (operation === 'getAll') {
|
|
|
|
// ----------------------------------
|
|
|
|
// item: getAll
|
|
|
|
// ----------------------------------
|
2021-04-17 00:25:51 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
// https://developers.webflow.com/#get-all-items-for-a-collection
|
2021-04-17 00:25:51 -07:00
|
|
|
|
2022-11-18 05:31:38 -08:00
|
|
|
const returnAll = this.getNodeParameter('returnAll', 0);
|
2021-07-19 23:58:54 -07:00
|
|
|
const collectionId = this.getNodeParameter('collectionId', i) as string;
|
|
|
|
const qs: IDataObject = {};
|
2021-04-17 00:25:51 -07:00
|
|
|
|
2022-12-02 12:54:28 -08:00
|
|
|
if (returnAll) {
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await webflowApiRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
`/collections/${collectionId}/items`,
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
} else {
|
2022-11-18 06:26:22 -08:00
|
|
|
qs.limit = this.getNodeParameter('limit', 0);
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await webflowApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
`/collections/${collectionId}/items`,
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = responseData.items;
|
|
|
|
}
|
|
|
|
} else if (operation === 'update') {
|
|
|
|
// ----------------------------------
|
|
|
|
// item: update
|
|
|
|
// ----------------------------------
|
2021-04-17 00:25:51 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
// https://developers.webflow.com/#update-collection-item
|
2021-04-17 00:25:51 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const collectionId = this.getNodeParameter('collectionId', i) as string;
|
2021-04-17 00:25:51 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const itemId = this.getNodeParameter('itemId', i) as string;
|
2021-04-17 00:25:51 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
const properties = this.getNodeParameter(
|
|
|
|
'fieldsUi.fieldValues',
|
|
|
|
i,
|
|
|
|
[],
|
|
|
|
) as IDataObject[];
|
2021-04-17 00:25:51 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const live = this.getNodeParameter('live', i) as boolean;
|
2021-04-17 00:25:51 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const fields = {} as IDataObject;
|
2021-04-17 00:25:51 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
properties.forEach((data) => (fields[data.fieldId as string] = data.fieldValue));
|
2021-04-17 00:25:51 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const body: IDataObject = {
|
|
|
|
fields,
|
|
|
|
};
|
2021-04-17 00:25:51 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await webflowApiRequest.call(
|
|
|
|
this,
|
|
|
|
'PUT',
|
|
|
|
`/collections/${collectionId}/items/${itemId}`,
|
|
|
|
body,
|
|
|
|
{ live },
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
2021-04-17 00:25:51 -07:00
|
|
|
}
|
2022-08-30 08:55:33 -07:00
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
2023-02-27 19:39:43 -08:00
|
|
|
this.helpers.returnJsonArray(responseData as IDataObject[]),
|
2022-08-30 08:55:33 -07:00
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
returnData.push(...executionData);
|
2021-07-19 23:58:54 -07:00
|
|
|
} catch (error) {
|
|
|
|
if (this.continueOnFail()) {
|
2022-08-30 08:55:33 -07:00
|
|
|
returnData.push({ json: { error: error.message } });
|
2021-07-19 23:58:54 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
throw error;
|
|
|
|
}
|
2021-04-17 00:25:51 -07:00
|
|
|
}
|
|
|
|
|
2022-08-30 08:55:33 -07:00
|
|
|
return this.prepareOutputData(returnData);
|
2021-04-17 00:25:51 -07:00
|
|
|
}
|
|
|
|
}
|