@preprio/n8n-nodes-prepr
Version:
N8n community node package for Prepr CMS
657 lines • 29 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Prepr = void 0;
const n8n_workflow_1 = require("n8n-workflow");
class Prepr {
constructor() {
this.methods = {
loadOptions: {
async getFieldsForType() {
var _a, _b, _c;
const typeName = this.getNodeParameter('typeName', 0);
const options = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: {
query: `
query IntrospectType($name: String!) {
__type(name: $name) {
fields {
name
description
}
}
}
`,
variables: { name: typeName },
},
json: true,
};
const response = await this.helpers.requestWithAuthentication.call(this, 'preprApi', options);
const fields = (_c = (_b = (_a = response.data) === null || _a === void 0 ? void 0 : _a.__type) === null || _b === void 0 ? void 0 : _b.fields) !== null && _c !== void 0 ? _c : [];
return fields
.filter((field) => !field.name.startsWith('_'))
.map((field) => ({
name: field.description || field.name,
value: field.name,
}));
},
async getTypenames() {
var _a, _b, _c;
let options = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: {
query: `
{
__schema {
types {
name
kind
description
}
}
}
`,
},
json: true,
};
const response = await this.helpers.requestWithAuthentication.call(this, 'preprApi', options);
const types = (_c = (_b = (_a = response.data) === null || _a === void 0 ? void 0 : _a.__schema) === null || _b === void 0 ? void 0 : _b.types) !== null && _c !== void 0 ? _c : [];
return types
.filter((t) => t.kind === 'OBJECT' &&
typeof t.description === 'string' &&
t.description.startsWith('Single'))
.map((t) => ({
name: t.name,
value: t.name,
}));
},
async getLocales() {
var _a, _b;
let options = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: {
query: `
{
_Locales
}
`,
},
json: true,
};
const response = await this.helpers.requestWithAuthentication.call(this, 'preprApi', options);
const types = (_b = (_a = response.data) === null || _a === void 0 ? void 0 : _a._Locales) !== null && _b !== void 0 ? _b : [];
return types
.map((t) => ({
name: t,
value: t,
}));
},
},
};
this.description = {
displayName: 'Prepr',
name: 'prepr',
icon: 'file:Prepr.svg',
group: [],
description: 'Interact with the Prepr API to create, update or delete items.',
version: 1,
defaults: {
name: 'Prepr',
},
inputs: ["main"],
outputs: ["main"],
credentials: [
{
name: 'preprApi',
required: true,
},
],
requestDefaults: {
baseURL: 'https://graphql.prepr.io',
url: 'graphql'
},
documentationUrl: 'https://docs.prepr.io',
properties: [
{
displayName: 'Resource',
name: 'resource',
type: 'options',
options: [
{
name: 'Item',
value: 'item',
},
],
default: 'item',
noDataExpression: true,
required: true,
description: 'Content Item',
},
{
displayName: 'Operation',
name: 'operation',
type: 'options',
displayOptions: {
show: {
resource: [
'item',
],
},
},
options: [
{
name: 'Assign',
value: 'assign',
description: 'Assign a item',
action: 'Assign a item',
},
{
name: 'Create',
value: 'create',
description: 'Create a item',
action: 'Create a item',
},
{
name: 'Delete',
value: 'delete',
description: 'Delete a item',
action: 'Delete a item',
},
{
name: 'Publish',
value: 'publish',
description: 'Publish a item',
action: 'Publish a item',
},
{
name: 'Unpublish',
value: 'unpublish',
description: 'Unpublish a item',
action: 'Unpublish a item',
},
{
name: 'Update',
value: 'update',
description: 'Update a item',
action: 'Update a item',
},
],
default: 'create',
noDataExpression: true,
},
{
displayName: 'Locale Name or ID',
name: 'locale',
type: 'options',
typeOptions: {
loadOptionsMethod: 'getLocales',
},
default: '',
required: true,
description: 'Select the locale. Choose from the list, or specify an ID using an <a href="https://docs.n8n.io/code/expressions/">expression</a>.',
},
{
displayName: 'Model Name or ID',
name: 'typeName',
type: 'options',
typeOptions: {
loadOptionsMethod: 'getTypenames',
},
displayOptions: {
show: {
operation: [
'create', 'update'
],
resource: [
'item',
],
},
},
default: '',
required: true,
description: 'Select the item model. Choose from the list, or specify an ID using an <a href="https://docs.n8n.io/code/expressions/">expression</a>.',
},
{
displayName: 'When updating an existing item, N8N will fetch the current state first, the fields specified below will be overwritten.',
name: 'notice',
type: 'notice',
default: '',
displayOptions: {
show: {
resource: ['item'],
operation: ['update'],
},
},
},
{
displayName: 'Workflow Stage',
name: 'workflowStage',
type: 'options',
options: [
{ name: 'Done', value: 'Done' },
{ name: 'In Progress', value: 'In progress' },
{ name: 'Review', value: 'Review' },
{ name: 'To Do', value: 'To do' },
{ name: 'Unchanged', value: 'Unchanged' },
],
displayOptions: {
show: {
operation: [
'create', 'update'
],
resource: [
'item',
],
},
},
required: true,
default: 'To do',
description: 'Set the stage in the content workflow',
},
{
displayName: 'Item ID',
name: 'itemId',
type: 'string',
required: true,
displayOptions: {
show: {
operation: [
'publish', 'unpublish', 'assign', 'delete', 'update'
],
resource: [
'item',
],
},
},
default: '',
placeholder: '',
description: 'ID of the item the action is preformed on',
},
{
displayName: 'User ID',
name: 'userId',
type: 'string',
required: true,
displayOptions: {
show: {
operation: [
'assign'
],
resource: [
'item',
],
},
},
default: '',
placeholder: '',
description: 'ID of the user the item gets assigned to',
},
{
displayName: 'Publish On',
name: 'publishOn',
type: 'dateTime',
default: '',
displayOptions: {
show: {
operation: [
'publish', 'create', 'update'
],
resource: [
'item',
],
},
},
description: 'Only return entries published after this date',
},
{
displayName: 'Unpublish On',
name: 'unpublishOn',
type: 'dateTime',
default: '',
displayOptions: {
show: {
operation: [
'unpublish', 'create', 'update'
],
resource: [
'item',
],
},
},
description: 'Only return entries published after this date',
},
{
displayName: 'Delete Whole Item?',
name: 'deleteWholeItem',
type: 'boolean',
displayOptions: {
show: {
operation: [
'delete'
],
resource: [
'item',
],
},
},
required: true,
default: false,
description: 'Whether to delete the entire item rather than just the selected locale',
},
{
displayName: 'Content Fields',
name: 'contentFields',
type: 'fixedCollection',
typeOptions: {
multipleValues: true,
},
displayOptions: {
show: {
resource: ['item'],
operation: ['create', 'update'],
},
},
required: true,
default: {},
options: [
{
name: 'filter',
displayName: 'Filter',
values: [
{
displayName: 'Field Name or ID',
name: 'key',
type: 'options',
description: 'Choose from the list, or specify an ID using an <a href="https://docs.n8n.io/code/expressions/">expression</a>',
typeOptions: {
loadOptionsMethod: 'getFieldsForType',
loadOptionsDependsOn: ['typeName'],
},
default: '',
},
{
displayName: 'Value',
name: 'value',
type: 'json',
default: '',
},
],
},
],
}
],
};
}
async execute() {
var _a, _b, _c, _d;
const items = this.getInputData();
const returnData = [];
const resource = this.getNodeParameter('resource', 0);
const operation = this.getNodeParameter('operation', 0);
for (let i = 0; i < items.length; i++) {
if (resource === 'item') {
if (operation === 'create') {
const typeName = this.getNodeParameter('typeName', i);
const locale = this.getNodeParameter('locale', i);
const workflowStage = this.getNodeParameter('workflowStage', i);
const publishOn = this.getNodeParameter('publishOn', i);
const unpublishOn = this.getNodeParameter('unpublishOn', i);
const contentFields = this.getNodeParameter('contentFields', i);
const formattedFields = {};
if (contentFields === null || contentFields === void 0 ? void 0 : contentFields.filter) {
for (const field of contentFields.filter) {
if (field.key) {
try {
formattedFields[field.key] = JSON.parse(field.value);
}
catch (e) {
formattedFields[field.key] = field.value;
}
}
}
}
if (Object.keys(formattedFields).length === 0) {
continue;
}
const body = {
model: {
body: typeName
},
locales: [
locale
],
workflow_stage: {
[locale]: workflowStage
},
items: {
[locale]: formattedFields
},
publish_on: undefined,
expire_on: undefined,
fields: 'items,workflow_stage'
};
if (publishOn) {
body.publish_on = {
[locale]: Math.floor(new Date(publishOn).getTime() / 1000)
};
}
if (unpublishOn) {
body.expire_on = {
[locale]: Math.floor(new Date(unpublishOn).getTime() / 1000)
};
}
const options = {
headers: {
'Accept': 'application/json',
},
method: 'POST',
body: body,
json: true,
url: `https://rest6.prepr.io/content_items`,
resolveWithFullResponse: true
};
const response = await this.helpers.requestWithAuthentication.call(this, 'preprApi', options);
if (response.statusCode !== 201) {
throw new n8n_workflow_1.NodeApiError(this.getNode(), response.body, { message: 'Create operation failed' });
}
returnData.push(response.body);
}
if (operation === 'update') {
const itemId = this.getNodeParameter('itemId', i);
const locale = this.getNodeParameter('locale', i);
const workflowStage = this.getNodeParameter('workflowStage', i);
const publishOn = this.getNodeParameter('publishOn', i);
const unpublishOn = this.getNodeParameter('unpublishOn', i);
const contentFields = this.getNodeParameter('contentFields', i);
const fetchOptions = {
headers: {
'Accept': 'application/json',
},
method: 'GET',
body: {
'fields': 'items,locales,workflow_stage'
},
json: true,
url: `https://api.eu1.prepr.io/content_items/` + itemId,
resolveWithFullResponse: true
};
const fetchResponse = await this.helpers.requestWithAuthentication.call(this, 'preprApi', fetchOptions);
if (fetchResponse.statusCode === 200) {
let formattedFields = {};
const existingFields = (_b = (_a = fetchResponse.body) === null || _a === void 0 ? void 0 : _a.items) === null || _b === void 0 ? void 0 : _b[locale];
if (existingFields) {
formattedFields = { ...existingFields };
}
if (contentFields === null || contentFields === void 0 ? void 0 : contentFields.filter) {
for (const field of contentFields.filter) {
if (field.key) {
try {
formattedFields[field.key] = JSON.parse(field.value);
}
catch (e) {
formattedFields[field.key] = field.value;
}
}
}
}
if (Object.keys(formattedFields).length === 0) {
continue;
}
const body = {
locales: [
locale
],
workflow_stage: {
[locale]: workflowStage === 'Unchanged'
? (_d = (_c = fetchResponse.body) === null || _c === void 0 ? void 0 : _c.workflow_stage) === null || _d === void 0 ? void 0 : _d[locale]
: workflowStage
},
items: {
[locale]: formattedFields
},
publish_on: undefined,
expire_on: undefined,
fields: 'items,workflow_stage'
};
if (publishOn) {
body.publish_on = {
[locale]: Math.floor(new Date(publishOn).getTime() / 1000)
};
}
if (unpublishOn) {
body.expire_on = {
[locale]: Math.floor(new Date(unpublishOn).getTime() / 1000)
};
}
const options = {
headers: {
'Accept': 'application/json',
},
method: 'PUT',
body: body,
json: true,
url: `https://api.eu1.prepr.io/content_items/` + itemId,
resolveWithFullResponse: true
};
const response = await this.helpers.requestWithAuthentication.call(this, 'preprApi', options);
if (response.statusCode !== 200) {
throw new n8n_workflow_1.NodeApiError(this.getNode(), response.body, { message: 'Update operation failed' });
}
returnData.push(response.body);
continue;
}
}
if (operation === 'publish') {
const itemId = this.getNodeParameter('itemId', i);
const locale = this.getNodeParameter('locale', i);
let publishOn = this.getNodeParameter('publishOn', i);
if (!publishOn) {
publishOn = new Date().toISOString();
}
const options = {
headers: {
'Accept': 'application/json',
},
method: 'PATCH',
body: {
publish_on: Math.floor(new Date(publishOn).getTime() / 1000)
},
json: true,
url: `https://api.eu1.prepr.io/content_items/` + itemId + `/` + locale + `/publish`,
resolveWithFullResponse: true
};
const response = await this.helpers.requestWithAuthentication.call(this, 'preprApi', options);
if (response.statusCode !== 200) {
throw new n8n_workflow_1.NodeApiError(this.getNode(), response.body, { message: 'Publish operation failed' });
}
returnData.push(response.body);
continue;
}
if (operation === 'unpublish') {
const itemId = this.getNodeParameter('itemId', i);
const locale = this.getNodeParameter('locale', i);
let unpublishOn = this.getNodeParameter('unpublishOn', i);
if (!unpublishOn) {
unpublishOn = new Date().toISOString();
}
const options = {
headers: {
'Accept': 'application/json',
},
method: 'PATCH',
body: {
unpublish_on: Math.floor(new Date(unpublishOn).getTime() / 1000)
},
json: true,
url: `https://api.eu1.prepr.io/content_items/` + itemId + `/` + locale + `/unpublish`,
resolveWithFullResponse: true
};
const response = await this.helpers.requestWithAuthentication.call(this, 'preprApi', options);
if (response.statusCode !== 200) {
throw new n8n_workflow_1.NodeApiError(this.getNode(), response.body, { message: 'Unpublish operation failed' });
}
returnData.push(response.body);
continue;
}
if (operation === 'assign') {
const itemId = this.getNodeParameter('itemId', i);
const locale = this.getNodeParameter('locale', i);
const userId = this.getNodeParameter('userId', i);
const options = {
headers: {
'Accept': 'application/json',
},
method: 'PATCH',
body: {},
json: true,
url: `https://api.eu1.prepr.io/content_items/` + itemId + `/` + locale + `/assign/` + userId,
resolveWithFullResponse: true
};
const response = await this.helpers.requestWithAuthentication.call(this, 'preprApi', options);
if (response.statusCode !== 200) {
throw new n8n_workflow_1.NodeApiError(this.getNode(), response.body, { message: 'Assign operation failed' });
}
returnData.push(response.body);
continue;
}
if (operation === 'delete') {
const itemId = this.getNodeParameter('itemId', i);
const locale = this.getNodeParameter('locale', i);
const deleteWholeItem = this.getNodeParameter('deleteWholeItem', i);
let deleteUri = 'content_items/' + itemId + '/' + locale;
if (deleteWholeItem === true) {
deleteUri = 'content_items/' + itemId;
}
const options = {
headers: {
'Accept': 'application/json',
},
method: 'DELETE',
body: {},
json: true,
url: `https://api.eu1.prepr.io/` + deleteUri,
resolveWithFullResponse: true
};
const response = await this.helpers.requestWithAuthentication.call(this, 'preprApi', options);
if (response.statusCode !== 204) {
throw new n8n_workflow_1.NodeApiError(this.getNode(), response.body, { message: 'Delete operation failed' });
}
returnData.push([]);
continue;
}
}
}
return [this.helpers.returnJsonArray(returnData)];
}
}
exports.Prepr = Prepr;
//# sourceMappingURL=Prepr.node.js.map