@veroxm/sdk 1.9.0 → 1.11.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/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
@@ -24,22 +31,25 @@ export class ContentResource {
24
31
  limit: options.limit,
25
32
  offset: options.offset,
26
33
  sort: options.sort,
34
+ q: options.q,
27
35
  state: options.state,
28
36
  count: options.count ? '1' : undefined,
29
37
  timestamps: options.timestamps ? '1' : undefined,
30
38
  locale: options.locale,
31
39
  ...callerQuery(options),
32
40
  },
41
+ ...control(options),
33
42
  });
34
43
  }
35
44
  /** Same filters as list(), sent as a JSON body -- use this for `where`/`whereRelation`, which are awkward to express as query-string params. */
36
45
  async search(options) {
37
- const { userId, ctx, ...body } = options;
46
+ const { userId, ctx, signal, timeoutMs, ...body } = options;
38
47
  return this.app.request({
39
48
  method: 'POST',
40
49
  path: `${this.root()}/search`,
41
50
  query: callerQuery({ userId, ctx }),
42
51
  json: body,
52
+ ...control({ signal, timeoutMs }),
43
53
  });
44
54
  }
45
55
  /** A typed shortcut for search({...options, count: true}) -- the API returns a bare number for a counting query, not a list. */
@@ -64,6 +74,7 @@ export class ContentResource {
64
74
  return this.app.request({
65
75
  path: `${this.root()}/${encodeURIComponent(String(id))}`,
66
76
  query: { timestamps: options.timestamps ? '1' : undefined, ...callerQuery(options) },
77
+ ...control(options),
67
78
  });
68
79
  }
69
80
  /**
package/dist/embed.d.ts CHANGED
@@ -50,6 +50,19 @@ export interface FormEmbedOptions {
50
50
  endingId: string | null;
51
51
  }) => void;
52
52
  onClose?: () => void;
53
+ /**
54
+ * Records the send as `$form_submitted` (usable as an experience's goal): your @veroxm/sdk tracker. The script-tag
55
+ * embed uses the VeroXM script tag's tracker (window.veroxm) when the page has it. Pass `false` to not record it.
56
+ * @since 1.10.0
57
+ */
58
+ tracker?: FormEmbedTracker | false;
59
+ }
60
+ /** The part of a VeroxmTracker the embed uses (structural: this file has no imports). */
61
+ export interface FormEmbedTracker {
62
+ formSubmitted(form: string, submission: {
63
+ responseId?: string;
64
+ endingId?: string | null;
65
+ }): void;
53
66
  }
54
67
  export interface FormEmbed {
55
68
  readonly iframe: HTMLIFrameElement;
package/dist/embed.js CHANGED
@@ -32,6 +32,15 @@ function el(tag, className) {
32
32
  e.className = className;
33
33
  return e;
34
34
  }
35
+ /** The tracker a send is recorded with: the option, else the VeroXM script tag's (window.veroxm.tracker), else none. */
36
+ function submitTracker(options) {
37
+ if (options.tracker === false)
38
+ return null;
39
+ if (options.tracker)
40
+ return options.tracker;
41
+ const global = window.veroxm?.tracker;
42
+ return global && typeof global.formSubmitted === 'function' ? global : null;
43
+ }
35
44
  function resolve(target) {
36
45
  if (!target)
37
46
  return null;
@@ -177,6 +186,12 @@ export function embedForm(options) {
177
186
  options.onStarted?.();
178
187
  else if (m.type === 'submitted' && m.responseId) {
179
188
  options.onSubmit?.({ responseId: m.responseId, endingId: m.endingId ?? null });
189
+ try {
190
+ submitTracker(options)?.formSubmitted(options.form, { responseId: m.responseId, endingId: m.endingId ?? null });
191
+ }
192
+ catch {
193
+ // tracking never breaks the form
194
+ }
180
195
  if (mode === 'toast')
181
196
  storageSet(dismissKey, String(Date.now() + 3650 * 86400000));
182
197
  if (options.autoCloseSeconds !== undefined && mode !== 'inline') {
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) {