box-node-sdk
Version:
Official SDK for Box Platform APIs
238 lines (236 loc) • 9.37 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.DownloadsManager = exports.DownloadFileHeaders = exports.GetDownloadFileUrlHeaders = exports.DownloadFileOptionals = exports.GetDownloadFileUrlOptionals = void 0;
const errors_1 = require("../box/errors");
const network_1 = require("../networking/network");
const fetchOptions_1 = require("../networking/fetchOptions");
const utils_1 = require("../internal/utils");
const utils_2 = require("../internal/utils");
const utils_3 = require("../internal/utils");
const utils_4 = require("../internal/utils");
class GetDownloadFileUrlOptionals {
queryParams = {};
headers = new GetDownloadFileUrlHeaders({});
cancellationToken = void 0;
constructor(fields) {
if (fields.queryParams !== undefined) {
this.queryParams = fields.queryParams;
}
if (fields.headers !== undefined) {
this.headers = fields.headers;
}
if (fields.cancellationToken !== undefined) {
this.cancellationToken = fields.cancellationToken;
}
}
}
exports.GetDownloadFileUrlOptionals = GetDownloadFileUrlOptionals;
class DownloadFileOptionals {
queryParams = {};
headers = new DownloadFileHeaders({});
cancellationToken = void 0;
constructor(fields) {
if (fields.queryParams !== undefined) {
this.queryParams = fields.queryParams;
}
if (fields.headers !== undefined) {
this.headers = fields.headers;
}
if (fields.cancellationToken !== undefined) {
this.cancellationToken = fields.cancellationToken;
}
}
}
exports.DownloadFileOptionals = DownloadFileOptionals;
class GetDownloadFileUrlHeaders {
/**
* The byte range of the content to download.
*
* The format `bytes={start_byte}-{end_byte}` can be used to specify
* what section of the file to download. */
range;
/**
* The URL, and optional password, for the shared link of this item.
*
* This header can be used to access items that have not been
* explicitly shared with a user.
*
* Use the format `shared_link=[link]` or if a password is required then
* use `shared_link=[link]&shared_link_password=[password]`.
*
* This header can be used on the file or folder shared, as well as on any files
* or folders nested within the item. */
boxapi;
/**
* Extra headers that will be included in the HTTP request. */
extraHeaders = {};
constructor(fields) {
if (fields.range !== undefined) {
this.range = fields.range;
}
if (fields.boxapi !== undefined) {
this.boxapi = fields.boxapi;
}
if (fields.extraHeaders !== undefined) {
this.extraHeaders = fields.extraHeaders;
}
}
}
exports.GetDownloadFileUrlHeaders = GetDownloadFileUrlHeaders;
class DownloadFileHeaders {
/**
* The byte range of the content to download.
*
* The format `bytes={start_byte}-{end_byte}` can be used to specify
* what section of the file to download. */
range;
/**
* The URL, and optional password, for the shared link of this item.
*
* This header can be used to access items that have not been
* explicitly shared with a user.
*
* Use the format `shared_link=[link]` or if a password is required then
* use `shared_link=[link]&shared_link_password=[password]`.
*
* This header can be used on the file or folder shared, as well as on any files
* or folders nested within the item. */
boxapi;
/**
* Extra headers that will be included in the HTTP request. */
extraHeaders = {};
constructor(fields) {
if (fields.range !== undefined) {
this.range = fields.range;
}
if (fields.boxapi !== undefined) {
this.boxapi = fields.boxapi;
}
if (fields.extraHeaders !== undefined) {
this.extraHeaders = fields.extraHeaders;
}
}
}
exports.DownloadFileHeaders = DownloadFileHeaders;
class DownloadsManager {
auth;
networkSession = new network_1.NetworkSession({});
constructor(fields) {
if (fields.auth !== undefined) {
this.auth = fields.auth;
}
if (fields.networkSession !== undefined) {
this.networkSession = fields.networkSession;
}
}
/**
* Returns the contents of a file in binary format.
* @param {string} fileId The unique identifier that represents a file.
The ID for any file can be determined
by visiting a file in the web application
and copying the ID from the URL. For example,
for the URL `https://*.app.box.com/files/123`
the `file_id` is `123`.
Example: "12345"
* @param {GetDownloadFileUrlOptionalsInput} optionalsInput
* @returns {Promise<string>}
*/
async getDownloadFileUrl(fileId, optionalsInput = {}) {
const optionals = new GetDownloadFileUrlOptionals({
queryParams: optionalsInput.queryParams,
headers: optionalsInput.headers,
cancellationToken: optionalsInput.cancellationToken,
});
const queryParams = optionals.queryParams;
const headers = optionals.headers;
const cancellationToken = optionals.cancellationToken;
const queryParamsMap = (0, utils_1.prepareParams)({
['version']: (0, utils_2.toString)(queryParams.version),
['access_token']: (0, utils_2.toString)(queryParams.accessToken),
});
const headersMap = (0, utils_1.prepareParams)({
...{
['range']: (0, utils_2.toString)(headers.range),
['boxapi']: (0, utils_2.toString)(headers.boxapi),
},
...headers.extraHeaders,
});
const cancellationController = (0, utils_4.createCancellationController)();
const response = await this.networkSession.networkClient.fetch(new fetchOptions_1.FetchOptions({
url: ''.concat(this.networkSession.baseUrls.baseUrl, '/2.0/files/', (0, utils_2.toString)(fileId), '/content'),
method: 'GET',
params: queryParamsMap,
headers: headersMap,
responseFormat: 'no_content',
auth: this.auth,
networkSession: this.networkSession,
cancellationToken: cancellationToken == void 0
? cancellationController.signal
: cancellationToken,
followRedirects: false,
}));
if ((0, utils_3.isBrowser)()) {
cancellationController.abort();
if (response.url == void 0) {
throw new errors_1.BoxSdkError({ message: 'Unable to get response URL' });
}
return response.url;
}
if ('location' in response.headers) {
return response.headers.location;
}
if ('Location' in response.headers) {
return response.headers.Location;
}
throw new errors_1.BoxSdkError({ message: 'No location header in response' });
}
/**
* Returns the contents of a file in binary format.
* @param {string} fileId The unique identifier that represents a file.
The ID for any file can be determined
by visiting a file in the web application
and copying the ID from the URL. For example,
for the URL `https://*.app.box.com/files/123`
the `file_id` is `123`.
Example: "12345"
* @param {DownloadFileOptionalsInput} optionalsInput
* @returns {Promise<undefined | ByteStream>}
*/
async downloadFile(fileId, optionalsInput = {}) {
const optionals = new DownloadFileOptionals({
queryParams: optionalsInput.queryParams,
headers: optionalsInput.headers,
cancellationToken: optionalsInput.cancellationToken,
});
const queryParams = optionals.queryParams;
const headers = optionals.headers;
const cancellationToken = optionals.cancellationToken;
const queryParamsMap = (0, utils_1.prepareParams)({
['version']: (0, utils_2.toString)(queryParams.version),
['access_token']: (0, utils_2.toString)(queryParams.accessToken),
});
const headersMap = (0, utils_1.prepareParams)({
...{
['range']: (0, utils_2.toString)(headers.range),
['boxapi']: (0, utils_2.toString)(headers.boxapi),
},
...headers.extraHeaders,
});
const response = await this.networkSession.networkClient.fetch(new fetchOptions_1.FetchOptions({
url: ''.concat(this.networkSession.baseUrls.baseUrl, '/2.0/files/', (0, utils_2.toString)(fileId), '/content'),
method: 'GET',
params: queryParamsMap,
headers: headersMap,
responseFormat: 'binary',
auth: this.auth,
networkSession: this.networkSession,
cancellationToken: cancellationToken,
}));
if ((0, utils_2.toString)(response.status) == '202') {
return void 0;
}
return response.content;
}
}
exports.DownloadsManager = DownloadsManager;
//# sourceMappingURL=downloads.js.map