@livespace/api 0.0.0-stage → 0.1.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 ADDED
@@ -0,0 +1,13 @@
1
+ # Changelog
2
+
3
+ What changed in `@livespace/api`, newest first. The format follows
4
+ [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and versions follow
5
+ [semver](https://semver.org/). v2's own version is `USER_API_VERSION`, not this package's.
6
+
7
+ ## [Unreleased]
8
+
9
+ ### Added
10
+
11
+ - The User API's definition, from Livespace API v2 2.0.0: its types, the operation table and
12
+ scopes, the façade builder, and the translation between a call and HTTP. Moved out of
13
+ `@livespace/plugin-sdk`, which bundles it.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Livespace S.A.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,70 @@
1
- # Temporary Holding Version
1
+ # @livespace/api
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ The Livespace User API's definition: the API Livespace's frontends call as the viewing user. Its
4
+ paths and shapes are Livespace API v2's, 1:1, so this package is generated from v2's OpenAPI file,
5
+ and serves v2 just as well.
6
+
7
+ - **Types** for every schema, as `UserApiTypes` (`UserApiTypes.DealEnvelope`).
8
+ - **The operation table**, `USER_API_OPERATIONS`: each operation's method, path, input, scopes,
9
+ variants and representations, as plain data. `USER_API_SCOPES` and `USER_API_VERSION` (v2's).
10
+ - **The façade**, `buildUserApi(invoke)`: typed resource methods (`api.deals.get(id)`), over a
11
+ transport you give it.
12
+ - **The translation**, pure: a call to an HTTP request, and an answer to a result or a problem.
13
+
14
+ It has no transport and no dependencies: no address, no `fetch`, no credential. The caller adds
15
+ those. Livespace's micro-apps call the User API with the session cookie; a plugin calls it through
16
+ `@livespace/plugin-sdk` (`LsPlugin.api`), which bundles this package; a backend calls v2 with a
17
+ service key.
18
+
19
+ ## The façade, over `fetch`
20
+
21
+ ```ts
22
+ import {
23
+ buildUserApi,
24
+ describeRequest,
25
+ readResponse,
26
+ toRequest,
27
+ } from "@livespace/api"
28
+
29
+ const api = buildUserApi(async (call) => {
30
+ const request = toRequest(call, () => crypto.randomUUID())
31
+ if ("error" in request) throw new Error(request.message)
32
+ const described = describeRequest(request)
33
+ if ("error" in described) throw new Error(described.message)
34
+ const query = described.query ? `?${described.query}` : ""
35
+ const response = await fetch(`${BASE_URL}${described.path}${query}`, {
36
+ method: described.method,
37
+ headers: { ...described.headers, ...yourAuthorisation },
38
+ body: described.body,
39
+ })
40
+ const text = await response.text()
41
+ const answer = readResponse(response.status, response.headers, text ? JSON.parse(text) : null)
42
+ if (!answer.ok) throw Object.assign(new Error(answer.problem.title), answer)
43
+ return answer.body
44
+ })
45
+
46
+ const { data: deal } = await api.deals.get("94103a00-…")
47
+ const { data: notes } = await api.deals.notes.list(deal.id, { perPage: 50 })
48
+ ```
49
+
50
+ - **Arguments**: the record ids in the path, then a `body` or a `query`, then `options`:
51
+ `representation` where the operation lists one (`"minimal"`), and whatever your `invoke` reads.
52
+ - **`toRequest`** checks the ids, drops query values left `undefined`, sends the empty body a
53
+ variant takes (`api.tasks.complete(id)`), and makes an `Idempotency-Key` for a write that takes
54
+ one.
55
+ - **`describeRequest`** gives the method, the encoded path, the query (arrays comma-joined, as v2
56
+ takes them), `Accept` for the representation, `Content-Type` (a variant's own media type), the
57
+ idempotency key, and the body as JSON. `checkRequest` is the same check alone, for a host that
58
+ receives a request.
59
+ - **`readResponse`** gives the body (`null` for 204) or v2's problem (RFC 9457, with `type`,
60
+ `status`, `detail` and `requestId`), with only the headers in `API_RESPONSE_HEADERS`.
61
+
62
+ Every method, with its scopes: [docs/reference.md](./docs/reference.md).
63
+
64
+ ## Updating
65
+
66
+ The spec is vendored in `spec/openapi.json`, and names the rules can't derive are in
67
+ `spec/overrides.json`. `yarn build:definition --fetch` downloads the spec and regenerates
68
+ `src/generated/`; the diff is the review. A test fails when the generated files are stale.
69
+
70
+ ADR 0011 in this repository's `docs/adr/` says why this is its own package.
@@ -0,0 +1,24 @@
1
+ /**
2
+ * Builds the API façade from the operation table: one function per operation, nested as its name
3
+ * says (`deals.notes.list`). Each turns its arguments into a {@link UserApiCall}, the record ids in
4
+ * the path, then a body or a query, then options, and hands it to `invoke`, which the caller
5
+ * provides: the plugin SDK's sends a message to the host; a micro-app's calls the User API, with
6
+ * `toRequest` and `describeRequest`. The façade's types come from `facade.ts`; nothing here depends on a single operation.
7
+ */
8
+ import type { UserApi } from "./generated/facade.js";
9
+ import { type UserApiOperation } from "./generated/operations.js";
10
+ import type { UserApiCallOptions } from "./options.js";
11
+ import type { ApiQueryValue } from "./translate.js";
12
+ export type UserApiCall = {
13
+ /** The operation's key in the table. */
14
+ key: string;
15
+ operation: UserApiOperation;
16
+ /** Each `{name}` of the path, with what the caller passed for it. */
17
+ path: Record<string, unknown>;
18
+ query?: Record<string, ApiQueryValue | undefined>;
19
+ body?: unknown;
20
+ options: UserApiCallOptions & {
21
+ representation?: string;
22
+ };
23
+ };
24
+ export declare function buildUserApi(invoke: (call: UserApiCall) => Promise<unknown>): UserApi;
@@ -0,0 +1,47 @@
1
+ /**
2
+ * Builds the API façade from the operation table: one function per operation, nested as its name
3
+ * says (`deals.notes.list`). Each turns its arguments into a {@link UserApiCall}, the record ids in
4
+ * the path, then a body or a query, then options, and hands it to `invoke`, which the caller
5
+ * provides: the plugin SDK's sends a message to the host; a micro-app's calls the User API, with
6
+ * `toRequest` and `describeRequest`. The façade's types come from `facade.ts`; nothing here depends on a single operation.
7
+ */
8
+ import { USER_API_OPERATIONS, } from "./generated/operations.js";
9
+ const PATH_PARAM = /\{([^}]+)\}/g;
10
+ export function buildUserApi(invoke) {
11
+ const root = {};
12
+ for (const [key, operation] of Object.entries(USER_API_OPERATIONS)) {
13
+ const names = [...operation.path.matchAll(PATH_PARAM)].map(([, n]) => n);
14
+ const takesInput = operation.input !== "none" && operation.emptyBody !== true;
15
+ const method = (...args) => {
16
+ const path = {};
17
+ names.forEach((name, i) => {
18
+ path[name] = args[i];
19
+ });
20
+ const input = takesInput ? args[names.length] : undefined;
21
+ const options = (args[names.length + (takesInput ? 1 : 0)] ??
22
+ {});
23
+ return invoke({
24
+ key,
25
+ operation,
26
+ path,
27
+ ...(operation.input === "query" && input !== undefined
28
+ ? { query: input }
29
+ : {}),
30
+ ...(operation.input === "body"
31
+ ? { body: operation.emptyBody ? {} : input }
32
+ : {}),
33
+ options,
34
+ });
35
+ };
36
+ const parts = operation.name.split(".");
37
+ const last = parts.pop();
38
+ let node = root;
39
+ for (const part of parts) {
40
+ node[part] ??= {};
41
+ node = node[part];
42
+ }
43
+ node[last] = method;
44
+ }
45
+ return root;
46
+ }
47
+ //# sourceMappingURL=build-facade.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"build-facade.js","sourceRoot":"","sources":["../src/build-facade.ts"],"names":[],"m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