@unofficialbox/box-open-sdk 0.6.1 → 0.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,6 +2,7 @@
2
2
  // Code generated by box-gantry (vendored from runtimes/typescript/gantryruntime/src/errors.ts). DO NOT EDIT.
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
4
  exports.BoxApiError = void 0;
5
+ exports.isPreconditionFailed = isPreconditionFailed;
5
6
  exports.apiErrorFromResponse = apiErrorFromResponse;
6
7
  /**
7
8
  * A runtime error: a failed request, auth acquisition, or body decode. The
@@ -31,6 +32,12 @@ class BoxApiError extends Error {
31
32
  }
32
33
  }
33
34
  exports.BoxApiError = BoxApiError;
35
+ /** Whether `err` is Box's 412 Precondition Failed: an `If-Match` write whose
36
+ * etag no longer matches, because the item changed since it was read. Re-read
37
+ * the item and retry with its new etag. */
38
+ function isPreconditionFailed(err) {
39
+ return err instanceof BoxApiError && err.status === 412;
40
+ }
34
41
  /** The error for a non-2xx response: `Client.fetch` throws it once the retry
35
42
  * budget and the single 401 refresh are spent, so a caller never mistakes an
36
43
  * error body for a result. Box's error fields are read from the body when it
@@ -65,6 +65,19 @@ class FilesManager {
65
65
  const data = runtime.responseBytes(resp);
66
66
  return JSON.parse(new TextDecoder().decode(data));
67
67
  }
68
+ /** `get` with `If-None-Match` set to `etag`: resolves `undefined` when
69
+ * the item is unchanged since `etag` (Box answers 304 Not Modified). */
70
+ async getIfModified(fileId, etag, opts) {
71
+ try {
72
+ return await this.get(fileId, { ...opts, "if-none-match": etag });
73
+ }
74
+ catch (err) {
75
+ if (err instanceof runtime.BoxApiError && err.status === 304) {
76
+ return undefined;
77
+ }
78
+ throw err;
79
+ }
80
+ }
68
81
  async update(fileId, body, opts) {
69
82
  let url = this.session.baseUrl("api");
70
83
  url += "/files";
@@ -74,6 +74,19 @@ class FoldersManager {
74
74
  const data = runtime.responseBytes(resp);
75
75
  return JSON.parse(new TextDecoder().decode(data));
76
76
  }
77
+ /** `get` with `If-None-Match` set to `etag`: resolves `undefined` when
78
+ * the item is unchanged since `etag` (Box answers 304 Not Modified). */
79
+ async getIfModified(folderId, etag, opts) {
80
+ try {
81
+ return await this.get(folderId, { ...opts, "if-none-match": etag });
82
+ }
83
+ catch (err) {
84
+ if (err instanceof runtime.BoxApiError && err.status === 304) {
85
+ return undefined;
86
+ }
87
+ throw err;
88
+ }
89
+ }
77
90
  async update(folderId, body, opts) {
78
91
  let url = this.session.baseUrl("api");
79
92
  url += "/folders";
@@ -59,6 +59,19 @@ class SharedLinksFilesManager {
59
59
  const data = runtime.responseBytes(resp);
60
60
  return JSON.parse(new TextDecoder().decode(data));
61
61
  }
62
+ /** `getSharedItems` with `If-None-Match` set to `etag`: resolves `undefined` when
63
+ * the item is unchanged since `etag` (Box answers 304 Not Modified). */
64
+ async getSharedItemsIfModified(boxapi, etag, opts) {
65
+ try {
66
+ return await this.getSharedItems(boxapi, { ...opts, "if-none-match": etag });
67
+ }
68
+ catch (err) {
69
+ if (err instanceof runtime.BoxApiError && err.status === 304) {
70
+ return undefined;
71
+ }
72
+ throw err;
73
+ }
74
+ }
62
75
  async getFileGetSharedLink(fileId, fields) {
63
76
  let url = this.session.baseUrl("api");
64
77
  url += "/files";
@@ -59,6 +59,19 @@ class SharedLinksFoldersManager {
59
59
  const data = runtime.responseBytes(resp);
60
60
  return JSON.parse(new TextDecoder().decode(data));
61
61
  }
62
+ /** `getSharedItemsFolders` with `If-None-Match` set to `etag`: resolves `undefined` when
63
+ * the item is unchanged since `etag` (Box answers 304 Not Modified). */
64
+ async getSharedItemsIfModifiedFolders(boxapi, etag, opts) {
65
+ try {
66
+ return await this.getSharedItemsFolders(boxapi, { ...opts, "if-none-match": etag });
67
+ }
68
+ catch (err) {
69
+ if (err instanceof runtime.BoxApiError && err.status === 304) {
70
+ return undefined;
71
+ }
72
+ throw err;
73
+ }
74
+ }
62
75
  async getFolderGetSharedLink(folderId, fields) {
63
76
  let url = this.session.baseUrl("api");
64
77
  url += "/folders";
@@ -59,6 +59,19 @@ class SharedLinksWebLinksManager {
59
59
  const data = runtime.responseBytes(resp);
60
60
  return JSON.parse(new TextDecoder().decode(data));
61
61
  }
62
+ /** `getSharedItemsWebLinks` with `If-None-Match` set to `etag`: resolves `undefined` when
63
+ * the item is unchanged since `etag` (Box answers 304 Not Modified). */
64
+ async getSharedItemsIfModifiedWebLinks(boxapi, etag, opts) {
65
+ try {
66
+ return await this.getSharedItemsWebLinks(boxapi, { ...opts, "if-none-match": etag });
67
+ }
68
+ catch (err) {
69
+ if (err instanceof runtime.BoxApiError && err.status === 304) {
70
+ return undefined;
71
+ }
72
+ throw err;
73
+ }
74
+ }
62
75
  async getWebLinkGetSharedLink(webLinkId, fields) {
63
76
  let url = this.session.baseUrl("api");
64
77
  url += "/web_links";
@@ -15,7 +15,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
15
15
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
16
16
  };
17
17
  Object.defineProperty(exports, "__esModule", { value: true });
18
- exports.Client = exports.Stream = exports.Response = exports.Request = exports.BoxApiError = void 0;
18
+ exports.Client = exports.Stream = exports.Response = exports.Request = exports.isPreconditionFailed = exports.BoxApiError = void 0;
19
19
  exports.withHeader = withHeader;
20
20
  exports.withQuery = withQuery;
21
21
  exports.withJsonBody = withJsonBody;
@@ -29,6 +29,7 @@ exports.statusCode = statusCode;
29
29
  const errors_js_1 = require("./errors.js");
30
30
  var errors_js_2 = require("./errors.js");
31
31
  Object.defineProperty(exports, "BoxApiError", { enumerable: true, get: function () { return errors_js_2.BoxApiError; } });
32
+ Object.defineProperty(exports, "isPreconditionFailed", { enumerable: true, get: function () { return errors_js_2.isPreconditionFailed; } });
32
33
  __exportStar(require("./auth.js"), exports);
33
34
  /** The default base URLs for the D-106 URL classes. */
34
35
  const DEFAULT_BASE_URLS = {
@@ -26,6 +26,12 @@ export class BoxApiError extends Error {
26
26
  this.body = options.body;
27
27
  }
28
28
  }
29
+ /** Whether `err` is Box's 412 Precondition Failed: an `If-Match` write whose
30
+ * etag no longer matches, because the item changed since it was read. Re-read
31
+ * the item and retry with its new etag. */
32
+ export function isPreconditionFailed(err) {
33
+ return err instanceof BoxApiError && err.status === 412;
34
+ }
29
35
  /** The error for a non-2xx response: `Client.fetch` throws it once the retry
30
36
  * budget and the single 401 refresh are spent, so a caller never mistakes an
31
37
  * error body for a result. Box's error fields are read from the body when it
@@ -29,6 +29,19 @@ export class FilesManager {
29
29
  const data = runtime.responseBytes(resp);
30
30
  return JSON.parse(new TextDecoder().decode(data));
31
31
  }
32
+ /** `get` with `If-None-Match` set to `etag`: resolves `undefined` when
33
+ * the item is unchanged since `etag` (Box answers 304 Not Modified). */
34
+ async getIfModified(fileId, etag, opts) {
35
+ try {
36
+ return await this.get(fileId, { ...opts, "if-none-match": etag });
37
+ }
38
+ catch (err) {
39
+ if (err instanceof runtime.BoxApiError && err.status === 304) {
40
+ return undefined;
41
+ }
42
+ throw err;
43
+ }
44
+ }
32
45
  async update(fileId, body, opts) {
33
46
  let url = this.session.baseUrl("api");
34
47
  url += "/files";
@@ -38,6 +38,19 @@ export class FoldersManager {
38
38
  const data = runtime.responseBytes(resp);
39
39
  return JSON.parse(new TextDecoder().decode(data));
40
40
  }
41
+ /** `get` with `If-None-Match` set to `etag`: resolves `undefined` when
42
+ * the item is unchanged since `etag` (Box answers 304 Not Modified). */
43
+ async getIfModified(folderId, etag, opts) {
44
+ try {
45
+ return await this.get(folderId, { ...opts, "if-none-match": etag });
46
+ }
47
+ catch (err) {
48
+ if (err instanceof runtime.BoxApiError && err.status === 304) {
49
+ return undefined;
50
+ }
51
+ throw err;
52
+ }
53
+ }
41
54
  async update(folderId, body, opts) {
42
55
  let url = this.session.baseUrl("api");
43
56
  url += "/folders";
@@ -23,6 +23,19 @@ export class SharedLinksFilesManager {
23
23
  const data = runtime.responseBytes(resp);
24
24
  return JSON.parse(new TextDecoder().decode(data));
25
25
  }
26
+ /** `getSharedItems` with `If-None-Match` set to `etag`: resolves `undefined` when
27
+ * the item is unchanged since `etag` (Box answers 304 Not Modified). */
28
+ async getSharedItemsIfModified(boxapi, etag, opts) {
29
+ try {
30
+ return await this.getSharedItems(boxapi, { ...opts, "if-none-match": etag });
31
+ }
32
+ catch (err) {
33
+ if (err instanceof runtime.BoxApiError && err.status === 304) {
34
+ return undefined;
35
+ }
36
+ throw err;
37
+ }
38
+ }
26
39
  async getFileGetSharedLink(fileId, fields) {
27
40
  let url = this.session.baseUrl("api");
28
41
  url += "/files";
@@ -23,6 +23,19 @@ export class SharedLinksFoldersManager {
23
23
  const data = runtime.responseBytes(resp);
24
24
  return JSON.parse(new TextDecoder().decode(data));
25
25
  }
26
+ /** `getSharedItemsFolders` with `If-None-Match` set to `etag`: resolves `undefined` when
27
+ * the item is unchanged since `etag` (Box answers 304 Not Modified). */
28
+ async getSharedItemsIfModifiedFolders(boxapi, etag, opts) {
29
+ try {
30
+ return await this.getSharedItemsFolders(boxapi, { ...opts, "if-none-match": etag });
31
+ }
32
+ catch (err) {
33
+ if (err instanceof runtime.BoxApiError && err.status === 304) {
34
+ return undefined;
35
+ }
36
+ throw err;
37
+ }
38
+ }
26
39
  async getFolderGetSharedLink(folderId, fields) {
27
40
  let url = this.session.baseUrl("api");
28
41
  url += "/folders";
@@ -23,6 +23,19 @@ export class SharedLinksWebLinksManager {
23
23
  const data = runtime.responseBytes(resp);
24
24
  return JSON.parse(new TextDecoder().decode(data));
25
25
  }
26
+ /** `getSharedItemsWebLinks` with `If-None-Match` set to `etag`: resolves `undefined` when
27
+ * the item is unchanged since `etag` (Box answers 304 Not Modified). */
28
+ async getSharedItemsIfModifiedWebLinks(boxapi, etag, opts) {
29
+ try {
30
+ return await this.getSharedItemsWebLinks(boxapi, { ...opts, "if-none-match": etag });
31
+ }
32
+ catch (err) {
33
+ if (err instanceof runtime.BoxApiError && err.status === 304) {
34
+ return undefined;
35
+ }
36
+ throw err;
37
+ }
38
+ }
26
39
  async getWebLinkGetSharedLink(webLinkId, fields) {
27
40
  let url = this.session.baseUrl("api");
28
41
  url += "/web_links";
@@ -1,6 +1,6 @@
1
1
  // Code generated by box-gantry (vendored from runtimes/typescript/gantryruntime/src/runtime.ts). DO NOT EDIT.
2
2
  import { BoxApiError, apiErrorFromResponse } from './errors.js';
3
- export { BoxApiError } from './errors.js';
3
+ export { BoxApiError, isPreconditionFailed } from './errors.js';
4
4
  export * from './auth.js';
5
5
  /** The default base URLs for the D-106 URL classes. */
6
6
  const DEFAULT_BASE_URLS = {
@@ -33,6 +33,10 @@ export declare class BoxApiError extends Error {
33
33
  readonly body?: Uint8Array;
34
34
  constructor(message: string, options?: BoxApiErrorOptions);
35
35
  }
36
+ /** Whether `err` is Box's 412 Precondition Failed: an `If-Match` write whose
37
+ * etag no longer matches, because the item changed since it was read. Re-read
38
+ * the item and retry with its new etag. */
39
+ export declare function isPreconditionFailed(err: unknown): err is BoxApiError;
36
40
  /** The error for a non-2xx response: `Client.fetch` throws it once the retry
37
41
  * budget and the single 401 refresh are spent, so a caller never mistakes an
38
42
  * error body for a result. Box's error fields are read from the body when it
@@ -32,6 +32,9 @@ export declare class FilesManager {
32
32
  private readonly session;
33
33
  constructor(session: runtime.Client);
34
34
  get(fileId: string, opts?: FilesManagerGetOptions): Promise<models.schemas.FileFull>;
35
+ /** `get` with `If-None-Match` set to `etag`: resolves `undefined` when
36
+ * the item is unchanged since `etag` (Box answers 304 Not Modified). */
37
+ getIfModified(fileId: string, etag: string, opts?: FilesManagerGetOptions): Promise<models.schemas.FileFull | undefined>;
35
38
  update(fileId: string, body: models.schemas.UpdateFileRequest, opts?: FilesManagerUpdateOptions): Promise<models.schemas.FileFull>;
36
39
  delete_(fileId: string, opts?: FilesManagerDeleteOptions): Promise<void>;
37
40
  copy(fileId: string, body: models.schemas.CopyFileRequest, opts?: FilesManagerCopyOptions): Promise<models.schemas.FileFull>;
@@ -44,6 +44,9 @@ export declare class FoldersManager {
44
44
  private readonly session;
45
45
  constructor(session: runtime.Client);
46
46
  get(folderId: string, opts?: FoldersManagerGetOptions): Promise<models.schemas.FolderFull>;
47
+ /** `get` with `If-None-Match` set to `etag`: resolves `undefined` when
48
+ * the item is unchanged since `etag` (Box answers 304 Not Modified). */
49
+ getIfModified(folderId: string, etag: string, opts?: FoldersManagerGetOptions): Promise<models.schemas.FolderFull | undefined>;
47
50
  update(folderId: string, body: models.schemas.UpdateFolderRequest, opts?: FoldersManagerUpdateOptions): Promise<models.schemas.FolderFull>;
48
51
  delete_(folderId: string, opts?: FoldersManagerDeleteOptions): Promise<void>;
49
52
  /** Iterate every `models.schemas.Item` across pages, threading the marker cursor. */
@@ -10,6 +10,9 @@ export declare class SharedLinksFilesManager {
10
10
  private readonly session;
11
11
  constructor(session: runtime.Client);
12
12
  getSharedItems(boxapi: string, opts?: SharedLinksFilesManagerGetSharedItemsOptions): Promise<models.schemas.FileFull>;
13
+ /** `getSharedItems` with `If-None-Match` set to `etag`: resolves `undefined` when
14
+ * the item is unchanged since `etag` (Box answers 304 Not Modified). */
15
+ getSharedItemsIfModified(boxapi: string, etag: string, opts?: SharedLinksFilesManagerGetSharedItemsOptions): Promise<models.schemas.FileFull | undefined>;
13
16
  getFileGetSharedLink(fileId: string, fields: string): Promise<models.schemas.FileFull>;
14
17
  updateFileAddSharedLink(fileId: string, fields: string, body: models.schemas.UpdateFileAddSharedLinkRequest): Promise<models.schemas.FileFull>;
15
18
  updateFileUpdateSharedLink(fileId: string, fields: string, body: models.schemas.UpdateFileAddSharedLinkRequest): Promise<models.schemas.FileFull>;
@@ -10,6 +10,9 @@ export declare class SharedLinksFoldersManager {
10
10
  private readonly session;
11
11
  constructor(session: runtime.Client);
12
12
  getSharedItemsFolders(boxapi: string, opts?: SharedLinksFoldersManagerGetSharedItemsFoldersOptions): Promise<models.schemas.FolderFull>;
13
+ /** `getSharedItemsFolders` with `If-None-Match` set to `etag`: resolves `undefined` when
14
+ * the item is unchanged since `etag` (Box answers 304 Not Modified). */
15
+ getSharedItemsIfModifiedFolders(boxapi: string, etag: string, opts?: SharedLinksFoldersManagerGetSharedItemsFoldersOptions): Promise<models.schemas.FolderFull | undefined>;
13
16
  getFolderGetSharedLink(folderId: string, fields: string): Promise<models.schemas.FolderFull>;
14
17
  updateFolderAddSharedLink(folderId: string, fields: string, body: models.schemas.UpdateFileAddSharedLinkRequest): Promise<models.schemas.FolderFull>;
15
18
  updateFolderUpdateSharedLink(folderId: string, fields: string, body: models.schemas.UpdateFolderUpdateSharedLinkRequest): Promise<models.schemas.FolderFull>;
@@ -10,6 +10,9 @@ export declare class SharedLinksWebLinksManager {
10
10
  private readonly session;
11
11
  constructor(session: runtime.Client);
12
12
  getSharedItemsWebLinks(boxapi: string, opts?: SharedLinksWebLinksManagerGetSharedItemsWebLinksOptions): Promise<models.schemas.WebLink>;
13
+ /** `getSharedItemsWebLinks` with `If-None-Match` set to `etag`: resolves `undefined` when
14
+ * the item is unchanged since `etag` (Box answers 304 Not Modified). */
15
+ getSharedItemsIfModifiedWebLinks(boxapi: string, etag: string, opts?: SharedLinksWebLinksManagerGetSharedItemsWebLinksOptions): Promise<models.schemas.WebLink | undefined>;
13
16
  getWebLinkGetSharedLink(webLinkId: string, fields: string): Promise<models.schemas.WebLink>;
14
17
  updateWebLinkAddSharedLink(webLinkId: string, fields: string, body: models.schemas.UpdateFileAddSharedLinkRequest): Promise<models.schemas.WebLink>;
15
18
  updateWebLinkUpdateSharedLink(webLinkId: string, fields: string, body: models.schemas.UpdateFileAddSharedLinkRequest): Promise<models.schemas.WebLink>;
@@ -1,5 +1,5 @@
1
1
  import type { Auth } from './auth.js';
2
- export { BoxApiError } from './errors.js';
2
+ export { BoxApiError, isPreconditionFailed } from './errors.js';
3
3
  export * from './auth.js';
4
4
  /** The runtime-owned HTTP request envelope, assembled by the `with*` builders
5
5
  * before `fetch` executes it. */
package/docs/errors.md CHANGED
@@ -26,28 +26,32 @@ Files and folders carry an `etag`. Send it back to stay in sync with Box
26
26
  (Box's [Ensure consistency](https://developer.box.com/guides/api-calls/ensure-consistency/)
27
27
  guide):
28
28
 
29
- - **Reads** — pass the stored etag as `if-none-match` on a file or folder
30
- `get` (or a shared-item lookup). Box answers `304 Not Modified` when
31
- nothing changed, and the SDK raises it as the error above with status
32
- `304`: keep your cached copy.
33
- - **Writes** — pass it as `if-match` on a file or folder update or delete,
34
- or a new file version upload. Box answers `412 Precondition Failed`
35
- (code `precondition_failed`) when the item changed since you read it:
36
- read it again, reapply your change, retry. `If-Match` can't block a move.
29
+ - **Reads** — a file or folder `get`, and each shared-item lookup, has an
30
+ `…IfModified` variant that takes the stored etag, sends it as
31
+ `If-None-Match`, and returns `undefined` when Box answers `304 Not Modified`:
32
+ keep your cached copy. (The plain method surfaces that 304 as the error
33
+ above if you set `if-none-match` on it yourself.)
34
+ - **Writes** — pass the etag as `'if-match'` on a file or folder update or
35
+ delete, or a new file version upload. Box answers `412 Precondition
36
+ Failed` (code `precondition_failed`) when the item changed since you read
37
+ it, and `runtime.isPreconditionFailed(err)` reports it: read it again,
38
+ reapply your change, retry. `If-Match` can't block a move.
37
39
 
38
40
  ```ts
39
- try {
40
- await client.files.get(fileId, { 'if-none-match': etag });
41
- } catch (err) {
42
- if (err instanceof runtime.BoxApiError && err.status === 304) {
43
- // unchanged since etag
44
- }
41
+ const file = await client.files.getIfModified(fileId, etag);
42
+ if (file === undefined) {
43
+ // unchanged since etag
45
44
  }
46
- try {
47
- await client.files.update(fileId, body, { 'if-match': etag });
48
- } catch (err) {
49
- if (err instanceof runtime.BoxApiError && err.status === 412) {
50
- // changed since etag: re-read and retry
45
+
46
+ // Re-read and retry: the update lands only on the version it was made from.
47
+ for (let attempt = 1; ; attempt++) {
48
+ try {
49
+ await client.files.update(fileId, body, { 'if-match': etag });
50
+ break;
51
+ } catch (err) {
52
+ if (!runtime.isPreconditionFailed(err) || attempt === 3) throw err;
53
+ const current = await client.files.get(fileId);
54
+ etag = current.etag ?? undefined; // reapply your change to current here
51
55
  }
52
56
  }
53
57
  ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@unofficialbox/box-open-sdk",
3
- "version": "0.6.1",
3
+ "version": "0.7.1",
4
4
  "description": "Box API client for TypeScript (open source, community, punk rock) — typed models, async managers, and a fetch runtime with retry, backoff, and token refresh.",
5
5
  "keywords": ["box", "box-api", "sdk", "api-client", "typescript", "unofficial"],
6
6
  "license": "MIT",