From 7e0725f381383f61863695e3035ddcd4dde1ae91 Mon Sep 17 00:00:00 2001 From: ricardo Date: Thu, 23 Jul 2020 21:08:34 -0400 Subject: [PATCH] :zap: Small improvements --- .../credentials/DropboxApi.credentials.ts | 1 - .../nodes-base/nodes/Dropbox/Dropbox.node.ts | 40 +++++++++++-------- .../nodes/Dropbox/GenericFunctions.ts | 26 ++++++++---- 3 files changed, 43 insertions(+), 24 deletions(-) diff --git a/packages/nodes-base/credentials/DropboxApi.credentials.ts b/packages/nodes-base/credentials/DropboxApi.credentials.ts index ba3b81e02c..a95df920b1 100644 --- a/packages/nodes-base/credentials/DropboxApi.credentials.ts +++ b/packages/nodes-base/credentials/DropboxApi.credentials.ts @@ -3,7 +3,6 @@ import { NodePropertyTypes, } from 'n8n-workflow'; - export class DropboxApi implements ICredentialType { name = 'dropboxApi'; displayName = 'Dropbox API'; diff --git a/packages/nodes-base/nodes/Dropbox/Dropbox.node.ts b/packages/nodes-base/nodes/Dropbox/Dropbox.node.ts index 88278a3191..6a1b5b13ff 100644 --- a/packages/nodes-base/nodes/Dropbox/Dropbox.node.ts +++ b/packages/nodes-base/nodes/Dropbox/Dropbox.node.ts @@ -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') { diff --git a/packages/nodes-base/nodes/Dropbox/GenericFunctions.ts b/packages/nodes-base/nodes/Dropbox/GenericFunctions.ts index b4744f5c72..c66a615bb8 100644 --- a/packages/nodes-base/nodes/Dropbox/GenericFunctions.ts +++ b/packages/nodes-base/nodes/Dropbox/GenericFunctions.ts @@ -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} */ -export async function dropboxApiRequest(this: IHookFunctions | IExecuteFunctions, method: string, endpoint: string, body: object, headers?: object, encoding?: string | null): Promise {// tslint:disable-line:no-any +export async function dropboxApiRequest(this: IHookFunctions | IExecuteFunctions, method: string, endpoint: string, body: object, headers?: object, option: IDataObject = {}): Promise {// 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);