@mentra/cloud-client 3.4.0-dev.1088 → 3.4.0-dev.1090

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mentra/cloud-client",
3
- "version": "3.4.0-dev.1088",
3
+ "version": "3.4.0-dev.1090",
4
4
  "type": "module",
5
5
  "main": "./src/index.ts",
6
6
  "exports": {
@@ -12,7 +12,7 @@
12
12
  "test": "bun test"
13
13
  },
14
14
  "dependencies": {
15
- "@mentra/cloud-protocol": "3.4.0-dev.1088",
15
+ "@mentra/cloud-protocol": "3.4.0-dev.1090",
16
16
  "tweetnacl": "^1.0.3"
17
17
  },
18
18
  "devDependencies": {
package/src/index.ts CHANGED
@@ -87,7 +87,8 @@ export type {
87
87
  SupportStateInput,
88
88
  } from "./modules/core/core";
89
89
 
90
- // Workspace and Fleet desired-state calls (`cloud.core.workspaces`, `cloud.core.fleet.desiredState`).
90
+ // Workspace, Fleet desired-state and registry calls (`cloud.core.workspaces`, `cloud.core.fleet.desiredState`,
91
+ // `cloud.core.registry`). The registry's request and answer types come from `@mentra/cloud-protocol`.
91
92
  export {CoreRequestError, CoreResponseError, SessionChangedError} from "./modules/core/core";
92
93
  export type {AccountCallOptions} from "./modules/core/core";
93
94
  export type {
@@ -7,7 +7,8 @@
7
7
  * core never touches credentials itself.
8
8
  *
9
9
  * Today it exposes the unified report submission surface, the support
10
- * profile update and the Fleet report.
10
+ * profile update, the Fleet report, the workspace calls and the miniapp
11
+ * registry calls.
11
12
  *
12
13
  * Guardrail: this is device-facing only. It deliberately carries none of the
13
14
  * Dev Console / OEM Portal / store web UI surface; those are separate clients.
@@ -16,7 +17,14 @@
16
17
  * ("src/modules/core/core.ts").
17
18
  */
18
19
  import type { HttpClient } from "../../http";
19
- import type { FleetDesiredState, FleetReport } from "@mentra/cloud-protocol";
20
+ import type {
21
+ FleetDesiredState,
22
+ FleetReport,
23
+ RegistryRegisterRequest,
24
+ RegistryRegisterResponse,
25
+ RegistryStatusRequest,
26
+ RegistryStatusResponse,
27
+ } from "@mentra/cloud-protocol";
20
28
  import type {
21
29
  AcceptInvitationResult,
22
30
  DeclineInvitationResult,
@@ -26,6 +34,7 @@ import type {
26
34
  } from "./workspace-types";
27
35
  import type { AccountCallOptions, CoreTokenSource } from "./account-call";
28
36
  import { Fleet, type FleetReportOutcome } from "./fleet";
37
+ import { Registry } from "./registry";
29
38
  import { Workspaces } from "./workspaces";
30
39
  import {
31
40
  Reports,
@@ -82,8 +91,8 @@ export type {
82
91
  export interface CoreDeps {
83
92
  http: HttpClient;
84
93
  /**
85
- * Where workspace and desired-state calls get the signed-in account's Core token (`cloud.auth`). Without it those
86
- * calls reject, because they must be made as one account.
94
+ * Where workspace, desired-state and registry calls get the signed-in account's Core token (`cloud.auth`). Without
95
+ * it those calls reject, because they must be made as one account.
87
96
  */
88
97
  auth?: CoreTokenSource;
89
98
  }
@@ -152,6 +161,13 @@ export class Core {
152
161
  /** `POST /api/client/workspaces/leave`, naming `opts.workspaceId` when given. */
153
162
  leave(opts?: AccountCallOptions & LeaveWorkspaceRequest): Promise<void>;
154
163
  };
164
+ /** The miniapp registry (`registry.ts`). Each call is made as one account. */
165
+ readonly registry: {
166
+ /** `POST /api/client/registry/register`, at each install of a miniapp URL. */
167
+ registerMiniappUrl(request: RegistryRegisterRequest, opts?: AccountCallOptions): Promise<RegistryRegisterResponse>;
168
+ /** `POST /api/client/registry/status`: one batch of installed URLs. */
169
+ registryStatus(request: RegistryStatusRequest, opts?: AccountCallOptions): Promise<RegistryStatusResponse>;
170
+ };
155
171
 
156
172
  constructor(deps: CoreDeps) {
157
173
  const { http, auth } = deps;
@@ -159,6 +175,7 @@ export class Core {
159
175
  const supportProfiles = new SupportProfiles(http);
160
176
  const fleet = new Fleet(http, auth);
161
177
  const workspaces = new Workspaces(http, auth);
178
+ const registry = new Registry(http, auth);
162
179
 
163
180
  this.reports = {
164
181
  submit: reports.submit.bind(reports),
@@ -183,5 +200,9 @@ export class Core {
183
200
  declineInvitation: workspaces.declineInvitation.bind(workspaces),
184
201
  leave: workspaces.leave.bind(workspaces),
185
202
  };
203
+ this.registry = {
204
+ registerMiniappUrl: registry.registerMiniappUrl.bind(registry),
205
+ registryStatus: registry.registryStatus.bind(registry),
206
+ };
186
207
  }
187
208
  }
@@ -0,0 +1,83 @@
1
+ /**
2
+ * @fileoverview `cloud.core.registry`: the Mentra App's registry calls, over Core's `/api/client/registry` router.
3
+ *
4
+ * POST /register at each install of a miniapp URL; Core verifies the URL and answers
5
+ * `accepted` (with Core's verified `miniapp.json`), `accepted_unverified`, `not_a_miniapp` or
6
+ * `archived`
7
+ * POST /status one batch of installed URLs and versions; Core answers `active`, `archived` or `unknown` for
8
+ * each, and how long the answer stays valid
9
+ *
10
+ * Each call is made as the account whose Core token it carries (see `account-call.ts`): an answer that comes back
11
+ * after the account changed is discarded as `SessionChangedError`, and a 401 refreshes the token once. A failure keeps
12
+ * Core's status and error code on a `CoreRequestError`: `email_unverified` (403), `rate_limited` (429, with
13
+ * `retryAfterSeconds`), `invalid_request` (400), `unavailable` (503), `network_error` (no answer). An answer of
14
+ * another shape, or a `result` this client doesn't know, is a `CoreResponseError`, never a guess.
15
+ */
16
+ import {
17
+ RegistryRegisterResponseSchema,
18
+ RegistryStatusResponseSchema,
19
+ type RegistryRegisterRequest,
20
+ type RegistryRegisterResponse,
21
+ type RegistryStatusRequest,
22
+ type RegistryStatusResponse,
23
+ } from "@mentra/cloud-protocol"
24
+ import type {HttpClient} from "../../http"
25
+ import {AccountCaller, CoreResponseError, type AccountCallOptions, type CoreTokenSource} from "./account-call"
26
+
27
+ const REGISTER_PATH = "/api/client/registry/register"
28
+ const STATUS_PATH = "/api/client/registry/status"
29
+ const KNOWN_RESULTS = new Set(["accepted", "accepted_unverified", "not_a_miniapp", "archived"])
30
+
31
+ export class Registry {
32
+ private readonly caller: AccountCaller
33
+
34
+ constructor(
35
+ private readonly http: HttpClient,
36
+ auth?: CoreTokenSource,
37
+ ) {
38
+ this.caller = new AccountCaller(auth)
39
+ }
40
+
41
+ /**
42
+ * Registers a miniapp URL at install. `reported` is what the phone read from `<url>/miniapp.json`; Core keeps it
43
+ * only when it can't read the URL itself. Resolves with Core's verdict; rejects for every failure (see the file
44
+ * comment), so a caller can tell "Core said no" from "Core didn't answer".
45
+ */
46
+ async registerMiniappUrl(request: RegistryRegisterRequest, opts?: AccountCallOptions): Promise<RegistryRegisterResponse> {
47
+ const body = await this.caller.run(
48
+ (bearer) =>
49
+ this.http.post<unknown>(REGISTER_PATH, request, {
50
+ bearer,
51
+ // Registering a URL twice gives the same answer, so a transient network failure may be retried.
52
+ idempotent: true,
53
+ ...(opts?.signal ? {signal: opts.signal} : {}),
54
+ }),
55
+ opts,
56
+ )
57
+ const parsed = RegistryRegisterResponseSchema.safeParse(body)
58
+ if (parsed.success) return parsed.data
59
+ const result = typeof body === "object" && body !== null && "result" in body ? body.result : undefined
60
+ throw new CoreResponseError(
61
+ typeof result === "string" && !KNOWN_RESULTS.has(result) ? "unknown_status" : "invalid_shape",
62
+ )
63
+ }
64
+
65
+ /** The registry status of each installed URL, in the order sent, with how long the answer stays valid. */
66
+ async registryStatus(request: RegistryStatusRequest, opts?: AccountCallOptions): Promise<RegistryStatusResponse> {
67
+ const body = await this.caller.run(
68
+ (bearer) =>
69
+ this.http.post<unknown>(STATUS_PATH, request, {
70
+ bearer,
71
+ // A read: safe to retry.
72
+ idempotent: true,
73
+ ...(opts?.signal ? {signal: opts.signal} : {}),
74
+ }),
75
+ opts,
76
+ )
77
+ const parsed = RegistryStatusResponseSchema.safeParse(body)
78
+ if (!parsed.success || parsed.data.statuses.length !== request.entries.length) {
79
+ throw new CoreResponseError("invalid_shape")
80
+ }
81
+ return parsed.data
82
+ }
83
+ }