box-node-sdk
Version:
Official SDK for Box Platform APIs
477 lines (472 loc) • 19.9 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.FileVersionsManager = exports.PromoteFileVersionHeaders = exports.UpdateFileVersionByIdHeaders = exports.DeleteFileVersionByIdHeaders = exports.GetFileVersionByIdHeaders = exports.GetFileVersionsHeaders = exports.PromoteFileVersionOptionals = exports.UpdateFileVersionByIdOptionals = exports.DeleteFileVersionByIdOptionals = exports.GetFileVersionByIdOptionals = exports.GetFileVersionsOptionals = void 0;
exports.serializeUpdateFileVersionByIdRequestBody = serializeUpdateFileVersionByIdRequestBody;
exports.deserializeUpdateFileVersionByIdRequestBody = deserializeUpdateFileVersionByIdRequestBody;
exports.serializePromoteFileVersionRequestBodyTypeField = serializePromoteFileVersionRequestBodyTypeField;
exports.deserializePromoteFileVersionRequestBodyTypeField = deserializePromoteFileVersionRequestBodyTypeField;
exports.serializePromoteFileVersionRequestBody = serializePromoteFileVersionRequestBody;
exports.deserializePromoteFileVersionRequestBody = deserializePromoteFileVersionRequestBody;
const fileVersions_1 = require("../schemas/fileVersions");
const fileVersionFull_1 = require("../schemas/fileVersionFull");
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 json_1 = require("../serialization/json");
const json_2 = require("../serialization/json");
class GetFileVersionsOptionals {
queryParams = {};
headers = new GetFileVersionsHeaders({});
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.GetFileVersionsOptionals = GetFileVersionsOptionals;
class GetFileVersionByIdOptionals {
queryParams = {};
headers = new GetFileVersionByIdHeaders({});
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.GetFileVersionByIdOptionals = GetFileVersionByIdOptionals;
class DeleteFileVersionByIdOptionals {
headers = new DeleteFileVersionByIdHeaders({});
cancellationToken = void 0;
constructor(fields) {
if (fields.headers !== undefined) {
this.headers = fields.headers;
}
if (fields.cancellationToken !== undefined) {
this.cancellationToken = fields.cancellationToken;
}
}
}
exports.DeleteFileVersionByIdOptionals = DeleteFileVersionByIdOptionals;
class UpdateFileVersionByIdOptionals {
requestBody = {};
headers = new UpdateFileVersionByIdHeaders({});
cancellationToken = void 0;
constructor(fields) {
if (fields.requestBody !== undefined) {
this.requestBody = fields.requestBody;
}
if (fields.headers !== undefined) {
this.headers = fields.headers;
}
if (fields.cancellationToken !== undefined) {
this.cancellationToken = fields.cancellationToken;
}
}
}
exports.UpdateFileVersionByIdOptionals = UpdateFileVersionByIdOptionals;
class PromoteFileVersionOptionals {
requestBody = {};
queryParams = {};
headers = new PromoteFileVersionHeaders({});
cancellationToken = void 0;
constructor(fields) {
if (fields.requestBody !== undefined) {
this.requestBody = fields.requestBody;
}
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.PromoteFileVersionOptionals = PromoteFileVersionOptionals;
class GetFileVersionsHeaders {
/**
* Extra headers that will be included in the HTTP request. */
extraHeaders = {};
constructor(fields) {
if (fields.extraHeaders !== undefined) {
this.extraHeaders = fields.extraHeaders;
}
}
}
exports.GetFileVersionsHeaders = GetFileVersionsHeaders;
class GetFileVersionByIdHeaders {
/**
* Extra headers that will be included in the HTTP request. */
extraHeaders = {};
constructor(fields) {
if (fields.extraHeaders !== undefined) {
this.extraHeaders = fields.extraHeaders;
}
}
}
exports.GetFileVersionByIdHeaders = GetFileVersionByIdHeaders;
class DeleteFileVersionByIdHeaders {
/**
* Ensures this item hasn't recently changed before
* making changes.
*
* Pass in the item's last observed `etag` value
* into this header and the endpoint will fail
* with a `412 Precondition Failed` if it
* has changed since. */
ifMatch;
/**
* Extra headers that will be included in the HTTP request. */
extraHeaders = {};
constructor(fields) {
if (fields.ifMatch !== undefined) {
this.ifMatch = fields.ifMatch;
}
if (fields.extraHeaders !== undefined) {
this.extraHeaders = fields.extraHeaders;
}
}
}
exports.DeleteFileVersionByIdHeaders = DeleteFileVersionByIdHeaders;
class UpdateFileVersionByIdHeaders {
/**
* Extra headers that will be included in the HTTP request. */
extraHeaders = {};
constructor(fields) {
if (fields.extraHeaders !== undefined) {
this.extraHeaders = fields.extraHeaders;
}
}
}
exports.UpdateFileVersionByIdHeaders = UpdateFileVersionByIdHeaders;
class PromoteFileVersionHeaders {
/**
* Extra headers that will be included in the HTTP request. */
extraHeaders = {};
constructor(fields) {
if (fields.extraHeaders !== undefined) {
this.extraHeaders = fields.extraHeaders;
}
}
}
exports.PromoteFileVersionHeaders = PromoteFileVersionHeaders;
class FileVersionsManager {
auth;
networkSession = new network_1.NetworkSession({});
constructor(fields) {
if (fields.auth !== undefined) {
this.auth = fields.auth;
}
if (fields.networkSession !== undefined) {
this.networkSession = fields.networkSession;
}
}
/**
* Retrieve a list of the past versions for a file.
*
* Versions are only tracked by Box users with premium accounts. To fetch the ID
* of the current version of a file, use the `GET /file/:id` API.
* @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 {GetFileVersionsOptionalsInput} optionalsInput
* @returns {Promise<FileVersions>}
*/
async getFileVersions(fileId, optionalsInput = {}) {
const optionals = new GetFileVersionsOptionals({
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)({
['fields']: queryParams.fields
? queryParams.fields.map(utils_2.toString).join(',')
: undefined,
['limit']: (0, utils_2.toString)(queryParams.limit),
['offset']: (0, utils_2.toString)(queryParams.offset),
});
const headersMap = (0, utils_1.prepareParams)({ ...{}, ...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), '/versions'),
method: 'GET',
params: queryParamsMap,
headers: headersMap,
responseFormat: 'json',
auth: this.auth,
networkSession: this.networkSession,
cancellationToken: cancellationToken,
}));
return {
...(0, fileVersions_1.deserializeFileVersions)(response.data),
rawData: response.data,
};
}
/**
* Retrieve a specific version of a file.
*
* Versions are only tracked for Box users with premium accounts.
* @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 {string} fileVersionId The ID of the file version.
Example: "1234"
* @param {GetFileVersionByIdOptionalsInput} optionalsInput
* @returns {Promise<FileVersionFull>}
*/
async getFileVersionById(fileId, fileVersionId, optionalsInput = {}) {
const optionals = new GetFileVersionByIdOptionals({
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)({
['fields']: queryParams.fields
? queryParams.fields.map(utils_2.toString).join(',')
: undefined,
});
const headersMap = (0, utils_1.prepareParams)({ ...{}, ...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), '/versions/', (0, utils_2.toString)(fileVersionId)),
method: 'GET',
params: queryParamsMap,
headers: headersMap,
responseFormat: 'json',
auth: this.auth,
networkSession: this.networkSession,
cancellationToken: cancellationToken,
}));
return {
...(0, fileVersionFull_1.deserializeFileVersionFull)(response.data),
rawData: response.data,
};
}
/**
* Move a file version to the trash.
*
* Versions are only tracked for Box users with premium accounts.
* @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 {string} fileVersionId The ID of the file version.
Example: "1234"
* @param {DeleteFileVersionByIdOptionalsInput} optionalsInput
* @returns {Promise<undefined>}
*/
async deleteFileVersionById(fileId, fileVersionId, optionalsInput = {}) {
const optionals = new DeleteFileVersionByIdOptionals({
headers: optionalsInput.headers,
cancellationToken: optionalsInput.cancellationToken,
});
const headers = optionals.headers;
const cancellationToken = optionals.cancellationToken;
const headersMap = (0, utils_1.prepareParams)({
...{ ['if-match']: (0, utils_2.toString)(headers.ifMatch) },
...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), '/versions/', (0, utils_2.toString)(fileVersionId)),
method: 'DELETE',
headers: headersMap,
responseFormat: 'no_content',
auth: this.auth,
networkSession: this.networkSession,
cancellationToken: cancellationToken,
}));
return void 0;
}
/**
* Restores a specific version of a file after it was deleted.
* Don't use this endpoint to restore Box Notes,
* as it works with file formats such as PDF, DOC,
* PPTX or similar.
* @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 {string} fileVersionId The ID of the file version.
Example: "1234"
* @param {UpdateFileVersionByIdOptionalsInput} optionalsInput
* @returns {Promise<FileVersionFull>}
*/
async updateFileVersionById(fileId, fileVersionId, optionalsInput = {}) {
const optionals = new UpdateFileVersionByIdOptionals({
requestBody: optionalsInput.requestBody,
headers: optionalsInput.headers,
cancellationToken: optionalsInput.cancellationToken,
});
const requestBody = optionals.requestBody;
const headers = optionals.headers;
const cancellationToken = optionals.cancellationToken;
const headersMap = (0, utils_1.prepareParams)({ ...{}, ...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), '/versions/', (0, utils_2.toString)(fileVersionId)),
method: 'PUT',
headers: headersMap,
data: serializeUpdateFileVersionByIdRequestBody(requestBody),
contentType: 'application/json',
responseFormat: 'json',
auth: this.auth,
networkSession: this.networkSession,
cancellationToken: cancellationToken,
}));
return {
...(0, fileVersionFull_1.deserializeFileVersionFull)(response.data),
rawData: response.data,
};
}
/**
* Promote a specific version of a file.
*
* If previous versions exist, this method can be used to
* promote one of the older versions to the top of the version history.
*
* This creates a new copy of the old version and puts it at the
* top of the versions history. The file will have the exact same contents
* as the older version, with the same hash digest, `etag`, and
* name as the original.
*
* Other properties such as comments do not get updated to their
* former values.
*
* Don't use this endpoint to restore Box Notes,
* as it works with file formats such as PDF, DOC,
* PPTX or similar.
* @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 {PromoteFileVersionOptionalsInput} optionalsInput
* @returns {Promise<FileVersionFull>}
*/
async promoteFileVersion(fileId, optionalsInput = {}) {
const optionals = new PromoteFileVersionOptionals({
requestBody: optionalsInput.requestBody,
queryParams: optionalsInput.queryParams,
headers: optionalsInput.headers,
cancellationToken: optionalsInput.cancellationToken,
});
const requestBody = optionals.requestBody;
const queryParams = optionals.queryParams;
const headers = optionals.headers;
const cancellationToken = optionals.cancellationToken;
const queryParamsMap = (0, utils_1.prepareParams)({
['fields']: queryParams.fields
? queryParams.fields.map(utils_2.toString).join(',')
: undefined,
});
const headersMap = (0, utils_1.prepareParams)({ ...{}, ...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), '/versions/current'),
method: 'POST',
params: queryParamsMap,
headers: headersMap,
data: serializePromoteFileVersionRequestBody(requestBody),
contentType: 'application/json',
responseFormat: 'json',
auth: this.auth,
networkSession: this.networkSession,
cancellationToken: cancellationToken,
}));
return {
...(0, fileVersionFull_1.deserializeFileVersionFull)(response.data),
rawData: response.data,
};
}
}
exports.FileVersionsManager = FileVersionsManager;
function serializeUpdateFileVersionByIdRequestBody(val) {
return { ['trashed_at']: val.trashedAt };
}
function deserializeUpdateFileVersionByIdRequestBody(val) {
if (!(0, json_2.sdIsMap)(val)) {
throw new errors_1.BoxSdkError({
message: 'Expecting a map for "UpdateFileVersionByIdRequestBody"',
});
}
if (!(val.trashed_at == void 0) && !(0, json_1.sdIsString)(val.trashed_at)) {
throw new errors_1.BoxSdkError({
message: 'Expecting string for "trashed_at" of type "UpdateFileVersionByIdRequestBody"',
});
}
const trashedAt = val.trashed_at == void 0 ? void 0 : val.trashed_at;
return { trashedAt: trashedAt };
}
function serializePromoteFileVersionRequestBodyTypeField(val) {
return val;
}
function deserializePromoteFileVersionRequestBodyTypeField(val) {
if (val == 'file_version') {
return val;
}
throw new errors_1.BoxSdkError({
message: "Can't deserialize PromoteFileVersionRequestBodyTypeField",
});
}
function serializePromoteFileVersionRequestBody(val) {
return {
['id']: val.id,
['type']: val.type == void 0
? val.type
: serializePromoteFileVersionRequestBodyTypeField(val.type),
};
}
function deserializePromoteFileVersionRequestBody(val) {
if (!(0, json_2.sdIsMap)(val)) {
throw new errors_1.BoxSdkError({
message: 'Expecting a map for "PromoteFileVersionRequestBody"',
});
}
if (!(val.id == void 0) && !(0, json_1.sdIsString)(val.id)) {
throw new errors_1.BoxSdkError({
message: 'Expecting string for "id" of type "PromoteFileVersionRequestBody"',
});
}
const id = val.id == void 0 ? void 0 : val.id;
const type = val.type == void 0
? void 0
: deserializePromoteFileVersionRequestBodyTypeField(val.type);
return { id: id, type: type };
}
//# sourceMappingURL=fileVersions.js.map