2023-01-27 03:22:44 -08:00
|
|
|
import type {
|
2023-03-09 09:13:15 -08:00
|
|
|
IExecuteFunctions,
|
2023-01-27 03:22:44 -08:00
|
|
|
IDataObject,
|
|
|
|
INodeExecutionData,
|
|
|
|
INodeType,
|
|
|
|
INodeTypeDescription,
|
|
|
|
} from 'n8n-workflow';
|
2022-08-01 13:47:55 -07:00
|
|
|
|
|
|
|
import { contenfulApiRequestAllItems, contentfulApiRequest } 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"]}}',
|
2022-06-20 07:54:01 -07:00
|
|
|
// eslint-disable-next-line n8n-nodes-base/node-class-description-icon-not-svg
|
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',
|
|
|
|
},
|
|
|
|
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',
|
2020-08-25 02:22:14 -07:00
|
|
|
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',
|
2022-05-20 14:47:24 -07:00
|
|
|
noDataExpression: true,
|
2020-07-09 02:36:28 -07:00
|
|
|
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
|
|
|
],
|
2020-08-25 02:22:14 -07:00
|
|
|
default: 'entry',
|
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[][]> {
|
2022-12-02 03:53:59 -08:00
|
|
|
const resource = this.getNodeParameter('resource', 0);
|
|
|
|
const operation = this.getNodeParameter('operation', 0);
|
2020-08-04 12:07:54 -07:00
|
|
|
let responseData;
|
2020-07-09 02:36:28 -07:00
|
|
|
|
|
|
|
const items = this.getInputData();
|
2022-08-30 08:55:33 -07:00
|
|
|
const returnData: INodeExecutionData[] = [];
|
2020-07-09 02:36:28 -07:00
|
|
|
const qs: Record<string, string | number> = {};
|
|
|
|
|
|
|
|
for (let i = 0; i < items.length; i++) {
|
2021-07-19 23:58:54 -07:00
|
|
|
try {
|
|
|
|
if (resource === 'space') {
|
|
|
|
if (operation === 'get') {
|
2021-08-20 09:57:30 -07:00
|
|
|
const credentials = await this.getCredentials('contentfulApi');
|
2020-08-04 12:07:54 -07:00
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
responseData = await contentfulApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
`/spaces/${credentials?.spaceId}`,
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
2020-07-09 02:36:28 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
if (resource === 'contentType') {
|
|
|
|
if (operation === 'get') {
|
2021-08-20 09:57:30 -07:00
|
|
|
const credentials = await this.getCredentials('contentfulApi');
|
2020-08-04 12:07:54 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const env = this.getNodeParameter('environmentId', 0) as string;
|
2020-08-04 12:07:54 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const id = this.getNodeParameter('contentTypeId', 0) as string;
|
2020-08-04 12:07:54 -07:00
|
|
|
|
2022-11-18 07:29:44 -08:00
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i);
|
2020-08-28 06:27:12 -07:00
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
responseData = await contentfulApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
`/spaces/${credentials?.spaceId}/environments/${env}/content_types/${id}`,
|
|
|
|
);
|
2020-08-28 06:27:12 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (!additionalFields.rawData) {
|
|
|
|
responseData = responseData.fields;
|
|
|
|
}
|
2020-08-28 06:27:12 -07:00
|
|
|
}
|
2020-07-09 02:36:28 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
if (resource === 'entry') {
|
|
|
|
if (operation === 'get') {
|
2021-08-20 09:57:30 -07:00
|
|
|
const credentials = await this.getCredentials('contentfulApi');
|
2020-08-04 12:07:54 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const env = this.getNodeParameter('environmentId', 0) as string;
|
2020-08-04 12:07:54 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const id = this.getNodeParameter('entryId', 0) as string;
|
2020-08-04 12:07:54 -07:00
|
|
|
|
2022-11-18 07:29:44 -08:00
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i);
|
2020-08-28 06:27:12 -07:00
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
responseData = await contentfulApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
`/spaces/${credentials?.spaceId}/environments/${env}/entries/${id}`,
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2020-08-04 12:07:54 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (!additionalFields.rawData) {
|
|
|
|
responseData = responseData.fields;
|
|
|
|
}
|
|
|
|
} else if (operation === 'getAll') {
|
2021-08-20 09:57:30 -07:00
|
|
|
const credentials = await this.getCredentials('contentfulApi');
|
2020-08-04 12:07:54 -07:00
|
|
|
|
2022-11-18 05:31:38 -08:00
|
|
|
const returnAll = this.getNodeParameter('returnAll', 0);
|
2020-08-04 12:07:54 -07:00
|
|
|
|
2022-11-18 07:29:44 -08:00
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i);
|
2021-07-19 23:58:54 -07:00
|
|
|
const rawData = additionalFields.rawData;
|
|
|
|
additionalFields.rawData = undefined;
|
2020-08-04 12:07:54 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const env = this.getNodeParameter('environmentId', i) as string;
|
2020-08-04 12:07:54 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
Object.assign(qs, additionalFields);
|
2020-08-04 12:07:54 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (qs.equal) {
|
|
|
|
const [atribute, value] = (qs.equal as string).split('=');
|
|
|
|
qs[atribute] = value;
|
|
|
|
delete qs.equal;
|
|
|
|
}
|
2020-08-04 12:07:54 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (qs.notEqual) {
|
|
|
|
const [atribute, value] = (qs.notEqual as string).split('=');
|
|
|
|
qs[atribute] = value;
|
|
|
|
delete qs.notEqual;
|
|
|
|
}
|
2020-08-04 12:07:54 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (qs.include) {
|
|
|
|
const [atribute, value] = (qs.include as string).split('=');
|
|
|
|
qs[atribute] = value;
|
|
|
|
delete qs.include;
|
|
|
|
}
|
2020-08-28 06:27:12 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (qs.exclude) {
|
|
|
|
const [atribute, value] = (qs.exclude as string).split('=');
|
|
|
|
qs[atribute] = value;
|
|
|
|
delete qs.exclude;
|
2020-08-28 06:27:12 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
|
|
|
|
if (returnAll) {
|
2022-08-01 13:47:55 -07:00
|
|
|
responseData = await contenfulApiRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
'items',
|
|
|
|
'GET',
|
|
|
|
`/spaces/${credentials?.spaceId}/environments/${env}/entries`,
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
|
|
|
|
if (!rawData) {
|
2022-08-01 13:47:55 -07:00
|
|
|
const assets: IDataObject[] = [];
|
2022-12-02 06:25:21 -08:00
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
responseData.map((asset: any) => {
|
2023-02-27 19:39:43 -08:00
|
|
|
assets.push(asset.fields as IDataObject);
|
2021-07-19 23:58:54 -07:00
|
|
|
});
|
|
|
|
responseData = assets;
|
|
|
|
}
|
|
|
|
} else {
|
2022-11-18 06:26:22 -08:00
|
|
|
const limit = this.getNodeParameter('limit', 0);
|
2021-07-19 23:58:54 -07:00
|
|
|
qs.limit = limit;
|
2022-08-01 13:47:55 -07:00
|
|
|
responseData = await contentfulApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
`/spaces/${credentials?.spaceId}/environments/${env}/entries`,
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = responseData.items;
|
|
|
|
|
|
|
|
if (!rawData) {
|
2022-08-01 13:47:55 -07:00
|
|
|
const assets: IDataObject[] = [];
|
2022-12-02 06:25:21 -08:00
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
responseData.map((asset: any) => {
|
2023-02-27 19:39:43 -08:00
|
|
|
assets.push(asset.fields as IDataObject);
|
2021-07-19 23:58:54 -07:00
|
|
|
});
|
|
|
|
responseData = assets;
|
|
|
|
}
|
2020-08-28 06:27:12 -07:00
|
|
|
}
|
2020-07-09 02:36:28 -07:00
|
|
|
}
|
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
if (resource === 'asset') {
|
|
|
|
if (operation === 'get') {
|
2021-08-20 09:57:30 -07:00
|
|
|
const credentials = await this.getCredentials('contentfulApi');
|
2020-08-04 12:07:54 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const env = this.getNodeParameter('environmentId', 0) as string;
|
2020-08-04 12:07:54 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const id = this.getNodeParameter('assetId', 0) as string;
|
2020-08-04 12:07:54 -07:00
|
|
|
|
2022-11-18 07:29:44 -08:00
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i);
|
2020-08-28 06:27:12 -07:00
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
responseData = await contentfulApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
`/spaces/${credentials?.spaceId}/environments/${env}/assets/${id}`,
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2020-08-04 12:07:54 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (!additionalFields.rawData) {
|
|
|
|
responseData = responseData.fields;
|
|
|
|
}
|
|
|
|
} else if (operation === 'getAll') {
|
2021-08-20 09:57:30 -07:00
|
|
|
const credentials = await this.getCredentials('contentfulApi');
|
2020-08-04 12:07:54 -07:00
|
|
|
|
2022-11-18 05:31:38 -08:00
|
|
|
const returnAll = this.getNodeParameter('returnAll', 0);
|
2020-08-04 12:07:54 -07:00
|
|
|
|
2022-11-18 07:29:44 -08:00
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i);
|
2021-07-19 23:58:54 -07:00
|
|
|
const rawData = additionalFields.rawData;
|
|
|
|
additionalFields.rawData = undefined;
|
2020-08-04 12:07:54 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const env = this.getNodeParameter('environmentId', i) as string;
|
2020-08-04 12:07:54 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
Object.assign(qs, additionalFields);
|
2020-08-04 12:07:54 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (qs.equal) {
|
|
|
|
const [atribute, value] = (qs.equal as string).split('=');
|
|
|
|
qs[atribute] = value;
|
|
|
|
delete qs.equal;
|
|
|
|
}
|
2020-08-04 12:07:54 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (qs.notEqual) {
|
|
|
|
const [atribute, value] = (qs.notEqual as string).split('=');
|
|
|
|
qs[atribute] = value;
|
|
|
|
delete qs.notEqual;
|
|
|
|
}
|
2020-08-04 12:07:54 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (qs.include) {
|
|
|
|
const [atribute, value] = (qs.include as string).split('=');
|
|
|
|
qs[atribute] = value;
|
|
|
|
delete qs.include;
|
|
|
|
}
|
2020-08-28 06:27:12 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (qs.exclude) {
|
|
|
|
const [atribute, value] = (qs.exclude as string).split('=');
|
|
|
|
qs[atribute] = value;
|
|
|
|
delete qs.exclude;
|
2020-08-28 06:27:12 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
|
|
|
|
if (returnAll) {
|
2022-08-01 13:47:55 -07:00
|
|
|
responseData = await contenfulApiRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
'items',
|
|
|
|
'GET',
|
|
|
|
`/spaces/${credentials?.spaceId}/environments/${env}/assets`,
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
|
|
|
|
if (!rawData) {
|
2022-08-01 13:47:55 -07:00
|
|
|
const assets: IDataObject[] = [];
|
2022-12-02 06:25:21 -08:00
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
responseData.map((asset: any) => {
|
2023-02-27 19:39:43 -08:00
|
|
|
assets.push(asset.fields as IDataObject);
|
2021-07-19 23:58:54 -07:00
|
|
|
});
|
|
|
|
responseData = assets;
|
|
|
|
}
|
|
|
|
} else {
|
2022-11-18 06:26:22 -08:00
|
|
|
const limit = this.getNodeParameter('limit', i);
|
2021-07-19 23:58:54 -07:00
|
|
|
qs.limit = limit;
|
2022-08-01 13:47:55 -07:00
|
|
|
responseData = await contentfulApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
`/spaces/${credentials?.spaceId}/environments/${env}/assets`,
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = responseData.items;
|
|
|
|
|
|
|
|
if (!rawData) {
|
2022-08-01 13:47:55 -07:00
|
|
|
const assets: IDataObject[] = [];
|
2022-12-02 06:25:21 -08:00
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
responseData.map((asset: any) => {
|
2023-02-27 19:39:43 -08:00
|
|
|
assets.push(asset.fields as IDataObject);
|
2021-07-19 23:58:54 -07:00
|
|
|
});
|
|
|
|
responseData = assets;
|
|
|
|
}
|
2020-08-28 06:27:12 -07:00
|
|
|
}
|
2020-08-04 12:07:54 -07:00
|
|
|
}
|
2020-07-09 02:36:28 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
if (resource === 'locale') {
|
|
|
|
if (operation === 'getAll') {
|
2021-08-20 09:57:30 -07:00
|
|
|
const credentials = await this.getCredentials('contentfulApi');
|
2020-08-04 12:07:54 -07:00
|
|
|
|
2022-11-18 05:31:38 -08:00
|
|
|
const returnAll = this.getNodeParameter('returnAll', 0);
|
2020-08-04 12:07:54 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const env = this.getNodeParameter('environmentId', i) as string;
|
2020-08-04 12:07:54 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (returnAll) {
|
2022-08-01 13:47:55 -07:00
|
|
|
responseData = await contenfulApiRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
'items',
|
|
|
|
'GET',
|
|
|
|
`/spaces/${credentials?.spaceId}/environments/${env}/locales`,
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
} else {
|
2022-11-18 06:26:22 -08:00
|
|
|
const limit = this.getNodeParameter('limit', 0);
|
2021-07-19 23:58:54 -07:00
|
|
|
qs.limit = limit;
|
2022-08-01 13:47:55 -07:00
|
|
|
responseData = await contentfulApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
`/spaces/${credentials?.spaceId}/environments/${env}/locales`,
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = responseData.items;
|
|
|
|
}
|
2020-08-04 12:07:54 -07:00
|
|
|
}
|
2020-07-09 02:36:28 -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({ error: error.message, json: {} });
|
2021-07-19 23:58:54 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
throw error;
|
2020-08-04 12:07:54 -07:00
|
|
|
}
|
2020-07-09 02:36:28 -07:00
|
|
|
}
|
2022-08-30 08:55:33 -07:00
|
|
|
return this.prepareOutputData(returnData);
|
2020-07-09 02:36:28 -07:00
|
|
|
}
|
|
|
|
}
|