Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 29 additions & 4 deletions docs/fileVersions.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ This operation is performed by calling function `getFileVersions`.
See the endpoint docs at
[API Reference](https://developer.box.com/reference/get-files-id-versions/).

_Currently we don't have an example for calling `getFileVersions` in integration tests_
<!-- sample get_files_id_versions -->

```ts
await client.fileVersions.getFileVersions(file.id);
```

### Arguments

Expand Down Expand Up @@ -46,7 +50,14 @@ This operation is performed by calling function `getFileVersionById`.
See the endpoint docs at
[API Reference](https://developer.box.com/reference/get-files-id-versions-id/).

_Currently we don't have an example for calling `getFileVersionById` in integration tests_
<!-- sample get_files_id_versions_id -->

```ts
await client.fileVersions.getFileVersionById(
file.id,
fileVersions.entries[0].id
);
```

### Arguments

Expand Down Expand Up @@ -111,7 +122,14 @@ This operation is performed by calling function `deleteFileVersionById`.
See the endpoint docs at
[API Reference](https://developer.box.com/reference/delete-files-id-versions-id/).

_Currently we don't have an example for calling `deleteFileVersionById` in integration tests_
<!-- sample delete_files_id_versions_id -->

```ts
await client.fileVersions.deleteFileVersionById(
file.id,
fileVersionsRestored.entries[0].id
);
```

### Arguments

Expand Down Expand Up @@ -153,7 +171,14 @@ This operation is performed by calling function `promoteFileVersion`.
See the endpoint docs at
[API Reference](https://developer.box.com/reference/post-files-id-versions-current/).

_Currently we don't have an example for calling `promoteFileVersion` in integration tests_
<!-- sample post_files_id_versions_current -->

```ts
await client.fileVersions.promoteFileVersion(file.id, {
id: fileVersions.entries[0].id,
type: 'file_version' as PromoteFileVersionRequestBodyArgTypeField,
} satisfies PromoteFileVersionRequestBodyArg);
```

### Arguments

Expand Down
16 changes: 14 additions & 2 deletions src/ccgAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,26 @@ export class CcgAuth implements Authentication {
}
}

async retrieveToken(networkSession?: NetworkSession) {
/**
* Get the access token for the app user. If the token is not cached or is expired, a new one will be fetched.
* @param networkSession An object to keep network session state
* @returns {Promise<AccessToken>} A promise resolving to the access token.
*/
async retrieveToken(networkSession?: NetworkSession): Promise<AccessToken> {
if (!this.token) {
await this.refreshToken(networkSession);
}
return this.token!;
}

async refreshToken(networkSession?: NetworkSession) {
/**
* Get a new access token for the app user.
* @param networkSession An object to keep network session state
* @returns {Promise<AccessToken | undefined>} A promise resolving to the access token.
*/
async refreshToken(
networkSession?: NetworkSession
): Promise<AccessToken | undefined> {
const requestBody = {
grant_type: 'client_credentials' as TokenRequestGrantType,
client_id: this.config.clientId,
Expand Down
10 changes: 10 additions & 0 deletions src/developerTokenAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,20 @@ export class DeveloperTokenAuth implements Authentication {
this.token = { accessToken: token } satisfies AccessToken;
}

/**
* Retrieves stored developer token
* @param networkSession An object to keep network session state
* @returns {AccessToken} A stored access token.
*/
async retrieveToken(networkSession?: NetworkSession) {
return this.token;
}

/**
* Developer token cannot be refreshed
* @param networkSession An object to keep network session state
* @returns Always throws an exception
*/
async refreshToken(networkSession?: NetworkSession) {
throw Error('Developer token has expired. Please provide a new one.');
}
Expand Down
8 changes: 5 additions & 3 deletions src/jwtAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,9 @@ export class JwtAuth implements Authentication {
}

/**
* Get the access token for the app user. If the token is not cached or is expired, a new one will be fetched.
* @returns {Promise<string>} A promise resolving to the access token.
* Get the access token for the app user. If the token is not cached or is expired, a new one will be fetched.
* @param networkSession An object to keep network session state
* @returns {Promise<AccessToken>} A promise resolving to the access token.
*/
async retrieveToken(networkSession?: NetworkSession): Promise<AccessToken> {
if (!this.token) {
Expand All @@ -160,7 +161,8 @@ export class JwtAuth implements Authentication {

/**
* Get a new access token for the app user.
* @returns {Promise<string>} A promise resolving to the access token.
* @param networkSession An object to keep network session state
* @returns {Promise<AccessToken | undefined>} A promise resolving to the access token.
*/
async refreshToken(
networkSession?: NetworkSession
Expand Down
5 changes: 3 additions & 2 deletions src/oauth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ export class OAuth implements Authentication {
/**
* Get the access token for the app user. If the token is not cached or is expired, a new one will be fetched.
* @param networkSession An object to keep network session state
* @returns {Promise<string>} A promise resolving to the access token.
* @returns {Promise<AccessToken>} A promise resolving to the access token.
*/
async retrieveToken(networkSession?: NetworkSession): Promise<AccessToken> {
if (!this.token) {
Expand All @@ -148,7 +148,8 @@ export class OAuth implements Authentication {
/**
* Get a new access token for the app user.
* @param networkSession An object to keep network session state
* @returns {Promise<string>} A promise resolving to the access token.
* @param refreshToken Refresh token, which can be used to obtain a new access token
* @returns {Promise<AccessToken | undefined>} A promise resolving to the access token.
*/
async refreshToken(
networkSession?: NetworkSession,
Expand Down
83 changes: 83 additions & 0 deletions src/test/fileVersions.generated.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { serializeFiles } from "../schemas.generated.js";
import { deserializeFiles } from "../schemas.generated.js";
import { serializeUploadFileRequestBodyArgAttributesField } from "../managers/uploads.generated.js";
import { deserializeUploadFileRequestBodyArgAttributesField } from "../managers/uploads.generated.js";
import { serializeUploadFileRequestBodyArgAttributesFieldParentField } from "../managers/uploads.generated.js";
import { deserializeUploadFileRequestBodyArgAttributesFieldParentField } from "../managers/uploads.generated.js";
import { serializeFile } from "../schemas.generated.js";
import { deserializeFile } from "../schemas.generated.js";
import { serializeUploadFileVersionRequestBodyArgAttributesField } from "../managers/uploads.generated.js";
import { deserializeUploadFileVersionRequestBodyArgAttributesField } from "../managers/uploads.generated.js";
import { serializeFileVersions } from "../schemas.generated.js";
import { deserializeFileVersions } from "../schemas.generated.js";
import { serializeFileVersionFull } from "../schemas.generated.js";
import { deserializeFileVersionFull } from "../schemas.generated.js";
import { serializePromoteFileVersionRequestBodyArg } from "../managers/fileVersions.generated.js";
import { deserializePromoteFileVersionRequestBodyArg } from "../managers/fileVersions.generated.js";
import { serializePromoteFileVersionRequestBodyArgTypeField } from "../managers/fileVersions.generated.js";
import { deserializePromoteFileVersionRequestBodyArgTypeField } from "../managers/fileVersions.generated.js";
import { serializeFileFull } from "../schemas.generated.js";
import { deserializeFileFull } from "../schemas.generated.js";
import { Files } from "../schemas.generated.js";
import { UploadFileRequestBodyArg } from "../managers/uploads.generated.js";
import { UploadFileRequestBodyArgAttributesField } from "../managers/uploads.generated.js";
import { UploadFileRequestBodyArgAttributesFieldParentField } from "../managers/uploads.generated.js";
import { File } from "../schemas.generated.js";
import { UploadFileVersionRequestBodyArg } from "../managers/uploads.generated.js";
import { UploadFileVersionRequestBodyArgAttributesField } from "../managers/uploads.generated.js";
import { FileVersions } from "../schemas.generated.js";
import { FileVersionFull } from "../schemas.generated.js";
import { PromoteFileVersionRequestBodyArg } from "../managers/fileVersions.generated.js";
import { PromoteFileVersionRequestBodyArgTypeField } from "../managers/fileVersions.generated.js";
import { FileFull } from "../schemas.generated.js";
import { decodeBase64 } from "../utils.js";
import { getEnvVar } from "../utils.js";
import { getUuid } from "../utils.js";
import { generateByteStream } from "../utils.js";
import { Client } from "../client.generated.js";
import { JwtAuth } from "../jwtAuth.js";
import { JwtConfig } from "../jwtAuth.js";
const jwtConfig: any = JwtConfig.fromConfigJsonString(decodeBase64(getEnvVar("JWT_CONFIG_BASE_64")));
const auth: any = new JwtAuth({ config: jwtConfig });
const client: any = new Client({ auth: auth });
test("testCreateListGetRestoreDeleteFileVersion", async function testCreateListGetRestoreDeleteFileVersion(): Promise<any> {
const oldName: any = getUuid();
const newName: any = getUuid();
const files: any = await client.uploads.uploadFile({ attributes: { name: oldName, parent: { id: "0" } satisfies UploadFileRequestBodyArgAttributesFieldParentField } satisfies UploadFileRequestBodyArgAttributesField, file: generateByteStream(10) } satisfies UploadFileRequestBodyArg);
const file: any = files.entries[0];
if (!(file.name == oldName)) {
throw "Assertion failed";
}
if (!(file.size == 10)) {
throw "Assertion failed";
}
const newFiles: any = await client.uploads.uploadFileVersion(file.id, { attributes: { name: newName } satisfies UploadFileVersionRequestBodyArgAttributesField, file: generateByteStream(20) } satisfies UploadFileVersionRequestBodyArg);
const newFile: any = newFiles.entries[0];
if (!(newFile.name == newName)) {
throw "Assertion failed";
}
if (!(newFile.size == 20)) {
throw "Assertion failed";
}
const fileVersions: any = await client.fileVersions.getFileVersions(file.id);
if (!(fileVersions.totalCount == 1)) {
throw "Assertion failed";
}
const fileVersion: any = await client.fileVersions.getFileVersionById(file.id, fileVersions.entries[0].id);
if (!(fileVersion.id == fileVersions.entries[0].id)) {
throw "Assertion failed";
}
await client.fileVersions.promoteFileVersion(file.id, { id: fileVersions.entries[0].id, type: "file_version" as PromoteFileVersionRequestBodyArgTypeField } satisfies PromoteFileVersionRequestBodyArg)
const fileRestored: any = await client.files.getFileById(file.id);
if (!(fileRestored.name == oldName)) {
throw "Assertion failed";
}
if (!(fileRestored.size == 10)) {
throw "Assertion failed";
}
const fileVersionsRestored: any = await client.fileVersions.getFileVersions(file.id);
await client.fileVersions.deleteFileVersionById(file.id, fileVersionsRestored.entries[0].id)
await client.fileVersions.getFileVersions(file.id)
await client.files.deleteFileById(file.id)
});
export {};