mirror of
https://github.com/n8n-io/n8n.git
synced 2024-11-10 14:44:05 -08:00
6dcdb30bf4
* ✏️ Alphabetize lint rules * 🔥 Remove duplicates * ⚡ Update `lintfix` script * 👕 Apply `node-param-operation-without-no-data-expression` (#3329) * 👕 Apply `node-param-operation-without-no-data-expression` * 👕 Add exceptions * 👕 Apply `node-param-description-weak` (#3328) Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * 👕 Apply `node-param-option-value-duplicate` (#3331) * 👕 Apply `node-param-description-miscased-json` (#3337) * 👕 Apply `node-param-display-name-excess-inner-whitespace` (#3335) Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * 👕 Apply `node-param-type-options-missing-from-limit` (#3336) * Rule workig as intended * ✏️ Uncomment rules Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * 👕 Apply `node-param-option-name-duplicate` (#3338) Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * 👕 Apply `node-param-description-wrong-for-simplify` (#3334) * ⚡ fix * ⚡ exceptions * ⚡ changed rule ignoring from file to line * 👕 Apply `node-param-resource-without-no-data-expression` (#3339) * 👕 Apply `node-param-display-name-untrimmed` (#3341) * 👕 Apply `node-param-display-name-miscased-id` (#3340) Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * 👕 Apply `node-param-resource-with-plural-option` (#3342) * 👕 Apply `node-param-description-wrong-for-upsert` (#3333) * ⚡ fix * ⚡ replaced record with contact in description * ⚡ fix Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * 👕 Apply `node-param-option-description-identical-to-name` (#3343) * 👕 Apply `node-param-option-name-containing-star` (#3347) * 👕 Apply `node-param-display-name-wrong-for-update-fields` (#3348) * 👕 Apply `node-param-option-name-wrong-for-get-all` (#3345) * ⚡ fix * ⚡ exceptions * 👕 Apply node-param-display-name-wrong-for-simplify (#3344) * Rule working as intended * Uncomented other rules * 👕 Undo and add exceptions Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * ⚡ Alphabetize lint rules * ⚡ Restore `lintfix` script Co-authored-by: Michael Kret <88898367+michael-radency@users.noreply.github.com> Co-authored-by: agobrech <45268029+agobrech@users.noreply.github.com>
269 lines
7.3 KiB
TypeScript
269 lines
7.3 KiB
TypeScript
import {
|
|
IExecuteFunctions,
|
|
} from 'n8n-core';
|
|
|
|
import {
|
|
IDataObject,
|
|
ILoadOptionsFunctions,
|
|
INodeExecutionData,
|
|
INodePropertyOptions,
|
|
INodeType,
|
|
INodeTypeDescription,
|
|
} from 'n8n-workflow';
|
|
|
|
import {
|
|
webflowApiRequest,
|
|
webflowApiRequestAllItems,
|
|
} from './GenericFunctions';
|
|
|
|
import {
|
|
itemFields,
|
|
itemOperations,
|
|
} from './ItemDescription';
|
|
|
|
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: {
|
|
authentication: [
|
|
'accessToken',
|
|
],
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: 'webflowOAuth2Api',
|
|
required: true,
|
|
displayOptions: {
|
|
show: {
|
|
authentication: [
|
|
'oAuth2',
|
|
],
|
|
},
|
|
},
|
|
},
|
|
],
|
|
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',
|
|
noDataExpression: true,
|
|
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');
|
|
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;
|
|
},
|
|
async getFields(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
const returnData: INodePropertyOptions[] = [];
|
|
const collectionId = this.getCurrentNodeParameter('collectionId');
|
|
const { fields } = await webflowApiRequest.call(this, 'GET', `/collections/${collectionId}`);
|
|
for (const field of fields) {
|
|
returnData.push({
|
|
name: `${field.name} (${field.type}) ${(field.required) ? ' (required)' : ''}`,
|
|
value: field.slug,
|
|
});
|
|
}
|
|
return returnData;
|
|
},
|
|
},
|
|
};
|
|
|
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
|
const items = this.getInputData();
|
|
|
|
const resource = this.getNodeParameter('resource', 0) as string;
|
|
const operation = this.getNodeParameter('operation', 0) as string;
|
|
const qs: IDataObject = {};
|
|
let responseData;
|
|
const returnData: IDataObject[] = [];
|
|
|
|
for (let i = 0; i < items.length; i++) {
|
|
|
|
try {
|
|
if (resource === 'item') {
|
|
|
|
// *********************************************************************
|
|
// item
|
|
// *********************************************************************
|
|
|
|
// https://developers.webflow.com/#item-model
|
|
|
|
if (operation === 'create') {
|
|
|
|
// ----------------------------------
|
|
// item: create
|
|
// ----------------------------------
|
|
|
|
// https://developers.webflow.com/#create-new-collection-item
|
|
|
|
const collectionId = this.getNodeParameter('collectionId', i) as string;
|
|
|
|
const properties = this.getNodeParameter('fieldsUi.fieldValues', i, []) as IDataObject[];
|
|
|
|
const live = this.getNodeParameter('live', i) as boolean;
|
|
|
|
const fields = {} as IDataObject;
|
|
|
|
properties.forEach(data => (fields[data.fieldId as string] = data.fieldValue));
|
|
|
|
const body: IDataObject = {
|
|
fields,
|
|
};
|
|
|
|
responseData = await webflowApiRequest.call(this, 'POST', `/collections/${collectionId}/items`, body, { live });
|
|
|
|
} else if (operation === 'delete') {
|
|
|
|
// ----------------------------------
|
|
// item: delete
|
|
// ----------------------------------
|
|
|
|
// https://developers.webflow.com/#remove-collection-item
|
|
|
|
const collectionId = this.getNodeParameter('collectionId', i) as string;
|
|
const itemId = this.getNodeParameter('itemId', i) as string;
|
|
responseData = await webflowApiRequest.call(this, 'DELETE', `/collections/${collectionId}/items/${itemId}`);
|
|
|
|
} else if (operation === 'get') {
|
|
|
|
// ----------------------------------
|
|
// item: get
|
|
// ----------------------------------
|
|
|
|
// https://developers.webflow.com/#get-single-item
|
|
|
|
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}`);
|
|
responseData = responseData.items;
|
|
|
|
} else if (operation === 'getAll') {
|
|
|
|
// ----------------------------------
|
|
// item: getAll
|
|
// ----------------------------------
|
|
|
|
// https://developers.webflow.com/#get-all-items-for-a-collection
|
|
|
|
const returnAll = this.getNodeParameter('returnAll', 0) as boolean;
|
|
const collectionId = this.getNodeParameter('collectionId', i) as string;
|
|
const qs: IDataObject = {};
|
|
|
|
if (returnAll === true) {
|
|
responseData = await webflowApiRequestAllItems.call(this, 'GET', `/collections/${collectionId}/items`, {}, qs);
|
|
} else {
|
|
qs.limit = this.getNodeParameter('limit', 0) as number;
|
|
responseData = await webflowApiRequest.call(this, 'GET', `/collections/${collectionId}/items`, {}, qs);
|
|
responseData = responseData.items;
|
|
}
|
|
|
|
} else if (operation === 'update') {
|
|
|
|
// ----------------------------------
|
|
// item: update
|
|
// ----------------------------------
|
|
|
|
// https://developers.webflow.com/#update-collection-item
|
|
|
|
const collectionId = this.getNodeParameter('collectionId', i) as string;
|
|
|
|
const itemId = this.getNodeParameter('itemId', i) as string;
|
|
|
|
const properties = this.getNodeParameter('fieldsUi.fieldValues', i, []) as IDataObject[];
|
|
|
|
const live = this.getNodeParameter('live', i) as boolean;
|
|
|
|
const fields = {} as IDataObject;
|
|
|
|
properties.forEach(data => (fields[data.fieldId as string] = data.fieldValue));
|
|
|
|
const body: IDataObject = {
|
|
fields,
|
|
};
|
|
|
|
responseData = await webflowApiRequest.call(this, 'PUT', `/collections/${collectionId}/items/${itemId}`, body, { live });
|
|
}
|
|
}
|
|
|
|
Array.isArray(responseData)
|
|
? returnData.push(...responseData)
|
|
: returnData.push(responseData);
|
|
} catch (error) {
|
|
if (this.continueOnFail()) {
|
|
returnData.push({ error: error.message });
|
|
continue;
|
|
}
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
return [this.helpers.returnJsonArray(returnData)];
|
|
}
|
|
}
|