mirror of
https://github.com/n8n-io/n8n.git
synced 2024-11-10 22:54:05 -08:00
d3fecb9f6d
* Centralized error throwing for encryption key * Unifying the error message used by cli and core packages * Improvements to error messages to make it more DRY * Removed unnecessary throw * Throwing error when credential does not exist to simplify node behavior (#3112) Co-authored-by: Iván Ovejero <ivov.src@gmail.com>
69 lines
1.8 KiB
TypeScript
69 lines
1.8 KiB
TypeScript
import {
|
|
IExecuteFunctions,
|
|
IHookFunctions,
|
|
} from 'n8n-core';
|
|
import { NodeApiError, NodeOperationError, } 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 resource = this.getNodeParameter('resource', 0);
|
|
const operation = this.getNodeParameter('operation', 0);
|
|
|
|
const options: OptionsWithUri = {
|
|
headers,
|
|
method,
|
|
body,
|
|
qs: query ?? {},
|
|
uri: '',
|
|
json: false,
|
|
};
|
|
|
|
if (encoding === null) {
|
|
options.encoding = null;
|
|
}
|
|
|
|
const authenticationMethod = this.getNodeParameter('authentication', 0);
|
|
|
|
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);
|
|
}
|
|
}
|