n8n/packages/nodes-base/nodes/Contentful/Contentful.node.ts

327 lines
9.4 KiB
TypeScript
Raw Normal View History

2020-08-04 12:07:54 -07:00
import {
IExecuteFunctions,
} from 'n8n-core';
2020-07-09 02:36:28 -07:00
2020-08-04 12:07:54 -07:00
import {
IDataObject,
INodeExecutionData,
INodeType,
INodeTypeDescription,
} from 'n8n-workflow';
import {
contenfulApiRequestAllItems,
contentfulApiRequest,
2020-08-04 12:07:54 -07:00
} from './GenericFunctions';
2020-07-09 02:36:28 -07:00
import * as SpaceDescription from './SpaceDescription';
import * as ContentTypeDescription from './ContentTypeDescription';
import * as EntryDescription from './EntryDescription';
import * as AssetDescription from './AssetDescription';
import * as LocaleDescription from './LocaleDescription';
export class Contentful implements INodeType {
description: INodeTypeDescription = {
displayName: 'Contentful',
name: 'contentful',
2020-08-04 12:07:54 -07:00
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
2020-07-09 02:36:28 -07:00
icon: 'file:contentful.png',
group: ['input'],
version: 1,
2020-08-04 12:07:54 -07:00
description: 'Consume Contenful API',
2020-07-09 02:36:28 -07:00
defaults: {
name: 'Contentful',
2020-10-22 06:46:03 -07:00
color: '#2E75D4',
2020-07-09 02:36:28 -07:00
},
inputs: ['main'],
outputs: ['main'],
credentials: [
{
2020-08-04 12:07:54 -07:00
name: 'contentfulApi',
2020-10-22 06:46:03 -07:00
required: true,
2020-08-04 12:07:54 -07:00
},
2020-07-09 02:36:28 -07:00
],
properties: [
2020-07-09 03:10:22 -07:00
{
displayName: 'Source',
name: 'source',
2020-08-04 12:07:54 -07:00
type: 'options',
default: 'deliveryApi',
2020-07-09 03:10:22 -07:00
description: 'Pick where your data comes from, delivery or preview API',
options: [
{
name: 'Delivery API',
2020-10-22 06:46:03 -07:00
value: 'deliveryApi',
2020-07-09 03:10:22 -07:00
},
{
name: 'Preview API',
2020-10-22 06:46:03 -07:00
value: 'previewApi',
2020-08-04 12:07:54 -07:00
},
],
2020-07-09 02:36:28 -07:00
},
// Resources:
{
displayName: 'Resource',
name: 'resource',
type: 'options',
options: [
2020-08-04 12:07:54 -07:00
AssetDescription.resource,
2020-07-09 02:36:28 -07:00
ContentTypeDescription.resource,
EntryDescription.resource,
2020-08-04 12:07:54 -07:00
LocaleDescription.resource,
SpaceDescription.resource,
2020-07-09 02:36:28 -07:00
],
default: 'entry',
2020-10-22 06:46:03 -07:00
description: 'The resource to operate on.',
2020-07-09 02:36:28 -07:00
},
// Operations:
...SpaceDescription.operations,
...ContentTypeDescription.operations,
...EntryDescription.operations,
...AssetDescription.operations,
...LocaleDescription.operations,
// Resource specific fields:
...SpaceDescription.fields,
...ContentTypeDescription.fields,
...EntryDescription.fields,
...AssetDescription.fields,
...LocaleDescription.fields,
2020-08-04 12:07:54 -07:00
],
2020-07-09 02:36:28 -07:00
};
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const resource = this.getNodeParameter('resource', 0) as string;
const operation = this.getNodeParameter('operation', 0) as string;
2020-08-04 12:07:54 -07:00
let responseData;
2020-07-09 02:36:28 -07:00
const items = this.getInputData();
const returnData: IDataObject[] = [];
const qs: Record<string, string | number> = {};
for (let i = 0; i < items.length; i++) {
if (resource === 'space') {
2020-08-04 12:07:54 -07:00
if (operation === 'get') {
const credentials = this.getCredentials('contentfulApi');
responseData = await contentfulApiRequest.call(this, 'GET', `/spaces/${credentials?.spaceId}`);
2020-07-09 02:36:28 -07:00
}
2020-08-04 12:07:54 -07:00
}
if (resource === 'contentType') {
if (operation === 'get') {
const credentials = this.getCredentials('contentfulApi');
const env = this.getNodeParameter('environmentId', 0) as string;
const id = this.getNodeParameter('contentTypeId', 0) as string;
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
2020-08-04 12:07:54 -07:00
responseData = await contentfulApiRequest.call(this, 'GET', `/spaces/${credentials?.spaceId}/environments/${env}/content_types/${id}`);
if (!additionalFields.rawData) {
responseData = responseData.fields;
}
2020-07-09 02:36:28 -07:00
}
2020-08-04 12:07:54 -07:00
}
if (resource === 'entry') {
if (operation === 'get') {
const credentials = this.getCredentials('contentfulApi');
const env = this.getNodeParameter('environmentId', 0) as string;
const id = this.getNodeParameter('entryId', 0) as string;
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
2020-08-04 12:07:54 -07:00
responseData = await contentfulApiRequest.call(this, 'GET', `/spaces/${credentials?.spaceId}/environments/${env}/entries/${id}`, {}, qs);
if (!additionalFields.rawData) {
responseData = responseData.fields;
}
2020-08-04 12:07:54 -07:00
} else if (operation === 'getAll') {
const credentials = this.getCredentials('contentfulApi');
const returnAll = this.getNodeParameter('returnAll', 0) as boolean;
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
const rawData = additionalFields.rawData;
additionalFields.rawData = undefined;
2020-08-04 12:07:54 -07:00
const env = this.getNodeParameter('environmentId', i) as string;
Object.assign(qs, additionalFields);
if (qs.equal) {
const [atribute, value] = (qs.equal as string).split('=');
qs[atribute] = value;
delete qs.equal;
}
if (qs.notEqual) {
const [atribute, value] = (qs.notEqual as string).split('=');
qs[atribute] = value;
delete qs.notEqual;
}
if (qs.include) {
const [atribute, value] = (qs.include as string).split('=');
qs[atribute] = value;
delete qs.include;
}
if (qs.exclude) {
const [atribute, value] = (qs.exclude as string).split('=');
qs[atribute] = value;
delete qs.exclude;
}
if (returnAll) {
responseData = await contenfulApiRequestAllItems.call(this, 'items', 'GET', `/spaces/${credentials?.spaceId}/environments/${env}/entries`, {}, qs);
if (!rawData) {
const assets : IDataObject[] = [];
// tslint:disable-next-line: no-any
responseData.map((asset : any) => {
assets.push(asset.fields);
});
responseData = assets;
}
2020-08-04 12:07:54 -07:00
} else {
const limit = this.getNodeParameter('limit', 0) as number;
qs.limit = limit;
responseData = await contentfulApiRequest.call(this, 'GET', `/spaces/${credentials?.spaceId}/environments/${env}/entries`, {}, qs);
responseData = responseData.items;
if (!rawData) {
const assets : IDataObject[] = [];
// tslint:disable-next-line: no-any
responseData.map((asset : any) => {
assets.push(asset.fields);
});
responseData = assets;
}
2020-07-09 02:36:28 -07:00
}
}
2020-08-04 12:07:54 -07:00
}
if (resource === 'asset') {
if (operation === 'get') {
const credentials = this.getCredentials('contentfulApi');
const env = this.getNodeParameter('environmentId', 0) as string;
const id = this.getNodeParameter('assetId', 0) as string;
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
2020-08-04 12:07:54 -07:00
responseData = await contentfulApiRequest.call(this, 'GET', `/spaces/${credentials?.spaceId}/environments/${env}/assets/${id}`, {}, qs);
if (!additionalFields.rawData) {
responseData = responseData.fields;
}
2020-08-04 12:07:54 -07:00
} else if (operation === 'getAll') {
const credentials = this.getCredentials('contentfulApi');
const returnAll = this.getNodeParameter('returnAll', 0) as boolean;
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
const rawData = additionalFields.rawData;
additionalFields.rawData = undefined;
2020-08-04 12:07:54 -07:00
const env = this.getNodeParameter('environmentId', i) as string;
Object.assign(qs, additionalFields);
if (qs.equal) {
const [atribute, value] = (qs.equal as string).split('=');
qs[atribute] = value;
delete qs.equal;
}
if (qs.notEqual) {
const [atribute, value] = (qs.notEqual as string).split('=');
qs[atribute] = value;
delete qs.notEqual;
}
if (qs.include) {
const [atribute, value] = (qs.include as string).split('=');
qs[atribute] = value;
delete qs.include;
}
if (qs.exclude) {
const [atribute, value] = (qs.exclude as string).split('=');
qs[atribute] = value;
delete qs.exclude;
}
if (returnAll) {
responseData = await contenfulApiRequestAllItems.call(this, 'items', 'GET', `/spaces/${credentials?.spaceId}/environments/${env}/assets`, {}, qs);
if (!rawData) {
const assets : IDataObject[] = [];
// tslint:disable-next-line: no-any
responseData.map((asset : any) => {
assets.push(asset.fields);
});
responseData = assets;
}
2020-08-04 12:07:54 -07:00
} else {
const limit = this.getNodeParameter('limit', i) as number;
2020-08-04 12:07:54 -07:00
qs.limit = limit;
responseData = await contentfulApiRequest.call(this, 'GET', `/spaces/${credentials?.spaceId}/environments/${env}/assets`, {}, qs);
responseData = responseData.items;
if (!rawData) {
const assets : IDataObject[] = [];
// tslint:disable-next-line: no-any
responseData.map((asset : any) => {
assets.push(asset.fields);
});
responseData = assets;
}
2020-08-04 12:07:54 -07:00
}
2020-07-09 02:36:28 -07:00
}
2020-08-04 12:07:54 -07:00
}
if (resource === 'locale') {
if (operation === 'getAll') {
const credentials = this.getCredentials('contentfulApi');
const returnAll = this.getNodeParameter('returnAll', 0) as boolean;
const env = this.getNodeParameter('environmentId', i) as string;
if (returnAll) {
responseData = await contenfulApiRequestAllItems.call(this, 'items', 'GET', `/spaces/${credentials?.spaceId}/environments/${env}/locales`, {}, qs);
2020-08-04 12:07:54 -07:00
} else {
const limit = this.getNodeParameter('limit', 0) as number;
qs.limit = limit;
responseData = await contentfulApiRequest.call(this, 'GET', `/spaces/${credentials?.spaceId}/environments/${env}/locales`, {}, qs);
responseData = responseData.items;
2020-08-04 12:07:54 -07:00
}
2020-07-09 02:36:28 -07:00
}
}
2020-08-04 12:07:54 -07:00
if (Array.isArray(responseData)) {
returnData.push.apply(returnData, responseData as IDataObject[]);
} else {
returnData.push(responseData as IDataObject);
}
2020-07-09 02:36:28 -07:00
}
return [this.helpers.returnJsonArray(returnData)];
}
}