n8n/packages/nodes-base/nodes/BambooHr/v1/transport/index.ts
Michael Kret 0ecbb4a19d
refactor: Format nodes-base package (A-F) (#3800)
* 🔨 prettier formated nodes - A

* 🔨 prettier formated nodes - B

*  prettier formated nodes - C

*  prettier formated nodes - D

*  prettier formated nodes - E-F

* 🎨 Adjust nodes-base formatting command (#3805)

* Format additional files in nodes A-F (#3811)

*  fixes

* 🎨 Add Mindee to ignored dirs

Co-authored-by: Iván Ovejero <ivov.src@gmail.com>
2022-08-01 22:47:55 +02:00

60 lines
1.4 KiB
TypeScript

import { IExecuteFunctions, IHookFunctions, ILoadOptionsFunctions } from 'n8n-core';
import { IDataObject, NodeApiError, NodeOperationError } from 'n8n-workflow';
import { OptionsWithUrl } from 'request';
/**
* Make an API request to Mattermost
*/
export async function apiRequest(
this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions,
method: 'GET' | 'POST' | 'PUT' | 'DELETE',
endpoint: string,
body: string[] | IDataObject = {},
query: IDataObject = {},
option: IDataObject = {},
) {
const credentials = await this.getCredentials('bambooHrApi');
//set-up credentials
const apiKey = credentials.apiKey;
const subdomain = credentials.subdomain;
//set-up uri
const uri = `https://api.bamboohr.com/api/gateway.php/${subdomain}/v1/${endpoint}`;
const options: OptionsWithUrl = {
method,
body,
qs: query,
url: uri,
auth: {
username: apiKey as string,
password: 'x',
},
json: true,
};
if (Object.keys(option).length) {
Object.assign(options, option);
}
if (!Object.keys(body).length) {
delete options.body;
}
if (!Object.keys(query).length) {
delete options.qs;
}
try {
//@ts-ignore
return await this.helpers.request(options);
} catch (error) {
const description = error?.response?.headers['x-bamboohr-error-messsage'] || '';
const message = error?.message || '';
throw new NodeApiError(this.getNode(), error, { message, description });
}
}