n8n/packages/nodes-base/nodes/HighLevel/GenericFunctions.ts
2021-03-24 08:57:26 +01:00

50 lines
918 B
TypeScript

import {
IExecuteFunctions,
ILoadOptionsFunctions,
} from 'n8n-core';
import {
IHookFunctions,
} from 'n8n-workflow';
/**
* Make an authenticated REST API request to HighLevel.
*/
export async function highLevelApiRequest(
this: IExecuteFunctions | ILoadOptionsFunctions | IHookFunctions,
method: string,
endpoint: string,
body: object = {},
qs: object = {},
) {
const { apiKey } = this.getCredentials('highLevelApi') as { apiKey: string };
const options = {
headers: {
Authorization: apiKey,
},
method,
body,
qs,
uri: `https://rest.gohighlevel.com/v1${endpoint}`,
json: true,
};
try {
return await this.helpers.request!.call(this, options);
} catch (error) {
if (error?.response?.body?.error) {
const { error: errorMessage } = error.response.body;
throw new Error(
`HighLevel error response [${error.statusCode}]: ${errorMessage}`,
);
}
throw error;
}
}