rocketchat-node-setup

This commit is contained in:
Ricardo Espinoza
2019-11-08 23:11:01 -05:00
parent f60b8e33f7
commit edc6651a9b
5 changed files with 126 additions and 2 deletions

View File

@@ -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<any> { // 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;
}
}

View File

@@ -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<INodeExecutionData> {
return {
json: {}
};
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.6 KiB