@acid-cat/catalog 0.8.0 → 0.10.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
@@ -20,10 +20,17 @@ catalog.capability("library.loan.return", {
20
20
  input: { type: "object", required: ["loan"], properties: { loan: { type: "string" } } },
21
21
  failures: { NotFound: notFound },
22
22
  });
23
+ const book = catalog.type("Book", { type: "object", properties: { isbn: { type: "string" } } });
24
+ catalog.event("library.book.returned", {
25
+ description: "A member returned a book. The occurrence gives the book.",
26
+ audience: ["desk"],
27
+ input: { type: "object", properties: {} },
28
+ payload: book,
29
+ });
23
30
 
24
31
  await writeFile("acidcat.catalog.json", catalog.render());
25
32
  ```
26
33
 
27
- `validate()` lists the rules a catalog breaks, and `render()` refuses to render one that breaks any. The full example is [`examples/library.ts`](examples/library.ts), a lending library.
34
+ `event()` declares an [event](../../../spec/events.md): clients subscribe to it, and get its payload in each occurrence. A payload is an object, or a reference to a type that is an object. `validate()` lists the rules a catalog breaks, and `render()` refuses to render one that breaks any. The full example is [`examples/library.ts`](examples/library.ts), a lending library.
28
35
 
29
36
  The tests compare the output of the library with files in this directory: the lending-library catalog [`examples/library.acidcat.json`](examples/library.acidcat.json), and a catalog of values that languages write differently, [`test/fixtures/edge.acidcat.json`](test/fixtures/edge.acidcat.json). A test also compares [`examples/aircon.ts`](examples/aircon.ts) with the air-conditioner catalog of the repository. Each committed catalog file is in [canonical form](../../../spec/catalog.md#canonical-form), and the root `moon run check` checks this. Thus all catalog libraries agree on the canonical form, and they do not share a test file. Test with `moon run catalog-typescript:test`.
package/dist/canonical.js CHANGED
@@ -6,7 +6,8 @@ const CATALOG_ORDER = [
6
6
  "description",
7
7
  "targets",
8
8
  "$defs",
9
- "capabilities"
9
+ "capabilities",
10
+ "events"
10
11
  ];
11
12
  const TARGET_ORDER = [
12
13
  "bindings",
@@ -22,6 +23,12 @@ const CAPABILITY_ORDER = [
22
23
  "readOnly",
23
24
  "state"
24
25
  ];
26
+ const EVENT_ORDER = [
27
+ "description",
28
+ "audience",
29
+ "input",
30
+ "payload"
31
+ ];
25
32
  /** A catalog in canonical form, as text. */
26
33
  function canonicalText(catalog) {
27
34
  return `${write(canonicalCatalog(catalog), "")}\n`;
@@ -32,6 +39,7 @@ function canonicalCatalog(catalog) {
32
39
  if (key === "targets") return byKey(value, (target) => ordered(target, TARGET_ORDER, sortedMember));
33
40
  if (key === "$defs") return byKey(value, sorted);
34
41
  if (key === "capabilities") return byKey(value, (capability) => ordered(capability, CAPABILITY_ORDER, (member, inner) => member === "failures" ? byKey(inner, sorted) : sortedMember(member, inner)));
42
+ if (key === "events") return byKey(value, (event) => ordered(event, EVENT_ORDER, sortedMember));
35
43
  return sortedMember(key, value);
36
44
  });
37
45
  }
package/dist/index.d.ts CHANGED
@@ -32,6 +32,16 @@ export interface CapabilityOptions {
32
32
  /** The schema of a value that clients can observe. */
33
33
  readonly state?: Schema;
34
34
  }
35
+ export interface EventOptions {
36
+ /** What happens, and when the provider sends an occurrence, written for people and agents. */
37
+ readonly description: string;
38
+ /** The targets the event is offered to. Empty means no target. */
39
+ readonly audience?: readonly string[];
40
+ /** The input schema of a subscription, for example a filter. It must describe an object. Absent means an empty object. */
41
+ readonly input?: Schema;
42
+ /** The schema of the data of each occurrence. It must describe an object. Absent means an empty object. */
43
+ readonly payload?: Schema;
44
+ }
35
45
  export declare class Catalog {
36
46
  #private;
37
47
  constructor(options?: {
@@ -44,6 +54,8 @@ export declare class Catalog {
44
54
  type(name: string, schema: Schema): Schema;
45
55
  /** Declare a capability. */
46
56
  capability(id: string, options: CapabilityOptions): this;
57
+ /** Declare an event (spec/events.md). */
58
+ event(id: string, options: EventOptions): this;
47
59
  /** The catalog rules this catalog breaks. Empty when it conforms. */
48
60
  validate(): string[];
49
61
  /** The catalog as a JSON value. Throws when the catalog breaks a rule. */
package/dist/index.js CHANGED
@@ -16,6 +16,7 @@ var Catalog = class {
16
16
  #targets = /* @__PURE__ */ new Map();
17
17
  #types = /* @__PURE__ */ new Map();
18
18
  #capabilities = /* @__PURE__ */ new Map();
19
+ #events = /* @__PURE__ */ new Map();
19
20
  constructor(options = {}) {
20
21
  this.#title = options.title;
21
22
  this.#description = options.description;
@@ -38,6 +39,12 @@ var Catalog = class {
38
39
  this.#capabilities.set(id, options);
39
40
  return this;
40
41
  }
42
+ /** Declare an event (spec/events.md). */
43
+ event(id, options) {
44
+ if (this.#events.has(id)) throw new Error(`The event ${id} is declared twice`);
45
+ this.#events.set(id, options);
46
+ return this;
47
+ }
41
48
  /** The catalog rules this catalog breaks. Empty when it conforms. */
42
49
  validate() {
43
50
  const problems = [];
@@ -52,12 +59,7 @@ var Catalog = class {
52
59
  problems.push(...this.#referenceProblems(schema).map((problem) => `the type ${name} ${problem}`));
53
60
  }
54
61
  for (const [id, capability] of this.#capabilities) {
55
- if (id.length > 128 || !ID.test(id)) problems.push(`the id ${id} is malformed`);
56
- if (capability.description.trim() === "") problems.push(`${id} has no description`);
57
- const audience = capability.audience ?? [];
58
- if (new Set(audience).size !== audience.length) problems.push(`${id} repeats a target in its audience`);
59
- for (const target of audience) if (!this.#targets.has(target)) problems.push(`${id} names the undeclared target ${target}`);
60
- if (capability.input !== void 0 && this.#resolve(capability.input).type !== "object") problems.push(`${id} input does not describe an object`);
62
+ problems.push(...this.#entryProblems(id, capability));
61
63
  const schemas = [
62
64
  capability.input,
63
65
  capability.output,
@@ -66,6 +68,23 @@ var Catalog = class {
66
68
  ];
67
69
  for (const schema of schemas) if (schema !== void 0) problems.push(...this.#referenceProblems(schema).map((problem) => `${id} ${problem}`));
68
70
  }
71
+ for (const [id, event] of this.#events) {
72
+ if (this.#capabilities.has(id)) problems.push(`${id} is both a capability and an event`);
73
+ problems.push(...this.#entryProblems(id, event));
74
+ if (event.payload !== void 0 && this.#resolve(event.payload).type !== "object") problems.push(`${id} payload does not describe an object`);
75
+ for (const schema of [event.input, event.payload]) if (schema !== void 0) problems.push(...this.#referenceProblems(schema).map((problem) => `${id} ${problem}`));
76
+ }
77
+ return problems;
78
+ }
79
+ /** The problems of the id, the description, the audience and the input of a capability or an event. */
80
+ #entryProblems(id, entry) {
81
+ const problems = [];
82
+ if (id.length > 128 || !ID.test(id)) problems.push(`the id ${id} is malformed`);
83
+ if (entry.description.trim() === "") problems.push(`${id} has no description`);
84
+ const audience = entry.audience ?? [];
85
+ if (new Set(audience).size !== audience.length) problems.push(`${id} repeats a target in its audience`);
86
+ for (const target of audience) if (!this.#targets.has(target)) problems.push(`${id} names the undeclared target ${target}`);
87
+ if (entry.input !== void 0 && this.#resolve(entry.input).type !== "object") problems.push(`${id} input does not describe an object`);
69
88
  return problems;
70
89
  }
71
90
  /** The catalog as a JSON value. Throws when the catalog breaks a rule. */
@@ -90,6 +109,12 @@ var Catalog = class {
90
109
  failures: Object.keys(capability.failures ?? {}).length === 0 ? void 0 : capability.failures,
91
110
  readOnly: capability.readOnly,
92
111
  state: capability.state
112
+ })])),
113
+ events: this.#events.size === 0 ? void 0 : Object.fromEntries([...this.#events].map(([id, event]) => [id, compact({
114
+ description: event.description,
115
+ audience: event.audience ?? [],
116
+ input: event.input,
117
+ payload: event.payload
93
118
  })]))
94
119
  });
95
120
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@acid-cat/catalog",
3
- "version": "0.8.0",
3
+ "version": "0.10.0",
4
4
  "description": "Write an AcidCat catalog in TypeScript and render it in canonical form.",
5
5
  "keywords": [
6
6
  "acidcat",