@veroxm/sdk 0.2.0 → 0.3.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/CHANGELOG.md CHANGED
@@ -1,5 +1,16 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.3.0
4
+
5
+ - `app.functions` (call functions with an HTTP trigger), `app.tasks` (process tasks for end users and server keys)
6
+ and `app.processes` (start runs and read them, server keys).
7
+ - Types generated from the API's OpenAPI document (`/public/v2/openapi.json`): `MediaItem`, `Account`,
8
+ `AuthUser` and the rest are the API's own schemas, exported as `Schemas`, with `ApiPaths` and `ApiOperations`.
9
+ `MediaItem` gains the WebP addresses, tags, focal point and hotspots the API already sent.
10
+ - `ExperienceDecision.reason` includes `'holdout'` (a visitor in the impact holdout): show your default.
11
+ - Fixed: for the API framework's own errors (a malformed id, a body that fails validation),
12
+ `VeroXMApiError.message` is the reason, such as "Validation failed (numeric string is expected)", not "Bad Request".
13
+
3
14
  ## 0.2.0
4
15
 
5
16
  - End-user accounts on `app.auth`: `signUp`, `verifyEmail`, `resendVerification`, `signInWithEmail`,
package/README.md CHANGED
@@ -239,7 +239,7 @@ and/or a JSON config.
239
239
 
240
240
  ```ts
241
241
  const d = await tracker.getExperience<HeroEntry, { cta?: string }>('home.hero');
242
- if (d.reason === 'none') renderDefaultHero();
242
+ if (d.reason === 'none' || d.reason === 'holdout') renderDefaultHero();
243
243
  else renderHero(d.content ?? defaultHero, d.config?.cta);
244
244
 
245
245
  // A config-only experience doubles as a feature flag:
@@ -251,6 +251,8 @@ const stop = tracker.subscribe(() => rerenderExperiences());
251
251
 
252
252
  - **Never throws.** Errors and timeouts (`timeoutMs`, default 1500) resolve
253
253
  to `reason: 'none'` with `error` set -- show your default.
254
+ - **Holdout:** a visitor in the project's impact holdout gets
255
+ `reason: 'holdout'` -- show your default for that too.
254
256
  - **Consent:** before analytics consent it resolves to `'none'` with
255
257
  `waitingForConsent: true` and makes no request (a sticky assignment needs
256
258
  a stored visitor id, and the server records an exposure). Segment
@@ -281,6 +283,46 @@ read; otherwise (and to fill a short list) you get the most-read entries.
281
283
  What the visitor already read is left out of personal results. Never
282
284
  throws: an error returns an empty list with `error` set.
283
285
 
286
+ ## Functions, tasks and processes
287
+
288
+ ```ts
289
+ // A function with an HTTP trigger (Developer > Functions). JSON comes back parsed,
290
+ // text as a string, and null when the function returned nothing.
291
+ const quote = await app.functions.call<{ total: number }>('price-quote', { items: [1, 2] });
292
+ const hello = await app.functions.get<string>('greet', { name: 'Amira' });
293
+
294
+ // Process tasks: a signed-in person's own, or any of the project's with a server key.
295
+ const tasks = await app.tasks.list(); // open ones; { status, entryId } to filter
296
+ const task = await app.tasks.get(tasks[0].id); // task.record: the entry's current values
297
+ await app.tasks.complete(task.id, 'approve', { note: 'Looks good' });
298
+
299
+ // Processes, from your server (a key with every ability).
300
+ const { runId } = await app.processes.start('onboarding', entryId, { plan: 'gold' });
301
+ const run = await app.processes.run(runId); // run.activity: what each step did
302
+ ```
303
+
304
+ A function that fails rejects with `VeroXMApiError` whose `code` is
305
+ `function_error` (the error is in the call log), `function_timeout`,
306
+ `function_limit` (the month's limit is used up) or `functions_disabled`.
307
+ A process that can't start rejects with a 409 and `code` `already_running`,
308
+ `already_ran`, `not_live` or `no_entry`.
309
+
310
+ ## Types from the API
311
+
312
+ The SDK's types are generated from the public API's OpenAPI document, which
313
+ the API serves at `/public/v2/openapi.json`, so they can't drift from what it
314
+ sends. They're exported too:
315
+
316
+ ```ts
317
+ import type { Schemas, ApiPaths, ApiOperations } from '@veroxm/sdk';
318
+
319
+ type Task = Schemas['Task'];
320
+ type Decision = Schemas['Decision'];
321
+ ```
322
+
323
+ `ApiPaths` works with typed fetch helpers such as `openapi-fetch` if you
324
+ call a route the SDK doesn't wrap.
325
+
284
326
  ## Errors
285
327
 
286
328
  Every non-2xx response throws `VeroXMApiError` (`.status`, `.body`, a
package/dist/auth.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import type { AuthUser, TokenStorage } from './token-storage.js';
2
+ import type { Schemas } from './schemas.js';
2
3
  /** A code from an email, with the address; or the token from the email's link. */
3
4
  export type EmailCode = {
4
5
  email: string;
@@ -6,19 +7,13 @@ export type EmailCode = {
6
7
  } | {
7
8
  token: string;
8
9
  };
9
- /** The signed-in person's account, from `getAccount()`. */
10
- export interface Account extends AuthUser {
11
- abilities: string[];
12
- hasPassword: boolean;
13
- /** How they sign in: `password`, a social provider's key such as `google`, or `identity_provider`. */
14
- signInMethods: string[];
15
- }
10
+ /**
11
+ * The signed-in person's account, from `getAccount()`. `signInMethods`: `password`, a social provider's key such as
12
+ * `google`, or `identity_provider`.
13
+ */
14
+ export type Account = Schemas['Account'];
16
15
  /** A social sign-in option to show a button for. */
17
- export interface SocialProvider {
18
- key: string;
19
- kind: 'google' | 'microsoft' | 'apple' | 'oidc';
20
- name: string;
21
- }
16
+ export type SocialProvider = Schemas['SocialProvider'];
22
17
  /** What `startSocialSignIn` returns: send the person to `url`, keep the rest until they come back. */
23
18
  export interface SocialSignInStart {
24
19
  url: string;
package/dist/client.d.ts CHANGED
@@ -2,6 +2,9 @@ import { type RawRequestOptions } from './http.js';
2
2
  import { VeroXMAuth, type PendingStore } from './auth.js';
3
3
  import { ContentResource } from './content.js';
4
4
  import { MediaResource } from './media.js';
5
+ import { FunctionsResource } from './functions.js';
6
+ import { TasksResource } from './tasks.js';
7
+ import { ProcessesResource } from './processes.js';
5
8
  import { type TokenStorage } from './token-storage.js';
6
9
  export interface VeroXMAppOptions {
7
10
  /** Your VeroXM deployment's origin, e.g. "https://cms.example.com" -- no trailing /public/... needed, VeroXMApp builds that itself. */
@@ -15,11 +18,17 @@ export interface VeroXMAppOptions {
15
18
  }
16
19
  /**
17
20
  * The SDK's entry point -- one `initializeApp()` call, Firebase-style,
18
- * then `app.auth`, `app.content(slug)`, and `app.media()` for everything
19
- * else. See this package's README for a full quickstart.
21
+ * then `app.auth`, `app.content(slug)`, `app.media()`, `app.functions`,
22
+ * `app.tasks` and `app.processes` for everything else. See this package's README for a full quickstart.
20
23
  */
21
24
  export declare class VeroXMApp {
22
25
  readonly auth: VeroXMAuth;
26
+ /** Project functions with an HTTP trigger: `app.functions.call('quote', {items})`. */
27
+ readonly functions: FunctionsResource;
28
+ /** Process tasks: an end user's own, or any of the project's with a server key. */
29
+ readonly tasks: TasksResource;
30
+ /** Start processes and read their runs (server keys). */
31
+ readonly processes: ProcessesResource;
23
32
  private readonly apiBase;
24
33
  private constructor();
25
34
  static initializeApp(options: VeroXMAppOptions): VeroXMApp;
package/dist/client.js CHANGED
@@ -3,6 +3,9 @@ import { VeroXMApiError } from './errors.js';
3
3
  import { VeroXMAuth } from './auth.js';
4
4
  import { ContentResource } from './content.js';
5
5
  import { MediaResource } from './media.js';
6
+ import { FunctionsResource } from './functions.js';
7
+ import { TasksResource } from './tasks.js';
8
+ import { ProcessesResource } from './processes.js';
6
9
  import { MemoryTokenStorage, BrowserLocalStorageTokenStorage } from './token-storage.js';
7
10
  function defaultTokenStorage(projectId) {
8
11
  if (typeof window !== 'undefined' && typeof window.localStorage !== 'undefined') {
@@ -12,13 +15,16 @@ function defaultTokenStorage(projectId) {
12
15
  }
13
16
  /**
14
17
  * The SDK's entry point -- one `initializeApp()` call, Firebase-style,
15
- * then `app.auth`, `app.content(slug)`, and `app.media()` for everything
16
- * else. See this package's README for a full quickstart.
18
+ * then `app.auth`, `app.content(slug)`, `app.media()`, `app.functions`,
19
+ * `app.tasks` and `app.processes` for everything else. See this package's README for a full quickstart.
17
20
  */
18
21
  export class VeroXMApp {
19
22
  constructor(apiBase, tokenStorage, pendingStore) {
20
23
  this.apiBase = apiBase;
21
24
  this.auth = new VeroXMAuth(apiBase, tokenStorage, pendingStore);
25
+ this.functions = new FunctionsResource(this);
26
+ this.tasks = new TasksResource(this);
27
+ this.processes = new ProcessesResource(this);
22
28
  }
23
29
  static initializeApp(options) {
24
30
  const base = options.baseUrl.replace(/\/$/, '');
package/dist/errors.js CHANGED
@@ -19,6 +19,12 @@ export class VeroXMApiError extends Error {
19
19
  static messageFrom(status, body) {
20
20
  if (body && typeof body === 'object') {
21
21
  const b = body;
22
+ // The framework's own shape names the status in `error` ("Bad Request") and says what's wrong in `message`.
23
+ const framework = typeof b.statusCode === 'number';
24
+ if (framework && typeof b.message === 'string')
25
+ return b.message;
26
+ if (framework && Array.isArray(b.message))
27
+ return b.message.join(', ');
22
28
  if (typeof b.error === 'string')
23
29
  return b.error;
24
30
  if (typeof b.message === 'string')
@@ -0,0 +1,21 @@
1
+ import type { VeroXMApp } from './client.js';
2
+ type Query = Record<string, string | number | boolean | undefined>;
3
+ /**
4
+ * Project functions with an HTTP trigger (Developer > Functions). Who may call one is the function's own setting:
5
+ * anyone with one of the project's keys, signed-in end users, or server keys only.
6
+ *
7
+ * Answers come back as the function returned them: JSON parsed, text as a string, and `null` when it returned
8
+ * nothing. A failure rejects with VeroXMApiError: `code` is "function_error" (500, the error is in the call log),
9
+ * "function_timeout" (504), "function_limit" (429, the month's limit is used up) or "functions_disabled" (503).
10
+ */
11
+ export declare class FunctionsResource {
12
+ private readonly app;
13
+ constructor(app: VeroXMApp);
14
+ /** POST /functions/:name with `body` (an object or array) as JSON; the function reads it as `request.body`. */
15
+ call<T = unknown>(name: string, body?: Record<string, unknown> | unknown[], options?: {
16
+ 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>;
20
+ }
21
+ export {};
@@ -0,0 +1,24 @@
1
+ /**
2
+ * Project functions with an HTTP trigger (Developer > Functions). Who may call one is the function's own setting:
3
+ * anyone with one of the project's keys, signed-in end users, or server keys only.
4
+ *
5
+ * Answers come back as the function returned them: JSON parsed, text as a string, and `null` when it returned
6
+ * nothing. A failure rejects with VeroXMApiError: `code` is "function_error" (500, the error is in the call log),
7
+ * "function_timeout" (504), "function_limit" (429, the month's limit is used up) or "functions_disabled" (503).
8
+ */
9
+ export class FunctionsResource {
10
+ constructor(app) {
11
+ this.app = app;
12
+ }
13
+ /** POST /functions/:name with `body` (an object or array) as JSON; the function reads it as `request.body`. */
14
+ async call(name, body, options = {}) {
15
+ return (await this.app.request({ method: 'POST', path: path(name), json: body, query: options.query }));
16
+ }
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 }));
20
+ }
21
+ }
22
+ function path(name) {
23
+ return `/functions/${encodeURIComponent(name)}`;
24
+ }