n8n/packages/nodes-base/nodes/Zulip/GenericFunctions.ts

65 lines
1.6 KiB
TypeScript
Raw Normal View History

2020-02-26 17:46:00 -08:00
import { OptionsWithUri } from 'request';
import {
IExecuteFunctions,
ILoadOptionsFunctions,
} from 'n8n-core';
import {
IDataObject,
IHookFunctions,
IWebhookFunctions
} from 'n8n-workflow';
export async function zulipApiRequest(this: IExecuteFunctions | IWebhookFunctions | IHookFunctions | ILoadOptionsFunctions, method: string, resource: string, body: any = {}, query: IDataObject = {}, uri?: string, option: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
const credentials = this.getCredentials('zulipApi');
if (credentials === undefined) {
throw new Error('No credentials got returned!');
}
const endpoint = `${credentials.url}/api/v1`;
let options: OptionsWithUri = {
2020-02-27 14:27:21 -08:00
auth: {
user: credentials.email as string,
password: credentials.apiKey as string,
},
2020-02-26 17:46:00 -08:00
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
method,
form: body,
qs: query,
uri: uri || `${endpoint}${resource}`,
json: true
};
if (!Object.keys(body).length) {
delete options.form;
}
if (!Object.keys(query).length) {
delete options.qs;
}
options = Object.assign({}, options, option);
try {
return await this.helpers.request!(options);
} catch (error) {
if (error.response) {
2020-02-27 14:27:21 -08:00
const errorMessage = error.response.body.message || error.response.body.description || error.message;
2020-02-26 17:46:00 -08:00
throw new Error(`Zulip error response [${error.statusCode}]: ${errorMessage}`);
}
throw error;
}
}
2020-05-19 06:19:35 -07:00
export function validateJSON(json: string | undefined): any { // tslint:disable-line:no-any
let result;
try {
result = JSON.parse(json!);
} catch (exception) {
result = undefined;
}
return result;
}