fix(NextCloud Node): Fix folder list with Nextcloud v24 (#3386)

* initial fix for v24 folder listing

* implemented new credential methods

* Nodelinter fixes
This commit is contained in:
Jonathan Bennetts
2022-05-27 16:15:12 +01:00
committed by GitHub
parent ed69c3cc18
commit 5f3bed3d4e
5 changed files with 59 additions and 42 deletions

View File

@@ -2,7 +2,11 @@ import {
IExecuteFunctions,
IHookFunctions,
} from 'n8n-core';
import { NodeApiError, NodeOperationError, } from 'n8n-workflow';
import {
JsonObject,
NodeApiError,
} from 'n8n-workflow';
import {
OptionsWithUri,
@@ -20,8 +24,17 @@ import {
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
const resource = this.getNodeParameter('resource', 0);
const operation = this.getNodeParameter('operation', 0);
const authenticationMethod = this.getNodeParameter('authentication', 0);
const options: OptionsWithUri = {
let credentials;
if (authenticationMethod === 'accessToken') {
credentials = await this.getCredentials('nextCloudApi') as { webDavUrl: string };
} else {
credentials = await this.getCredentials('nextCloudOAuth2Api') as { webDavUrl: string };
}
let options: OptionsWithUri = {
headers,
method,
body,
@@ -34,35 +47,16 @@ export async function nextCloudApiRequest(this: IHookFunctions | IExecuteFunctio
options.encoding = null;
}
const authenticationMethod = this.getNodeParameter('authentication', 0);
options.uri = `${credentials.webDavUrl}/${encodeURI(endpoint)}`;
if (resource === 'user' && operation === 'create') {
options.uri = options.uri.replace('/remote.php/webdav', '');
}
const credentialType = authenticationMethod === 'accessToken' ? 'nextCloudApi' : 'nextCloudOAuth2Api';
try {
if (authenticationMethod === 'accessToken') {
const credentials = await this.getCredentials('nextCloudApi');
options.auth = {
user: credentials.user as string,
pass: credentials.password as string,
};
options.uri = `${credentials.webDavUrl}/${encodeURI(endpoint)}`;
if (resource === 'user' || operation === 'share') {
options.uri = options.uri.replace('/remote.php/webdav', '');
}
return await this.helpers.request(options);
} else {
const credentials = await this.getCredentials('nextCloudOAuth2Api');
options.uri = `${credentials.webDavUrl}/${encodeURI(endpoint)}`;
if (resource === 'user' && operation === 'create') {
options.uri = options.uri.replace('/remote.php/webdav', '');
}
return await this.helpers.requestOAuth2!.call(this, 'nextCloudOAuth2Api', options);
}
} catch (error) {
throw new NodeApiError(this.getNode(), error);
return await this.helpers.requestWithAuthentication.call(this, credentialType, options);
} catch(error) {
throw new NodeApiError(this.getNode(), error as JsonObject);
}
}