n8n/packages/nodes-base/nodes/NextCloud/GenericFunctions.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

75 lines
1.9 KiB
TypeScript
Raw Normal View History

import {
NodeOperationError,
type IExecuteFunctions,
type IHookFunctions,
type IHttpRequestMethods,
type IRequestOptions,
type IDataObject,
} from 'n8n-workflow';
2020-06-17 10:38:30 -07:00
/**
* Make an API request to NextCloud
*
*/
export async function nextCloudApiRequest(
this: IHookFunctions | IExecuteFunctions,
method: IHttpRequestMethods,
endpoint: string,
body: object | string | Buffer,
headers?: IDataObject,
encoding?: null | undefined,
query?: IDataObject,
) {
2021-01-22 11:58:06 -08:00
const resource = this.getNodeParameter('resource', 0);
const operation = this.getNodeParameter('operation', 0);
const authenticationMethod = this.getNodeParameter('authentication', 0);
let credentials;
if (authenticationMethod === 'accessToken') {
credentials = (await this.getCredentials('nextCloudApi')) as { webDavUrl: string };
} else {
credentials = (await this.getCredentials('nextCloudOAuth2Api')) as { webDavUrl: string };
}
const options: IRequestOptions = {
2020-06-17 10:38:30 -07:00
headers,
method,
body,
qs: query ?? {},
2020-06-17 10:38:30 -07:00
uri: '',
json: false,
};
if (encoding === null) {
options.encoding = null;
}
options.uri = `${credentials.webDavUrl}/${encodeURI(endpoint)}`;
if (resource === 'user' && operation === 'create') {
options.uri = options.uri.replace('/remote.php/webdav', '');
}
2020-06-17 10:38:30 -07:00
if (resource === 'file' && operation === 'share') {
options.uri = options.uri.replace('/remote.php/webdav', '');
}
const credentialType =
authenticationMethod === 'accessToken' ? 'nextCloudApi' : 'nextCloudOAuth2Api';
const response = await this.helpers.requestWithAuthentication.call(this, credentialType, options);
if (typeof response === 'string' && response.includes('<b>Fatal error</b>')) {
throw new NodeOperationError(
this.getNode(),
"NextCloud responded with a 'Fatal error', check description for more details",
{
description: `Server response:\n${response}`,
},
);
}
return response;
2020-06-17 10:38:30 -07:00
}