2022-08-17 08:50:24 -07:00
|
|
|
import { IExecuteFunctions, IHookFunctions, ILoadOptionsFunctions } from 'n8n-core';
|
|
|
|
import { NodeApiError, NodeOperationError } from 'n8n-workflow';
|
2020-08-04 08:30:08 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { OptionsWithUri } from 'request';
|
2020-08-04 08:30:08 -07:00
|
|
|
|
2020-08-04 08:23:06 -07:00
|
|
|
/**
|
|
|
|
* Make an API request to Twake
|
|
|
|
*
|
|
|
|
* @param {IHookFunctions} this
|
|
|
|
* @param {string} method
|
|
|
|
* @param {string} url
|
|
|
|
* @param {object} body
|
|
|
|
* @returns {Promise<any>}
|
|
|
|
*/
|
2022-08-17 08:50:24 -07:00
|
|
|
export async function twakeApiRequest(
|
|
|
|
this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions,
|
|
|
|
method: string,
|
|
|
|
resource: string,
|
|
|
|
body: object,
|
|
|
|
query?: object,
|
|
|
|
uri?: string,
|
|
|
|
// tslint:disable-next-line:no-any
|
|
|
|
): Promise<any> {
|
2020-08-04 08:23:06 -07:00
|
|
|
const authenticationMethod = this.getNodeParameter('twakeVersion', 0, 'twakeCloudApi') as string;
|
|
|
|
|
|
|
|
const options: OptionsWithUri = {
|
|
|
|
headers: {},
|
|
|
|
method,
|
|
|
|
body,
|
|
|
|
qs: query,
|
2022-06-18 20:10:08 -07:00
|
|
|
uri: uri || `https://plugins.twake.app/plugins/n8n${resource}`,
|
2020-08-04 08:23:06 -07:00
|
|
|
json: true,
|
|
|
|
};
|
|
|
|
|
2020-08-04 08:30:08 -07:00
|
|
|
// if (authenticationMethod === 'cloud') {
|
|
|
|
// } else {
|
2021-08-20 09:57:30 -07:00
|
|
|
// const credentials = await this.getCredentials('twakeServerApi');
|
2020-08-04 08:30:08 -07:00
|
|
|
// options.auth = { user: credentials!.publicId as string, pass: credentials!.privateApiKey as string };
|
|
|
|
// options.uri = `${credentials!.hostUrl}/api/v1${resource}`;
|
|
|
|
// }
|
2020-08-04 08:23:06 -07:00
|
|
|
|
|
|
|
try {
|
2022-06-18 20:10:08 -07:00
|
|
|
return await this.helpers.requestWithAuthentication.call(this, 'twakeCloudApi', options);
|
2020-08-04 08:23:06 -07:00
|
|
|
} catch (error) {
|
2022-06-18 20:10:08 -07:00
|
|
|
if (error.error?.code === 'ECONNREFUSED') {
|
2021-04-16 09:33:36 -07:00
|
|
|
throw new NodeApiError(this.getNode(), error, { message: 'Twake host is not accessible!' });
|
2020-08-04 08:23:06 -07:00
|
|
|
}
|
2021-04-16 09:33:36 -07:00
|
|
|
throw new NodeApiError(this.getNode(), error);
|
2020-08-04 08:23:06 -07:00
|
|
|
}
|
|
|
|
}
|