@veroxm/sdk 1.8.0 → 1.10.0

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.
package/dist/client.js CHANGED
@@ -1,4 +1,4 @@
1
- import { rawRequest } from './http.js';
1
+ import { checkRetries, checkTimeout, rawRequest } from './http.js';
2
2
  import { VeroXMApiError } from './errors.js';
3
3
  import { VeroXMAuth } from './auth.js';
4
4
  import { ContentResource } from './content.js';
@@ -20,8 +20,10 @@ function defaultTokenStorage(projectId) {
20
20
  * `app.tasks` and `app.processes` for everything else. See this package's README for a full quickstart.
21
21
  */
22
22
  export class VeroXMApp {
23
- constructor(apiBase, tokenStorage, pendingStore) {
23
+ constructor(apiBase, tokenStorage, pendingStore, control = {}) {
24
24
  this.apiBase = apiBase;
25
+ this.timeoutMs = control.timeoutMs;
26
+ this.retries = control.retries;
25
27
  this.auth = new VeroXMAuth(apiBase, tokenStorage, pendingStore);
26
28
  this.functions = new FunctionsResource(this);
27
29
  this.tasks = new TasksResource(this);
@@ -29,9 +31,14 @@ export class VeroXMApp {
29
31
  this.forms = new FormsResource(this);
30
32
  }
31
33
  static initializeApp(options) {
34
+ checkTimeout(options.timeoutMs);
35
+ checkRetries(options.retries);
32
36
  const base = options.baseUrl.replace(/\/$/, '');
33
37
  const apiBase = `${base}/public/v2/projects/${options.projectId}`;
34
- return new VeroXMApp(apiBase, options.tokenStorage ?? defaultTokenStorage(options.projectId), options.pendingStore);
38
+ return new VeroXMApp(apiBase, options.tokenStorage ?? defaultTokenStorage(options.projectId), options.pendingStore, {
39
+ timeoutMs: options.timeoutMs,
40
+ retries: options.retries,
41
+ });
35
42
  }
36
43
  /** Typed access to one content collection's entries (list/get/create/update/delete/search). */
37
44
  content(collectionSlug) {
@@ -52,14 +59,20 @@ export class VeroXMApp {
52
59
  * resources (`content`, `media`, `functions`, `tasks`, `processes`), or `ApiPaths` with a typed fetch helper.
53
60
  */
54
61
  async request(opts) {
62
+ // The app's defaults, unless the call set its own. Uploads (form bodies) keep no time limit unless the call sets one.
63
+ const sent = {
64
+ ...opts,
65
+ timeoutMs: opts.timeoutMs ?? (opts.form ? undefined : this.timeoutMs),
66
+ retries: opts.retries ?? this.retries,
67
+ };
55
68
  const token = await this.auth.getAccessToken();
56
69
  try {
57
- return await rawRequest(this.apiBase, withAuth(opts, token));
70
+ return await rawRequest(this.apiBase, withAuth(sent, token));
58
71
  }
59
72
  catch (e) {
60
73
  if (e instanceof VeroXMApiError && e.status === 401 && this.auth.canRefresh()) {
61
74
  const retryToken = await this.auth.forceRefreshAndGetToken();
62
- return rawRequest(this.apiBase, withAuth(opts, retryToken));
75
+ return rawRequest(this.apiBase, withAuth(sent, retryToken));
63
76
  }
64
77
  throw e;
65
78
  }
package/dist/content.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import type { VeroXMApp } from './client.js';
2
- export interface ListOptions {
2
+ import type { RequestControl } from './http.js';
3
+ export interface ListOptions extends RequestControl {
3
4
  /** Field-value filter -- an array of `{field: value}` clauses (AND'ed together), each value either a scalar or an operator object like `{like: '...'}`, `{gte: '...'}`, `{in: 'a,b,c'}`, etc. See the project's SDK Docs tab for the full operator list. */
4
5
  where?: unknown;
5
6
  whereRelation?: Record<string, Record<string, string>>;
@@ -67,7 +68,7 @@ export declare class ContentResource<T = Record<string, unknown>> {
67
68
  */
68
69
  get(id: number | string, options?: {
69
70
  timestamps?: boolean;
70
- } & Pick<ListOptions, 'userId' | 'ctx'>): Promise<T>;
71
+ } & Pick<ListOptions, 'userId' | 'ctx'> & RequestControl): Promise<T>;
71
72
  /**
72
73
  * Fields are sent flat (no wrapper). Pass `draft: true` to create without publishing; omit it (or pass false) to
73
74
  * publish immediately. A database content model that allows creating entries takes its writable fields (and `id` when
package/dist/content.js CHANGED
@@ -2,6 +2,13 @@
2
2
  function callerQuery(options) {
3
3
  return { userId: options.userId, ...Object.fromEntries(Object.entries(options.ctx ?? {}).map(([k, v]) => [`ctx.${k}`, v])) };
4
4
  }
5
+ /** Only the keys that were given, so a request without them is exactly what it was before they existed. */
6
+ function control(options) {
7
+ return {
8
+ ...(options.signal ? { signal: options.signal } : {}),
9
+ ...(options.timeoutMs !== undefined ? { timeoutMs: options.timeoutMs } : {}),
10
+ };
11
+ }
5
12
  /**
6
13
  * Typed access to one content collection's entries. `T` is whatever
7
14
  * shape you expect back (the API's actual response is a plain object per
@@ -30,16 +37,18 @@ export class ContentResource {
30
37
  locale: options.locale,
31
38
  ...callerQuery(options),
32
39
  },
40
+ ...control(options),
33
41
  });
34
42
  }
35
43
  /** Same filters as list(), sent as a JSON body -- use this for `where`/`whereRelation`, which are awkward to express as query-string params. */
36
44
  async search(options) {
37
- const { userId, ctx, ...body } = options;
45
+ const { userId, ctx, signal, timeoutMs, ...body } = options;
38
46
  return this.app.request({
39
47
  method: 'POST',
40
48
  path: `${this.root()}/search`,
41
49
  query: callerQuery({ userId, ctx }),
42
50
  json: body,
51
+ ...control({ signal, timeoutMs }),
43
52
  });
44
53
  }
45
54
  /** A typed shortcut for search({...options, count: true}) -- the API returns a bare number for a counting query, not a list. */
@@ -64,6 +73,7 @@ export class ContentResource {
64
73
  return this.app.request({
65
74
  path: `${this.root()}/${encodeURIComponent(String(id))}`,
66
75
  query: { timestamps: options.timestamps ? '1' : undefined, ...callerQuery(options) },
76
+ ...control(options),
67
77
  });
68
78
  }
69
79
  /**
package/dist/errors.d.ts CHANGED
@@ -38,6 +38,16 @@ export declare class VeroXMTwoStepRequiredError extends VeroXMApiError {
38
38
  }
39
39
  /** The error for an API answer: a two-step sign-in challenge, or a plain VeroXMApiError. */
40
40
  export declare function apiError(status: number, body: unknown): VeroXMApiError;
41
+ /**
42
+ * Thrown when a request set to give up after `timeoutMs` (on the app or the call) got no complete answer in time. The
43
+ * API may still have received it. Only GET and HEAD requests are retried after a timeout (with `retries`).
44
+ */
45
+ export declare class VeroXMTimeoutError extends Error {
46
+ /** Always `timeout`, so code can branch on `code` across this and VeroXMApiError. */
47
+ readonly code: "timeout";
48
+ readonly timeoutMs: number;
49
+ constructor(timeoutMs: number);
50
+ }
41
51
  /** Thrown when a request needs an authenticated client but no credentials have been provided yet. */
42
52
  export declare class VeroXMNotAuthenticatedError extends Error {
43
53
  constructor();
package/dist/errors.js CHANGED
@@ -60,6 +60,19 @@ export function apiError(status, body) {
60
60
  }
61
61
  return new VeroXMApiError(status, body);
62
62
  }
63
+ /**
64
+ * Thrown when a request set to give up after `timeoutMs` (on the app or the call) got no complete answer in time. The
65
+ * API may still have received it. Only GET and HEAD requests are retried after a timeout (with `retries`).
66
+ */
67
+ export class VeroXMTimeoutError extends Error {
68
+ constructor(timeoutMs) {
69
+ super(`VeroXM didn't answer within ${timeoutMs} ms`);
70
+ /** Always `timeout`, so code can branch on `code` across this and VeroXMApiError. */
71
+ this.code = 'timeout';
72
+ this.name = 'VeroXMTimeoutError';
73
+ this.timeoutMs = timeoutMs;
74
+ }
75
+ }
63
76
  /** Thrown when a request needs an authenticated client but no credentials have been provided yet. */
64
77
  export class VeroXMNotAuthenticatedError extends Error {
65
78
  constructor() {
@@ -1,4 +1,5 @@
1
1
  import type { VeroXMApp } from './client.js';
2
+ import type { RequestControl } from './http.js';
2
3
  type Query = Record<string, string | number | boolean | undefined>;
3
4
  /**
4
5
  * Project functions with an HTTP trigger (Developer > Functions). Who may call one is the function's own setting:
@@ -14,8 +15,8 @@ export declare class FunctionsResource {
14
15
  /** POST /functions/:name with `body` (an object or array) as JSON; the function reads it as `request.body`. */
15
16
  call<T = unknown>(name: string, body?: Record<string, unknown> | unknown[], options?: {
16
17
  query?: Query;
17
- }): Promise<T>;
18
- /** GET /functions/:name (the function reads `query` as `request.query`). */
19
- get<T = unknown>(name: string, query?: Query): Promise<T>;
18
+ } & RequestControl): Promise<T>;
19
+ /** GET /functions/:name (the function reads `query` as `request.query`). Retried with the app's `retries`. */
20
+ get<T = unknown>(name: string, query?: Query, options?: RequestControl): Promise<T>;
20
21
  }
21
22
  export {};
package/dist/functions.js CHANGED
@@ -12,11 +12,12 @@ export class FunctionsResource {
12
12
  }
13
13
  /** POST /functions/:name with `body` (an object or array) as JSON; the function reads it as `request.body`. */
14
14
  async call(name, body, options = {}) {
15
- return (await this.app.request({ method: 'POST', path: path(name), json: body, query: options.query }));
15
+ const { query, signal, timeoutMs } = options;
16
+ return (await this.app.request({ method: 'POST', path: path(name), json: body, query, signal, timeoutMs }));
16
17
  }
17
- /** GET /functions/:name (the function reads `query` as `request.query`). */
18
- async get(name, query = {}) {
19
- return (await this.app.request({ path: path(name), query }));
18
+ /** GET /functions/:name (the function reads `query` as `request.query`). Retried with the app's `retries`. */
19
+ async get(name, query = {}, options = {}) {
20
+ return (await this.app.request({ path: path(name), query, signal: options.signal, timeoutMs: options.timeoutMs }));
20
21
  }
21
22
  }
22
23
  function path(name) {