🔀 Merge branch 'terenaa-feature/cockpit-improvements'

This commit is contained in:
Jan Oberhauser 2020-04-12 13:10:23 +02:00
commit a3a1d9244d
7 changed files with 208 additions and 57 deletions

View file

@ -21,6 +21,7 @@ import {
formOperations formOperations
} from './FormDescription'; } from './FormDescription';
import { submitForm } from './FormFunctions'; import { submitForm } from './FormFunctions';
import { createDataFromParameters } from './GenericFunctions';
import { import {
singletonFields, singletonFields,
singletonOperations, singletonOperations,
@ -56,7 +57,7 @@ export class Cockpit implements INodeType {
displayName: 'Resource', displayName: 'Resource',
name: 'resource', name: 'resource',
type: 'options', type: 'options',
default: 'collections', default: 'collection',
description: 'Resource to consume.', description: 'Resource to consume.',
options: [ options: [
{ {
@ -74,7 +75,6 @@ export class Cockpit implements INodeType {
], ],
}, },
...collectionOperations, ...collectionOperations,
...collectionFields, ...collectionFields,
...formOperations, ...formOperations,
@ -84,7 +84,6 @@ export class Cockpit implements INodeType {
], ],
}; };
methods = { methods = {
loadOptions: { loadOptions: {
async getCollections(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> { async getCollections(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
@ -123,34 +122,37 @@ export class Cockpit implements INodeType {
for (let i = 0; i < length; i++) { for (let i = 0; i < length; i++) {
if (resource === 'collection') { if (resource === 'collection') {
const collectionName = this.getNodeParameter('collection', i) as string; const collectionName = this.getNodeParameter('collection', i) as string;
if (operation === 'create') { if (operation === 'create') {
const data = this.getNodeParameter('data', i) as IDataObject; const data = createDataFromParameters.call(this, i);
responseData = await createCollectionEntry.call(this, collectionName, data); responseData = await createCollectionEntry.call(this, collectionName, data);
} else if (operation === 'getAll') { } else if (operation === 'getAll') {
const options = this.getNodeParameter('options', i) as IDataObject; const options = this.getNodeParameter('options', i) as IDataObject;
const returnAll = this.getNodeParameter('returnAll', i) as boolean; const returnAll = this.getNodeParameter('returnAll', i) as boolean;
if (returnAll !== true) { if (!returnAll) {
options.limit = this.getNodeParameter('limit', i) as number; options.limit = this.getNodeParameter('limit', i) as number;
} }
responseData = await getAllCollectionEntries.call(this, collectionName, options); 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 = createDataFromParameters.call(this, i);
responseData = await createCollectionEntry.call(this, collectionName, data, id); responseData = await createCollectionEntry.call(this, collectionName, data, id);
} }
} else if (resource === 'form') { } else if (resource === 'form') {
const formName = this.getNodeParameter('form', i) as string; const formName = this.getNodeParameter('form', i) as string;
if (operation === 'submit') { if (operation === 'submit') {
const form = this.getNodeParameter('form', i) as IDataObject; const form = createDataFromParameters.call(this, i);
responseData = await submitForm.call(this, formName, form); responseData = await submitForm.call(this, formName, form);
} }
} else if (resource === 'singleton') { } else if (resource === 'singleton') {
const singletonName = this.getNodeParameter('singleton', i) as string; const singletonName = this.getNodeParameter('singleton', i) as string;
if (operation === 'get') { if (operation === 'get') {
responseData = await getSingleton.call(this, singletonName); responseData = await getSingleton.call(this, singletonName);
} }

View file

@ -14,17 +14,17 @@ export const collectionOperations = [
}, },
options: [ options: [
{ {
name: 'Create an entry', name: 'Create an Entry',
value: 'create', value: 'create',
description: 'Create a collection entry', description: 'Create a collection entry',
}, },
{ {
name: 'Get all entries', name: 'Get all Entries',
value: 'getAll', value: 'getAll',
description: 'Get all collection entries', description: 'Get all collection entries',
}, },
{ {
name: 'Update an entry', name: 'Update an Entry',
value: 'update', value: 'update',
description: 'Update a collection entries', description: 'Update a collection entries',
}, },
@ -54,29 +54,6 @@ export const collectionFields = [
description: 'Name of the collection to operate on.' description: 'Name of the collection to operate on.'
}, },
// Collection:entry:create
{
displayName: 'Data',
name: 'data',
type: 'json',
required: true,
default: '',
typeOptions: {
alwaysOpenEditWindow: true,
},
displayOptions: {
show: {
resource: [
'collection',
],
operation: [
'create',
]
},
},
description: 'The data to create.',
},
// Collection:entry:getAll // Collection:entry:getAll
{ {
displayName: 'Return All', displayName: 'Return All',
@ -139,22 +116,24 @@ export const collectionFields = [
{ {
displayName: 'Fields', displayName: 'Fields',
name: 'fields', name: 'fields',
type: 'json', type: 'string',
default: '', default: '',
typeOptions: { typeOptions: {
alwaysOpenEditWindow: true, alwaysOpenEditWindow: true,
}, },
description: 'Fields to get.', placeholder: '_id,name',
description: 'Comma separated list of fields to get.',
}, },
{ {
displayName: 'Filter', displayName: 'Filter Query',
name: 'filter', name: 'filter',
type: 'json', type: 'json',
default: '', default: '',
typeOptions: { typeOptions: {
alwaysOpenEditWindow: true, alwaysOpenEditWindow: true,
}, },
description: 'Filter result by fields.', placeholder: '{"name": "Jim"}',
description: 'Filter query in <a href="https://jeroen.github.io/mongolite/query-data.html" target="_blank">Mongolite format</a>.',
}, },
{ {
displayName: 'Language', displayName: 'Language',
@ -186,11 +165,12 @@ export const collectionFields = [
description: 'Skip number of entries.', description: 'Skip number of entries.',
}, },
{ {
displayName: 'Sort', displayName: 'Sort Query',
name: 'sort', name: 'sort',
type: 'json', type: 'json',
default: '', default: '',
description: 'Sort result by fields.', placeholder: '{"price": -1}',
description: 'Sort query in <a href="https://jeroen.github.io/mongolite/query-data.html" target="_blank">Mongolite format</a>.',
}, },
], ],
}, },
@ -214,25 +194,95 @@ export const collectionFields = [
}, },
description: 'The entry ID.', description: 'The entry ID.',
}, },
// Collection:entry:create
// Collection:entry:update
{ {
displayName: 'Data', displayName: 'JSON Data fields',
name: 'data', name: 'jsonDataFields',
type: 'json', type: 'boolean',
required: true, default: false,
default: '',
typeOptions: {
alwaysOpenEditWindow: true,
},
displayOptions: { displayOptions: {
show: { show: {
resource: [ resource: [
'collection', 'collection',
], ],
operation: [ operation: [
'create',
'update', 'update',
] ]
}, },
}, },
description: 'The data to update.', description: 'If new entry fields should be set via the value-key pair UI or JSON.',
},
{
displayName: 'Entry Data',
name: 'dataFieldsJson',
type: 'json',
default: '',
typeOptions: {
alwaysOpenEditWindow: true,
},
displayOptions: {
show: {
jsonDataFields: [
true,
],
resource: [
'collection',
],
operation: [
'create',
'update',
]
},
},
description: 'Entry data to send as JSON.',
},
{
displayName: 'Entry Data',
name: 'dataFieldsUi',
type: 'fixedCollection',
typeOptions: {
multipleValues: true,
},
default: {},
displayOptions: {
show: {
jsonDataFields: [
false,
],
resource: [
'collection',
],
operation: [
'create',
'update',
]
},
},
options: [
{
displayName: 'Field',
name: 'field',
values: [
{
displayName: 'Name',
name: 'name',
type: 'string',
default: '',
description: 'Name of the field.',
},
{
displayName: 'Value',
name: 'value',
type: 'string',
default: '',
description: 'Value of the field.',
},
],
},
],
description: 'Entry data to send.',
}, },
] as INodeProperties[]; ] as INodeProperties[];

View file

@ -9,7 +9,7 @@ import { cockpitApiRequest } from './GenericFunctions';
export async function createCollectionEntry(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,
}; };
if (id) { if (id) {
@ -27,7 +27,16 @@ export async function getAllCollectionEntries(this: IExecuteFunctions | IExecute
const body: ICollection = {}; const body: ICollection = {};
if (options.fields) { if (options.fields) {
body.fields = JSON.parse(options.fields.toString()); const fields = (options.fields as string).split(',').map(field => field.trim() );
const bodyFields = {
_id: false,
} as IDataObject;
for (const field of fields) {
bodyFields[field] = true;
}
body.fields = bodyFields;
} }
if (options.filter) { if (options.filter) {

View file

@ -14,7 +14,7 @@ export const formOperations = [
}, },
options: [ options: [
{ {
name: 'Submit a form', name: 'Submit a Form',
value: 'submit', value: 'submit',
description: 'Store submission of a form', description: 'Store submission of a form',
}, },
@ -44,21 +44,88 @@ export const formFields = [
// Form:submit // Form:submit
{ {
displayName: 'Form data', displayName: 'JSON Data fields',
name: 'form', name: 'jsonDataFields',
type: 'boolean',
default: false,
displayOptions: {
show: {
resource: [
'form',
],
operation: [
'submit',
]
},
},
description: 'If form fields should be set via the value-key pair UI or JSON.',
},
{
displayName: 'Form Data',
name: 'dataFieldsJson',
type: 'json', type: 'json',
required: true,
default: '', default: '',
typeOptions: { typeOptions: {
alwaysOpenEditWindow: true, alwaysOpenEditWindow: true,
}, },
displayOptions: { displayOptions: {
show: { show: {
jsonDataFields: [
true,
],
resource: [ resource: [
'form', 'form',
], ],
operation: [
'submit',
]
}, },
}, },
description: 'The data to save.', description: 'Form data to send as JSON.',
},
{
displayName: 'Form Data',
name: 'dataFieldsUi',
type: 'fixedCollection',
typeOptions: {
multipleValues: true,
},
default: {},
displayOptions: {
show: {
jsonDataFields: [
false,
],
resource: [
'form',
],
operation: [
'submit',
]
},
},
options: [
{
displayName: 'Field',
name: 'field',
values: [
{
displayName: 'Name',
name: 'name',
type: 'string',
default: '',
description: 'Name of the field.',
},
{
displayName: 'Value',
name: 'value',
type: 'string',
default: '',
description: 'Value of the field.',
},
],
},
],
description: 'Form data to send.',
}, },
] as INodeProperties[]; ] as INodeProperties[];

View file

@ -9,7 +9,7 @@ import { cockpitApiRequest } from './GenericFunctions';
export async function submitForm(this: IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, resourceName: string, form: IDataObject) { export async function submitForm(this: IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, resourceName: string, form: IDataObject) {
const body: IForm = { const body: IForm = {
form: JSON.parse(form.toString()) form
}; };
return cockpitApiRequest.call(this, 'post', `/forms/submit/${resourceName}`, body); return cockpitApiRequest.call(this, 'post', `/forms/submit/${resourceName}`, body);

View file

@ -44,3 +44,26 @@ export async function cockpitApiRequest(this: IExecuteFunctions | IExecuteSingle
throw new Error(`Cockpit error [${error.statusCode}]: ` + errorMessage); throw new Error(`Cockpit error [${error.statusCode}]: ` + errorMessage);
} }
} }
export function createDataFromParameters(this: IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, itemIndex: number): IDataObject {
const dataFieldsAreJson = this.getNodeParameter('jsonDataFields', itemIndex) as boolean;
if (dataFieldsAreJson) {
// Parameters are defined as JSON
return JSON.parse(this.getNodeParameter('dataFieldsJson', itemIndex, {}) as string);
}
// Parameters are defined in UI
const uiDataFields = this.getNodeParameter('dataFieldsUi', itemIndex, {}) as IDataObject;
const unpacked: IDataObject = {};
if (uiDataFields.field === undefined) {
return unpacked;
}
for (const field of uiDataFields!.field as IDataObject[]) {
unpacked[field!.name as string] = field!.value;
}
return unpacked;
}

View file

@ -16,7 +16,7 @@ export const singletonOperations = [
{ {
name: 'Get', name: 'Get',
value: 'get', value: 'get',
description: 'Gets a singleton', description: 'Gets a Singleton',
}, },
], ],
default: 'get', default: 'get',