Small improvements

This commit is contained in:
ricardo 2020-07-23 21:08:34 -04:00
parent 0e0f183675
commit 7e0725f381
3 changed files with 43 additions and 24 deletions

View file

@ -3,7 +3,6 @@ import {
NodePropertyTypes,
} from 'n8n-workflow';
export class DropboxApi implements ICredentialType {
name = 'dropboxApi';
displayName = 'Dropbox API';

View file

@ -2,6 +2,7 @@ import {
BINARY_ENCODING,
IExecuteFunctions,
} from 'n8n-core';
import {
IDataObject,
INodeTypeDescription,
@ -9,9 +10,9 @@ import {
INodeType,
} from 'n8n-workflow';
import { OptionsWithUri } from 'request';
import { dropboxApiRequest } from './GenericFunctions';
import {
dropboxApiRequest
} from './GenericFunctions';
export class Dropbox implements INodeType {
description: INodeTypeDescription = {
@ -36,9 +37,9 @@ export class Dropbox implements INodeType {
show: {
authentication: [
'accessToken',
]
}
}
],
},
},
},
{
name: 'dropboxOAuth2Api',
@ -47,10 +48,10 @@ export class Dropbox implements INodeType {
show: {
authentication: [
'oAuth2',
]
}
}
}
],
},
},
},
],
properties: [
{
@ -60,15 +61,15 @@ export class Dropbox implements INodeType {
options: [
{
name: 'Access Token',
value: 'accessToken'
value: 'accessToken',
},
{
name: 'OAuth2',
value: 'oAuth2'
value: 'oAuth2',
}
],
default: 'accessToken',
description: 'Means of authenticating with the serivce.'
description: 'Means of authenticating with the service.',
},
{
displayName: 'Resource',
@ -484,6 +485,7 @@ export class Dropbox implements INodeType {
let endpoint = '';
let requestMethod = '';
let body: IDataObject | Buffer;
let options;
const headers: IDataObject = {};
@ -518,6 +520,9 @@ export class Dropbox implements INodeType {
endpoint = 'https://content.dropboxapi.com/2/files/upload';
if (this.getNodeParameter('binaryData', i) === true) {
options = { json: false };
// Is binary file to upload
const item = items[i];
@ -612,13 +617,16 @@ export class Dropbox implements INodeType {
throw new Error(`The resource "${resource}" is not known!`);
}
let encoding: string | null = '';
if (resource === 'file' && operation === 'download') {
// Return the data as a buffer
encoding = null;
options = { encoding: null };
}
const responseData = await dropboxApiRequest.call(this, requestMethod, endpoint, body, headers, encoding);
let responseData = await dropboxApiRequest.call(this, requestMethod, endpoint, body, headers, options);
if (resource === 'file' && operation === 'upload') {
responseData = JSON.parse(responseData);
}
if (resource === 'file' && operation === 'download') {

View file

@ -1,5 +1,15 @@
import { IExecuteFunctions, IHookFunctions } from 'n8n-core';
import { OptionsWithUri } from 'request';
import {
IExecuteFunctions,
IHookFunctions,
} from 'n8n-core';
import {
OptionsWithUri,
} from 'request';
import {
IDataObject,
} from 'n8n-workflow';
/**
* Make an API request to Dropbox
@ -10,7 +20,7 @@ import { OptionsWithUri } from 'request';
* @param {object} body
* @returns {Promise<any>}
*/
export async function dropboxApiRequest(this: IHookFunctions | IExecuteFunctions, method: string, endpoint: string, body: object, headers?: object, encoding?: string | null): Promise<any> {// tslint:disable-line:no-any
export async function dropboxApiRequest(this: IHookFunctions | IExecuteFunctions, method: string, endpoint: string, body: object, headers?: object, option: IDataObject = {}): Promise<any> {// tslint:disable-line:no-any
const options: OptionsWithUri = {
headers,
@ -18,21 +28,23 @@ export async function dropboxApiRequest(this: IHookFunctions | IExecuteFunctions
body,
uri: endpoint,
json: true,
encoding
};
if (!Object.keys(body).length) {
delete options.body;
}
if (encoding !== null) {
delete options.encoding;
}
Object.assign(options, option);
const authenticationMethod = this.getNodeParameter('authentication', 0) as string;
try {
if (authenticationMethod === 'accessToken') {
const credentials = this.getCredentials('dropboxApi') as IDataObject;
options.headers!['Authorization'] = `Bearer ${credentials.accessToken}`;
return await this.helpers.request(options);
} else {
return await this.helpers.requestOAuth2.call(this, 'dropboxOAuth2Api', options);