@amritk/helpers 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/README.md ADDED
@@ -0,0 +1,100 @@
1
+ <div align="center">
2
+
3
+ # @amritk/helpers
4
+
5
+ **Shared schema-traversal and runtime helpers for the mjst code generation ecosystem.**
6
+
7
+ ![status](https://img.shields.io/badge/status-pre--alpha-ef4444?style=flat-square)&nbsp;
8
+ ![version](https://img.shields.io/badge/version-v0.1.0-6366f1?style=flat-square&logo=npm&logoColor=white)&nbsp;
9
+ ![license](https://img.shields.io/badge/license-MIT-22c55e?style=flat-square)&nbsp;
10
+ ![JSON Schema](https://img.shields.io/badge/JSON%20Schema-2020--12-f97316?style=flat-square)&nbsp;
11
+ ![node](https://img.shields.io/badge/node-%E2%89%A520-339933?style=flat-square&logo=node.js&logoColor=white)&nbsp;
12
+ ![vibe coded](https://img.shields.io/badge/vibe%20coded-94%25-a855f7?style=flat-square)
13
+
14
+ </div>
15
+
16
+ ---
17
+
18
+ ## Overview
19
+
20
+ `@amritk/helpers` is the shared utility belt used by [mjst](../../README.md)'s generators **and** by the runtime code those generators produce. Each helper is published as its own subpath export so consumers (and generated output) only pull in what they need — no barrel, no incidental dependencies.
21
+
22
+ ---
23
+
24
+ ## Installation
25
+
26
+ ```bash
27
+ npm install @amritk/helpers
28
+ # or
29
+ pnpm add @amritk/helpers
30
+ # or
31
+ yarn add @amritk/helpers
32
+ # or
33
+ bun add @amritk/helpers
34
+ ```
35
+
36
+ ---
37
+
38
+ ## Modules
39
+
40
+ ### Schema traversal
41
+
42
+ | Subpath | Exports | Purpose |
43
+ |:---|:---|:---|
44
+ | `@amritk/helpers/extract-refs` | `extractRefs` | Collect every `$ref` reachable from a schema. |
45
+ | `@amritk/helpers/resolve-ref` | `resolveRef` | Resolve a JSON pointer `$ref` against a root schema. |
46
+ | `@amritk/helpers/build-dynamic-ref-map` | `buildDynamicRefMap` | Build a map of `$dynamicAnchor` → resolved location. |
47
+ | `@amritk/helpers/resolve-dynamic-refs` | `resolveDynamicRefs` | Replace `$dynamicRef` occurrences using the map above. |
48
+ | `@amritk/helpers/upgrade-draft07-schema` | `upgradeDraft07Schema`, `isDraft07Schema` | Upgrade a Draft-07 schema to 2020-12. |
49
+ | `@amritk/helpers/ref-to-filename` | `refToFilename`, `toKebabCase` | Convert a `$ref` to a stable filename. |
50
+ | `@amritk/helpers/ref-to-name` | `refToName` | Convert a `$ref` to a TypeScript identifier (PascalCase). |
51
+ | `@amritk/helpers/schema-guards` | `isSchemaObject`, `hasType`, `hasProperties`, `hasOneOf`, `hasAnyOf`, `hasAllOf`, `hasEnum`, `hasConst`, `hasPattern`, `hasFormat`, `hasDefault`, `hasExamples`, `hasRequired`, `hasItems`, `hasAdditionalProperties`, `hasMinLength`, `hasMaxLength`, `hasMinimum`, `hasMaximum`, `hasExclusiveMinimum`, `hasExclusiveMaximum`, `hasMultipleOf`, `hasMinItems`, `hasMaxItems`, `hasUniqueItems`, `hasMinProperties`, `hasMaxProperties`, … | Type-narrowing predicates for JSON Schema keywords. |
52
+
53
+ ### Codegen utilities
54
+
55
+ | Subpath | Exports | Purpose |
56
+ |:---|:---|:---|
57
+ | `@amritk/helpers/generate-type-definition` | `generateTypeDefinition` | Render a TypeScript type from a schema node. |
58
+ | `@amritk/helpers/parse-documentation` | `parseDocumentation`, `ObjectDocumentation` | Parse a markdown doc file into per-property descriptions. |
59
+
60
+ ### Runtime helpers (also copied into generated output)
61
+
62
+ | Subpath | Exports | Purpose |
63
+ |:---|:---|:---|
64
+ | `@amritk/helpers/is-object` | `isObject` | Narrow `unknown` → `Record<string, unknown>`. |
65
+ | `@amritk/helpers/safe-accessor` | `safeAccessor` | Read a key from an unknown value without throwing. |
66
+ | `@amritk/helpers/validate-array` | `validateArray` | Validate array shape and items. |
67
+ | `@amritk/helpers/validate-record` | `validateRecord` | Validate record shape and additional properties. |
68
+
69
+ ---
70
+
71
+ ## Usage
72
+
73
+ ```ts
74
+ import { resolveRef } from '@amritk/helpers/resolve-ref'
75
+ import { isObjectSchema, hasProperties } from '@amritk/helpers/schema-guards'
76
+
77
+ const node = resolveRef('#/$defs/info', rootSchema)
78
+
79
+ if (isObjectSchema(node) && hasProperties(node)) {
80
+ for (const [name, property] of Object.entries(node.properties)) {
81
+ // ...
82
+ }
83
+ }
84
+ ```
85
+
86
+ Each helper has its own colocated test file (`*.test.ts`) — read those for canonical examples.
87
+
88
+ ---
89
+
90
+ ## Related packages
91
+
92
+ - [`@amritk/generate-parsers`](../generate-parsers) — primary consumer
93
+ - [`@amritk/generate-validators`](../generate-validators) — primary consumer
94
+ - [`@amritk/mjst`](../cli) — the CLI surface
95
+
96
+ ---
97
+
98
+ ## License
99
+
100
+ [MIT](../../LICENSE)
@@ -0,0 +1,17 @@
1
+ import type { JSONSchema } from 'json-schema-typed/draft-2020-12';
2
+ /**
3
+ * Builds a map of $dynamicRef anchor values to their corresponding $ref paths.
4
+ *
5
+ * JSON Schema 2020-12 uses $dynamicAnchor and $dynamicRef for late-binding references.
6
+ * In the OpenAPI spec, $dynamicAnchor: "meta" on the schema definition allows properties
7
+ * like media-type.schema to reference it via $dynamicRef: "#meta".
8
+ *
9
+ * This function scans all $defs for entries with $dynamicAnchor and builds a lookup
10
+ * so we can convert $dynamicRef values to concrete $ref paths.
11
+ *
12
+ * @example
13
+ * // Given a schema with $defs.schema having $dynamicAnchor: "meta"
14
+ * buildDynamicRefMap(rootSchema)
15
+ * // Returns: { "#meta": "#/$defs/schema" }
16
+ */
17
+ export declare const buildDynamicRefMap: (rootSchema: JSONSchema) => Record<string, string>;
@@ -0,0 +1,107 @@
1
+ // src/schema-guards.ts
2
+ var isSchemaObject = (schema) => {
3
+ return typeof schema === "object" && schema !== null && typeof schema !== "boolean";
4
+ };
5
+ var hasType = (schema) => {
6
+ return isSchemaObject(schema) && "type" in schema && typeof schema.type === "string";
7
+ };
8
+ var isObjectSchema = (schema) => {
9
+ return isSchemaObject(schema) && (("type" in schema) && schema.type === "object" || ("properties" in schema));
10
+ };
11
+ var hasProperties = (schema) => {
12
+ return isSchemaObject(schema) && "properties" in schema && typeof schema.properties === "object" && schema.properties !== null;
13
+ };
14
+ var hasEnum = (schema) => {
15
+ return isSchemaObject(schema) && "enum" in schema && Array.isArray(schema.enum);
16
+ };
17
+ var hasConst = (schema) => {
18
+ return isSchemaObject(schema) && "const" in schema;
19
+ };
20
+ var hasPattern = (schema) => {
21
+ return isSchemaObject(schema) && "pattern" in schema && typeof schema.pattern === "string";
22
+ };
23
+ var hasFormat = (schema) => {
24
+ return isSchemaObject(schema) && "format" in schema && typeof schema.format === "string";
25
+ };
26
+ var hasDefault = (schema) => {
27
+ return isSchemaObject(schema) && "default" in schema;
28
+ };
29
+ var hasExamples = (schema) => {
30
+ return isSchemaObject(schema) && "examples" in schema && Array.isArray(schema.examples);
31
+ };
32
+ var hasOneOf = (schema) => {
33
+ return isSchemaObject(schema) && "oneOf" in schema && Array.isArray(schema.oneOf);
34
+ };
35
+ var hasAnyOf = (schema) => {
36
+ return isSchemaObject(schema) && "anyOf" in schema && Array.isArray(schema.anyOf);
37
+ };
38
+ var hasAllOf = (schema) => {
39
+ return isSchemaObject(schema) && "allOf" in schema && Array.isArray(schema.allOf);
40
+ };
41
+ var hasRequired = (schema) => {
42
+ return isSchemaObject(schema) && "required" in schema && Array.isArray(schema.required);
43
+ };
44
+ var hasItems = (schema) => {
45
+ return isSchemaObject(schema) && "items" in schema && typeof schema.items === "object" && schema.items !== null && typeof schema.items !== "boolean";
46
+ };
47
+ var hasAdditionalProperties = (schema) => {
48
+ return isSchemaObject(schema) && "additionalProperties" in schema;
49
+ };
50
+ var hasMinLength = (schema) => {
51
+ return isSchemaObject(schema) && "minLength" in schema && typeof schema.minLength === "number";
52
+ };
53
+ var hasMaxLength = (schema) => {
54
+ return isSchemaObject(schema) && "maxLength" in schema && typeof schema.maxLength === "number";
55
+ };
56
+ var hasMinimum = (schema) => {
57
+ return isSchemaObject(schema) && "minimum" in schema && typeof schema.minimum === "number";
58
+ };
59
+ var hasMaximum = (schema) => {
60
+ return isSchemaObject(schema) && "maximum" in schema && typeof schema.maximum === "number";
61
+ };
62
+ var hasExclusiveMinimum = (schema) => {
63
+ return isSchemaObject(schema) && "exclusiveMinimum" in schema && typeof schema.exclusiveMinimum === "number";
64
+ };
65
+ var hasExclusiveMaximum = (schema) => {
66
+ return isSchemaObject(schema) && "exclusiveMaximum" in schema && typeof schema.exclusiveMaximum === "number";
67
+ };
68
+ var hasMultipleOf = (schema) => {
69
+ return isSchemaObject(schema) && "multipleOf" in schema && typeof schema.multipleOf === "number";
70
+ };
71
+ var hasMinItems = (schema) => {
72
+ return isSchemaObject(schema) && "minItems" in schema && typeof schema.minItems === "number";
73
+ };
74
+ var hasMaxItems = (schema) => {
75
+ return isSchemaObject(schema) && "maxItems" in schema && typeof schema.maxItems === "number";
76
+ };
77
+ var hasUniqueItems = (schema) => {
78
+ return isSchemaObject(schema) && "uniqueItems" in schema && typeof schema.uniqueItems === "boolean";
79
+ };
80
+ var hasMinProperties = (schema) => {
81
+ return isSchemaObject(schema) && "minProperties" in schema && typeof schema.minProperties === "number";
82
+ };
83
+ var hasMaxProperties = (schema) => {
84
+ return isSchemaObject(schema) && "maxProperties" in schema && typeof schema.maxProperties === "number";
85
+ };
86
+ var hasRef = (schema) => {
87
+ return isSchemaObject(schema) && "$ref" in schema && typeof schema.$ref === "string";
88
+ };
89
+
90
+ // src/build-dynamic-ref-map.ts
91
+ var buildDynamicRefMap = (rootSchema) => {
92
+ const map = {};
93
+ if (!isSchemaObject(rootSchema) || !("$defs" in rootSchema)) {
94
+ return map;
95
+ }
96
+ const defs = rootSchema.$defs;
97
+ for (const [key, value] of Object.entries(defs)) {
98
+ if (typeof value === "object" && value !== null && "$dynamicAnchor" in value && typeof value["$dynamicAnchor"] === "string") {
99
+ const anchor = value["$dynamicAnchor"];
100
+ map[`#${anchor}`] = `#/$defs/${key}`;
101
+ }
102
+ }
103
+ return map;
104
+ };
105
+ export {
106
+ buildDynamicRefMap
107
+ };
@@ -0,0 +1,23 @@
1
+ import type { JSONSchema } from 'json-schema-typed/draft-2020-12';
2
+ /**
3
+ * Extracts all $ref values from a JSON Schema recursively.
4
+ * Returns both internal (`#`-prefixed) and URI refs so the build pipeline
5
+ * can resolve and generate files for all referenced definitions.
6
+ *
7
+ * @param schema - The JSON Schema to extract refs from
8
+ * @returns A Set of unique ref strings found in the schema
9
+ *
10
+ * @example
11
+ * ```ts
12
+ * const schema = {
13
+ * type: 'object',
14
+ * properties: {
15
+ * contact: { $ref: '#/$defs/contact' },
16
+ * channel: { $ref: 'http://example.com/channel.json' },
17
+ * }
18
+ * }
19
+ * const refs = extractRefs(schema)
20
+ * // refs = Set(['#/$defs/contact', 'http://example.com/channel.json'])
21
+ * ```
22
+ */
23
+ export declare const extractRefs: (schema: JSONSchema) => Set<string>;
@@ -0,0 +1,36 @@
1
+ // src/extract-refs.ts
2
+ var isResolvableRef = (ref) => {
3
+ if (ref === "#")
4
+ return false;
5
+ if (ref.startsWith("#"))
6
+ return true;
7
+ if (ref.startsWith("http://") || ref.startsWith("https://"))
8
+ return true;
9
+ return false;
10
+ };
11
+ var extractRefs = (schema) => {
12
+ const refs = new Set;
13
+ const traverse = (obj) => {
14
+ if (typeof obj !== "object" || obj === null) {
15
+ return;
16
+ }
17
+ if (Array.isArray(obj)) {
18
+ for (const item of obj) {
19
+ traverse(item);
20
+ }
21
+ return;
22
+ }
23
+ const record = obj;
24
+ if ("$ref" in record && typeof record["$ref"] === "string" && isResolvableRef(record["$ref"])) {
25
+ refs.add(record["$ref"]);
26
+ }
27
+ for (const key in record) {
28
+ traverse(record[key]);
29
+ }
30
+ };
31
+ traverse(schema);
32
+ return refs;
33
+ };
34
+ export {
35
+ extractRefs
36
+ };
@@ -0,0 +1,7 @@
1
+ import type { JSONSchema } from 'json-schema-typed/draft-2020-12';
2
+ /**
3
+ * Generates a TypeScript type definition from a JSON Schema.
4
+ * Handles required vs optional properties based on the schema's required array.
5
+ * Uses $comment as inline JSDoc description when present.
6
+ */
7
+ export declare const generateTypeDefinition: (schema: JSONSchema, typeName: string) => string;