Skip to content

Commit

Permalink
fix: Add fields query parameter to file and folder update (#776)
Browse files Browse the repository at this point in the history
  • Loading branch information
congminh1254 authored Nov 10, 2022
1 parent 7cdbbbd commit a327deb
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 21 deletions.
9 changes: 9 additions & 0 deletions src/managers/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,7 @@ class Files {
* @param {string} fileID - Box ID of the file being requested
* @param {Object} updates - File fields to update
* @param {string} [updates.etag] Only apply the updates if the file etag matches
* @param {string} [updates.fields] Comma-separated list of fields to return
* @param {Function} [callback] - Passed the updated file information if it was acquired successfully
* @returns {Promise<Object>} A promise resolving to the update file object
*/
Expand All @@ -446,6 +447,7 @@ class Files {
[key: string]: any;
etag?: string;
shared_link?: FileSharedLink;
fields?: string;
},
callback?: Function
) {
Expand All @@ -460,6 +462,13 @@ class Files {
delete updates.etag;
}

if (updates && updates.fields) {
params.qs = {
fields: updates.fields,
};
delete updates.fields;
}

var apiPath = urlPath(BASE_PATH, fileID);
return this.client.wrapWithDefaultHandler(this.client.put)(
apiPath,
Expand Down
9 changes: 9 additions & 0 deletions src/managers/folders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ class Folders {
* @param {string} folderID - The Box ID of the folder being requested
* @param {Object} updates - Folder fields to update
* @param {string} [updates.etag] Only update the folder if the ETag matches
* @param {string} [updates.fields] Comma-separated list of fields to return
* @param {Function} [callback] - Passed the updated folder information if it was acquired successfully
* @returns {Promise<Object>} A promise resolving to the updated folder object
*/
Expand All @@ -257,6 +258,7 @@ class Folders {
[key: string]: any;
etag?: string;
shared_link?: FolderSharedLink;
fields?: string;
},
callback?: Function
) {
Expand All @@ -271,6 +273,13 @@ class Folders {
delete updates.etag;
}

if (updates && updates.fields) {
params.qs = {
fields: updates.fields,
};
delete updates.fields;
}

var apiPath = urlPath(BASE_PATH, folderID);
return this.client.wrapWithDefaultHandler(this.client.put)(
apiPath,
Expand Down
43 changes: 43 additions & 0 deletions tests/endpoint-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1109,6 +1109,49 @@ describe('Endpoint', () => {
done();
});
});
it('should create a shared link with custom return params', done => {
const fileID = '1234567890';
const updates = {
shared_link: {
access: 'open',
password: 'do-not-use-this-password',
unshared_at: '2022-12-12T10:53:43-08:00',
permissions: {
can_edit: true
}
}
};
const fixture = getFixture('files/put_files_shared_link_custom_params_200');
apiMock
.put(`/2.0/files/${fileID}?fields=shared_link`, updates)
.matchHeader('Authorization', authHeader => {
assert.equal(authHeader, `Bearer ${TEST_ACCESS_TOKEN}`);
return true;
})
.matchHeader('User-Agent', uaHeader => {
assert.include(uaHeader, 'Box Node.js SDK v');
return true;
})
.reply(200, fixture);
basicClient.files.update(
fileID,
{
shared_link: {
access: 'open',
password: 'do-not-use-this-password',
unshared_at: '2022-12-12T10:53:43-08:00',
permissions: {
can_edit: true
}
},
fields: 'shared_link'
},
(err, data) => {
assert.isNull(err);
assert.deepEqual(data, JSON.parse(fixture));
done();
});
});
});
describe('addToCollection()', () => {
it('should make correct requests and correctly parse responses when API call is successful', done => {
Expand Down
78 changes: 57 additions & 21 deletions tests/fixtures/endpoints/files/put_files_shared_link_200.json
Original file line number Diff line number Diff line change
@@ -1,23 +1,59 @@
{
"type": "file",
"id": "1234567890",
"etag": "4",
"shared_link": {
"url": "https://someurl.box.com/s/2mgkgxe98et1li2w0lslbqv9wa724m3b",
"download_url": "https://someurl.box.com/shared/static/2mgkgxe98et1li2w0lslbqv9wa724m3b",
"vanity_url": null,
"vanity_name": null,
"effective_access": "open",
"effective_permission": "can_edit",
"is_password_enabled": true,
"unshared_at": "2022-12-12T10:53:43-08:00",
"download_count": 0,
"preview_count": 0,
"access": "open",
"permissions": {
"can_preview": true,
"can_download": true,
"can_edit": true
"type": "file",
"id": "1234567890",
"etag": "4",
"path_collection": {
"total_count": 1,
"entries": [
{
"type": "folder",
"id": "0",
"sequence_id": null,
"etag": null,
"name": "All Files"
}
]
},
"created_at": "2016-12-07T15:56:54-08:00",
"modified_at": "2016-12-07T15:56:54-08:00",
"trashed_at": null,
"purged_at": null,
"content_created_at": "2016-12-07T15:53:59-08:00",
"content_modified_at": "2016-12-07T15:53:59-08:00",
"created_by": {
"type": "user",
"id": "33333",
"name": "Test User",
"login": "[email protected]"
},
"modified_by": {
"type": "user",
"id": "33333",
"name": "Test User",
"login": "[email protected]"
},
"owned_by": {
"type": "user",
"id": "33333",
"name": "Test User",
"login": "[email protected]"
},
"shared_link": {
"url": "https://someurl.box.com/s/2mgkgxe98et1li2w0lslbqv9wa724m3b",
"download_url": "https://someurl.box.com/shared/static/2mgkgxe98et1li2w0lslbqv9wa724m3b",
"vanity_url": null,
"vanity_name": null,
"effective_access": "open",
"effective_permission": "can_edit",
"is_password_enabled": true,
"unshared_at": "2022-12-12T10:53:43-08:00",
"download_count": 0,
"preview_count": 0,
"access": "open",
"permissions": {
"can_preview": true,
"can_download": true,
"can_edit": true
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"type": "file",
"id": "1234567890",
"etag": "4",
"shared_link": {
"url": "https://someurl.box.com/s/2mgkgxe98et1li2w0lslbqv9wa724m3b",
"download_url": "https://someurl.box.com/shared/static/2mgkgxe98et1li2w0lslbqv9wa724m3b",
"vanity_url": null,
"vanity_name": null,
"effective_access": "open",
"effective_permission": "can_edit",
"is_password_enabled": true,
"unshared_at": "2022-12-12T10:53:43-08:00",
"download_count": 0,
"preview_count": 0,
"access": "open",
"permissions": {
"can_preview": true,
"can_download": true,
"can_edit": true
}
}
}

0 comments on commit a327deb

Please sign in to comment.