diff --git a/package.json b/package.json index d57d81c3..564ea2ec 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "dropbox", - "version": "10.14.0", + "version": "10.15.0", "registry": "npm", "description": "The Dropbox JavaScript SDK is a lightweight, promise based interface to the Dropbox v2 API that works in both nodejs and browser environments.", "main": "cjs/index.js", diff --git a/src/dropbox.js b/src/dropbox.js index 925fc75a..089a669a 100644 --- a/src/dropbox.js +++ b/src/dropbox.js @@ -86,6 +86,8 @@ export default class Dropbox { auth = TEAM_AUTH; } else if (authTypes.includes(APP_AUTH)) { auth = APP_AUTH; + } else { + auth = USER_AUTH; // Default to user auth } } @@ -114,28 +116,9 @@ export default class Dropbox { fetchOptions.headers['Content-Type'] = 'application/json'; } - let authHeader; - switch (auth) { - case APP_AUTH: - if (!this.auth.clientId || !this.auth.clientSecret) { - throw new Error('A client id and secret is required for this function'); - } - authHeader = b64(`${this.auth.clientId}:${this.auth.clientSecret}`); - fetchOptions.headers.Authorization = `Basic ${authHeader}`; - break; - case TEAM_AUTH: - case USER_AUTH: - fetchOptions.headers.Authorization = `Bearer ${this.auth.getAccessToken()}`; - break; - case NO_AUTH: - break; - case COOKIE: - break; - default: - throw new Error(`Unhandled auth type: ${auth}`); - } - + this.setAuthHeaders(auth, fetchOptions); this.setCommonHeaders(fetchOptions); + return fetchOptions; }) .then((fetchOptions) => this.fetch( @@ -148,18 +131,14 @@ export default class Dropbox { downloadRequest(path, args, auth, host) { return this.auth.checkAndRefreshAccessToken() .then(() => { - if (auth !== USER_AUTH) { - throw new Error(`Unexpected auth type: ${auth}`); - } - const fetchOptions = { method: 'POST', headers: { - Authorization: `Bearer ${this.auth.getAccessToken()}`, 'Dropbox-API-Arg': httpHeaderSafeJson(args), }, }; + this.setAuthHeaders(auth, fetchOptions); this.setCommonHeaders(fetchOptions); return fetchOptions; @@ -174,10 +153,6 @@ export default class Dropbox { uploadRequest(path, args, auth, host) { return this.auth.checkAndRefreshAccessToken() .then(() => { - if (auth !== USER_AUTH) { - throw new Error(`Unexpected auth type: ${auth}`); - } - const { contents } = args; delete args.contents; @@ -185,12 +160,12 @@ export default class Dropbox { body: contents, method: 'POST', headers: { - Authorization: `Bearer ${this.auth.getAccessToken()}`, 'Content-Type': 'application/octet-stream', 'Dropbox-API-Arg': httpHeaderSafeJson(args), }, }; + this.setAuthHeaders(auth, fetchOptions); this.setCommonHeaders(fetchOptions); return fetchOptions; @@ -202,6 +177,28 @@ export default class Dropbox { .then((res) => parseResponse(res)); } + setAuthHeaders(auth, fetchOptions) { + switch (auth) { + case APP_AUTH: + if (this.auth.clientId && this.auth.clientSecret) { + const authHeader = b64(`${this.auth.clientId}:${this.auth.clientSecret}`); + fetchOptions.headers.Authorization = `Basic ${authHeader}`; + } + break; + case TEAM_AUTH: + case USER_AUTH: + if (this.auth.getAccessToken()) { + fetchOptions.headers.Authorization = `Bearer ${this.auth.getAccessToken()}`; + } + break; + case NO_AUTH: + case COOKIE: + break; + default: + throw new Error(`Unhandled auth type: ${auth}`); + } + } + setCommonHeaders(options) { if (this.selectUser) { options.headers['Dropbox-API-Select-User'] = this.selectUser; diff --git a/test/unit/dropbox.js b/test/unit/dropbox.js index cad8a22f..8b7f192c 100644 --- a/test/unit/dropbox.js +++ b/test/unit/dropbox.js @@ -134,23 +134,6 @@ describe('Dropbox', () => { chai.assert.equal('path', dbx.uploadRequest.getCall(0).args[0]); chai.assert.deepEqual({}, dbx.uploadRequest.getCall(0).args[1]); }); - - it('throws an error for team auth', () => { - const dbx = new Dropbox(); - return chai.assert.isRejected(dbx.uploadRequest('path', {}, TEAM_AUTH, 'api'), Error, `Unexpected auth type: ${TEAM_AUTH}`); - }); - it('throws an error for app auth', () => { - const dbx = new Dropbox(); - return chai.assert.isRejected(dbx.uploadRequest('path', {}, APP_AUTH, 'api'), Error, `Unexpected auth type: ${APP_AUTH}`); - }); - it('throws an error for no-auth', () => { - const dbx = new Dropbox(); - return chai.assert.isRejected(dbx.uploadRequest('path', {}, NO_AUTH, 'api'), Error, `Unexpected auth type: ${NO_AUTH}`); - }); - it('throws an error for cookie auth', () => { - const dbx = new Dropbox(); - return chai.assert.isRejected(dbx.uploadRequest('path', {}, COOKIE, 'api'), Error, `Unexpected auth type: ${COOKIE}`); - }); }); describe('Download Requests', () => { @@ -165,26 +148,6 @@ describe('Dropbox', () => { chai.assert.equal('path', dbx.downloadRequest.getCall(0).args[0]); chai.assert.deepEqual({}, dbx.downloadRequest.getCall(0).args[1]); }); - - it('throws an error for team auth', () => { - const dbx = new Dropbox(); - return chai.assert.isRejected(dbx.downloadRequest('path', {}, TEAM_AUTH, 'api'), Error, `Unexpected auth type: ${TEAM_AUTH}`); - }); - - it('throws an error for app auth', () => { - const dbx = new Dropbox(); - return chai.assert.isRejected(dbx.downloadRequest('path', {}, APP_AUTH, 'api'), Error, `Unexpected auth type: ${APP_AUTH}`); - }); - - it('throws an error for no-auth', () => { - const dbx = new Dropbox(); - return chai.assert.isRejected(dbx.downloadRequest('path', {}, NO_AUTH, 'api'), Error, `Unexpected auth type: ${NO_AUTH}`); - }); - - it('throws an error for cookie auth', () => { - const dbx = new Dropbox(); - return chai.assert.isRejected(dbx.downloadRequest('path', {}, COOKIE, 'api'), Error, `Unexpected auth type: ${COOKIE}`); - }); }); describe('pathRoot', () => {