box-node-sdk
Version:
Official SDK for Box Platform APIs
193 lines • 8.03 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.CollectionsManager = exports.GetCollectionByIdHeaders = exports.GetCollectionItemsHeaders = exports.GetCollectionsHeaders = exports.GetCollectionByIdOptionals = exports.GetCollectionItemsOptionals = void 0;
const collections_1 = require("../schemas/collections");
const itemsOffsetPaginated_1 = require("../schemas/itemsOffsetPaginated");
const collection_1 = require("../schemas/collection");
const network_1 = require("../networking/network");
const fetchOptions_1 = require("../networking/fetchOptions");
const utils_1 = require("../internal/utils");
const utils_2 = require("../internal/utils");
class GetCollectionItemsOptionals {
queryParams = {};
headers = new GetCollectionItemsHeaders({});
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.GetCollectionItemsOptionals = GetCollectionItemsOptionals;
class GetCollectionByIdOptionals {
headers = new GetCollectionByIdHeaders({});
cancellationToken = void 0;
constructor(fields) {
if (fields.headers !== undefined) {
this.headers = fields.headers;
}
if (fields.cancellationToken !== undefined) {
this.cancellationToken = fields.cancellationToken;
}
}
}
exports.GetCollectionByIdOptionals = GetCollectionByIdOptionals;
class GetCollectionsHeaders {
/**
* Extra headers that will be included in the HTTP request. */
extraHeaders = {};
constructor(fields) {
if (fields.extraHeaders !== undefined) {
this.extraHeaders = fields.extraHeaders;
}
}
}
exports.GetCollectionsHeaders = GetCollectionsHeaders;
class GetCollectionItemsHeaders {
/**
* Extra headers that will be included in the HTTP request. */
extraHeaders = {};
constructor(fields) {
if (fields.extraHeaders !== undefined) {
this.extraHeaders = fields.extraHeaders;
}
}
}
exports.GetCollectionItemsHeaders = GetCollectionItemsHeaders;
class GetCollectionByIdHeaders {
/**
* Extra headers that will be included in the HTTP request. */
extraHeaders = {};
constructor(fields) {
if (fields.extraHeaders !== undefined) {
this.extraHeaders = fields.extraHeaders;
}
}
}
exports.GetCollectionByIdHeaders = GetCollectionByIdHeaders;
class CollectionsManager {
auth;
networkSession = new network_1.NetworkSession({});
constructor(fields) {
if (fields.auth !== undefined) {
this.auth = fields.auth;
}
if (fields.networkSession !== undefined) {
this.networkSession = fields.networkSession;
}
}
/**
* Retrieves all collections for a given user.
*
* Currently, only the `favorites` collection
* is supported.
* @param {GetCollectionsQueryParams} queryParams Query parameters of getCollections method
* @param {GetCollectionsHeadersInput} headersInput Headers of getCollections method
* @param {CancellationToken} cancellationToken Token used for request cancellation.
* @returns {Promise<Collections>}
*/
async getCollections(queryParams = {}, headersInput = new GetCollectionsHeaders({}), cancellationToken) {
const headers = new GetCollectionsHeaders({
extraHeaders: headersInput.extraHeaders,
});
const queryParamsMap = (0, utils_1.prepareParams)({
['fields']: queryParams.fields
? queryParams.fields.map(utils_2.toString).join(',')
: undefined,
['offset']: (0, utils_2.toString)(queryParams.offset),
['limit']: (0, utils_2.toString)(queryParams.limit),
});
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/collections'),
method: 'GET',
params: queryParamsMap,
headers: headersMap,
responseFormat: 'json',
auth: this.auth,
networkSession: this.networkSession,
cancellationToken: cancellationToken,
}));
return {
...(0, collections_1.deserializeCollections)(response.data),
rawData: response.data,
};
}
/**
* Retrieves the files and/or folders contained within
* this collection.
* @param {string} collectionId The ID of the collection.
Example: "926489"
* @param {GetCollectionItemsOptionalsInput} optionalsInput
* @returns {Promise<ItemsOffsetPaginated>}
*/
async getCollectionItems(collectionId, optionalsInput = {}) {
const optionals = new GetCollectionItemsOptionals({
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,
['offset']: (0, utils_2.toString)(queryParams.offset),
['limit']: (0, utils_2.toString)(queryParams.limit),
});
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/collections/', (0, utils_2.toString)(collectionId), '/items'),
method: 'GET',
params: queryParamsMap,
headers: headersMap,
responseFormat: 'json',
auth: this.auth,
networkSession: this.networkSession,
cancellationToken: cancellationToken,
}));
return {
...(0, itemsOffsetPaginated_1.deserializeItemsOffsetPaginated)(response.data),
rawData: response.data,
};
}
/**
* Retrieves a collection by its ID.
* @param {string} collectionId The ID of the collection.
Example: "926489"
* @param {GetCollectionByIdOptionalsInput} optionalsInput
* @returns {Promise<Collection>}
*/
async getCollectionById(collectionId, optionalsInput = {}) {
const optionals = new GetCollectionByIdOptionals({
headers: optionalsInput.headers,
cancellationToken: optionalsInput.cancellationToken,
});
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/collections/', (0, utils_2.toString)(collectionId)),
method: 'GET',
headers: headersMap,
responseFormat: 'json',
auth: this.auth,
networkSession: this.networkSession,
cancellationToken: cancellationToken,
}));
return {
...(0, collection_1.deserializeCollection)(response.data),
rawData: response.data,
};
}
}
exports.CollectionsManager = CollectionsManager;
//# sourceMappingURL=collections.js.map