diff --git a/packages/nodes-base/credentials/RocketchatApi.credentials.ts b/packages/nodes-base/credentials/RocketchatApi.credentials.ts new file mode 100644 index 0000000000..ab6d0019ed --- /dev/null +++ b/packages/nodes-base/credentials/RocketchatApi.credentials.ts @@ -0,0 +1,18 @@ +import { + ICredentialType, + NodePropertyTypes, +} from 'n8n-workflow'; + + +export class RocketchatApi implements ICredentialType { + name = 'rocketchatApi'; + displayName = 'Rocket API'; + properties = [ + { + displayName: 'API Key', + name: 'apiKey', + type: 'string' as NodePropertyTypes, + default: '', + }, + ]; +} diff --git a/packages/nodes-base/nodes/Rocketchat/GenericFunctions.ts b/packages/nodes-base/nodes/Rocketchat/GenericFunctions.ts new file mode 100644 index 0000000000..1e00514fc7 --- /dev/null +++ b/packages/nodes-base/nodes/Rocketchat/GenericFunctions.ts @@ -0,0 +1,51 @@ +import { OptionsWithUri } from 'request'; + +import { + IExecuteFunctions, + IHookFunctions, + ILoadOptionsFunctions, + IExecuteSingleFunctions, + BINARY_ENCODING +} from 'n8n-core'; + +import * as _ from 'lodash'; + +export async function rocketchatApiRequest(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, resource: string, method: string, body: any = {}, headers?: object): Promise { // tslint:disable-line:no-any + + const credentials = this.getCredentials('rocketchatApi'); + + if (credentials === undefined) { + throw new Error('No credentials got returned!'); + } + + const apiKey = `${credentials.apiKey}:X`; + + const headerWithAuthentication = Object.assign({}, headers, { Authorization: `${Buffer.from(apiKey).toString(BINARY_ENCODING)}` }); + + const endpoint = ''; + + const options: OptionsWithUri = { + headers: headerWithAuthentication, + method, + body, + uri: `https://${credentials.domain}.${endpoint}${resource}`, + json: true + }; + + if (_.isEmpty(options.body)) { + delete options.body; + } + + try { + return await this.helpers.request!(options); + } catch (error) { + console.error(error); + + const errorMessage = error.response.body.message || error.response.body.Message; + + if (errorMessage !== undefined) { + throw errorMessage; + } + throw error.response.body; + } +} diff --git a/packages/nodes-base/nodes/Rocketchat/Rocketchat.node.ts b/packages/nodes-base/nodes/Rocketchat/Rocketchat.node.ts new file mode 100644 index 0000000000..1a2ff65341 --- /dev/null +++ b/packages/nodes-base/nodes/Rocketchat/Rocketchat.node.ts @@ -0,0 +1,53 @@ +import { + IExecuteSingleFunctions, +} from 'n8n-core'; +import { + IDataObject, + INodeTypeDescription, + INodeExecutionData, + INodeType, + ILoadOptionsFunctions, + INodePropertyOptions, +} from 'n8n-workflow'; +import { + rocketchatApiRequest +} from './GenericFunctions'; + + +export class Rocketchat implements INodeType { + + description: INodeTypeDescription = { + displayName: 'Rocketchat', + name: 'Rocketchat', + icon: 'file:Rocketchat.png', + group: ['output'], + version: 1, + subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}', + description: 'Consume Rocketchat API', + defaults: { + name: 'Rocketchat', + color: '#c02428', + }, + inputs: ['main'], + outputs: ['main'], + credentials: [ + { + name: 'rocketchatApi', + required: true, + } + ], + properties: [ + + ] + }; + + methods = { + + }; + + async executeSingle(this: IExecuteSingleFunctions): Promise { + return { + json: {} + }; + } +} diff --git a/packages/nodes-base/nodes/Rocketchat/rocketchat.png b/packages/nodes-base/nodes/Rocketchat/rocketchat.png new file mode 100644 index 0000000000..93dfcacf17 Binary files /dev/null and b/packages/nodes-base/nodes/Rocketchat/rocketchat.png differ diff --git a/packages/nodes-base/package.json b/packages/nodes-base/package.json index 6af0a99a5c..45f5a36615 100644 --- a/packages/nodes-base/package.json +++ b/packages/nodes-base/package.json @@ -57,7 +57,8 @@ "dist/credentials/TwilioApi.credentials.js", "dist/credentials/TypeformApi.credentials.js", "dist/credentials/MandrillApi.credentials.js", - "dist/credentials/TodoistApi.credentials.js" + "dist/credentials/TodoistApi.credentials.js", + "dist/credentials/RocketchatApi.credentials.js" ], "nodes": [ "dist/nodes/ActiveCampaign/ActiveCampaign.node.js", @@ -124,7 +125,8 @@ "dist/nodes/Webhook.node.js", "dist/nodes/Xml.node.js", "dist/nodes/Mandrill/Mandrill.node.js", - "dist/nodes/Todoist/Todoist.node.js" + "dist/nodes/Todoist/Todoist.node.js", + "dist/nodes/Rocketchat/Rocketchat.node.js" ] }, "devDependencies": {