2020-06-17 10:38:30 -07:00
|
|
|
import {
|
|
|
|
IExecuteFunctions,
|
|
|
|
IHookFunctions,
|
|
|
|
} from 'n8n-core';
|
2021-04-16 09:33:36 -07:00
|
|
|
import { NodeApiError, NodeOperationError, } from 'n8n-workflow';
|
2020-06-17 10:38:30 -07:00
|
|
|
|
|
|
|
import {
|
2020-07-22 14:52:40 -07:00
|
|
|
OptionsWithUri,
|
|
|
|
} from 'request';
|
2020-06-17 10:38:30 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Make an API request to NextCloud
|
|
|
|
*
|
|
|
|
* @param {IHookFunctions} this
|
|
|
|
* @param {string} method
|
|
|
|
* @param {string} url
|
|
|
|
* @param {object} body
|
|
|
|
* @returns {Promise<any>}
|
|
|
|
*/
|
|
|
|
export async function nextCloudApiRequest(this: IHookFunctions | IExecuteFunctions, method: string, endpoint: string, body: object | string | Buffer, headers?: object, encoding?: null | undefined, query?: object): Promise<any> { // tslint:disable-line:no-any
|
2021-01-22 11:58:06 -08:00
|
|
|
const resource = this.getNodeParameter('resource', 0);
|
|
|
|
const operation = this.getNodeParameter('operation', 0);
|
2021-01-23 03:57:30 -08:00
|
|
|
|
|
|
|
const options: OptionsWithUri = {
|
2020-06-17 10:38:30 -07:00
|
|
|
headers,
|
|
|
|
method,
|
|
|
|
body,
|
2021-04-23 15:08:08 -07:00
|
|
|
qs: query ?? {},
|
2020-06-17 10:38:30 -07:00
|
|
|
uri: '',
|
|
|
|
json: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (encoding === null) {
|
|
|
|
options.encoding = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const authenticationMethod = this.getNodeParameter('authentication', 0);
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (authenticationMethod === 'accessToken') {
|
2021-08-20 09:57:30 -07:00
|
|
|
const credentials = await this.getCredentials('nextCloudApi');
|
2020-06-17 10:38:30 -07:00
|
|
|
if (credentials === undefined) {
|
2021-04-16 09:33:36 -07:00
|
|
|
throw new NodeOperationError(this.getNode(), 'No credentials got returned!');
|
2020-06-17 10:38:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
options.auth = {
|
|
|
|
user: credentials.user as string,
|
|
|
|
pass: credentials.password as string,
|
|
|
|
};
|
|
|
|
|
|
|
|
options.uri = `${credentials.webDavUrl}/${encodeURI(endpoint)}`;
|
2021-01-18 13:52:24 -08:00
|
|
|
|
2021-10-14 05:47:29 -07:00
|
|
|
if (resource === 'user' || operation === 'share') {
|
2021-01-18 13:52:24 -08:00
|
|
|
options.uri = options.uri.replace('/remote.php/webdav', '');
|
|
|
|
}
|
2020-06-17 10:38:30 -07:00
|
|
|
return await this.helpers.request(options);
|
|
|
|
} else {
|
2021-08-20 09:57:30 -07:00
|
|
|
const credentials = await this.getCredentials('nextCloudOAuth2Api');
|
2020-06-17 10:38:30 -07:00
|
|
|
if (credentials === undefined) {
|
2021-04-16 09:33:36 -07:00
|
|
|
throw new NodeOperationError(this.getNode(), 'No credentials got returned!');
|
2020-06-17 10:38:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
options.uri = `${credentials.webDavUrl}/${encodeURI(endpoint)}`;
|
|
|
|
|
2021-01-22 11:58:06 -08:00
|
|
|
if (resource === 'user' && operation === 'create') {
|
|
|
|
options.uri = options.uri.replace('/remote.php/webdav', '');
|
|
|
|
}
|
|
|
|
|
2020-06-17 10:38:30 -07:00
|
|
|
return await this.helpers.requestOAuth2!.call(this, 'nextCloudOAuth2Api', options);
|
|
|
|
}
|
|
|
|
} catch (error) {
|
2021-04-16 09:33:36 -07:00
|
|
|
throw new NodeApiError(this.getNode(), error);
|
2020-06-17 10:38:30 -07:00
|
|
|
}
|
|
|
|
}
|