Skip to content

Commit b82cf54

Browse files
Bohdan-Teretapran1990
authored andcommitted
Added pathRoot option, added typings for fetch option (#230)
* feat(options): added pathRoot and fetch options to Dropbox * feat(options): fixes and tests for pathRoot and fetch options * feat(frontend): reverted changes for package.json * feat(code-styles): fixed code-styles * feat(code-styles): fixed code-styles (2) * feat(fetch-option): changed option type to Function * feat(options): removed changes in generated code * feat(options): reverted autoFormatting * feat(options): reset dropbox_types.d.ts
1 parent 004feba commit b82cf54

File tree

10 files changed

+77
-21
lines changed

10 files changed

+77
-21
lines changed

generator/typescript/dropbox_types.d.tstemplate

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ declare module DropboxTypes {
66
clientId?: string;
77
// Select user is only used by team endpoints. It specifies which user the team access token should be acting as.
88
selectUser?: string;
9+
// Root path used to access namespaces different from home namespace (team folders etc)
10+
pathRoot?: string;
11+
// Fetch library for making requests.
12+
fetch?: Function
913
}
1014

1115
class DropboxBase {

src/download-request.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ export function downloadRequest(fetch) {
5050
if (options.selectAdmin) {
5151
fetchOptions.headers['Dropbox-API-Select-Admin'] = options.selectAdmin;
5252
}
53+
if (options.pathRoot) {
54+
fetchOptions.headers['Dropbox-API-Path-Root'] = options.pathRoot;
55+
}
5356
}
5457

5558

src/dropbox-base.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ if (!Array.prototype.includes) {
106106
* to act as.
107107
* @arg {String} [options.selectAdmin] - Team admin that the team access token would like
108108
* to act as.
109+
* @arg {String} [options.pathRoot] - root pass to access other namespaces
110+
* Use to access team folders for example
109111
*/
110112

111113
function parseBodyToType(res) {
@@ -126,6 +128,7 @@ export class DropboxBase {
126128
this.selectUser = options.selectUser;
127129
this.selectAdmin = options.selectAdmin;
128130
this.fetch = options.fetch || fetch;
131+
this.pathRoot = options.pathRoot;
129132
if (!options.fetch) { console.warn('Global fetch is deprecated and will be unsupported in a future version. Please pass fetch function as option when instantiating dropbox instance: new Dropbox({fetch})'); } // eslint-disable-line no-console
130133
}
131134

@@ -274,11 +277,11 @@ client_id=${clientId}&client_secret=${clientSecret}`;
274277
*/
275278

276279

277-
/**
278-
* An authentication process that works with cordova applications.
279-
* @param {successCallback} successCallback
280-
* @param {errorCallback} errorCallback
281-
*/
280+
/**
281+
* An authentication process that works with cordova applications.
282+
* @param {successCallback} successCallback
283+
* @param {errorCallback} errorCallback
284+
*/
282285
authenticateWithCordova(successCallback, errorCallback) {
283286
const redirectUrl = 'https://www.dropbox.com/1/oauth2/redirect_receiver';
284287
const url = this.getAuthenticationUrl(redirectUrl);
@@ -352,6 +355,7 @@ client_id=${clientId}&client_secret=${clientSecret}`;
352355
selectAdmin: this.selectAdmin,
353356
clientId: this.getClientId(),
354357
clientSecret: this.getClientSecret(),
358+
pathRoot: this.pathRoot,
355359
};
356360
return request(path, args, auth, host, this.getAccessToken(), options);
357361
}

src/dropbox.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import { DropboxBase } from './dropbox-base';
1414
* authentication URL.
1515
* @arg {String} [options.selectUser] - Select user is only used by DropboxTeam.
1616
* It specifies which user the team access token should be acting as.
17+
* @arg {String} [options.pathRoot] - root pass to access other namespaces
18+
* Use to access team folders for example
1719
*/
1820
export class Dropbox extends DropboxBase {
1921

src/rpc-request.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ export function rpcRequest(fetch) {
4545
if (options.selectAdmin) {
4646
headers['Dropbox-API-Select-Admin'] = options.selectAdmin;
4747
}
48+
if (options.pathRoot) {
49+
headers['Dropbox-API-Path-Root'] = options.pathRoot;
50+
}
4851
}
4952

5053
fetchOptions.headers = headers;

src/upload-request.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ export function uploadRequest(fetch) {
3535
if (options.selectAdmin) {
3636
fetchOptions.headers['Dropbox-API-Select-Admin'] = options.selectAdmin;
3737
}
38+
if (options.pathRoot) {
39+
fetchOptions.headers['Dropbox-API-Path-Root'] = options.pathRoot;
40+
}
3841
}
3942

4043
return fetch(getBaseURL(host) + path, fetchOptions)

test/download-request.js

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ describe('downloadRequest', function () {
1414
fetchMock = FetchMock.sandbox().mock('*', new Response(
1515
createReadStream(resolve(__dirname, './file.txt')),
1616
{
17-
status : 200 ,
18-
statusText : "OK",
17+
status: 200,
18+
statusText: "OK",
1919
headers: {
2020
'Content-Type': 'application/octet-stream',
2121
'dropbox-api-result': '{"test":"json"}'
@@ -73,20 +73,20 @@ describe('downloadRequest', function () {
7373

7474
it('sets the Dropbox-API-Arg header', function (done) {
7575
downloadRequest(fetchMock)('sharing/create_shared_link', { foo: 'bar' }, 'user', 'content', 'atoken')
76-
.then((data) => {
77-
assert.equal(fetchMock.lastOptions().headers['Authorization'], 'Bearer atoken');
78-
assert.equal(fetchMock.lastOptions().headers['Dropbox-API-Arg'], JSON.stringify({ foo: 'bar' }));
79-
done();
80-
}, done);
76+
.then((data) => {
77+
assert.equal(fetchMock.lastOptions().headers['Authorization'], 'Bearer atoken');
78+
assert.equal(fetchMock.lastOptions().headers['Dropbox-API-Arg'], JSON.stringify({ foo: 'bar' }));
79+
done();
80+
}, done);
8181
});
8282

8383
it('escapes special characters in the Dropbox-API-Arg header', function (done) {
8484
downloadRequest(fetchMock)('sharing/create_shared_link', { foo: 'bar单bazá' }, 'user', 'content', 'atoken')
85-
.then((data) => {
86-
assert.equal(fetchMock.lastOptions().headers['Authorization'], 'Bearer atoken');
87-
assert.equal(fetchMock.lastOptions().headers['Dropbox-API-Arg'], '{"foo":"bar\\u5355baz\\u00e1"}');
88-
done();
89-
}, done);
85+
.then((data) => {
86+
assert.equal(fetchMock.lastOptions().headers['Authorization'], 'Bearer atoken');
87+
assert.equal(fetchMock.lastOptions().headers['Dropbox-API-Arg'], '{"foo":"bar\\u5355baz\\u00e1"}');
88+
done();
89+
}, done);
9090
});
9191

9292
it('returns a valid response', function (done) {
@@ -96,4 +96,13 @@ describe('downloadRequest', function () {
9696
done();
9797
}, done);
9898
});
99+
100+
it('sets Dropbox-Api-Path-Root header if pathRoot set', function (done) {
101+
downloadRequest(fetchMock)('sharing/create_shared_link', { foo: 'bar' }, 'user', 'content', 'atoken', {pathRoot: 'selectedPathRoot'})
102+
.then((data) => {
103+
assert.equal(fetchMock.lastOptions().headers['Dropbox-API-Path-Root'], 'selectedPathRoot');
104+
done();
105+
}, done);
106+
});
107+
99108
});

test/dropbox-base.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,4 +301,16 @@ describe('DropboxBase', function () {
301301
);
302302
});
303303
});
304+
305+
describe('.pathRoot', function () {
306+
it('can be set in the constructor', function () {
307+
dbx = new DropboxBase({ pathRoot: 'foo' });
308+
assert.equal(dbx.pathRoot, 'foo');
309+
});
310+
311+
it('is undefined if not set in constructor', function () {
312+
dbx = new DropboxBase();
313+
assert.equal(dbx.pathRoot, undefined);
314+
});
315+
});
304316
});

test/rpc-request.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ describe('rpcRequest', function () {
1212
fetchMock = FetchMock.sandbox().mock('*', new Response(
1313
'{"test": "test"}',
1414
{
15-
status : 200 ,
16-
statusText : "OK",
15+
status: 200,
16+
statusText: "OK",
1717
headers: {
1818
'Content-Type': 'application/json',
1919
'dropbox-api-result': '{"test":"json"}'
@@ -90,4 +90,12 @@ describe('rpcRequest', function () {
9090
});
9191
});
9292

93+
it('sets Dropbox-Api-Path-Root header if pathRoot set', function (done) {
94+
rpcRequest(fetchMock)('files/list', { foo: 'bar' }, 'user', 'api', 'atoken', { pathRoot: 'selectedPathRoot' })
95+
.then((data) => {
96+
assert.equal(fetchMock.lastOptions().headers['Dropbox-API-Path-Root'], 'selectedPathRoot');
97+
done();
98+
}, done);
99+
});
100+
93101
});

test/upload-request.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ describe('uploadRequest', function () {
1010
fetchMock = FetchMock.sandbox().post('*', new Response(
1111
'{"foo": "bar"}',
1212
{
13-
status : 200 ,
14-
statusText : "OK",
13+
status: 200,
14+
statusText: "OK",
1515
headers: {
1616
'Content-Type': 'application/json',
1717
'dropbox-api-result': '{"test":"json"}'
@@ -101,4 +101,12 @@ describe('uploadRequest', function () {
101101
}, done);
102102
});
103103

104+
it('sets Dropbox-Api-Path-Root header if pathRoot set', function (done) {
105+
uploadRequest(fetchMock)('files/upload', { foo: 'bar' }, 'user', 'content', 'atoken', {pathRoot: 'selectedPathRoot'})
106+
.then((data) => {
107+
assert.equal(fetchMock.lastOptions().headers['Dropbox-API-Path-Root'], 'selectedPathRoot');
108+
done();
109+
}, done);
110+
});
111+
104112
});

0 commit comments

Comments
 (0)