:sparkles Done

This commit is contained in:
Ricardo Espinoza
2019-11-09 20:29:05 -05:00
parent 526320f26a
commit e9613944db
3 changed files with 251 additions and 25 deletions

View File

@@ -5,34 +5,30 @@ import {
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
export async function rocketchatApiRequest(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, resource: string, method: string, operation: 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!');
throw new Error('No credentials got returned!');
}
const apiKey = `${credentials.apiKey}:X`;
const headerWithAuthentication = Object.assign({}, headers,
{ 'X-Auth-Token': credentials.authKey, 'X-User-Id': credentials.userId });
const headerWithAuthentication = Object.assign({}, headers, { Authorization: `${Buffer.from(apiKey).toString(BINARY_ENCODING)}` });
const endpoint = '';
const endpoint = 'rocket.chat/api/v1';
const options: OptionsWithUri = {
headers: headerWithAuthentication,
method,
body,
uri: `https://${credentials.domain}.${endpoint}${resource}`,
uri: `https://${credentials.subdomain}.${endpoint}${resource}.${operation}`,
json: true
};
if (_.isEmpty(options.body)) {
if (Object.keys(options.body).length === 0) {
delete options.body;
}
@@ -49,3 +45,13 @@ export async function rocketchatApiRequest(this: IHookFunctions | IExecuteFuncti
throw error.response.body;
}
}
export function validateJSON(json: string | undefined): any { // tslint:disable-line:no-any
let result;
try {
result = JSON.parse(json!);
} catch (exception) {
result = [];
}
return result;
}