Calendly Trigger

This commit is contained in:
Ricardo
2020-03-10 12:42:57 -04:00
parent 312f960535
commit 6222861912
5 changed files with 219 additions and 2 deletions

View File

@@ -0,0 +1,51 @@
import { OptionsWithUri } from 'request';
import {
IExecuteFunctions,
ILoadOptionsFunctions,
} from 'n8n-core';
import {
IDataObject,
IHookFunctions,
IWebhookFunctions
} from 'n8n-workflow';
export async function calendlyApiRequest(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('calendlyApi');
if (credentials === undefined) {
throw new Error('No credentials got returned!');
}
const endpoint = 'https://calendly.com/api/v1';
let options: OptionsWithUri = {
headers: {
'Content-Type': 'application/json',
'X-TOKEN': credentials.apiKey,
},
method,
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) {
const errorMessage = error.response.body.message || error.response.body.description || error.message;
throw new Error(`Calendly error response [${error.statusCode}]: ${errorMessage}`);
}
throw error;
}
}