@amritk/generate-parsers 0.4.0 → 0.6.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/dist/generators/build-schema.d.ts +10 -1
- package/dist/generators/generate-files.d.ts +17 -0
- package/dist/generators/generate-parser-function.d.ts +6 -1
- package/dist/helpers/collect-helpers.d.ts +9 -2
- package/dist/helpers/collect-imports.d.ts +6 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +135 -121
- package/package.json +2 -2
- package/src/generators/build-schema.test.ts +41 -17
- package/src/generators/build-schema.ts +19 -1
- package/src/generators/generate-files.test.ts +65 -52
- package/src/generators/generate-files.ts +24 -5
- package/src/generators/generate-parser-function.test.ts +254 -258
- package/src/generators/generate-parser-function.ts +87 -58
- package/src/helpers/collect-helpers.test.ts +12 -0
- package/src/helpers/collect-helpers.ts +27 -12
- package/src/helpers/collect-imports.test.ts +44 -84
- package/src/helpers/collect-imports.ts +8 -1
- package/src/index.ts +1 -1
|
@@ -34,6 +34,15 @@ export type GeneratedFile = {
|
|
|
34
34
|
* @param helpersMode - `'package'` (default) emits `import ... from '@amritk/helpers/...'`.
|
|
35
35
|
* `'embedded'` emits `import ... from './_helpers/...'` and appends the helper sources
|
|
36
36
|
* as additional `GeneratedFile` entries so the output directory is self-contained.
|
|
37
|
+
* @param helpersImportPrefix - Relative path prefix to the `_helpers/` directory in
|
|
38
|
+
* embedded mode. Defaults to `'./'`. The recursive multi-schema build passes `'../'`,
|
|
39
|
+
* `'../../'`, etc. so nested parsers can import from a single shared `_helpers/`
|
|
40
|
+
* directory while the helper sources are emitted once at the output root.
|
|
41
|
+
* @param readonly - When true, every property, array, and record in the generated type
|
|
42
|
+
* definitions is emitted as `readonly`.
|
|
43
|
+
* @param typeSuffix - Suffix appended to every type/parser name derived from a `$ref`
|
|
44
|
+
* (e.g. `'Object'` → `ContactObject`). Defaults to `''` (no suffix). The root type name
|
|
45
|
+
* is used verbatim and is not affected by this suffix.
|
|
37
46
|
* @returns An array of generated TypeScript files
|
|
38
47
|
*
|
|
39
48
|
* @example
|
|
@@ -66,4 +75,4 @@ export type GeneratedFile = {
|
|
|
66
75
|
* });
|
|
67
76
|
* ```
|
|
68
77
|
*/
|
|
69
|
-
export declare const buildSchema: (rootSchema: JSONSchema, rootTypeName: string, extensions?: SchemaExtensions, typesOnly?: boolean, logWarnings?: boolean, strict?: boolean, helpersMode?: HelpersMode) => Promise<GeneratedFile[]>;
|
|
78
|
+
export declare const buildSchema: (rootSchema: JSONSchema, rootTypeName: string, extensions?: SchemaExtensions, typesOnly?: boolean, logWarnings?: boolean, strict?: boolean, helpersMode?: HelpersMode, helpersImportPrefix?: string, readonly?: boolean, typeSuffix?: string) => Promise<GeneratedFile[]>;
|
|
@@ -37,6 +37,23 @@ type GenerateFileOptions = {
|
|
|
37
37
|
* be shipped alongside the generated files.
|
|
38
38
|
*/
|
|
39
39
|
readonly helpersMode?: HelpersMode;
|
|
40
|
+
/**
|
|
41
|
+
* Relative path prefix to the shared `_helpers/` directory in embedded mode.
|
|
42
|
+
* Defaults to `'./'`. The recursive multi-schema build passes `'../'`,
|
|
43
|
+
* `'../../'`, etc. so a nested parser can reach a single `_helpers/` directory
|
|
44
|
+
* at the output root.
|
|
45
|
+
*/
|
|
46
|
+
readonly helpersImportPrefix?: string;
|
|
47
|
+
/**
|
|
48
|
+
* When true, every property, array, and record in the generated type
|
|
49
|
+
* definitions is emitted as `readonly`.
|
|
50
|
+
*/
|
|
51
|
+
readonly readonly?: boolean;
|
|
52
|
+
/**
|
|
53
|
+
* Suffix appended to every type/parser name derived from a `$ref`.
|
|
54
|
+
* Defaults to `''` (no suffix). Set to e.g. `'Object'` to emit `ContactObject`.
|
|
55
|
+
*/
|
|
56
|
+
readonly typeSuffix?: string;
|
|
40
57
|
};
|
|
41
58
|
/** Result of generating a single parser file. */
|
|
42
59
|
export type GeneratedFileContent = {
|
|
@@ -20,6 +20,11 @@ type GenerateParserOptions = {
|
|
|
20
20
|
* with a path-aware Error. Unknown extra keys are still allowed.
|
|
21
21
|
*/
|
|
22
22
|
readonly strict?: boolean;
|
|
23
|
+
/**
|
|
24
|
+
* Suffix appended to every type/parser name derived from a `$ref`. Must match
|
|
25
|
+
* the suffix used when generating the referenced files. Defaults to `''`.
|
|
26
|
+
*/
|
|
27
|
+
readonly typeSuffix?: string;
|
|
23
28
|
};
|
|
24
29
|
/**
|
|
25
30
|
* Generates a safe parser as an arrow function that never throws.
|
|
@@ -49,5 +54,5 @@ export declare const shapeValidatorFunctionName: (typeName: string) => string;
|
|
|
49
54
|
* in additionalProperties). The stub is safe: parents calling it will fall
|
|
50
55
|
* through to the slow path, matching the pre-deep-fast-path behavior.
|
|
51
56
|
*/
|
|
52
|
-
export declare const generateShapeValidator: (schema: JSONSchema, typeName: string, useRefImports: boolean) => string;
|
|
57
|
+
export declare const generateShapeValidator: (schema: JSONSchema, typeName: string, useRefImports: boolean, suffix?: string) => string;
|
|
53
58
|
export {};
|
|
@@ -12,5 +12,12 @@ export type CollectedHelpers = {
|
|
|
12
12
|
*/
|
|
13
13
|
readonly used: Set<RuntimeHelperName>;
|
|
14
14
|
};
|
|
15
|
-
/**
|
|
16
|
-
|
|
15
|
+
/**
|
|
16
|
+
* Detects which runtime helpers a generated parser body references.
|
|
17
|
+
*
|
|
18
|
+
* @param helpersImportPrefix - Relative path prefix to the shared `_helpers/`
|
|
19
|
+
* directory in embedded mode. Defaults to `'./'`. The recursive multi-schema
|
|
20
|
+
* build passes `'../'`, `'../../'`, etc. so nested parsers can reach a single
|
|
21
|
+
* `_helpers/` directory at the output root.
|
|
22
|
+
*/
|
|
23
|
+
export declare const collectHelpers: (parserFunction: string, mode: HelpersMode, helpersImportPrefix?: string) => CollectedHelpers;
|
|
@@ -20,6 +20,12 @@ type CollectImportsOptions = {
|
|
|
20
20
|
* imports for external schemas that were never generated as files.
|
|
21
21
|
*/
|
|
22
22
|
readonly rootSchema?: Record<string, unknown> | undefined;
|
|
23
|
+
/**
|
|
24
|
+
* Suffix appended to every type name derived from a `$ref`. Must match the
|
|
25
|
+
* suffix used when generating the referenced files so imports resolve.
|
|
26
|
+
* Defaults to `''` (no suffix).
|
|
27
|
+
*/
|
|
28
|
+
readonly typeSuffix?: string;
|
|
23
29
|
};
|
|
24
30
|
/**
|
|
25
31
|
* Collects all import statements needed for a schema by finding $ref references
|
package/dist/index.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { buildSchema } from './generators/build-schema';
|
|
1
|
+
export { buildSchema, type GeneratedFile } from './generators/build-schema';
|