General improvements

Improve explanatory comments, correct typings, make param names consistent, and simplify execute block.
This commit is contained in:
Iván Ovejero 2022-06-28 15:27:25 +02:00
commit 82b2552d10
4 changed files with 36 additions and 42 deletions

View file

@ -1,22 +1,22 @@
import { ICredentialType, NodePropertyTypes } from 'n8n-workflow'; import { ICredentialType, INodeProperties } from 'n8n-workflow';
export class ExampleCredentials implements ICredentialType { export class ExampleCredentials implements ICredentialType {
name = 'exampleCredentials'; name = 'exampleCredentials';
displayName = 'Example Credentials'; displayName = 'Example Credentials';
properties = [ properties: INodeProperties[] = [
// The credentials to get from user and save encrypted. // Credential data to request from the user, saved in encrypted format.
// Properties can be defined exactly in the same way // Credential properties are defined exactly like node properties.
// as node properties.
{ {
displayName: 'User', displayName: 'Base URL',
name: 'user', name: 'url',
type: 'string' as NodePropertyTypes, type: 'string',
default: '', default: '',
placeholder: 'https://example.com',
}, },
{ {
displayName: 'Access Token', displayName: 'Access Token',
name: 'accessToken', name: 'accessToken',
type: 'string' as NodePropertyTypes, type: 'string',
default: '', default: '',
}, },
]; ];

View file

@ -10,20 +10,18 @@ export class ExampleNode implements INodeType {
description: 'Basic Example Node', description: 'Basic Example Node',
defaults: { defaults: {
name: 'Example Node', name: 'Example Node',
color: '#772244',
}, },
inputs: ['main'], inputs: ['main'],
outputs: ['main'], outputs: ['main'],
properties: [ properties: [
// Node properties which the user gets displayed and // Node properties that the user can see and change on the node.
// can change on the node.
{ {
displayName: 'My String', displayName: 'My String',
name: 'myString', name: 'myString',
type: 'string', type: 'string',
default: '', default: '',
placeholder: 'Placeholder value', placeholder: 'Placeholder value',
description: 'The description text', description: 'This is a description',
}, },
], ],
}; };
@ -31,15 +29,12 @@ export class ExampleNode implements INodeType {
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> { async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const items = this.getInputData(); const items = this.getInputData();
let item: INodeExecutionData; // The node iterates over all input items and adds the key "myString" with the
let myString: string; // value the parameter "myString" resolves to. (This could be a different value
// for each item in case it contains an expression.)
// Iterates over all input items and add the key "myString" with the
// value the parameter "myString" resolves to.
// (This could be a different value for each item in case it contains an expression)
for (let itemIndex = 0; itemIndex < items.length; itemIndex++) { for (let itemIndex = 0; itemIndex < items.length; itemIndex++) {
myString = this.getNodeParameter('myString', itemIndex, '') as string; const myString = this.getNodeParameter('myString', itemIndex, '') as string;
item = items[itemIndex]; const item = items[itemIndex];
item.json['myString'] = myString; item.json['myString'] = myString;
} }

View file

@ -1,11 +1,10 @@
/* eslint-disable n8n-nodes-base/filesystem-wrong-node-filename */
import { INodeType, INodeTypeDescription } from 'n8n-workflow'; import { INodeType, INodeTypeDescription } from 'n8n-workflow';
import { httpVerbFields, httpVerbOperations } from './HttpVerbDescriptions'; import { httpVerbFields, httpVerbOperations } from './HttpVerbDescription';
export class HttpBin implements INodeType { export class HttpBin implements INodeType {
description: INodeTypeDescription = { description: INodeTypeDescription = {
displayName: 'HttpBin', displayName: 'HttpBin',
name: 'httpbin', name: 'httpBin',
icon: 'file:httpbin.svg', icon: 'file:httpbin.svg',
group: ['transform'], group: ['transform'],
version: 1, version: 1,
@ -36,11 +35,10 @@ export class HttpBin implements INodeType {
* *
* [Resource & Operation] * [Resource & Operation]
* *
*
* https://docs.n8n.io/integrations/creating-nodes/code/create-first-node/#resources-and-operations * https://docs.n8n.io/integrations/creating-nodes/code/create-first-node/#resources-and-operations
* *
* In our example, the operations are separated into their own file (HTTPVerbDescription) * In our example, the operations are separated into their own file (HTTPVerbDescription.ts)
* to keep this class easy to read * to keep this class easy to read.
* *
*/ */
properties: [ properties: [
@ -52,10 +50,10 @@ export class HttpBin implements INodeType {
options: [ options: [
{ {
name: 'HTTP Verb', name: 'HTTP Verb',
value: 'httpverbs', value: 'httpVerb',
}, },
], ],
default: 'httpverbs', default: 'httpVerb',
}, },
...httpVerbOperations, ...httpVerbOperations,

View file

@ -1,15 +1,16 @@
import { INodeProperties } from 'n8n-workflow'; import { INodeProperties } from 'n8n-workflow';
// This maps the operations to when the Resource option HTTP Verbs is selected // When the resource `httpVerb` is selected, this `operation` parameter will be shown.
export const httpVerbOperations: INodeProperties[] = [ export const httpVerbOperations: INodeProperties[] = [
{ {
displayName: 'Operation', displayName: 'Operation',
name: 'operation', name: 'operation',
type: 'options', type: 'options',
noDataExpression: true, noDataExpression: true,
displayOptions: { displayOptions: {
show: { show: {
resource: ['httpverbs'], resource: ['httpVerb'],
}, },
}, },
options: [ options: [
@ -38,8 +39,8 @@ export const httpVerbOperations: INodeProperties[] = [
}, },
]; ];
// Here we define what to show when the GET Operation is selected // Here we define what to show when the `get` operation is selected.
// We do that by adding operation: ["get"], to "displayOptions.show" // We do that by adding `operation: ["get"]` to `displayOptions.show`
const getOperation: INodeProperties[] = [ const getOperation: INodeProperties[] = [
{ {
name: 'typeofData', name: 'typeofData',
@ -48,7 +49,7 @@ const getOperation: INodeProperties[] = [
displayName: 'Type of Data', displayName: 'Type of Data',
displayOptions: { displayOptions: {
show: { show: {
resource: ['httpverbs'], resource: ['httpVerb'],
operation: ['get'], operation: ['get'],
}, },
}, },
@ -68,7 +69,7 @@ const getOperation: INodeProperties[] = [
displayName: 'Query Parameters', displayName: 'Query Parameters',
displayOptions: { displayOptions: {
show: { show: {
resource: ['httpverbs'], resource: ['httpVerb'],
operation: ['get'], operation: ['get'],
}, },
}, },
@ -109,8 +110,8 @@ const getOperation: INodeProperties[] = [
}, },
]; ];
// Here we define what to show when the DELETE Operation is selected // Here we define what to show when the DELETE Operation is selected.
// We do that by adding operation: ["delete"], to "displayOptions.show" // We do that by adding `operation: ["delete"]` to `displayOptions.show`
const deleteOperation: INodeProperties[] = [ const deleteOperation: INodeProperties[] = [
{ {
name: 'typeofData', name: 'typeofData',
@ -119,7 +120,7 @@ const deleteOperation: INodeProperties[] = [
displayName: 'Type of Data', displayName: 'Type of Data',
displayOptions: { displayOptions: {
show: { show: {
resource: ['httpverbs'], resource: ['httpVerb'],
operation: ['delete'], operation: ['delete'],
}, },
}, },
@ -143,7 +144,7 @@ const deleteOperation: INodeProperties[] = [
displayName: 'Query Parameters', displayName: 'Query Parameters',
displayOptions: { displayOptions: {
show: { show: {
resource: ['httpverbs'], resource: ['httpVerb'],
operation: ['delete'], operation: ['delete'],
typeofData: ['queryParameter'], typeofData: ['queryParameter'],
}, },
@ -190,7 +191,7 @@ const deleteOperation: INodeProperties[] = [
displayName: 'JSON Object', displayName: 'JSON Object',
displayOptions: { displayOptions: {
show: { show: {
resource: ['httpverbs'], resource: ['httpVerb'],
operation: ['delete'], operation: ['delete'],
typeofData: ['jsonData'], typeofData: ['jsonData'],
}, },
@ -234,12 +235,12 @@ const deleteOperation: INodeProperties[] = [
export const httpVerbFields: INodeProperties[] = [ export const httpVerbFields: INodeProperties[] = [
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
/* Http Verbs:Get */ /* httpVerb:get */
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
...getOperation, ...getOperation,
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
/* Http Verbs:Delete */ /* httpVerb:delete */
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
...deleteOperation, ...deleteOperation,
]; ];