2022-08-17 08:50:24 -07:00
|
|
|
import { IExecuteFunctions, IHookFunctions } from 'n8n-core';
|
2022-05-27 08:15:12 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { JsonObject, NodeApiError } from 'n8n-workflow';
|
2020-06-17 10:38:30 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { 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>}
|
|
|
|
*/
|
2022-08-17 08:50:24 -07:00
|
|
|
export async function nextCloudApiRequest(
|
|
|
|
this: IHookFunctions | IExecuteFunctions,
|
|
|
|
method: string,
|
|
|
|
endpoint: string,
|
|
|
|
body: object | string | Buffer,
|
|
|
|
headers?: object,
|
|
|
|
encoding?: null | undefined,
|
|
|
|
query?: object,
|
2022-08-19 06:35:01 -07:00
|
|
|
) {
|
2021-01-22 11:58:06 -08:00
|
|
|
const resource = this.getNodeParameter('resource', 0);
|
|
|
|
const operation = this.getNodeParameter('operation', 0);
|
2022-05-27 08:15:12 -07:00
|
|
|
const authenticationMethod = this.getNodeParameter('authentication', 0);
|
|
|
|
|
|
|
|
let credentials;
|
2021-01-23 03:57:30 -08:00
|
|
|
|
2022-05-27 08:15:12 -07:00
|
|
|
if (authenticationMethod === 'accessToken') {
|
2022-08-17 08:50:24 -07:00
|
|
|
credentials = (await this.getCredentials('nextCloudApi')) as { webDavUrl: string };
|
2022-05-27 08:15:12 -07:00
|
|
|
} else {
|
2022-08-17 08:50:24 -07:00
|
|
|
credentials = (await this.getCredentials('nextCloudOAuth2Api')) as { webDavUrl: string };
|
2022-05-27 08:15:12 -07:00
|
|
|
}
|
|
|
|
|
2022-05-27 09:15:05 -07: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;
|
|
|
|
}
|
|
|
|
|
2022-05-27 08:15:12 -07:00
|
|
|
options.uri = `${credentials.webDavUrl}/${encodeURI(endpoint)}`;
|
2022-08-19 06:35:01 -07:00
|
|
|
|
2022-05-27 08:15:12 -07:00
|
|
|
if (resource === 'user' && operation === 'create') {
|
|
|
|
options.uri = options.uri.replace('/remote.php/webdav', '');
|
|
|
|
}
|
2020-06-17 10:38:30 -07:00
|
|
|
|
2022-08-19 06:35:01 -07:00
|
|
|
if (resource === 'file' && operation === 'share') {
|
|
|
|
options.uri = options.uri.replace('/remote.php/webdav', '');
|
|
|
|
}
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
const credentialType =
|
|
|
|
authenticationMethod === 'accessToken' ? 'nextCloudApi' : 'nextCloudOAuth2Api';
|
2021-01-22 11:58:06 -08:00
|
|
|
|
2022-05-27 08:15:12 -07:00
|
|
|
try {
|
|
|
|
return await this.helpers.requestWithAuthentication.call(this, credentialType, options);
|
2022-08-17 08:50:24 -07:00
|
|
|
} catch (error) {
|
2022-05-27 08:15:12 -07:00
|
|
|
throw new NodeApiError(this.getNode(), error as JsonObject);
|
2020-06-17 10:38:30 -07:00
|
|
|
}
|
|
|
|
}
|