Skip to content

Commit

Permalink
feat(n8nApi node): add core node for consuming the n8n API (n8n-io#4076)
Browse files Browse the repository at this point in the history
* feat(n8n node): create n8n core node for consuming the n8n API

Co-authored-by: Michael Kret <michael.k@radency.com>
  • Loading branch information
mieky and michael-radency committed Sep 27, 2022
1 parent 67513e1 commit 929315f
Show file tree
Hide file tree
Showing 10 changed files with 1,206 additions and 0 deletions.
43 changes: 43 additions & 0 deletions packages/nodes-base/credentials/N8nApi.credentials.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import {
IAuthenticateGeneric,
ICredentialTestRequest,
ICredentialType,
INodeProperties,
} from 'n8n-workflow';

export class N8nApi implements ICredentialType {
name = 'n8nApi';
displayName = 'n8n API';
documentationUrl = 'n8nApi';
properties: INodeProperties[] = [
{
displayName: 'API Key',
name: 'apiKey',
type: 'string',
default: '',
description: 'The API key for the n8n instance',
},
{
displayName: 'Base URL',
name: 'baseUrl',
type: 'string',
default: '',
placeholder: 'https://<name>.app.n8n.cloud/api/v1',
description: 'The API URL of the n8n instance',
},
];
authenticate: IAuthenticateGeneric = {
type: 'generic',
properties: {
headers: {
'X-N8N-API-KEY': '={{ $credentials.apiKey }}',
},
},
};
test: ICredentialTestRequest = {
request: {
baseURL: '={{ $credentials.baseUrl }}',
url: '/workflows?limit=5',
},
};
}
168 changes: 168 additions & 0 deletions packages/nodes-base/nodes/N8n/CredentialDescription.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
import { INodeProperties } from 'n8n-workflow';
import { parseAndSetBodyJson } from './GenericFunctions';

export const credentialOperations: INodeProperties[] = [
{
displayName: 'Operation',
name: 'operation',
type: 'options',
noDataExpression: true,
default: 'create',
displayOptions: {
show: {
resource: ['credential'],
},
},
options: [
{
name: 'Create',
value: 'create',
action: 'Create a credential',
routing: {
request: {
method: 'POST',
url: '/credentials',
},
},
},
{
name: 'Delete',
value: 'delete',
action: 'Delete a credential',
routing: {
request: {
method: 'DELETE',
url: '=/credentials/{{ $parameter.credentialId }}',
},
},
},
{
name: 'Get Schema',
value: 'getSchema',
action: 'Get credential data schema for type',
routing: {
request: {
method: 'GET',
url: '=/credentials/schema/{{ $parameter.credentialTypeName }}',
},
},
},
],
},
];

const createOperation: INodeProperties[] = [
{
displayName: 'Name',
name: 'name',
type: 'string',
default: '',
placeholder: 'e.g. n8n account',
required: true,
displayOptions: {
show: {
resource: ['credential'],
operation: ['create'],
},
},
routing: {
request: {
body: {
name: '={{ $value }}',
},
},
},
description: 'Name of the new credential',
},
{
displayName: 'Credential Type',
name: 'credentialTypeName',
type: 'string',
placeholder: 'e.g. n8nApi',
default: '',
required: true,
displayOptions: {
show: {
resource: ['credential'],
operation: ['create'],
},
},
routing: {
request: {
body: {
type: '={{ $value }}',
},
},
},
description:
"The available types depend on nodes installed on the n8n instance. Some built-in types include e.g. 'githubApi', 'notionApi', and 'slackApi'.",
},
{
displayName: 'Data',
name: 'data',
type: 'json',
default: '',
placeholder:
'// e.g. for n8nApi \n{\n "apiKey": "my-n8n-api-key",\n "baseUrl": "https://<name>.app.n8n.cloud/api/v1",\n}',
required: true,
typeOptions: {
alwaysOpenEditWindow: true,
},
displayOptions: {
show: {
resource: ['credential'],
operation: ['create'],
},
},
routing: {
send: {
// Validate that the 'data' property is parseable as JSON and
// set it into the request as body.data.
preSend: [parseAndSetBodyJson('data', 'data')],
},
},
description:
"A valid JSON object with properties required for this Credential Type. To see the expected format, you can use 'Get Schema' operation.",
},
];

const deleteOperation: INodeProperties[] = [
{
displayName: 'Credential ID',
name: 'credentialId',
type: 'string',
required: true,
default: '',
displayOptions: {
show: {
resource: ['credential'],
operation: ['delete'],
},
},
},
];

const getSchemaOperation: INodeProperties[] = [
{
displayName: 'Credential Type',
name: 'credentialTypeName',
default: '',
placeholder: 'e.g. n8nApi',
required: true,
type: 'string',
displayOptions: {
show: {
resource: ['credential'],
operation: ['getSchema'],
},
},
description:
"The available types depend on nodes installed on the n8n instance. Some built-in types include e.g. 'githubApi', 'notionApi', and 'slackApi'.",
},
];

export const credentialFields: INodeProperties[] = [
...createOperation,
...deleteOperation,
...getSchemaOperation,
];
Loading

0 comments on commit 929315f

Please sign in to comment.