@amritk/helpers 0.19.0 → 0.20.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.
@@ -19,11 +19,18 @@ export declare const MJST_EXTENSION_KEY = "x-mjst";
19
19
  * runtime value still validates as its underlying JSON Schema type, but the
20
20
  * generated TypeScript type is intersected with a unique brand so values are
21
21
  * not interchangeable with the unbranded base type.
22
+ * - `discriminator` names the property that says *which message* a frame is.
23
+ * Unlike the three above it is not read off a schema: it belongs on an
24
+ * AsyncAPI channel, whose messages become one `@amritk/api` message contract
25
+ * and therefore need one tag property between them. A document that spells
26
+ * its tag `event` rather than `type` says so once, on the channel, instead of
27
+ * the generator's caller having to remember per run.
22
28
  */
23
29
  export type MjstExtension = {
24
30
  readonly instanceOf?: string;
25
31
  readonly primitive?: string;
26
32
  readonly brand?: string;
33
+ readonly discriminator?: string;
27
34
  };
28
35
  /**
29
36
  * Reads the `instanceOf` class name from a schema's `x-mjst` extension, when it
@@ -43,3 +50,12 @@ export declare const getMjstPrimitive: (schema: JSONSchema) => string | undefine
43
50
  * present and safe to embed in generated output. Returns undefined otherwise.
44
51
  */
45
52
  export declare const getMjstBrand: (schema: JSONSchema) => string | undefined;
53
+ /**
54
+ * Reads the message `discriminator` property name from a node's `x-mjst`
55
+ * extension — an AsyncAPI channel, in practice, rather than a schema.
56
+ *
57
+ * A name we could not safely emit (see {@link SAFE_DISCRIMINATOR}) is warned
58
+ * about once and ignored, so the caller falls back to its own default rather
59
+ * than writing a broken literal into generated code.
60
+ */
61
+ export declare const getMjstDiscriminator: (node: unknown) => string | undefined;
@@ -5,10 +5,12 @@ const SUPPORTED_INSTANCE_CLASSES = /* @__PURE__ */ new Set(["Date"]);
5
5
  const SUPPORTED_PRIMITIVES = /* @__PURE__ */ new Set(["bigint"]);
6
6
  const warnedInstanceClasses = /* @__PURE__ */ new Set();
7
7
  const SAFE_BRAND = /^[\w$ -]+$/;
8
- const readExtensionString = (schema, field) => {
9
- if (!isSchemaObject(schema))
8
+ const SAFE_DISCRIMINATOR = /^[A-Za-z_$][\w$-]*$/;
9
+ const warnedDiscriminators = /* @__PURE__ */ new Set();
10
+ const readExtensionString = (node, field) => {
11
+ if (!isSchemaObject(node))
10
12
  return void 0;
11
- const extension = readKey(schema, MJST_EXTENSION_KEY);
13
+ const extension = readKey(node, MJST_EXTENSION_KEY);
12
14
  if (typeof extension !== "object" || extension === null)
13
15
  return void 0;
14
16
  const value = readKey(extension, field);
@@ -34,9 +36,22 @@ const getMjstBrand = (schema) => {
34
36
  const brand = readExtensionString(schema, "brand");
35
37
  return brand !== void 0 && SAFE_BRAND.test(brand) ? brand : void 0;
36
38
  };
39
+ const getMjstDiscriminator = (node) => {
40
+ const discriminator = readExtensionString(node, "discriminator");
41
+ if (discriminator === void 0)
42
+ return void 0;
43
+ if (SAFE_DISCRIMINATOR.test(discriminator))
44
+ return discriminator;
45
+ if (!warnedDiscriminators.has(discriminator)) {
46
+ warnedDiscriminators.add(discriminator);
47
+ console.warn(`Warning: ignoring unsupported x-mjst discriminator "${discriminator}". Expected a property name starting with a letter, "_" or "$".`);
48
+ }
49
+ return void 0;
50
+ };
37
51
  export {
38
52
  MJST_EXTENSION_KEY,
39
53
  getMjstBrand,
54
+ getMjstDiscriminator,
40
55
  getMjstInstanceOf,
41
56
  getMjstPrimitive
42
57
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@amritk/helpers",
3
- "version": "0.19.0",
3
+ "version": "0.20.0",
4
4
  "description": "Shared utilities for the mjst code generation ecosystem.",
5
5
  "type": "module",
6
6
  "sideEffects": false,