UNPKG

box-node-sdk

Version:

Official SDK for Box Platform APIs

1,391 lines (1,390 loc) 68.7 kB
import { serializeUploadSession } from '../schemas/uploadSession'; import { deserializeUploadSession } from '../schemas/uploadSession'; import { serializeClientError } from '../schemas/clientError'; import { deserializeClientError } from '../schemas/clientError'; import { serializeUploadedPart } from '../schemas/uploadedPart'; import { deserializeUploadedPart } from '../schemas/uploadedPart'; import { serializeUploadParts } from '../schemas/uploadParts'; import { deserializeUploadParts } from '../schemas/uploadParts'; import { serializeFiles } from '../schemas/files'; import { deserializeFiles } from '../schemas/files'; import { serializeUploadPart } from '../schemas/uploadPart'; import { deserializeUploadPart } from '../schemas/uploadPart'; import { serializeFileFull } from '../schemas/fileFull'; import { deserializeFileFull } from '../schemas/fileFull'; import { ResponseFormat } from '../networking/fetchOptions'; import { Buffer } from '../internal/utils'; import { HashName } from '../internal/utils'; import { Iterator } from '../internal/utils'; import { UploadSession } from '../schemas/uploadSession'; import { ClientError } from '../schemas/clientError'; import { UploadedPart } from '../schemas/uploadedPart'; import { UploadParts } from '../schemas/uploadParts'; import { Files } from '../schemas/files'; import { UploadPart } from '../schemas/uploadPart'; import { BoxSdkError } from '../box/errors'; import { Authentication } from '../networking/auth'; import { NetworkSession } from '../networking/network'; import { FetchOptions } from '../networking/fetchOptions'; import { FetchResponse } from '../networking/fetchResponse'; import { prepareParams } from '../internal/utils'; import { toString } from '../internal/utils'; import { ByteStream } from '../internal/utils'; import { CancellationToken } from '../internal/utils'; import { SerializedData } from '../serialization/json'; import { sdToJson } from '../serialization/json'; import { generateByteStreamFromBuffer } from '../internal/utils'; import { hexToBase64 } from '../internal/utils'; import { iterateChunks } from '../internal/utils'; import { readByteStream } from '../internal/utils'; import { reduceIterator } from '../internal/utils'; import { Hash } from '../internal/utils'; import { bufferLength } from '../internal/utils'; import { FileFull } from '../schemas/fileFull'; import { sdIsEmpty } from '../serialization/json'; import { sdIsBoolean } from '../serialization/json'; import { sdIsNumber } from '../serialization/json'; import { sdIsString } from '../serialization/json'; import { sdIsList } from '../serialization/json'; import { sdIsMap } from '../serialization/json'; export class CreateFileUploadSessionOptionals { readonly headers: CreateFileUploadSessionHeaders = new CreateFileUploadSessionHeaders({}); readonly cancellationToken?: CancellationToken = void 0; constructor( fields: Omit< CreateFileUploadSessionOptionals, 'headers' | 'cancellationToken' > & Partial< Pick<CreateFileUploadSessionOptionals, 'headers' | 'cancellationToken'> >, ) { if (fields.headers !== undefined) { this.headers = fields.headers; } if (fields.cancellationToken !== undefined) { this.cancellationToken = fields.cancellationToken; } } } export interface CreateFileUploadSessionOptionalsInput { readonly headers?: CreateFileUploadSessionHeaders; readonly cancellationToken?: CancellationToken; } export class CreateFileUploadSessionForExistingFileOptionals { readonly headers: CreateFileUploadSessionForExistingFileHeaders = new CreateFileUploadSessionForExistingFileHeaders({}); readonly cancellationToken?: CancellationToken = void 0; constructor( fields: Omit< CreateFileUploadSessionForExistingFileOptionals, 'headers' | 'cancellationToken' > & Partial< Pick< CreateFileUploadSessionForExistingFileOptionals, 'headers' | 'cancellationToken' > >, ) { if (fields.headers !== undefined) { this.headers = fields.headers; } if (fields.cancellationToken !== undefined) { this.cancellationToken = fields.cancellationToken; } } } export interface CreateFileUploadSessionForExistingFileOptionalsInput { readonly headers?: CreateFileUploadSessionForExistingFileHeaders; readonly cancellationToken?: CancellationToken; } export class GetFileUploadSessionByUrlOptionals { readonly headers: GetFileUploadSessionByUrlHeaders = new GetFileUploadSessionByUrlHeaders({}); readonly cancellationToken?: CancellationToken = void 0; constructor( fields: Omit< GetFileUploadSessionByUrlOptionals, 'headers' | 'cancellationToken' > & Partial< Pick< GetFileUploadSessionByUrlOptionals, 'headers' | 'cancellationToken' > >, ) { if (fields.headers !== undefined) { this.headers = fields.headers; } if (fields.cancellationToken !== undefined) { this.cancellationToken = fields.cancellationToken; } } } export interface GetFileUploadSessionByUrlOptionalsInput { readonly headers?: GetFileUploadSessionByUrlHeaders; readonly cancellationToken?: CancellationToken; } export class GetFileUploadSessionByIdOptionals { readonly headers: GetFileUploadSessionByIdHeaders = new GetFileUploadSessionByIdHeaders({}); readonly cancellationToken?: CancellationToken = void 0; constructor( fields: Omit< GetFileUploadSessionByIdOptionals, 'headers' | 'cancellationToken' > & Partial< Pick<GetFileUploadSessionByIdOptionals, 'headers' | 'cancellationToken'> >, ) { if (fields.headers !== undefined) { this.headers = fields.headers; } if (fields.cancellationToken !== undefined) { this.cancellationToken = fields.cancellationToken; } } } export interface GetFileUploadSessionByIdOptionalsInput { readonly headers?: GetFileUploadSessionByIdHeaders; readonly cancellationToken?: CancellationToken; } export class UploadFilePartByUrlOptionals { readonly cancellationToken?: CancellationToken = void 0; constructor( fields: Omit<UploadFilePartByUrlOptionals, 'cancellationToken'> & Partial<Pick<UploadFilePartByUrlOptionals, 'cancellationToken'>>, ) { if (fields.cancellationToken !== undefined) { this.cancellationToken = fields.cancellationToken; } } } export interface UploadFilePartByUrlOptionalsInput { readonly cancellationToken?: CancellationToken; } export class UploadFilePartOptionals { readonly cancellationToken?: CancellationToken = void 0; constructor( fields: Omit<UploadFilePartOptionals, 'cancellationToken'> & Partial<Pick<UploadFilePartOptionals, 'cancellationToken'>>, ) { if (fields.cancellationToken !== undefined) { this.cancellationToken = fields.cancellationToken; } } } export interface UploadFilePartOptionalsInput { readonly cancellationToken?: CancellationToken; } export class DeleteFileUploadSessionByUrlOptionals { readonly headers: DeleteFileUploadSessionByUrlHeaders = new DeleteFileUploadSessionByUrlHeaders({}); readonly cancellationToken?: CancellationToken = void 0; constructor( fields: Omit< DeleteFileUploadSessionByUrlOptionals, 'headers' | 'cancellationToken' > & Partial< Pick< DeleteFileUploadSessionByUrlOptionals, 'headers' | 'cancellationToken' > >, ) { if (fields.headers !== undefined) { this.headers = fields.headers; } if (fields.cancellationToken !== undefined) { this.cancellationToken = fields.cancellationToken; } } } export interface DeleteFileUploadSessionByUrlOptionalsInput { readonly headers?: DeleteFileUploadSessionByUrlHeaders; readonly cancellationToken?: CancellationToken; } export class DeleteFileUploadSessionByIdOptionals { readonly headers: DeleteFileUploadSessionByIdHeaders = new DeleteFileUploadSessionByIdHeaders({}); readonly cancellationToken?: CancellationToken = void 0; constructor( fields: Omit< DeleteFileUploadSessionByIdOptionals, 'headers' | 'cancellationToken' > & Partial< Pick< DeleteFileUploadSessionByIdOptionals, 'headers' | 'cancellationToken' > >, ) { if (fields.headers !== undefined) { this.headers = fields.headers; } if (fields.cancellationToken !== undefined) { this.cancellationToken = fields.cancellationToken; } } } export interface DeleteFileUploadSessionByIdOptionalsInput { readonly headers?: DeleteFileUploadSessionByIdHeaders; readonly cancellationToken?: CancellationToken; } export class GetFileUploadSessionPartsByUrlOptionals { readonly queryParams: GetFileUploadSessionPartsByUrlQueryParams = {} satisfies GetFileUploadSessionPartsByUrlQueryParams; readonly headers: GetFileUploadSessionPartsByUrlHeaders = new GetFileUploadSessionPartsByUrlHeaders({}); readonly cancellationToken?: CancellationToken = void 0; constructor( fields: Omit< GetFileUploadSessionPartsByUrlOptionals, 'queryParams' | 'headers' | 'cancellationToken' > & Partial< Pick< GetFileUploadSessionPartsByUrlOptionals, 'queryParams' | 'headers' | 'cancellationToken' > >, ) { if (fields.queryParams !== undefined) { this.queryParams = fields.queryParams; } if (fields.headers !== undefined) { this.headers = fields.headers; } if (fields.cancellationToken !== undefined) { this.cancellationToken = fields.cancellationToken; } } } export interface GetFileUploadSessionPartsByUrlOptionalsInput { readonly queryParams?: GetFileUploadSessionPartsByUrlQueryParams; readonly headers?: GetFileUploadSessionPartsByUrlHeaders; readonly cancellationToken?: CancellationToken; } export class GetFileUploadSessionPartsOptionals { readonly queryParams: GetFileUploadSessionPartsQueryParams = {} satisfies GetFileUploadSessionPartsQueryParams; readonly headers: GetFileUploadSessionPartsHeaders = new GetFileUploadSessionPartsHeaders({}); readonly cancellationToken?: CancellationToken = void 0; constructor( fields: Omit< GetFileUploadSessionPartsOptionals, 'queryParams' | 'headers' | 'cancellationToken' > & Partial< Pick< GetFileUploadSessionPartsOptionals, 'queryParams' | 'headers' | 'cancellationToken' > >, ) { if (fields.queryParams !== undefined) { this.queryParams = fields.queryParams; } if (fields.headers !== undefined) { this.headers = fields.headers; } if (fields.cancellationToken !== undefined) { this.cancellationToken = fields.cancellationToken; } } } export interface GetFileUploadSessionPartsOptionalsInput { readonly queryParams?: GetFileUploadSessionPartsQueryParams; readonly headers?: GetFileUploadSessionPartsHeaders; readonly cancellationToken?: CancellationToken; } export class CreateFileUploadSessionCommitByUrlOptionals { readonly cancellationToken?: CancellationToken = void 0; constructor( fields: Omit< CreateFileUploadSessionCommitByUrlOptionals, 'cancellationToken' > & Partial< Pick<CreateFileUploadSessionCommitByUrlOptionals, 'cancellationToken'> >, ) { if (fields.cancellationToken !== undefined) { this.cancellationToken = fields.cancellationToken; } } } export interface CreateFileUploadSessionCommitByUrlOptionalsInput { readonly cancellationToken?: CancellationToken; } export class CreateFileUploadSessionCommitOptionals { readonly cancellationToken?: CancellationToken = void 0; constructor( fields: Omit<CreateFileUploadSessionCommitOptionals, 'cancellationToken'> & Partial< Pick<CreateFileUploadSessionCommitOptionals, 'cancellationToken'> >, ) { if (fields.cancellationToken !== undefined) { this.cancellationToken = fields.cancellationToken; } } } export interface CreateFileUploadSessionCommitOptionalsInput { readonly cancellationToken?: CancellationToken; } interface PartAccumulator { readonly lastIndex: number; readonly parts: readonly UploadPart[]; readonly fileSize: number; readonly uploadPartUrl: string; readonly fileHash: Hash; } export interface CreateFileUploadSessionRequestBody { /** * The ID of the folder to upload the new file to. */ readonly folderId: string; /** * The total number of bytes of the file to be uploaded. */ readonly fileSize: number; /** * The name of new file. */ readonly fileName: string; readonly rawData?: SerializedData; } export class CreateFileUploadSessionHeaders { /** * Extra headers that will be included in the HTTP request. */ readonly extraHeaders?: { readonly [key: string]: undefined | string; } = {}; constructor( fields: Omit<CreateFileUploadSessionHeaders, 'extraHeaders'> & Partial<Pick<CreateFileUploadSessionHeaders, 'extraHeaders'>>, ) { if (fields.extraHeaders !== undefined) { this.extraHeaders = fields.extraHeaders; } } } export interface CreateFileUploadSessionHeadersInput { /** * Extra headers that will be included in the HTTP request. */ readonly extraHeaders?: { readonly [key: string]: undefined | string; }; } export interface CreateFileUploadSessionForExistingFileRequestBody { /** * The total number of bytes of the file to be uploaded. */ readonly fileSize: number; /** * The optional new name of new file. */ readonly fileName?: string; readonly rawData?: SerializedData; } export class CreateFileUploadSessionForExistingFileHeaders { /** * Extra headers that will be included in the HTTP request. */ readonly extraHeaders?: { readonly [key: string]: undefined | string; } = {}; constructor( fields: Omit< CreateFileUploadSessionForExistingFileHeaders, 'extraHeaders' > & Partial< Pick<CreateFileUploadSessionForExistingFileHeaders, 'extraHeaders'> >, ) { if (fields.extraHeaders !== undefined) { this.extraHeaders = fields.extraHeaders; } } } export interface CreateFileUploadSessionForExistingFileHeadersInput { /** * Extra headers that will be included in the HTTP request. */ readonly extraHeaders?: { readonly [key: string]: undefined | string; }; } export class GetFileUploadSessionByUrlHeaders { /** * Extra headers that will be included in the HTTP request. */ readonly extraHeaders?: { readonly [key: string]: undefined | string; } = {}; constructor( fields: Omit<GetFileUploadSessionByUrlHeaders, 'extraHeaders'> & Partial<Pick<GetFileUploadSessionByUrlHeaders, 'extraHeaders'>>, ) { if (fields.extraHeaders !== undefined) { this.extraHeaders = fields.extraHeaders; } } } export interface GetFileUploadSessionByUrlHeadersInput { /** * Extra headers that will be included in the HTTP request. */ readonly extraHeaders?: { readonly [key: string]: undefined | string; }; } export class GetFileUploadSessionByIdHeaders { /** * Extra headers that will be included in the HTTP request. */ readonly extraHeaders?: { readonly [key: string]: undefined | string; } = {}; constructor( fields: Omit<GetFileUploadSessionByIdHeaders, 'extraHeaders'> & Partial<Pick<GetFileUploadSessionByIdHeaders, 'extraHeaders'>>, ) { if (fields.extraHeaders !== undefined) { this.extraHeaders = fields.extraHeaders; } } } export interface GetFileUploadSessionByIdHeadersInput { /** * Extra headers that will be included in the HTTP request. */ readonly extraHeaders?: { readonly [key: string]: undefined | string; }; } export class UploadFilePartByUrlHeaders { /** * The [RFC3230][1] message digest of the chunk uploaded. * * Only SHA1 is supported. The SHA1 digest must be base64 * encoded. The format of this header is as * `sha=BASE64_ENCODED_DIGEST`. * * To get the value for the `SHA` digest, use the * openSSL command to encode the file part: * `openssl sha1 -binary <FILE_PART_NAME> | base64`. * * [1]: https://tools.ietf.org/html/rfc3230 */ readonly digest!: string; /** * The byte range of the chunk. * * Must not overlap with the range of a part already * uploaded this session. Each part’s size must be * exactly equal in size to the part size specified * in the upload session that you created. * One exception is the last part of the file, as this can be smaller. * * When providing the value for `content-range`, remember that: * * * The lower bound of each part's byte range * must be a multiple of the part size. * * The higher bound must be a multiple of the part size - 1. */ readonly contentRange!: string; /** * Extra headers that will be included in the HTTP request. */ readonly extraHeaders?: { readonly [key: string]: undefined | string; } = {}; constructor( fields: Omit<UploadFilePartByUrlHeaders, 'extraHeaders'> & Partial<Pick<UploadFilePartByUrlHeaders, 'extraHeaders'>>, ) { if (fields.digest !== undefined) { this.digest = fields.digest; } if (fields.contentRange !== undefined) { this.contentRange = fields.contentRange; } if (fields.extraHeaders !== undefined) { this.extraHeaders = fields.extraHeaders; } } } export interface UploadFilePartByUrlHeadersInput { /** * The [RFC3230][1] message digest of the chunk uploaded. * * Only SHA1 is supported. The SHA1 digest must be base64 * encoded. The format of this header is as * `sha=BASE64_ENCODED_DIGEST`. * * To get the value for the `SHA` digest, use the * openSSL command to encode the file part: * `openssl sha1 -binary <FILE_PART_NAME> | base64`. * * [1]: https://tools.ietf.org/html/rfc3230 */ readonly digest: string; /** * The byte range of the chunk. * * Must not overlap with the range of a part already * uploaded this session. Each part’s size must be * exactly equal in size to the part size specified * in the upload session that you created. * One exception is the last part of the file, as this can be smaller. * * When providing the value for `content-range`, remember that: * * * The lower bound of each part's byte range * must be a multiple of the part size. * * The higher bound must be a multiple of the part size - 1. */ readonly contentRange: string; /** * Extra headers that will be included in the HTTP request. */ readonly extraHeaders?: { readonly [key: string]: undefined | string; }; } export class UploadFilePartHeaders { /** * The [RFC3230][1] message digest of the chunk uploaded. * * Only SHA1 is supported. The SHA1 digest must be base64 * encoded. The format of this header is as * `sha=BASE64_ENCODED_DIGEST`. * * To get the value for the `SHA` digest, use the * openSSL command to encode the file part: * `openssl sha1 -binary <FILE_PART_NAME> | base64`. * * [1]: https://tools.ietf.org/html/rfc3230 */ readonly digest!: string; /** * The byte range of the chunk. * * Must not overlap with the range of a part already * uploaded this session. Each part’s size must be * exactly equal in size to the part size specified * in the upload session that you created. * One exception is the last part of the file, as this can be smaller. * * When providing the value for `content-range`, remember that: * * * The lower bound of each part's byte range * must be a multiple of the part size. * * The higher bound must be a multiple of the part size - 1. */ readonly contentRange!: string; /** * Extra headers that will be included in the HTTP request. */ readonly extraHeaders?: { readonly [key: string]: undefined | string; } = {}; constructor( fields: Omit<UploadFilePartHeaders, 'extraHeaders'> & Partial<Pick<UploadFilePartHeaders, 'extraHeaders'>>, ) { if (fields.digest !== undefined) { this.digest = fields.digest; } if (fields.contentRange !== undefined) { this.contentRange = fields.contentRange; } if (fields.extraHeaders !== undefined) { this.extraHeaders = fields.extraHeaders; } } } export interface UploadFilePartHeadersInput { /** * The [RFC3230][1] message digest of the chunk uploaded. * * Only SHA1 is supported. The SHA1 digest must be base64 * encoded. The format of this header is as * `sha=BASE64_ENCODED_DIGEST`. * * To get the value for the `SHA` digest, use the * openSSL command to encode the file part: * `openssl sha1 -binary <FILE_PART_NAME> | base64`. * * [1]: https://tools.ietf.org/html/rfc3230 */ readonly digest: string; /** * The byte range of the chunk. * * Must not overlap with the range of a part already * uploaded this session. Each part’s size must be * exactly equal in size to the part size specified * in the upload session that you created. * One exception is the last part of the file, as this can be smaller. * * When providing the value for `content-range`, remember that: * * * The lower bound of each part's byte range * must be a multiple of the part size. * * The higher bound must be a multiple of the part size - 1. */ readonly contentRange: string; /** * Extra headers that will be included in the HTTP request. */ readonly extraHeaders?: { readonly [key: string]: undefined | string; }; } export class DeleteFileUploadSessionByUrlHeaders { /** * Extra headers that will be included in the HTTP request. */ readonly extraHeaders?: { readonly [key: string]: undefined | string; } = {}; constructor( fields: Omit<DeleteFileUploadSessionByUrlHeaders, 'extraHeaders'> & Partial<Pick<DeleteFileUploadSessionByUrlHeaders, 'extraHeaders'>>, ) { if (fields.extraHeaders !== undefined) { this.extraHeaders = fields.extraHeaders; } } } export interface DeleteFileUploadSessionByUrlHeadersInput { /** * Extra headers that will be included in the HTTP request. */ readonly extraHeaders?: { readonly [key: string]: undefined | string; }; } export class DeleteFileUploadSessionByIdHeaders { /** * Extra headers that will be included in the HTTP request. */ readonly extraHeaders?: { readonly [key: string]: undefined | string; } = {}; constructor( fields: Omit<DeleteFileUploadSessionByIdHeaders, 'extraHeaders'> & Partial<Pick<DeleteFileUploadSessionByIdHeaders, 'extraHeaders'>>, ) { if (fields.extraHeaders !== undefined) { this.extraHeaders = fields.extraHeaders; } } } export interface DeleteFileUploadSessionByIdHeadersInput { /** * Extra headers that will be included in the HTTP request. */ readonly extraHeaders?: { readonly [key: string]: undefined | string; }; } export interface GetFileUploadSessionPartsByUrlQueryParams { /** * The offset of the item at which to begin the response. * * Queries with offset parameter value * exceeding 10000 will be rejected * with a 400 response. */ readonly offset?: number; /** * The maximum number of items to return per page. */ readonly limit?: number; } export class GetFileUploadSessionPartsByUrlHeaders { /** * Extra headers that will be included in the HTTP request. */ readonly extraHeaders?: { readonly [key: string]: undefined | string; } = {}; constructor( fields: Omit<GetFileUploadSessionPartsByUrlHeaders, 'extraHeaders'> & Partial<Pick<GetFileUploadSessionPartsByUrlHeaders, 'extraHeaders'>>, ) { if (fields.extraHeaders !== undefined) { this.extraHeaders = fields.extraHeaders; } } } export interface GetFileUploadSessionPartsByUrlHeadersInput { /** * Extra headers that will be included in the HTTP request. */ readonly extraHeaders?: { readonly [key: string]: undefined | string; }; } export interface GetFileUploadSessionPartsQueryParams { /** * The offset of the item at which to begin the response. * * Queries with offset parameter value * exceeding 10000 will be rejected * with a 400 response. */ readonly offset?: number; /** * The maximum number of items to return per page. */ readonly limit?: number; } export class GetFileUploadSessionPartsHeaders { /** * Extra headers that will be included in the HTTP request. */ readonly extraHeaders?: { readonly [key: string]: undefined | string; } = {}; constructor( fields: Omit<GetFileUploadSessionPartsHeaders, 'extraHeaders'> & Partial<Pick<GetFileUploadSessionPartsHeaders, 'extraHeaders'>>, ) { if (fields.extraHeaders !== undefined) { this.extraHeaders = fields.extraHeaders; } } } export interface GetFileUploadSessionPartsHeadersInput { /** * Extra headers that will be included in the HTTP request. */ readonly extraHeaders?: { readonly [key: string]: undefined | string; }; } export interface CreateFileUploadSessionCommitByUrlRequestBody { /** * The list details for the uploaded parts. */ readonly parts: readonly UploadPart[]; readonly rawData?: SerializedData; } export class CreateFileUploadSessionCommitByUrlHeaders { /** * The [RFC3230][1] message digest of the whole file. * * Only SHA1 is supported. The SHA1 digest must be Base64 * encoded. The format of this header is as * `sha=BASE64_ENCODED_DIGEST`. * * [1]: https://tools.ietf.org/html/rfc3230 */ readonly digest!: string; /** * 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. */ readonly ifMatch?: string; /** * Ensures an item is only returned if it has changed. * * Pass in the item's last observed `etag` value * into this header and the endpoint will fail * with a `304 Not Modified` if the item has not * changed since. */ readonly ifNoneMatch?: string; /** * Extra headers that will be included in the HTTP request. */ readonly extraHeaders?: { readonly [key: string]: undefined | string; } = {}; constructor( fields: Omit<CreateFileUploadSessionCommitByUrlHeaders, 'extraHeaders'> & Partial<Pick<CreateFileUploadSessionCommitByUrlHeaders, 'extraHeaders'>>, ) { if (fields.digest !== undefined) { this.digest = fields.digest; } if (fields.ifMatch !== undefined) { this.ifMatch = fields.ifMatch; } if (fields.ifNoneMatch !== undefined) { this.ifNoneMatch = fields.ifNoneMatch; } if (fields.extraHeaders !== undefined) { this.extraHeaders = fields.extraHeaders; } } } export interface CreateFileUploadSessionCommitByUrlHeadersInput { /** * The [RFC3230][1] message digest of the whole file. * * Only SHA1 is supported. The SHA1 digest must be Base64 * encoded. The format of this header is as * `sha=BASE64_ENCODED_DIGEST`. * * [1]: https://tools.ietf.org/html/rfc3230 */ readonly digest: string; /** * 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. */ readonly ifMatch?: string; /** * Ensures an item is only returned if it has changed. * * Pass in the item's last observed `etag` value * into this header and the endpoint will fail * with a `304 Not Modified` if the item has not * changed since. */ readonly ifNoneMatch?: string; /** * Extra headers that will be included in the HTTP request. */ readonly extraHeaders?: { readonly [key: string]: undefined | string; }; } export interface CreateFileUploadSessionCommitRequestBody { /** * The list details for the uploaded parts. */ readonly parts: readonly UploadPart[]; readonly rawData?: SerializedData; } export class CreateFileUploadSessionCommitHeaders { /** * The [RFC3230][1] message digest of the whole file. * * Only SHA1 is supported. The SHA1 digest must be Base64 * encoded. The format of this header is as * `sha=BASE64_ENCODED_DIGEST`. * * [1]: https://tools.ietf.org/html/rfc3230 */ readonly digest!: string; /** * 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. */ readonly ifMatch?: string; /** * Ensures an item is only returned if it has changed. * * Pass in the item's last observed `etag` value * into this header and the endpoint will fail * with a `304 Not Modified` if the item has not * changed since. */ readonly ifNoneMatch?: string; /** * Extra headers that will be included in the HTTP request. */ readonly extraHeaders?: { readonly [key: string]: undefined | string; } = {}; constructor( fields: Omit<CreateFileUploadSessionCommitHeaders, 'extraHeaders'> & Partial<Pick<CreateFileUploadSessionCommitHeaders, 'extraHeaders'>>, ) { if (fields.digest !== undefined) { this.digest = fields.digest; } if (fields.ifMatch !== undefined) { this.ifMatch = fields.ifMatch; } if (fields.ifNoneMatch !== undefined) { this.ifNoneMatch = fields.ifNoneMatch; } if (fields.extraHeaders !== undefined) { this.extraHeaders = fields.extraHeaders; } } } export interface CreateFileUploadSessionCommitHeadersInput { /** * The [RFC3230][1] message digest of the whole file. * * Only SHA1 is supported. The SHA1 digest must be Base64 * encoded. The format of this header is as * `sha=BASE64_ENCODED_DIGEST`. * * [1]: https://tools.ietf.org/html/rfc3230 */ readonly digest: string; /** * 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. */ readonly ifMatch?: string; /** * Ensures an item is only returned if it has changed. * * Pass in the item's last observed `etag` value * into this header and the endpoint will fail * with a `304 Not Modified` if the item has not * changed since. */ readonly ifNoneMatch?: string; /** * Extra headers that will be included in the HTTP request. */ readonly extraHeaders?: { readonly [key: string]: undefined | string; }; } export class ChunkedUploadsManager { readonly auth?: Authentication; readonly networkSession: NetworkSession = new NetworkSession({}); constructor( fields: Omit< ChunkedUploadsManager, | 'networkSession' | 'createFileUploadSession' | 'createFileUploadSessionForExistingFile' | 'getFileUploadSessionByUrl' | 'getFileUploadSessionById' | 'uploadFilePartByUrl' | 'uploadFilePart' | 'deleteFileUploadSessionByUrl' | 'deleteFileUploadSessionById' | 'getFileUploadSessionPartsByUrl' | 'getFileUploadSessionParts' | 'createFileUploadSessionCommitByUrl' | 'createFileUploadSessionCommit' | 'reducer' | 'uploadBigFile' > & Partial<Pick<ChunkedUploadsManager, 'networkSession'>>, ) { if (fields.auth !== undefined) { this.auth = fields.auth; } if (fields.networkSession !== undefined) { this.networkSession = fields.networkSession; } } /** * Creates an upload session for a new file. * @param {CreateFileUploadSessionRequestBody} requestBody Request body of createFileUploadSession method * @param {CreateFileUploadSessionOptionalsInput} optionalsInput * @returns {Promise<UploadSession>} */ async createFileUploadSession( requestBody: CreateFileUploadSessionRequestBody, optionalsInput: CreateFileUploadSessionOptionalsInput = {}, ): Promise<UploadSession> { const optionals: CreateFileUploadSessionOptionals = new CreateFileUploadSessionOptionals({ headers: optionalsInput.headers, cancellationToken: optionalsInput.cancellationToken, }); const headers: any = optionals.headers; const cancellationToken: any = optionals.cancellationToken; const headersMap: { readonly [key: string]: string; } = prepareParams({ ...{}, ...headers.extraHeaders }); const response: FetchResponse = await this.networkSession.networkClient.fetch( new FetchOptions({ url: ''.concat( this.networkSession.baseUrls.uploadUrl, '/2.0/files/upload_sessions', ) as string, method: 'POST', headers: headersMap, data: serializeCreateFileUploadSessionRequestBody(requestBody), contentType: 'application/json', responseFormat: 'json' as ResponseFormat, auth: this.auth, networkSession: this.networkSession, cancellationToken: cancellationToken, }), ); return { ...deserializeUploadSession(response.data!), rawData: response.data!, }; } /** * Creates an upload session for an existing file. * @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 {CreateFileUploadSessionForExistingFileRequestBody} requestBody Request body of createFileUploadSessionForExistingFile method * @param {CreateFileUploadSessionForExistingFileOptionalsInput} optionalsInput * @returns {Promise<UploadSession>} */ async createFileUploadSessionForExistingFile( fileId: string, requestBody: CreateFileUploadSessionForExistingFileRequestBody, optionalsInput: CreateFileUploadSessionForExistingFileOptionalsInput = {}, ): Promise<UploadSession> { const optionals: CreateFileUploadSessionForExistingFileOptionals = new CreateFileUploadSessionForExistingFileOptionals({ headers: optionalsInput.headers, cancellationToken: optionalsInput.cancellationToken, }); const headers: any = optionals.headers; const cancellationToken: any = optionals.cancellationToken; const headersMap: { readonly [key: string]: string; } = prepareParams({ ...{}, ...headers.extraHeaders }); const response: FetchResponse = await this.networkSession.networkClient.fetch( new FetchOptions({ url: ''.concat( this.networkSession.baseUrls.uploadUrl, '/2.0/files/', (toString(fileId) as string)!, '/upload_sessions', ) as string, method: 'POST', headers: headersMap, data: serializeCreateFileUploadSessionForExistingFileRequestBody( requestBody, ), contentType: 'application/json', responseFormat: 'json' as ResponseFormat, auth: this.auth, networkSession: this.networkSession, cancellationToken: cancellationToken, }), ); return { ...deserializeUploadSession(response.data!), rawData: response.data!, }; } /** * Using this method with urls provided in response when creating a new upload session is preferred to use over GetFileUploadSessionById method. * This allows to always upload your content to the closest Box data center and can significantly improve upload speed. * Return information about an upload session. * * The actual endpoint URL is returned by the [`Create upload session`](https://developer.box.com/reference/post-files-upload-sessions) endpoint. * @param {string} url URL of getFileUploadSessionById method * @param {GetFileUploadSessionByUrlOptionalsInput} optionalsInput * @returns {Promise<UploadSession>} */ async getFileUploadSessionByUrl( url: string, optionalsInput: GetFileUploadSessionByUrlOptionalsInput = {}, ): Promise<UploadSession> { const optionals: GetFileUploadSessionByUrlOptionals = new GetFileUploadSessionByUrlOptionals({ headers: optionalsInput.headers, cancellationToken: optionalsInput.cancellationToken, }); const headers: any = optionals.headers; const cancellationToken: any = optionals.cancellationToken; const headersMap: { readonly [key: string]: string; } = prepareParams({ ...{}, ...headers.extraHeaders }); const response: FetchResponse = await this.networkSession.networkClient.fetch( new FetchOptions({ url: url, method: 'GET', headers: headersMap, responseFormat: 'json' as ResponseFormat, auth: this.auth, networkSession: this.networkSession, cancellationToken: cancellationToken, }), ); return { ...deserializeUploadSession(response.data!), rawData: response.data!, }; } /** * Return information about an upload session. * * The actual endpoint URL is returned by the [`Create upload session`](https://developer.box.com/reference/post-files-upload-sessions) endpoint. * @param {string} uploadSessionId The ID of the upload session. Example: "D5E3F7A" * @param {GetFileUploadSessionByIdOptionalsInput} optionalsInput * @returns {Promise<UploadSession>} */ async getFileUploadSessionById( uploadSessionId: string, optionalsInput: GetFileUploadSessionByIdOptionalsInput = {}, ): Promise<UploadSession> { const optionals: GetFileUploadSessionByIdOptionals = new GetFileUploadSessionByIdOptionals({ headers: optionalsInput.headers, cancellationToken: optionalsInput.cancellationToken, }); const headers: any = optionals.headers; const cancellationToken: any = optionals.cancellationToken; const headersMap: { readonly [key: string]: string; } = prepareParams({ ...{}, ...headers.extraHeaders }); const response: FetchResponse = await this.networkSession.networkClient.fetch( new FetchOptions({ url: ''.concat( this.networkSession.baseUrls.uploadUrl, '/2.0/files/upload_sessions/', (toString(uploadSessionId) as string)!, ) as string, method: 'GET', headers: headersMap, responseFormat: 'json' as ResponseFormat, auth: this.auth, networkSession: this.networkSession, cancellationToken: cancellationToken, }), ); return { ...deserializeUploadSession(response.data!), rawData: response.data!, }; } /** * Using this method with urls provided in response when creating a new upload session is preferred to use over UploadFilePart method. * This allows to always upload your content to the closest Box data center and can significantly improve upload speed. * Uploads a chunk of a file for an upload session. * * The actual endpoint URL is returned by the [`Create upload session`](https://developer.box.com/reference/post-files-upload-sessions) * and [`Get upload session`](https://developer.box.com/reference/get-files-upload-sessions-id) endpoints. * @param {string} url URL of uploadFilePart method * @param {ByteStream} requestBody Request body of uploadFilePart method * @param {UploadFilePartByUrlHeadersInput} headersInput Headers of uploadFilePart method * @param {UploadFilePartByUrlOptionalsInput} optionalsInput * @returns {Promise<UploadedPart>} */ async uploadFilePartByUrl( url: string, requestBody: ByteStream, headersInput: UploadFilePartByUrlHeadersInput, optionalsInput: UploadFilePartByUrlOptionalsInput = {}, ): Promise<UploadedPart> { const headers: UploadFilePartByUrlHeaders = new UploadFilePartByUrlHeaders({ digest: headersInput.digest, contentRange: headersInput.contentRange, extraHeaders: headersInput.extraHeaders, }); const optionals: UploadFilePartByUrlOptionals = new UploadFilePartByUrlOptionals({ cancellationToken: optionalsInput.cancellationToken, }); const cancellationToken: any = optionals.cancellationToken; const headersMap: { readonly [key: string]: string; } = prepareParams({ ...{ ['digest']: toString(headers.digest) as string, ['content-range']: toString(headers.contentRange) as string, }, ...headers.extraHeaders, }); const response: FetchResponse = await this.networkSession.networkClient.fetch( new FetchOptions({ url: url, method: 'PUT', headers: headersMap, fileStream: requestBody, contentType: 'application/octet-stream', responseFormat: 'json' as ResponseFormat, auth: this.auth, networkSession: this.networkSession, cancellationToken: cancellationToken, }), ); return { ...deserializeUploadedPart(response.data!), rawData: response.data!, }; } /** * Uploads a chunk of a file for an upload session. * * The actual endpoint URL is returned by the [`Create upload session`](https://developer.box.com/reference/post-files-upload-sessions) * and [`Get upload session`](https://developer.box.com/reference/get-files-upload-sessions-id) endpoints. * @param {string} uploadSessionId The ID of the upload session. Example: "D5E3F7A" * @param {ByteStream} requestBody Request body of uploadFilePart method * @param {UploadFilePartHeadersInput} headersInput Headers of uploadFilePart method * @param {UploadFilePartOptionalsInput} optionalsInput * @returns {Promise<UploadedPart>} */ async uploadFilePart( uploadSessionId: string, requestBody: ByteStream, headersInput: UploadFilePartHeadersInput, optionalsInput: UploadFilePartOptionalsInput = {}, ): Promise<UploadedPart> { const headers: UploadFilePartHeaders = new UploadFilePartHeaders({ digest: headersInput.digest, contentRange: headersInput.contentRange, extraHeaders: headersInput.extraHeaders, }); const optionals: UploadFilePartOptionals = new UploadFilePartOptionals({ cancellationToken: optionalsInput.cancellationToken, }); const cancellationToken: any = optionals.cancellationToken; const headersMap: { readonly [key: string]: string; } = prepareParams({ ...{ ['digest']: toString(headers.digest) as string, ['content-range']: toString(headers.contentRange) as string, }, ...headers.extraHeaders, }); const response: FetchResponse = await this.networkSession.networkClient.fetch( new FetchOptions({ url: ''.concat( this.networkSession.baseUrls.uploadUrl, '/2.0/files/upload_sessions/', (toString(uploadSessionId) as string)!, ) as string, method: 'PUT', headers: headersMap, fileStream: requestBody, contentType: 'application/octet-stream', responseFormat: 'json' as ResponseFormat, auth: this.auth, networkSession: this.networkSession, cancellationToken: cancellationToken, }), ); return { ...deserializeUploadedPart(response.data!), rawData: response.data!, }; } /** * Using this method with urls provided in response when creating a new upload session is preferred to use over DeleteFileUploadSessionById method. * This allows to always upload your content to the closest Box data center and can significantly improve upload speed. * Abort an upload session and discard all data uploaded. * * This cannot be reversed. * * The actual endpoint URL is returned by the [`Create upload session`](https://developer.box.com/reference/post-files-upload-sessions) * and [`Get upload session`](https://developer.box.com/reference/get-files-upload-sessions-id) endpoints. * @param {string} url URL of deleteFileUploadSessionById method * @param {DeleteFileUploadSessionByUrlOptionalsInput} optionalsInput * @returns {Promise<undefined>} */ async deleteFileUploadSessionByUrl( url: string, optionalsInput: DeleteFileUploadSessionByUrlOptionalsInput = {}, ): Promise<undefined> { const optionals: DeleteFileUploadSessionByUrlOptionals = new DeleteFileUploadSessionByUrlOptionals({ headers: optionalsInput.headers, cancellationToken: optionalsInput.cancellationToken, }); const headers: any = optionals.headers; const cancellationToken: any = optionals.cancellationToken; const headersMap: { readonly [key: string]: string; } = prepareParams({ ...{}, ...headers.extraHeaders }); const response: FetchResponse = await this.networkSession.networkClient.fetch( new FetchOptions({ url: url, method: 'DELETE', headers: headersMap, responseFormat: 'no_content' as ResponseFormat, auth: this.auth, networkSession: this.networkSession, cancellationToken: cancellationToken, }), ); return void 0; } /** * Abort an upload session and discard all data uploaded. * * This cannot be reversed. * * The actual endpoint URL is returned by the [`Create upload session`](https://developer.box.com/reference/post-files-upload-sessions) * and [`Get upload session`](https://developer.box.com/reference/get-files-upload-sessions-id) endpoints. * @param {string} uploadSessionId The ID of the upload session. Example: "D5E3F7A" * @param {DeleteFileUploadSessionByIdOptionalsInput} optionalsInput * @returns {Promise<undefined>} */ async deleteFileUploadSessionById( uploadSessionId: string, optionalsInput: DeleteFileUploadSessionByIdOptionalsInput = {}, ): Promise<undefined> { const optionals: DeleteFileUploadSessionByIdOptionals = new DeleteFileUploadSessionByIdOptionals({ headers: optionalsInput.headers, cancellationToken: optionalsInput.cancellationToken, }); const headers: any = optionals.headers; const cancellationToken: any = optionals.cancellationToken; const headersMap: { readonly [key: string]: string; } = prepareParams({ ...{}, ...headers.extraHeaders }); const response: FetchResponse = await this.networkSession.networkClient.fetch( new FetchOptions({ url: ''.concat( this.networkSession.baseUrls.uploadUrl, '/2.0/files/upload_sessions/', (toString(uploadSessionId) as string)!, ) as string, method: 'DELETE', headers: headersMap, responseFormat: 'no_content' as ResponseFormat, auth: this.auth, networkSession: this.networkSession, cancellationToken: cancellationToken, }), ); return void 0; } /** * Using this method with urls provided in response when creating a new upload session is preferred to use over GetFileUploadSessionParts method. * This allows to always upload your content to the closest Box data center and can significantly improve upload speed. * Return a list of the chunks uploaded to the upload session so far. * * The actual endpoint URL is returned by the [`Create upload session`](https://developer.box.com/reference/post-files-upload-sessions) * and [`Get upload session`](https://developer.box.com/reference/get-files-upload-sessions-id) endpoints. * @param {string} url URL of getFileUploadSessionParts method * @param {GetFileUploadSessionPartsByUrlOptionalsInput} optionalsInput * @returns {Promise<UploadParts>} */ async getFileUploadSessionPartsByUrl( url: string, optionalsInput: GetFileUploadSessionPartsByUrlOptionalsInput = {}, ): Promise<UploadParts> { const optionals: GetFileUploadSessionPartsByUrlOptionals = new GetFileUploadSessionPartsByUrlOptionals({ queryParams: optionalsInput.queryParams, headers: optionalsInput.headers, cancellationToken: optionalsInput.cancellationToken, }); const queryParams: any = optionals.queryParams; const headers: any = optionals.headers; const cancellationToken: any = optionals.cancellationToken; const queryParamsMap: { readonly [key: string]: string; } = prepareParams({ ['offset']: toString(queryParams.offset) as string, ['limit']: toString(queryParams.limit) as string, }); const headersMap: { readonly [key: string]: string; } = prepareParams({ ...{}, ...headers.extraHeaders }); const response: FetchResponse = await this.networkSession.networkClient.fetch( new FetchOptions({ url: url, method: 'GET', params: queryParamsMap, headers: headersMap, responseFormat: 'json' as ResponseFormat, auth: this.auth, networkSession: this.networkSession, cancellationToken: cancellationToken, }), ); return { ...deserializeUploadParts(response.data!), rawData: response.data!, }; } /** * Return a list of the chunks uploaded to the upload session so far. * * The actual endpoint URL is returned by the [`Create upload session`](https://developer.box.com/reference/post-files-upload-sessions) * and [`Get upload session`](https://developer.box.com/reference/get-files-u