mirror of
https://github.com/n8n-io/n8n.git
synced 2024-12-26 05:04:05 -08:00
d3fecb9f6d
* Centralized error throwing for encryption key * Unifying the error message used by cli and core packages * Improvements to error messages to make it more DRY * Removed unnecessary throw * Throwing error when credential does not exist to simplify node behavior (#3112) Co-authored-by: Iván Ovejero <ivov.src@gmail.com>
48 lines
1.2 KiB
TypeScript
48 lines
1.2 KiB
TypeScript
import {
|
|
OptionsWithUri,
|
|
} from 'request';
|
|
|
|
import {
|
|
IExecuteFunctions,
|
|
IHookFunctions,
|
|
ILoadOptionsFunctions,
|
|
} from 'n8n-core';
|
|
import { NodeApiError, NodeOperationError, } from 'n8n-workflow';
|
|
|
|
export async function rocketchatApiRequest(this: IExecuteFunctions | ILoadOptionsFunctions, resource: string, method: string, operation: string, body: any = {}, headers?: object): Promise<any> { // tslint:disable-line:no-any
|
|
const credentials = await this.getCredentials('rocketchatApi');
|
|
|
|
const headerWithAuthentication = Object.assign({}, headers,
|
|
{
|
|
'X-Auth-Token': credentials.authKey,
|
|
'X-User-Id': credentials.userId,
|
|
},
|
|
);
|
|
|
|
const options: OptionsWithUri = {
|
|
headers: headerWithAuthentication,
|
|
method,
|
|
body,
|
|
uri: `${credentials.domain}/api/v1${resource}.${operation}`,
|
|
json: true,
|
|
};
|
|
if (Object.keys(options.body).length === 0) {
|
|
delete options.body;
|
|
}
|
|
try {
|
|
return await this.helpers.request!(options);
|
|
} catch (error) {
|
|
throw new NodeApiError(this.getNode(), error);
|
|
}
|
|
}
|
|
|
|
export function validateJSON(json: string | undefined): any { // tslint:disable-line:no-any
|
|
let result;
|
|
try {
|
|
result = JSON.parse(json!);
|
|
} catch (exception) {
|
|
result = [];
|
|
}
|
|
return result;
|
|
}
|