mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-17 10:02:05 +00:00
NextCloud OAuth2 support
This commit is contained in:
64
packages/nodes-base/nodes/NextCloud/GenericFunctions.ts
Normal file
64
packages/nodes-base/nodes/NextCloud/GenericFunctions.ts
Normal file
@@ -0,0 +1,64 @@
|
||||
import {
|
||||
IExecuteFunctions,
|
||||
IHookFunctions,
|
||||
} from 'n8n-core';
|
||||
|
||||
import {
|
||||
IDataObject,
|
||||
} from 'n8n-workflow';
|
||||
import { OptionsWithUri } from 'request';
|
||||
|
||||
/**
|
||||
* 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
|
||||
const options : OptionsWithUri = {
|
||||
headers,
|
||||
method,
|
||||
body,
|
||||
qs: {},
|
||||
uri: '',
|
||||
json: false,
|
||||
};
|
||||
|
||||
if (encoding === null) {
|
||||
options.encoding = null;
|
||||
}
|
||||
|
||||
const authenticationMethod = this.getNodeParameter('authentication', 0);
|
||||
|
||||
try {
|
||||
if (authenticationMethod === 'accessToken') {
|
||||
const credentials = this.getCredentials('nextCloudApi');
|
||||
if (credentials === undefined) {
|
||||
throw new Error('No credentials got returned!');
|
||||
}
|
||||
|
||||
options.auth = {
|
||||
user: credentials.user as string,
|
||||
pass: credentials.password as string,
|
||||
};
|
||||
|
||||
options.uri = `${credentials.webDavUrl}/${encodeURI(endpoint)}`;
|
||||
|
||||
return await this.helpers.request(options);
|
||||
} else {
|
||||
const credentials = this.getCredentials('nextCloudOAuth2Api');
|
||||
if (credentials === undefined) {
|
||||
throw new Error('No credentials got returned!');
|
||||
}
|
||||
|
||||
options.uri = `${credentials.webDavUrl}/${encodeURI(endpoint)}`;
|
||||
|
||||
return await this.helpers.requestOAuth2!.call(this, 'nextCloudOAuth2Api', options);
|
||||
}
|
||||
} catch (error) {
|
||||
throw new Error(`NextCloud Error. Status Code: ${error.statusCode}. Message: ${error.message}`);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user