Small improvements on Cockpit-Node

This commit is contained in:
Jan Oberhauser 2020-04-05 15:49:47 +02:00
parent 52cbd323f2
commit f34a1d577c
8 changed files with 201 additions and 86 deletions

View file

@ -1,25 +1,34 @@
import { IExecuteFunctions } from 'n8n-core'; import { IExecuteFunctions } from 'n8n-core';
import { import {
IDataObject, IDataObject,
ILoadOptionsFunctions,
INodeExecutionData, INodeExecutionData,
INodePropertyOptions,
INodeType, INodeType,
INodeTypeDescription INodeTypeDescription,
} from 'n8n-workflow'; } from 'n8n-workflow';
import { import {
collectionFields, collectionFields,
collectionOperations collectionOperations,
} from './CollectionDescription'; } from './CollectionDescription';
import { import {
getCollectionEntries, createCollectionEntry,
saveCollectionEntry getAllCollectionEntries,
getAllCollectionNames,
} from './CollectionFunctions'; } from './CollectionFunctions';
import { import {
formFields, formFields,
formOperations formOperations
} from './FormDescription'; } from './FormDescription';
import { submitForm } from './FormFunctions'; import { submitForm } from './FormFunctions';
import { singletonOperations } from "./SingletonDescription"; import {
import { getSingleton } from "./SingletonFunctions"; singletonFields,
singletonOperations,
} from './SingletonDescription';
import {
getAllSingleton,
getAllSingletonNames,
} from './SingletonFunctions';
export class Cockpit implements INodeType { export class Cockpit implements INodeType {
description: INodeTypeDescription = { description: INodeTypeDescription = {
@ -28,7 +37,7 @@ export class Cockpit implements INodeType {
icon: 'file:cockpit.png', icon: 'file:cockpit.png',
group: ['output'], group: ['output'],
version: 1, version: 1,
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"] + "/" + $parameter["resourceName"]}}', subtitle: '={{ $parameter["operation"] + ": " + $parameter["resource"] }}',
description: 'Consume Cockpit API', description: 'Consume Cockpit API',
defaults: { defaults: {
name: 'Cockpit', name: 'Cockpit',
@ -64,57 +73,81 @@ export class Cockpit implements INodeType {
}, },
], ],
}, },
{
displayName: 'Resource name',
name: 'resourceName',
type: 'string',
default: '',
required: true,
description: 'Name of resource to consume.'
},
...collectionOperations, ...collectionOperations,
...collectionFields, ...collectionFields,
...formOperations, ...formOperations,
...formFields, ...formFields,
...singletonOperations, ...singletonOperations,
...singletonFields,
], ],
}; };
methods = {
loadOptions: {
async getCollections(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const collections = await getAllCollectionNames.call(this);
return collections.map(itemName => {
return {
name: itemName,
value: itemName,
}
});
},
async getSingletons(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const singletons = await getAllSingletonNames.call(this);
return singletons.map(itemName => {
return {
name: itemName,
value: itemName,
}
});
},
},
};
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> { async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const items = this.getInputData(); const items = this.getInputData();
const returnData: IDataObject[] = []; const returnData: IDataObject[] = [];
const length = items.length as unknown as number; const length = items.length as unknown as number;
const resource = this.getNodeParameter('resource', 0) as string; const resource = this.getNodeParameter('resource', 0) as string;
const resourceName = this.getNodeParameter('resourceName', 0) as string;
const operation = this.getNodeParameter('operation', 0) as string; const operation = this.getNodeParameter('operation', 0) as string;
let responseData; let responseData;
for (let i = 0; i < length; i++) { for (let i = 0; i < length; i++) {
if (resource === 'collections') { if (resource === 'collections') {
if (operation === 'save') { const collectionName = this.getNodeParameter('collection', i) as string;
if (operation === 'create') {
const data = this.getNodeParameter('data', i) as IDataObject; const data = this.getNodeParameter('data', i) as IDataObject;
responseData = await saveCollectionEntry.call(this, resourceName, data); responseData = await createCollectionEntry.call(this, collectionName, data);
} else if (operation === 'get') { } else if (operation === 'getAll') {
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject; const options = this.getNodeParameter('options', i) as IDataObject;
responseData = await getCollectionEntries.call(this, resourceName, additionalFields); responseData = await getAllCollectionEntries.call(this, collectionName, options);
} else if (operation === 'update') { } else if (operation === 'update') {
const id = this.getNodeParameter('id', i) as string; const id = this.getNodeParameter('id', i) as string;
const data = this.getNodeParameter('data', i) as IDataObject; const data = this.getNodeParameter('data', i) as IDataObject;
responseData = await saveCollectionEntry.call(this, resourceName, data, id); responseData = await createCollectionEntry.call(this, collectionName, data, id);
} }
} else if (resource === 'forms') { } else if (resource === 'forms') {
const formName = this.getNodeParameter('form', i) as string;
if (operation === 'submit') { if (operation === 'submit') {
const form = this.getNodeParameter('form', i) as IDataObject; const form = this.getNodeParameter('form', i) as IDataObject;
responseData = await submitForm.call(this, resourceName, form); responseData = await submitForm.call(this, formName, form);
} }
} else if (resource === 'singletons') { } else if (resource === 'singletons') {
if (operation === 'get') { const singletonName = this.getNodeParameter('singleton', i) as string;
responseData = await getSingleton.call(this, resourceName); if (operation === 'getAll') {
responseData = await getAllSingleton.call(this, singletonName);
} }
} }

View file

@ -15,12 +15,12 @@ export const collectionOperations = [
options: [ options: [
{ {
name: 'Create an entry', name: 'Create an entry',
value: 'save', value: 'create',
description: 'Create a collection entry', description: 'Create a collection entry',
}, },
{ {
name: 'Get all entries', name: 'Get all entries',
value: 'get', value: 'getAll',
description: 'Get all collection entries', description: 'Get all collection entries',
}, },
{ {
@ -29,13 +29,32 @@ export const collectionOperations = [
description: 'Update a collection entries', description: 'Update a collection entries',
}, },
], ],
default: 'get', default: 'getAll',
description: 'The operation to perform.', description: 'The operation to perform.',
} },
] as INodeProperties[]; ] as INodeProperties[];
export const collectionFields = [ export const collectionFields = [
// Collections:entry:save {
displayName: 'Collection',
name: 'collection',
type: 'options',
default: '',
typeOptions: {
loadOptionsMethod: 'getCollections',
},
displayOptions: {
show: {
resource: [
'collections',
],
},
},
required: true,
description: 'Name of the collection to operate on.'
},
// Collections:entry:create
{ {
displayName: 'Data', displayName: 'Data',
name: 'data', name: 'data',
@ -51,19 +70,19 @@ export const collectionFields = [
'collections', 'collections',
], ],
operation: [ operation: [
'save', 'create',
] ]
}, },
}, },
description: 'The data to save.', description: 'The data to create.',
}, },
// Collections:entry:get // Collections:entry:getAll
{ {
displayName: 'Additional fields', displayName: 'Options',
name: 'additionalFields', name: 'options',
type: 'collection', type: 'collection',
placeholder: 'Add field', placeholder: 'Add Option',
default: {}, default: {},
displayOptions: { displayOptions: {
show: { show: {
@ -71,7 +90,7 @@ export const collectionFields = [
'collections', 'collections',
], ],
operation: [ operation: [
'get', 'getAll',
] ]
}, },
}, },
@ -96,6 +115,13 @@ export const collectionFields = [
}, },
description: 'Filter result by fields.', description: 'Filter result by fields.',
}, },
{
displayName: 'Language',
name: 'language',
type: 'string',
default: '',
description: 'Return normalized language fields.',
},
{ {
displayName: 'Limit', displayName: 'Limit',
name: 'limit', name: 'limit',
@ -103,6 +129,29 @@ export const collectionFields = [
default: '', default: '',
description: 'Limit number of returned entries.', description: 'Limit number of returned entries.',
}, },
{
displayName: 'Populate',
name: 'populate',
type: 'boolean',
required: true,
default: true,
description: 'Resolve linked collection items.',
},
{
displayName: 'RAW Data',
name: 'rawData',
type: 'boolean',
default: false,
description: `Returns the data exactly in the way it got received from the API.`,
},
{
displayName: 'Simple',
name: 'simple',
type: 'boolean',
required: true,
default: true,
description: 'Return only result entries.',
},
{ {
displayName: 'Skip', displayName: 'Skip',
name: 'skip', name: 'skip',
@ -117,29 +166,6 @@ export const collectionFields = [
default: '', default: '',
description: 'Sort result by fields.', description: 'Sort result by fields.',
}, },
{
displayName: 'Populate',
name: 'populate',
type: 'boolean',
required: true,
default: true,
description: 'Resolve linked collection items.',
},
{
displayName: 'Simple',
name: 'simple',
type: 'boolean',
required: true,
default: true,
description: 'Return only result entries.',
},
{
displayName: 'Language',
name: 'language',
type: 'string',
default: '',
description: 'Return normalized language fields.',
},
], ],
}, },

View file

@ -7,7 +7,7 @@ import { IDataObject } from 'n8n-workflow';
import { ICollection } from './CollectionInterface'; import { ICollection } from './CollectionInterface';
import { cockpitApiRequest } from './GenericFunctions'; import { cockpitApiRequest } from './GenericFunctions';
export async function saveCollectionEntry(this: IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, resourceName: string, data: IDataObject, id?: string): Promise<any> { // tslint:disable-line:no-any export async function createCollectionEntry(this: IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, resourceName: string, data: IDataObject, id?: string): Promise<any> { // tslint:disable-line:no-any
const body: ICollection = { const body: ICollection = {
data: JSON.parse(data.toString()) data: JSON.parse(data.toString())
}; };
@ -22,40 +22,52 @@ export async function saveCollectionEntry(this: IExecuteFunctions | IExecuteSing
return cockpitApiRequest.call(this, 'post', `/collections/save/${resourceName}`, body); return cockpitApiRequest.call(this, 'post', `/collections/save/${resourceName}`, body);
} }
export async function getCollectionEntries(this: IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, resourceName: string, additionalFields: IDataObject): Promise<any> { // tslint:disable-line:no-any
export async function getAllCollectionEntries(this: IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, resourceName: string, options: IDataObject): Promise<any> { // tslint:disable-line:no-any
const body: ICollection = {}; const body: ICollection = {};
if (additionalFields.fields) { if (options.fields) {
body.fields = JSON.parse(additionalFields.fields.toString()); body.fields = JSON.parse(options.fields.toString());
} }
if (additionalFields.filter) { if (options.filter) {
body.filter = JSON.parse(additionalFields.filter.toString()); body.filter = JSON.parse(options.filter.toString());
} }
if (additionalFields.limit) { if (options.limit) {
body.limit = additionalFields.limit as number; body.limit = options.limit as number;
} }
if (additionalFields.skip) { if (options.skip) {
body.skip = additionalFields.skip as number; body.skip = options.skip as number;
} }
if (additionalFields.sort) { if (options.sort) {
body.sort = JSON.parse(additionalFields.sort.toString()); body.sort = JSON.parse(options.sort.toString());
} }
if (additionalFields.populate) { if (options.populate) {
body.populate = additionalFields.populate as boolean; body.populate = options.populate as boolean;
} }
if (additionalFields.simple) { if (options.simple) {
body.simple = additionalFields.simple as boolean; body.simple = options.simple as boolean;
} }
if (additionalFields.language) { if (options.language) {
body.lang = additionalFields.language as string; body.lang = options.language as string;
} }
return cockpitApiRequest.call(this, 'post', `/collections/get/${resourceName}`, body); const resultData = await cockpitApiRequest.call(this, 'post', `/collections/get/${resourceName}`, body);
if (options.rawData === true) {
return resultData;
}
return (resultData as unknown as IDataObject).entries;
}
export async function getAllCollectionNames(this: IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions): Promise<string[]> {
return cockpitApiRequest.call(this, 'GET', `/collections/listCollections`, {});
} }

View file

@ -26,6 +26,22 @@ export const formOperations = [
] as INodeProperties[]; ] as INodeProperties[];
export const formFields = [ export const formFields = [
{
displayName: 'Form',
name: 'form',
type: 'string',
displayOptions: {
show: {
resource: [
'forms',
],
},
},
default: '',
required: true,
description: 'Name of the form to operate on.'
},
// Forms:submit // Forms:submit
{ {
displayName: 'Form data', displayName: 'Form data',

View file

@ -36,8 +36,11 @@ export async function cockpitApiRequest(this: IExecuteFunctions | IExecuteSingle
try { try {
return await this.helpers.request!(options); return await this.helpers.request!(options);
} catch (error) { } catch (error) {
const errorMessage = error.error.message || error.error.error; let errorMessage = error.message;
if (error.error) {
errorMessage = error.error.message || error.error.error;
}
throw new Error('Cockpit error: ' + errorMessage); throw new Error(`Cockpit error [${error.statusCode}]: ` + errorMessage);
} }
} }

View file

@ -14,12 +14,33 @@ export const singletonOperations = [
}, },
options: [ options: [
{ {
name: 'Get data', name: 'Get All',
value: 'get', value: 'getAll',
description: 'Get singleton data', description: 'Get all singletons',
}, },
], ],
default: 'get', default: 'getAll',
description: 'The operation to perform.', description: 'The operation to perform.',
} }
] as INodeProperties[]; ] as INodeProperties[];
export const singletonFields = [
{
displayName: 'Singleton',
name: 'singleton',
type: 'options',
default: '',
typeOptions: {
loadOptionsMethod: 'getSingletons',
},
displayOptions: {
show: {
resource: [
'singletons',
],
},
},
required: true,
description: 'Name of the singleton to operate on.'
},
] as INodeProperties[];

View file

@ -5,6 +5,10 @@ import {
} from 'n8n-core'; } from 'n8n-core';
import { cockpitApiRequest } from './GenericFunctions'; import { cockpitApiRequest } from './GenericFunctions';
export async function getSingleton(this: IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, resourceName: string): Promise<any> { // tslint:disable-line:no-any export async function getAllSingleton(this: IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, resourceName: string): Promise<any> { // tslint:disable-line:no-any
return cockpitApiRequest.call(this, 'get', `/singletons/get/${resourceName}`); return cockpitApiRequest.call(this, 'get', `/singletons/get/${resourceName}`);
} }
export async function getAllSingletonNames(this: IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions): Promise<string[]> {
return cockpitApiRequest.call(this, 'GET', `/singletons/listSingletons`, {});
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.9 KiB

After

Width:  |  Height:  |  Size: 834 B