@segmentstream-cloud/app 0.6.0 → 0.7.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/README.md CHANGED
@@ -1,10 +1,11 @@
1
1
  # @segmentstream-cloud/app
2
2
 
3
- The ontology client uses snake_case entity fields and action names. Its TypeScript
4
- options remain camelCase; it maps `orderBy`, `isNull`, `startsWith`, and
5
- `validateOnly` to the generated REST contract and returns pagination/history
6
- metadata as camelCase. Queries using multiword filter operators lazily read and
7
- cache the ontology catalog so JSON property contents remain unchanged.
3
+ The ontology client uses camelCase entities, records and actions, including
4
+ `createdAt`, `assignedOwnerId`, and `actions.changeStatus(...)`. It translates
5
+ queries and declared action inputs to snake_case REST and decodes records back to
6
+ camelCase. The catalog is read lazily and cached by ontology hash so nested
7
+ relationships and select identifiers convert while JSON property contents,
8
+ provider payloads and opaque action results remain unchanged.
8
9
 
9
10
  Sign-in, users, permissions, the ontology client and warehouse data access for
10
11
  SegmentStream workspace apps built with Next.js (App Router, version 16 or later) and
@@ -117,7 +118,7 @@ authUserId?, organizationId, workspaceId }`. `id` is the person's ontology `user
117
118
  or use another email.
118
119
  - `getUser()` returns the same, or `null` when signed out; it throws
119
120
  `SegmentStreamApiError` with `code: "not_invited"` for people without an invitation.
120
- - `can("lead:change_status")` is `true` when the user's permissions cover the
121
+ - `can("lead:changeStatus")` is `true` when the user's permissions cover the
121
122
  permission. Roles may hold wildcards: `lead:*`, `*:read`, `*:*`. `isAllowed` and
122
123
  `grants` apply the same rule to any list of permissions.
123
124
  - `signOut()` ends the session, clears its cookies and redirects to `/sign-in` (or
@@ -132,22 +133,22 @@ user in Server Functions and Route Handlers too; do not rely on the proxy alone.
132
133
  (no Tailwind). Every class starts with `ss-`; the `--ss-*` custom properties on
133
134
  `:root` set colours, radius and font.
134
135
 
135
- | Export | Does |
136
- | --------------------------------------- | ------------------------------------------------------------------------------- |
137
- | `<SignIn />` | Email, then a six-digit code or a sign-in link; resend after 30 seconds, errors |
138
- | `useSignIn()` | The same sign-in without markup, for the app's own form |
139
- | `<SignedIn>`, `<SignedOut>` | Render children by session state |
140
- | `<Can permission="lead:change_status">` | Renders children when the user holds the permission; `fallback` otherwise |
141
- | `<UserMenu />` | Name, email, roles and sign-out |
142
- | `useUser()` | `{ status, user, error }` from `/api/ontology/me`, loaded once per session |
143
- | `usePermissions()` | `{ isLoading, roles, permissions, can(permission) }` |
144
- | `useSession()`, `signOut()` | The Neon Auth session in the browser; sign-out from the browser |
136
+ | Export | Does |
137
+ | -------------------------------------- | ------------------------------------------------------------------------------- |
138
+ | `<SignIn />` | Email, then a six-digit code or a sign-in link; resend after 30 seconds, errors |
139
+ | `useSignIn()` | The same sign-in without markup, for the app's own form |
140
+ | `<SignedIn>`, `<SignedOut>` | Render children by session state |
141
+ | `<Can permission="lead:changeStatus">` | Renders children when the user holds the permission; `fallback` otherwise |
142
+ | `<UserMenu />` | Name, email, roles and sign-out |
143
+ | `useUser()` | `{ status, user, error }` from `/api/ontology/me`, loaded once per session |
144
+ | `usePermissions()` | `{ isLoading, roles, permissions, can(permission) }` |
145
+ | `useSession()`, `signOut()` | The Neon Auth session in the browser; sign-out from the browser |
145
146
 
146
147
  `<Can>` only hides controls: the ontology service checks every read and action. A
147
148
  Server Component may wrap server-rendered children in `<Can>`:
148
149
 
149
150
  ```tsx
150
- <Can permission="lead:change_status" fallback={lead.status}>
151
+ <Can permission="lead:changeStatus" fallback={lead.status}>
151
152
  <ChangeStatusForm lead={lead.id} />
152
153
  </Can>
153
154
  ```
@@ -260,7 +261,7 @@ Records, action inputs and action results are typed from the ontology:
260
261
  ```ts
261
262
  const { items, nextCursor } = await segmentstream.ontology.lead.find({
262
263
  where: { status: { in: ["new", "contacted"] } },
263
- orderBy: [{ field: "created_at", direction: "desc" }],
264
+ orderBy: [{ field: "createdAt", direction: "desc" }],
264
265
  include: { owner: true },
265
266
  limit: 20,
266
267
  });
@@ -269,11 +270,10 @@ const history = await segmentstream.ontology.lead.history(id, { limit: 50 });
269
270
  const page = await segmentstream.ontology.lead.query({ where: { email: { startsWith: "ada" } } });
270
271
 
271
272
  const input = { lead: id, status: "contacted" } as const;
272
- const check = await segmentstream.ontology.lead.actions.change_status(input, {
273
+ const check = await segmentstream.ontology.lead.actions.changeStatus(input, {
273
274
  validateOnly: true,
274
275
  });
275
- const { result, validation, edits } =
276
- await segmentstream.ontology.lead.actions.change_status(input);
276
+ const { result, validation, edits } = await segmentstream.ontology.lead.actions.changeStatus(input);
277
277
  ```
278
278
 
279
279
  - `find` sends the query as URL parameters, `query` as a JSON body; both return
package/dist/me.d.ts CHANGED
@@ -1,4 +1,3 @@
1
- /** The ontology service's account of the caller: `GET /api/ontology/me`. */
2
1
  export declare const ME_PATH = "/api/ontology/me";
3
2
  /**
4
3
  * The signed-in person as the ontology knows them: their `user` record, the roles they hold and the
package/dist/me.js CHANGED
@@ -1,4 +1,5 @@
1
1
  /** The ontology service's account of the caller: `GET /api/ontology/me`. */
2
+ import { toCamelCase } from "./ontology-names.js";
2
3
  export const ME_PATH = "/api/ontology/me";
3
4
  /**
4
5
  * Reads `{ user: { id, name, email, kind, active }, roles, permissions }` tolerantly: unknown
@@ -22,8 +23,8 @@ export function parseMe(payload) {
22
23
  email: typeof email === "string" ? email : null,
23
24
  kind: typeof kind === "string" ? kind : "member",
24
25
  active: typeof active === "boolean" ? active : true,
25
- roles: strings(roles),
26
- permissions: strings(permissions),
26
+ roles: strings(roles).map(toCamelCase),
27
+ permissions: strings(permissions).map(toCamelCase),
27
28
  };
28
29
  }
29
30
  function strings(value) {
@@ -0,0 +1,3 @@
1
+ /** Convert each character once, including acronym boundaries such as HTTPClient. */
2
+ export declare function toSnakeCase(name: string): string;
3
+ export declare function toCamelCase(name: string): string;
@@ -0,0 +1,26 @@
1
+ /** Ontology identifiers in TypeScript and their canonical transport spelling. */
2
+ function isUppercase(letter) {
3
+ return letter !== undefined && letter >= "A" && letter <= "Z";
4
+ }
5
+ function isLowercase(letter) {
6
+ return letter !== undefined && letter >= "a" && letter <= "z";
7
+ }
8
+ /** Convert each character once, including acronym boundaries such as HTTPClient. */
9
+ export function toSnakeCase(name) {
10
+ const characters = [];
11
+ for (let index = 0; index < name.length; index++) {
12
+ const current = name[index];
13
+ const previous = name[index - 1];
14
+ const next = name[index + 1];
15
+ const followsWord = isLowercase(previous) || (previous !== undefined && previous >= "0" && previous <= "9");
16
+ const endsAcronym = isUppercase(previous) && isLowercase(next);
17
+ if (isUppercase(current) && (followsWord || endsAcronym)) {
18
+ characters.push("_");
19
+ }
20
+ characters.push(current);
21
+ }
22
+ return characters.join("").toLowerCase();
23
+ }
24
+ export function toCamelCase(name) {
25
+ return name.replace(/_([a-z0-9])/g, (_, letter) => letter.toUpperCase());
26
+ }
@@ -1,7 +1,10 @@
1
- import type { OntologyQuery, Where } from "./ontology.js";
1
+ import type { Edit, OntologyQuery, Where } from "./ontology.js";
2
2
  interface CatalogProperty {
3
3
  readonly name: string;
4
4
  readonly type: string;
5
+ readonly options?: ReadonlyArray<{
6
+ readonly value: string;
7
+ }>;
5
8
  }
6
9
  interface CatalogRelationship {
7
10
  readonly name: string;
@@ -13,6 +16,22 @@ interface CatalogEntity {
13
16
  readonly name: string;
14
17
  readonly properties: ReadonlyArray<CatalogProperty>;
15
18
  readonly relationships: ReadonlyArray<CatalogRelationship>;
19
+ readonly actions?: ReadonlyArray<{
20
+ readonly name: string;
21
+ readonly input: JsonSchema;
22
+ }>;
23
+ }
24
+ interface JsonSchema {
25
+ readonly $ref?: string;
26
+ readonly $defs?: Readonly<Record<string, JsonSchema>>;
27
+ readonly properties?: Readonly<Record<string, JsonSchema>>;
28
+ readonly items?: JsonSchema;
29
+ readonly anyOf?: ReadonlyArray<JsonSchema>;
30
+ readonly oneOf?: ReadonlyArray<JsonSchema>;
31
+ readonly enum?: ReadonlyArray<unknown>;
32
+ readonly const?: unknown;
33
+ readonly additionalProperties?: boolean | JsonSchema;
34
+ readonly required?: ReadonlyArray<string>;
16
35
  }
17
36
  export interface WireCatalog {
18
37
  readonly hash?: string;
@@ -25,9 +44,14 @@ export declare function encodeWhere(input: {
25
44
  readonly getCatalog: () => Promise<WireCatalog>;
26
45
  }): Promise<Where | undefined>;
27
46
  export declare function queryToWire(query: OntologyQuery): unknown;
47
+ /** Decode record fields and included relationships without descending into JSON properties. */
48
+ export declare function recordFromWire<A = unknown>(value: unknown, entity: string, catalog: WireCatalog): A;
49
+ export declare function actionInputToWire(input: unknown, entity: string, action: string, catalog: WireCatalog): unknown;
28
50
  export declare function responseFromWire(input: {
29
51
  readonly method: "GET" | "POST";
30
52
  readonly path: string;
31
53
  readonly payload: unknown;
32
54
  }): unknown;
55
+ /** Decode current edits using the current catalog; stored historical changes retain their names. */
56
+ export declare function editsFromWire(edits: ReadonlyArray<Edit>, catalog: WireCatalog): ReadonlyArray<Edit>;
33
57
  export {};
@@ -1,3 +1,4 @@
1
+ import { toCamelCase, toSnakeCase } from "./ontology-names.js";
1
2
  const FILTER_OPERATORS = [
2
3
  "eq",
3
4
  "ne",
@@ -15,24 +16,10 @@ const SYSTEM_FIELDS = ["id", "created_at", "updated_at", "created_by", "updated_
15
16
  function isRecord(value) {
16
17
  return value !== null && typeof value === "object" && !Array.isArray(value);
17
18
  }
18
- function needsOperatorEncoding(value) {
19
- if (value === null || typeof value !== "object") {
20
- return false;
21
- }
22
- for (const [key, child] of Object.entries(value)) {
23
- if (key === "isNull" || key === "startsWith") {
24
- return true;
25
- }
26
- if (needsOperatorEncoding(child)) {
27
- return true;
28
- }
29
- }
30
- return false;
31
- }
32
19
  /** Catalog metadata distinguishes relationship filters from opaque JSON property values. */
33
20
  export async function encodeWhere(input) {
34
21
  const { where, entity, getCatalog } = input;
35
- if (where === undefined || !needsOperatorEncoding(where)) {
22
+ if (where === undefined) {
36
23
  return where;
37
24
  }
38
25
  const catalog = await getCatalog();
@@ -52,12 +39,13 @@ function whereFromCatalog(input) {
52
39
  encodedWhere[field] = whereFromCatalog({ where: condition, entity, catalog });
53
40
  continue;
54
41
  }
55
- const relationship = relationshipTarget({ field, entity, definition, catalog });
42
+ const wireField = toSnakeCase(field);
43
+ const relationship = relationshipTarget({ field: wireField, entity, definition, catalog });
56
44
  if (relationship !== undefined && isRecord(condition)) {
57
- encodedWhere[field] = relationshipFilterToWire({ condition, relationship, catalog });
45
+ encodedWhere[wireField] = relationshipFilterToWire({ condition, relationship, catalog });
58
46
  continue;
59
47
  }
60
- encodedWhere[field] = fieldFilterToWire({ field, condition, definition });
48
+ encodedWhere[wireField] = fieldFilterToWire({ field: wireField, condition, definition });
61
49
  }
62
50
  return encodedWhere;
63
51
  }
@@ -95,10 +83,10 @@ function isGeneratedField(field, definition) {
95
83
  }
96
84
  function fieldFilterToWire(input) {
97
85
  const { field, condition, definition } = input;
86
+ const property = definition?.properties.find((candidate) => candidate.name === field);
98
87
  if (!isRecord(condition)) {
99
- return condition;
88
+ return selectToWire(condition, property);
100
89
  }
101
- const property = definition?.properties.find((candidate) => candidate.name === field);
102
90
  if (property === undefined && !isGeneratedField(field, definition)) {
103
91
  return condition;
104
92
  }
@@ -108,7 +96,7 @@ function fieldFilterToWire(input) {
108
96
  }
109
97
  const encodedFilter = Object.create(null);
110
98
  for (const [operator, operand] of Object.entries(condition)) {
111
- encodedFilter[operatorToWire(operator)] = operand;
99
+ encodedFilter[operatorToWire(operator)] = selectToWire(operand, property);
112
100
  }
113
101
  return encodedFilter;
114
102
  }
@@ -125,10 +113,134 @@ export function queryToWire(query) {
125
113
  const { orderBy, where, ...rest } = query;
126
114
  return {
127
115
  ...rest,
128
- ...(orderBy === undefined ? {} : { order_by: orderBy }),
116
+ ...(orderBy === undefined
117
+ ? {}
118
+ : { order_by: orderBy.map((order) => ({ ...order, field: toSnakeCase(order.field) })) }),
119
+ ...(query.include === undefined
120
+ ? {}
121
+ : {
122
+ include: Object.fromEntries(Object.entries(query.include).map(([name, value]) => [toSnakeCase(name), value])),
123
+ }),
129
124
  ...(where === undefined ? {} : { where }),
130
125
  };
131
126
  }
127
+ function selectToWire(value, property) {
128
+ if (property?.type !== "select") {
129
+ return value;
130
+ }
131
+ const convert = (child) => typeof child === "string" &&
132
+ property.options?.some((option) => option.value === toSnakeCase(child))
133
+ ? toSnakeCase(child)
134
+ : child;
135
+ return Array.isArray(value) ? value.map(convert) : convert(value);
136
+ }
137
+ /** Decode record fields and included relationships without descending into JSON properties. */
138
+ export function recordFromWire(value, entity, catalog) {
139
+ if (!isRecord(value)) {
140
+ return value;
141
+ }
142
+ const definition = catalog.entities.find((candidate) => candidate.name === entity);
143
+ return Object.fromEntries(Object.entries(value).map(([field, child]) => {
144
+ const property = definition?.properties.find((candidate) => candidate.name === field);
145
+ if (property?.type === "select" && typeof child === "string") {
146
+ return [toCamelCase(field), toCamelCase(child)];
147
+ }
148
+ const relationship = relationshipTarget({ field, entity, definition, catalog });
149
+ if (relationship !== undefined) {
150
+ const decoded = Array.isArray(child)
151
+ ? child.map((record) => recordFromWire(record, relationship.entity, catalog))
152
+ : recordFromWire(child, relationship.entity, catalog);
153
+ return [toCamelCase(field), decoded];
154
+ }
155
+ return [toCamelCase(field), child];
156
+ }));
157
+ }
158
+ function schemaInputToWire(value, schema, definitions) {
159
+ if (schema.$ref?.startsWith("#/$defs/")) {
160
+ const target = definitions[schema.$ref.slice("#/$defs/".length)];
161
+ return target === undefined ? value : schemaInputToWire(value, target, definitions);
162
+ }
163
+ const alternatives = schema.anyOf ?? schema.oneOf;
164
+ if (alternatives !== undefined) {
165
+ const object = alternatives.find((candidate) => matchesObjectSchema(value, candidate, definitions));
166
+ const array = alternatives.find((candidate) => candidate.items !== undefined && Array.isArray(value));
167
+ const literals = alternatives.flatMap((candidate) => candidate.enum ?? (candidate.const === undefined ? [] : [candidate.const]));
168
+ if (typeof value === "string" && literals.includes(toSnakeCase(value))) {
169
+ return toSnakeCase(value);
170
+ }
171
+ const target = object ?? array;
172
+ return target === undefined ? value : schemaInputToWire(value, target, definitions);
173
+ }
174
+ if (typeof value === "string" &&
175
+ (schema.enum?.includes(toSnakeCase(value)) || schema.const === toSnakeCase(value))) {
176
+ return toSnakeCase(value);
177
+ }
178
+ if (Array.isArray(value) && schema.items !== undefined) {
179
+ return value.map((child) => schemaInputToWire(child, schema.items, definitions));
180
+ }
181
+ if (!isRecord(value)) {
182
+ return value;
183
+ }
184
+ if (schema.properties === undefined) {
185
+ const childSchema = schema.additionalProperties;
186
+ return typeof childSchema === "object"
187
+ ? Object.fromEntries(Object.entries(value).map(([key, child]) => [
188
+ key,
189
+ schemaInputToWire(child, childSchema, definitions),
190
+ ]))
191
+ : value;
192
+ }
193
+ return Object.fromEntries(Object.entries(value).map(([field, child]) => {
194
+ const wireField = toSnakeCase(field);
195
+ const childSchema = schema.properties?.[wireField];
196
+ return [
197
+ wireField,
198
+ childSchema === undefined ? child : schemaInputToWire(child, childSchema, definitions),
199
+ ];
200
+ }));
201
+ }
202
+ function matchesObjectSchema(value, schema, definitions) {
203
+ if (schema.$ref?.startsWith("#/$defs/")) {
204
+ const target = definitions[schema.$ref.slice("#/$defs/".length)];
205
+ return (target !== undefined && target !== schema && matchesObjectSchema(value, target, definitions));
206
+ }
207
+ if (!isRecord(value) || schema.properties === undefined) {
208
+ return false;
209
+ }
210
+ if (schema.required?.some((key) => !Object.hasOwn(value, key) && !Object.hasOwn(value, toCamelCase(key)))) {
211
+ return false;
212
+ }
213
+ return Object.entries(schema.properties).every(([key, childSchema]) => {
214
+ if (childSchema.const === undefined) {
215
+ return true;
216
+ }
217
+ const child = value[key] ?? value[toCamelCase(key)];
218
+ return (child === childSchema.const ||
219
+ (typeof child === "string" && toSnakeCase(child) === childSchema.const));
220
+ });
221
+ }
222
+ export function actionInputToWire(input, entity, action, catalog) {
223
+ const definition = catalog.entities.find((candidate) => candidate.name === entity);
224
+ const authored = definition?.actions?.find((candidate) => candidate.name === action);
225
+ if (authored !== undefined) {
226
+ return schemaInputToWire(input, authored.input, authored.input.$defs ?? {});
227
+ }
228
+ if (!isRecord(input)) {
229
+ return input;
230
+ }
231
+ return Object.fromEntries(Object.entries(input).map(([field, child]) => {
232
+ const wireField = toSnakeCase(field);
233
+ const property = definition?.properties.find((candidate) => candidate.name === wireField);
234
+ return [
235
+ wireField,
236
+ (action === "link" || action === "unlink") &&
237
+ field === "relationship" &&
238
+ typeof child === "string"
239
+ ? toSnakeCase(child)
240
+ : selectToWire(child, property),
241
+ ];
242
+ }));
243
+ }
132
244
  function isPagedRoute(method, route) {
133
245
  if (route.endsWith("/history")) {
134
246
  return true;
@@ -159,3 +271,18 @@ export function responseFromWire(input) {
159
271
  const items = page.items.map(historyEntryFromWire);
160
272
  return { ...page, nextCursor, items };
161
273
  }
274
+ /** Decode current edits using the current catalog; stored historical changes retain their names. */
275
+ export function editsFromWire(edits, catalog) {
276
+ return edits.map((edit) => {
277
+ const before = recordFromWire(Object.fromEntries(Object.entries(edit.changes).map(([field, change]) => [field, change.before])), edit.entity, catalog);
278
+ const after = recordFromWire(Object.fromEntries(Object.entries(edit.changes).map(([field, change]) => [field, change.after])), edit.entity, catalog);
279
+ return {
280
+ ...edit,
281
+ entity: toCamelCase(edit.entity),
282
+ changes: Object.fromEntries(Object.keys(before).map((field) => [
283
+ field,
284
+ { before: before[field], after: after[field] },
285
+ ])),
286
+ };
287
+ });
288
+ }
@@ -222,7 +222,7 @@ export interface OntologyTransport {
222
222
  }
223
223
  /**
224
224
  * Entities and actions exist only in the app's types, so the client resolves them by name when
225
- * they are used: `ontology.lead.actions.change_status(…)` posts to `lead/actions/change_status`.
225
+ * they are used: `ontology.lead.actions.changeStatus(…)` posts to `lead/actions/change_status`.
226
226
  */
227
227
  export declare function createOntologyClient<O>(transport: OntologyTransport, aggregateTransport?: OntologyTransport): OntologyClient<O>;
228
228
  /** `find` sends the query as URL parameters: `where` as JSON, `orderBy` as `field:direction`. */
package/dist/ontology.js CHANGED
@@ -1,7 +1,8 @@
1
1
  import { SegmentStreamApiError, apiErrorFrom, readJson } from "./errors.js";
2
- import { encodeWhere, queryToWire, responseFromWire } from "./ontology-wire.js";
3
- const ENTITY_NAME = /^[a-z][a-z0-9]*(?:_[a-z0-9]+)*$/;
4
- const ACTION_NAME = /^[a-z][a-z0-9]*(?:_[a-z0-9]+)*$/;
2
+ import { actionInputToWire, encodeWhere, editsFromWire, queryToWire, recordFromWire, responseFromWire, } from "./ontology-wire.js";
3
+ import { toCamelCase, toSnakeCase } from "./ontology-names.js";
4
+ const ENTITY_NAME = /^[a-z][a-zA-Z0-9]*(?:_[a-z0-9]+)*$/;
5
+ const ACTION_NAME = ENTITY_NAME;
5
6
  /** Names a caller may probe on any object; never an entity or an action. */
6
7
  const RESERVED = new Set([
7
8
  "then",
@@ -18,7 +19,7 @@ const RESERVED = new Set([
18
19
  ]);
19
20
  /**
20
21
  * Entities and actions exist only in the app's types, so the client resolves them by name when
21
- * they are used: `ontology.lead.actions.change_status(…)` posts to `lead/actions/change_status`.
22
+ * they are used: `ontology.lead.actions.changeStatus(…)` posts to `lead/actions/change_status`.
22
23
  */
23
24
  export function createOntologyClient(transport, aggregateTransport) {
24
25
  const entities = new Map();
@@ -31,12 +32,16 @@ export function createOntologyClient(transport, aggregateTransport) {
31
32
  let client = entities.get(property);
32
33
  if (client === undefined) {
33
34
  client = {
34
- ...entityClient(property, catalogReader.transport, catalogReader.getCatalog),
35
+ ...entityClient(toSnakeCase(property), catalogReader.transport, catalogReader.getCatalog),
35
36
  aggregate: (request, options = {}) => {
36
37
  if (!aggregateTransport) {
37
38
  throw new TypeError("An aggregate transport is required.");
38
39
  }
39
- return send(aggregateTransport, "POST", `/${property}/aggregate`, request, options.signal);
40
+ return send(aggregateTransport, "POST", `/${toSnakeCase(property)}/aggregate`, aggregateToWire(request), options.signal).then((result) => ({
41
+ ...result,
42
+ fields: result.fields.map((field) => ({ ...field, name: toCamelCase(field.name) })),
43
+ rows: result.rows.map((row) => Object.fromEntries(Object.entries(row).map(([name, value]) => [toCamelCase(name), value]))),
44
+ }));
40
45
  },
41
46
  };
42
47
  entities.set(property, client);
@@ -79,12 +84,33 @@ function createCatalogReader(transport) {
79
84
  function entityClient(entity, transport, getCatalog) {
80
85
  const base = `/${entity}`;
81
86
  const actionCalls = new Map();
82
- const runAction = (name, input, options = {}) => send(transport, "POST", `${base}/actions/${encodeURIComponent(name)}`, {
83
- input,
84
- ...(options.validateOnly === undefined
85
- ? {}
86
- : { options: { validate_only: options.validateOnly } }),
87
- }, options.signal);
87
+ const runAction = async (name, input, options = {}) => {
88
+ const action = toSnakeCase(name);
89
+ const catalog = await getCatalog();
90
+ const wireResponse = await send(transport, "POST", `${base}/actions/${encodeURIComponent(action)}`, {
91
+ input: actionInputToWire(input, entity, action, catalog),
92
+ ...(options.validateOnly === undefined
93
+ ? {}
94
+ : { options: { validate_only: options.validateOnly } }),
95
+ }, options.signal);
96
+ const response = { ...wireResponse, edits: editsFromWire(wireResponse.edits, catalog) };
97
+ if (action === "create" || action === "update") {
98
+ return { ...response, result: recordFromWire(response.result, entity, catalog) };
99
+ }
100
+ if (action.startsWith("upsert_by_") &&
101
+ response.result !== null &&
102
+ typeof response.result === "object" &&
103
+ !Array.isArray(response.result)) {
104
+ return {
105
+ ...response,
106
+ result: {
107
+ ...response.result,
108
+ record: recordFromWire(response.result.record, entity, catalog),
109
+ },
110
+ };
111
+ }
112
+ return response;
113
+ };
88
114
  const actions = new Proxy(Object.create(null), {
89
115
  get(_target, property) {
90
116
  if (typeof property !== "string" || RESERVED.has(property) || !ACTION_NAME.test(property)) {
@@ -100,15 +126,28 @@ function entityClient(entity, transport, getCatalog) {
100
126
  });
101
127
  return {
102
128
  find: async (query = {}, options = {}) => {
129
+ const catalog = await getCatalog();
103
130
  const where = await encodeWhere({ where: query.where, entity, getCatalog });
104
131
  const search = findParameters({ ...query, where }).toString();
105
- return send(transport, "GET", search === "" ? base : `${base}?${search}`, undefined, options.signal);
132
+ const page = await send(transport, "GET", search === "" ? base : `${base}?${search}`, undefined, options.signal);
133
+ return {
134
+ ...page,
135
+ items: page.items.map((record) => recordFromWire(record, entity, catalog)),
136
+ };
106
137
  },
107
138
  query: async (query = {}, options = {}) => {
139
+ const catalog = await getCatalog();
108
140
  const where = await encodeWhere({ where: query.where, entity, getCatalog });
109
- return send(transport, "POST", `${base}/query`, queryToWire({ ...query, where }), options.signal);
141
+ const page = await send(transport, "POST", `${base}/query`, queryToWire({ ...query, where }), options.signal);
142
+ return {
143
+ ...page,
144
+ items: page.items.map((record) => recordFromWire(record, entity, catalog)),
145
+ };
146
+ },
147
+ get: (id, options = {}) => {
148
+ const encodedId = recordId(id);
149
+ return getCatalog().then(async (catalog) => recordFromWire(await send(transport, "GET", `${base}/${encodedId}`, undefined, options.signal), entity, catalog));
110
150
  },
111
- get: (id, options = {}) => send(transport, "GET", `${base}/${recordId(id)}`, undefined, options.signal),
112
151
  history: (id, options = {}) => {
113
152
  const search = new URLSearchParams();
114
153
  if (options.after !== undefined) {
@@ -143,12 +182,14 @@ export function findParameters(query) {
143
182
  search.set("where", JSON.stringify(query.where));
144
183
  }
145
184
  if (query.orderBy !== undefined && query.orderBy.length > 0) {
146
- search.set("order_by", query.orderBy.map(({ field, direction }) => `${field}:${direction ?? "asc"}`).join(","));
185
+ search.set("order_by", query.orderBy
186
+ .map(({ field, direction }) => `${toSnakeCase(field)}:${direction ?? "asc"}`)
187
+ .join(","));
147
188
  }
148
189
  if (query.include !== undefined) {
149
190
  const relationships = Object.keys(query.include).filter((name) => query.include?.[name] === true);
150
191
  if (relationships.length > 0) {
151
- search.set("include", relationships.join(","));
192
+ search.set("include", relationships.map(toSnakeCase).join(","));
152
193
  }
153
194
  }
154
195
  if (query.after !== undefined) {
@@ -159,6 +200,34 @@ export function findParameters(query) {
159
200
  }
160
201
  return search;
161
202
  }
203
+ /** The platform aggregate API keeps its existing protocol keys; ontology identifiers are canonical. */
204
+ function aggregateToWire(request) {
205
+ return {
206
+ ...request,
207
+ dimensions: request.dimensions.map(toSnakeCase),
208
+ measures: request.measures.map(toSnakeCase),
209
+ ...(request.granularityOf === undefined
210
+ ? {}
211
+ : { granularityOf: toSnakeCase(request.granularityOf) }),
212
+ ...(request.filters === undefined
213
+ ? {}
214
+ : {
215
+ filters: request.filters.map((filter) => ({
216
+ ...filter,
217
+ dimension: toSnakeCase(filter.dimension),
218
+ })),
219
+ }),
220
+ ...(request.orderBy === undefined
221
+ ? {}
222
+ : {
223
+ orderBy: request.orderBy.map((order) => ({
224
+ ...order,
225
+ ...(order.dimension === undefined ? {} : { dimension: toSnakeCase(order.dimension) }),
226
+ ...(order.measure === undefined ? {} : { measure: toSnakeCase(order.measure) }),
227
+ })),
228
+ }),
229
+ };
230
+ }
162
231
  async function send(transport, method, path, body, signal) {
163
232
  const headers = new Headers({ accept: "application/json" });
164
233
  if (body !== undefined) {
@@ -7,15 +7,18 @@
7
7
  * (`src/permissions.ts`) so that the app SDK and its browser components do not depend on the
8
8
  * ontology runtime. Keep the two in step.
9
9
  */
10
+ import { toSnakeCase } from "./ontology-names.js";
10
11
  const WILDCARD = "*";
11
12
  /** `<entity>:<verb>` with lowercase snake_case entities and lowercase snake_case verbs, or `*` for either. */
12
- const PERMISSION = /^(\*|[a-z][a-z0-9]*(?:_[a-z0-9]+)*):(\*|[a-z][a-z0-9]*(?:_[a-z0-9]+)*)$/;
13
+ const PERMISSION = /^(\*|[a-z][a-zA-Z0-9]*(?:_[a-z0-9]+)*):(\*|[a-z][a-zA-Z0-9]*(?:_[a-z0-9]+)*)$/;
13
14
  function parsePermission(value) {
14
15
  if (typeof value !== "string") {
15
16
  return undefined;
16
17
  }
17
18
  const match = PERMISSION.exec(value);
18
- return match === null ? undefined : { entity: match[1], verb: match[2] };
19
+ return match === null
20
+ ? undefined
21
+ : { entity: toSnakeCase(match[1]), verb: toSnakeCase(match[2]) };
19
22
  }
20
23
  /** Whether a granted permission, which may hold wildcards, covers the required one. */
21
24
  export function grants(granted, required) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@segmentstream-cloud/app",
3
- "version": "0.6.0",
3
+ "version": "0.7.0",
4
4
  "description": "Sign-in, users, permissions, the ontology client and warehouse data access for SegmentStream workspace apps built with Next.js.",
5
5
  "license": "Apache-2.0",
6
6
  "author": "SegmentStream",