2022-08-01 13:47:55 -07:00
|
|
|
import { OptionsWithUri } from 'request';
|
2020-04-26 12:56:50 -07:00
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
import { IExecuteFunctions, ILoadOptionsFunctions } from 'n8n-core';
|
2020-04-26 12:56:50 -07:00
|
|
|
|
2022-11-08 06:28:21 -08:00
|
|
|
import { IDataObject, IHookFunctions, IWebhookFunctions, NodeApiError } from 'n8n-workflow';
|
2020-04-26 12:56:50 -07:00
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
import { snakeCase } from 'change-case';
|
2020-04-26 12:56:50 -07:00
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
export async function bannerbearApiRequest(
|
|
|
|
this: IExecuteFunctions | IWebhookFunctions | IHookFunctions | ILoadOptionsFunctions,
|
|
|
|
method: string,
|
|
|
|
resource: string,
|
|
|
|
// tslint:disable-next-line:no-any
|
|
|
|
body: any = {},
|
|
|
|
query: IDataObject = {},
|
|
|
|
uri?: string,
|
|
|
|
headers: IDataObject = {},
|
|
|
|
// tslint:disable-next-line:no-any
|
|
|
|
): Promise<any> {
|
2021-08-20 09:57:30 -07:00
|
|
|
const credentials = await this.getCredentials('bannerbearApi');
|
2020-04-26 12:56:50 -07:00
|
|
|
|
|
|
|
const options: OptionsWithUri = {
|
|
|
|
headers: {
|
|
|
|
Accept: 'application/json',
|
|
|
|
Authorization: `Bearer ${credentials.apiKey}`,
|
|
|
|
},
|
|
|
|
method,
|
|
|
|
body,
|
|
|
|
qs: query,
|
|
|
|
uri: uri || `https://api.bannerbear.com/v2${resource}`,
|
|
|
|
json: true,
|
|
|
|
};
|
|
|
|
if (!Object.keys(body).length) {
|
|
|
|
delete options.form;
|
|
|
|
}
|
|
|
|
if (!Object.keys(query).length) {
|
|
|
|
delete options.qs;
|
|
|
|
}
|
|
|
|
options.headers = Object.assign({}, options.headers, headers);
|
|
|
|
try {
|
|
|
|
return await this.helpers.request!(options);
|
|
|
|
} catch (error) {
|
2021-04-16 09:33:36 -07:00
|
|
|
throw new NodeApiError(this.getNode(), error);
|
2020-04-26 12:56:50 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
export function keysToSnakeCase(elements: IDataObject[] | IDataObject): IDataObject[] {
|
2020-04-26 12:56:50 -07:00
|
|
|
if (!Array.isArray(elements)) {
|
|
|
|
elements = [elements];
|
|
|
|
}
|
|
|
|
for (const element of elements) {
|
|
|
|
for (const key of Object.keys(element)) {
|
|
|
|
if (key !== snakeCase(key)) {
|
|
|
|
element[snakeCase(key)] = element[key];
|
|
|
|
delete element[key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return elements;
|
|
|
|
}
|