Skip to content

Commit

Permalink
test: Create preflight file upload check test (box/box-codegen#653) (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
box-sdk-build authored Jan 31, 2025
1 parent 502ac11 commit 5cdf5af
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .codegen.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{ "engineHash": "a74691d", "specHash": "1fdcbef", "version": "1.11.0" }
{ "engineHash": "1965ba0", "specHash": "1fdcbef", "version": "1.11.0" }
110 changes: 102 additions & 8 deletions docs/uploads.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,99 @@
# Uploads
# UploadsManager

Uploads module is used to upload files to Box. It supports uploading files from a readable stream. For now, it only supports uploading small files without chunked upload.
- [Upload file version](#upload-file-version)
- [Preflight check before upload](#preflight-check-before-upload)
- [Upload file](#upload-file)

<!-- START doctoc generated TOC please keep comment here to allow auto update -->
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
## Upload file version

- [Upload a File](#upload-a-file)
Update a file's content. For file sizes over 50MB we recommend
using the Chunk Upload APIs.

<!-- END doctoc generated TOC please keep comment here to allow auto update -->
The `attributes` part of the body must come **before** the
`file` part. Requests that do not follow this format when
uploading the file will receive a HTTP `400` error with a
`metadata_after_file_contents` error code.

## Upload a File
This operation is performed by calling function `uploadFileVersion`.

To upload a small file from a readable stream, call `uploadFile` method. This method returns a `Files` object which contains information about the uploaded files.
See the endpoint docs at
[API Reference](https://developer.box.com/reference/post-files-id-content/).

<!-- sample post_files_id_content -->

```ts
await client.uploads.uploadFileVersion(file.id, {
attributes: {
name: file.name!,
} satisfies UploadFileVersionRequestBodyAttributesField,
file: generateByteStream(20),
} satisfies UploadFileVersionRequestBody);
```

### Arguments

- fileId `string`
- 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"
- requestBody `UploadFileVersionRequestBody`
- Request body of uploadFileVersion method
- optionalsInput `UploadFileVersionOptionalsInput`

### Returns

This function returns a value of type `Files`.

Returns the new file object in a list.

## Preflight check before upload

Performs a check to verify that a file will be accepted by Box
before you upload the entire file.

This operation is performed by calling function `preflightFileUploadCheck`.

See the endpoint docs at
[API Reference](https://developer.box.com/reference/options-files-content/).

<!-- sample options_files_content -->

```ts
await client.uploads.preflightFileUploadCheck({
name: newFileName,
size: 1024 * 1024,
parent: { id: '0' } satisfies PreflightFileUploadCheckRequestBodyParentField,
} satisfies PreflightFileUploadCheckRequestBody);
```

### Arguments

- requestBody `PreflightFileUploadCheckRequestBody`
- Request body of preflightFileUploadCheck method
- headersInput `PreflightFileUploadCheckHeadersInput`
- Headers of preflightFileUploadCheck method
- cancellationToken `undefined | CancellationToken`
- Token used for request cancellation.

### Returns

This function returns a value of type `UploadUrl`.

If the check passed, the response will include a session URL that
can be used to upload the file to.

## Upload file

Uploads a small file to Box. For file sizes over 50MB we recommend
using the Chunk Upload APIs.

The `attributes` part of the body must come **before** the
`file` part. Requests that do not follow this format when
uploading the file will receive a HTTP `400` error with a
`metadata_after_file_contents` error code.

This operation is performed by calling function `uploadFile`.

See the endpoint docs at
[API Reference](https://developer.box.com/reference/post-files-content/).

<!-- sample post_files_content -->

Expand All @@ -27,3 +109,15 @@ const files = await client.uploads.uploadFile(body);
const file = files.entries[0];
console.log(`File uploaded with id ${file.id}, name ${file.name}`);
```

### Arguments

- requestBody `UploadFileRequestBody`
- Request body of uploadFile method
- ## optionalsInput `UploadFileOptionalsInput`

### Returns

This function returns a value of type `Files`.

Returns the new file object in a list.
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src/test/search.generated.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ import { MetadataFilterScopeField } from '../schemas/metadataFilter.generated.js
import { getUuid } from '../internal/utils.js';
import { generateByteStream } from '../internal/utils.js';
import { dateTimeFromString } from '../internal/utils.js';
import { delayInSeconds } from '../internal/utils.js';
import { getDefaultClient } from './commons.generated.js';
import { MetadataFieldFilterDateRange } from '../schemas/metadataFieldFilterDateRange.generated.js';
import { MetadataFieldFilterFloatRange } from '../schemas/metadataFieldFilterFloatRange.generated.js';
Expand Down Expand Up @@ -159,6 +160,7 @@ test('testCreateMetaDataQueryExecuteRead', async function testCreateMetaDataQuer
if (!(metadata.scope == template.scope)) {
throw new Error('Assertion failed');
}
await delayInSeconds(5);
const searchFrom: string = ''.concat(
template.scope!,
'.',
Expand Down
23 changes: 23 additions & 0 deletions src/test/uploads.generated.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ import { serializeFileFull } from '../schemas/fileFull.generated.js';
import { deserializeFileFull } from '../schemas/fileFull.generated.js';
import { serializeUploadFileVersionRequestBodyAttributesField } from '../managers/uploads.generated.js';
import { deserializeUploadFileVersionRequestBodyAttributesField } from '../managers/uploads.generated.js';
import { serializeUploadUrl } from '../schemas/uploadUrl.generated.js';
import { deserializeUploadUrl } from '../schemas/uploadUrl.generated.js';
import { serializePreflightFileUploadCheckRequestBody } from '../managers/uploads.generated.js';
import { deserializePreflightFileUploadCheckRequestBody } from '../managers/uploads.generated.js';
import { serializePreflightFileUploadCheckRequestBodyParentField } from '../managers/uploads.generated.js';
import { deserializePreflightFileUploadCheckRequestBodyParentField } from '../managers/uploads.generated.js';
import { UploadFileOptionalsInput } from '../managers/uploads.generated.js';
import { UploadFileOptionals } from '../managers/uploads.generated.js';
import { ByteStream } from '../internal/utils.js';
Expand All @@ -21,6 +27,9 @@ import { UploadFileVersionRequestBodyAttributesField } from '../managers/uploads
import { CancellationToken } from '../internal/utils.js';
import { UploadFileQueryParams } from '../managers/uploads.generated.js';
import { UploadFileHeaders } from '../managers/uploads.generated.js';
import { UploadUrl } from '../schemas/uploadUrl.generated.js';
import { PreflightFileUploadCheckRequestBody } from '../managers/uploads.generated.js';
import { PreflightFileUploadCheckRequestBodyParentField } from '../managers/uploads.generated.js';
import { getUuid } from '../internal/utils.js';
import { generateByteStream } from '../internal/utils.js';
import { createTokenAndCancelAfter } from '../internal/utils.js';
Expand Down Expand Up @@ -89,4 +98,18 @@ test('testRequestCancellation', async function testRequestCancellation(): Promis
);
}).rejects.toThrow();
});
test('testPreflightCheck', async function testPreflightCheck(): Promise<any> {
const newFileName: string = getUuid();
const preflightCheckResult: UploadUrl =
await client.uploads.preflightFileUploadCheck({
name: newFileName,
size: 1024 * 1024,
parent: {
id: '0',
} satisfies PreflightFileUploadCheckRequestBodyParentField,
} satisfies PreflightFileUploadCheckRequestBody);
if (!!(preflightCheckResult.uploadUrl == '')) {
throw new Error('Assertion failed');
}
});
export {};

0 comments on commit 5cdf5af

Please sign in to comment.