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, NodePropertyTypes,
} from 'n8n-workflow'; } from 'n8n-workflow';
export class DropboxApi implements ICredentialType { export class DropboxApi implements ICredentialType {
name = 'dropboxApi'; name = 'dropboxApi';
displayName = 'Dropbox API'; displayName = 'Dropbox API';

View File

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

View File

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