contractdiff 0.0.0-stage → 0.4.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/LICENSE +21 -0
- package/README.md +228 -2
- package/dist/bin.js +4 -0
- package/dist/cli.d.ts +3 -0
- package/dist/cli.js +24 -0
- package/dist/command-diff.js +310 -0
- package/dist/command-explain-cap.js +36 -0
- package/dist/command-explain-inline.js +17 -0
- package/dist/command-explain-name.js +31 -0
- package/dist/command-explain-operation.js +85 -0
- package/dist/command-explain-text.js +21 -0
- package/dist/command-explain.js +54 -0
- package/dist/command-lint.js +280 -0
- package/dist/command-migrate.js +70 -0
- package/dist/command-plugin.js +32 -0
- package/dist/command-props.js +116 -0
- package/dist/commands.js +7 -0
- package/dist/core/index.d.ts +2 -0
- package/dist/core/index.js +2 -0
- package/dist/core/model.d.ts +46 -0
- package/dist/core/model.js +1 -0
- package/dist/core/resolve.d.ts +22 -0
- package/dist/core/resolve.js +71 -0
- package/dist/diff-classify.js +30 -0
- package/dist/diff-contract.js +176 -0
- package/dist/diff-decisions.js +446 -0
- package/dist/diff-normalise.js +143 -0
- package/dist/diff-rules.js +109 -0
- package/dist/diff-schema.js +335 -0
- package/dist/diff-security.js +33 -0
- package/dist/diff-transitive.js +26 -0
- package/dist/diff-usage.js +59 -0
- package/dist/extract-normalise.js +85 -0
- package/dist/extract-roundtrip.js +28 -0
- package/dist/extract-standard.js +79 -0
- package/dist/gen-ts-ast.d.ts +135 -0
- package/dist/gen-ts-ast.js +52 -0
- package/dist/gen-ts-bundle.js +233 -0
- package/dist/gen-ts-comment.js +48 -0
- package/dist/gen-ts-context.d.ts +83 -0
- package/dist/gen-ts-context.js +45 -0
- package/dist/gen-ts-dict.d.ts +14 -0
- package/dist/gen-ts-dict.js +14 -0
- package/dist/gen-ts-discriminators.d.ts +7 -0
- package/dist/gen-ts-discriminators.js +117 -0
- package/dist/gen-ts-document.d.ts +12 -0
- package/dist/gen-ts-document.js +82 -0
- package/dist/gen-ts-enums.js +139 -0
- package/dist/gen-ts-helpers.js +30 -0
- package/dist/gen-ts-index.d.ts +49 -0
- package/dist/gen-ts-index.js +110 -0
- package/dist/gen-ts-operations.js +277 -0
- package/dist/gen-ts-print.js +113 -0
- package/dist/gen-ts-ref.d.ts +30 -0
- package/dist/gen-ts-ref.js +75 -0
- package/dist/gen-ts-schema.js +283 -0
- package/dist/gen-ts-text.d.ts +24 -0
- package/dist/gen-ts-text.js +98 -0
- package/dist/host.d.ts +145 -0
- package/dist/host.js +273 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +5 -0
- package/dist/input-asyncapi.js +167 -0
- package/dist/input-buf.js +58 -0
- package/dist/input-jsonschema.js +165 -0
- package/dist/input-mcp.js +111 -0
- package/dist/input-overlay-jsonpath.js +265 -0
- package/dist/input-overlay.js +138 -0
- package/dist/input-protobuf.js +212 -0
- package/dist/input-swagger2.js +331 -0
- package/dist/inputs.js +8 -0
- package/dist/lint-code-read.js +129 -0
- package/dist/lint-code.d.ts +56 -0
- package/dist/lint-code.js +64 -0
- package/dist/lint-engine.d.ts +93 -0
- package/dist/lint-engine.js +87 -0
- package/dist/lint-functions.js +95 -0
- package/dist/lint-history.js +133 -0
- package/dist/lint-jsonpath.js +92 -0
- package/dist/lint-names.d.ts +11 -0
- package/dist/lint-names.js +2 -0
- package/dist/lint-redocly.js +141 -0
- package/dist/lint-rules.js +189 -0
- package/dist/lint-select.d.ts +33 -0
- package/dist/lint-select.js +129 -0
- package/dist/lint-since.js +20 -0
- package/dist/lint-spectral.js +167 -0
- package/dist/lint-stats.js +58 -0
- package/dist/migrate-hey-api.js +144 -0
- package/dist/migrate-literal.js +139 -0
- package/dist/migrate-openapi-fetch.js +62 -0
- package/dist/migrate-openapi-typescript.js +159 -0
- package/dist/migrate-orval.js +119 -0
- package/dist/migrate-shared.js +31 -0
- package/dist/openapi-typescript.d.ts +9 -0
- package/dist/openapi-typescript.js +36 -0
- package/dist/openapi.d.ts +20 -0
- package/dist/openapi.js +94 -0
- package/dist/plugin-express.js +66 -0
- package/dist/plugin-extract-hono.js +1 -0
- package/dist/plugin-extract-nest.js +1 -0
- package/dist/plugin-extract-openapi.js +246 -0
- package/dist/plugin-extract-standard-schema.js +42 -0
- package/dist/plugin-extract-trpc.js +1 -0
- package/dist/plugin-extract-ts-rest.js +77 -0
- package/dist/plugin-fastify.js +68 -0
- package/dist/plugin-fetch-client.js +172 -0
- package/dist/plugin-fetch-routes.js +110 -0
- package/dist/plugin-fetch-sample.js +155 -0
- package/dist/plugin-fetch-text.js +5 -0
- package/dist/plugin-fetch.js +19 -0
- package/dist/plugin-hono.js +42 -0
- package/dist/plugin-json-schema-build.js +289 -0
- package/dist/plugin-json-schema-dialect-anthropic.js +41 -0
- package/dist/plugin-json-schema-dialect-gemini.js +29 -0
- package/dist/plugin-json-schema-dialect-mcp-elicitation.js +32 -0
- package/dist/plugin-json-schema-dialect-mcp.js +13 -0
- package/dist/plugin-json-schema-dialect-openai.js +37 -0
- package/dist/plugin-json-schema-dialect.js +180 -0
- package/dist/plugin-json-schema.js +125 -0
- package/dist/plugin-markdown.js +114 -0
- package/dist/plugin-mcp-code.js +28 -0
- package/dist/plugin-mcp-tools.js +152 -0
- package/dist/plugin-mcp.js +107 -0
- package/dist/plugin-msw-files.js +50 -0
- package/dist/plugin-msw-sample.js +45 -0
- package/dist/plugin-msw-synth.js +116 -0
- package/dist/plugin-msw.js +30 -0
- package/dist/plugin-nest-code.js +68 -0
- package/dist/plugin-nest-contract.js +23 -0
- package/dist/plugin-nest.js +44 -0
- package/dist/plugin-server-code.js +61 -0
- package/dist/plugin-server-contract.js +48 -0
- package/dist/plugin-server-example.js +62 -0
- package/dist/plugin-server-router.js +77 -0
- package/dist/plugin-server-routes.js +155 -0
- package/dist/plugin-server-runtime.js +3 -0
- package/dist/plugin-server-sample.js +40 -0
- package/dist/plugin-swr-sample.js +21 -0
- package/dist/plugin-swr.js +40 -0
- package/dist/plugin-tanstack-query-sample.js +21 -0
- package/dist/plugin-tanstack-query.js +59 -0
- package/dist/plugin-types.js +80 -0
- package/dist/plugin-validator-code.js +41 -0
- package/dist/plugin-validator-names.js +27 -0
- package/dist/plugin-validator.js +103 -0
- package/dist/plugins.js +21 -0
- package/dist/props-emit.js +194 -0
- package/dist/props-generate.js +408 -0
- package/dist/props-judge.js +30 -0
- package/dist/props-mutate.js +144 -0
- package/dist/props-random.js +25 -0
- package/dist/props-run.js +170 -0
- package/dist/props-shrink.js +75 -0
- package/dist/run-apply.js +25 -0
- package/dist/run-command.js +31 -0
- package/dist/run-commit.d.ts +51 -0
- package/dist/run-commit.js +48 -0
- package/dist/run-config.d.ts +193 -0
- package/dist/run-config.js +256 -0
- package/dist/run-diff.js +24 -0
- package/dist/run-extract.d.ts +13 -0
- package/dist/run-extract.js +21 -0
- package/dist/run-gen.d.ts +15 -0
- package/dist/run-gen.js +43 -0
- package/dist/run-header.js +37 -0
- package/dist/run-input.js +31 -0
- package/dist/run-load.js +32 -0
- package/dist/run-map.d.ts +41 -0
- package/dist/run-map.js +52 -0
- package/dist/run-plan.d.ts +31 -0
- package/dist/run-plan.js +35 -0
- package/dist/scaffold-seed.js +43 -0
- package/dist/scaffold-write.js +17 -0
- package/dist/scaffold.js +196 -0
- package/dist/testing-lint.js +43 -0
- package/dist/testing-owner.js +17 -0
- package/dist/testing-probe.js +25 -0
- package/dist/testing-rules.d.ts +65 -0
- package/dist/testing-rules.js +115 -0
- package/dist/testing.d.ts +32 -0
- package/dist/testing.js +79 -0
- package/package.json +108 -4
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { compareContracts } from './diff-contract.js';
|
|
2
|
+
import { normalise, stable } from './extract-normalise.js';
|
|
3
|
+
import { escape, isRecord } from './extract-standard.js';
|
|
4
|
+
export const differences = (spec, extracted, pointer = '') => {
|
|
5
|
+
if (isRecord(spec) && isRecord(extracted)) {
|
|
6
|
+
const inSpec = new Set(Object.keys(spec));
|
|
7
|
+
const inExtracted = new Set(Object.keys(extracted));
|
|
8
|
+
return [...new Set([...inSpec, ...inExtracted])].toSorted().flatMap((key) => {
|
|
9
|
+
const at = `${pointer}/${escape(key)}`;
|
|
10
|
+
if (!inExtracted.has(key))
|
|
11
|
+
return [{ pointer: at, problem: 'missing', spec: spec[key] }];
|
|
12
|
+
if (!inSpec.has(key))
|
|
13
|
+
return [{ pointer: at, problem: 'unexpected', extracted: extracted[key] }];
|
|
14
|
+
return differences(spec[key], extracted[key], at);
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
if (Array.isArray(spec) && Array.isArray(extracted) && spec.length === extracted.length) {
|
|
18
|
+
return spec.flatMap((item, index) => differences(item, extracted[index], `${pointer}/${String(index)}`));
|
|
19
|
+
}
|
|
20
|
+
return stable(spec) === stable(extracted) ? [] : [{ pointer, problem: 'different', spec, extracted }];
|
|
21
|
+
};
|
|
22
|
+
export const roundtrip = async (spec, generate, extract, options = {}) => {
|
|
23
|
+
const back = await extract(await generate(spec));
|
|
24
|
+
const extracted = normalise(back, options);
|
|
25
|
+
const diff = differences(normalise(spec, options), extracted);
|
|
26
|
+
const { read } = options;
|
|
27
|
+
return { equal: diff.length === 0, diff, extracted, ...(read === undefined ? {} : { comparison: compareContracts(read(spec), read(back)) }) };
|
|
28
|
+
};
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { readFile } from 'node:fs/promises';
|
|
2
|
+
import { resolve } from 'node:path';
|
|
3
|
+
import { pathToFileURL } from 'node:url';
|
|
4
|
+
import { stable } from './extract-normalise.js';
|
|
5
|
+
export const TARGET = 'draft-2020-12';
|
|
6
|
+
export const isRecord = (value) => typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
7
|
+
export const isStandard = (value) => (isRecord(value) || typeof value === 'function') && isRecord(value['~standard']);
|
|
8
|
+
export const escape = (step) => step.replaceAll('~', '~0').replaceAll('/', '~1');
|
|
9
|
+
export const finding = (code, pointer, message, fix) => ({ code, pointer, message, fix });
|
|
10
|
+
export const loadModule = async (module) => {
|
|
11
|
+
if (isRecord(module))
|
|
12
|
+
return module;
|
|
13
|
+
if (typeof module !== 'string' || module === '') {
|
|
14
|
+
throw new TypeError('the `module` option names no module. fix: set `"options": { "module": "./src/schemas.ts" }`');
|
|
15
|
+
}
|
|
16
|
+
if (module.endsWith('.json'))
|
|
17
|
+
return { default: JSON.parse(String(await readFile(module))) };
|
|
18
|
+
return { ...(await import(pathToFileURL(resolve(module)).href)) };
|
|
19
|
+
};
|
|
20
|
+
export const sideOf = (value) => {
|
|
21
|
+
if (value === undefined || value === 'input')
|
|
22
|
+
return 'input';
|
|
23
|
+
if (value === 'output')
|
|
24
|
+
return 'output';
|
|
25
|
+
throw new TypeError(`the \`side\` option is ${JSON.stringify(value)}. fix: write "input" or "output"`);
|
|
26
|
+
};
|
|
27
|
+
export const toJsonSchema = (value, side, pointer) => {
|
|
28
|
+
if (!isStandard(value)) {
|
|
29
|
+
return { finding: finding('not-a-standard-schema', pointer, `${pointer} is not a Standard Schema: it has no \`~standard\``, 'export a schema of a Standard Schema library (Zod 4, Valibot, ArkType, validator), or leave it out of the extract') };
|
|
30
|
+
}
|
|
31
|
+
const props = value['~standard'];
|
|
32
|
+
const vendor = typeof props.vendor === 'string' ? props.vendor : 'an unnamed library';
|
|
33
|
+
const converter = props.jsonSchema;
|
|
34
|
+
if (!isRecord(converter) || typeof converter[side] !== 'function') {
|
|
35
|
+
return {
|
|
36
|
+
finding: finding('no-standard-json-schema', pointer, `${pointer} is a schema of ${vendor}, which does not implement Standard JSON Schema (\`~standard.jsonSchema.${side}\`)`, `upgrade ${vendor} to a version that implements Standard JSON Schema (Zod 4.2+), or export the schema from a library that does`),
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
try {
|
|
40
|
+
const { $schema: _dialect, ...schema } = converter[side]({ target: TARGET });
|
|
41
|
+
return { schema };
|
|
42
|
+
}
|
|
43
|
+
catch (error) {
|
|
44
|
+
return { finding: finding('not-representable', pointer, `${vendor} cannot write the ${side} of ${pointer} as JSON Schema: ${error instanceof Error ? error.message : String(error)}`, side === 'output' ? 'extract with `"side": "input"`, or describe the output with a schema of its own' : 'replace the part JSON Schema cannot hold (a transform, a date, a custom check) with one it can') };
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
export const placeAt = (base) => ({ base, defs: new Map(), findings: [] });
|
|
48
|
+
export const definitions = (place) => Object.fromEntries(place.defs);
|
|
49
|
+
const reference = (ref, target) => {
|
|
50
|
+
if (ref === '#')
|
|
51
|
+
return `${target.base}${escape(target.name)}`;
|
|
52
|
+
if (!ref.startsWith('#/$defs/'))
|
|
53
|
+
return ref;
|
|
54
|
+
const [step, ...tail] = ref.slice('#/$defs/'.length).split('/');
|
|
55
|
+
const key = step.replaceAll('~1', '/').replaceAll('~0', '~');
|
|
56
|
+
return [`${target.base}${escape(target.renamed.get(key) ?? key)}`, ...tail].join('/');
|
|
57
|
+
};
|
|
58
|
+
const rewrite = (value, target) => {
|
|
59
|
+
if (Array.isArray(value))
|
|
60
|
+
return value.map((item) => rewrite(item, target));
|
|
61
|
+
if (!isRecord(value))
|
|
62
|
+
return value;
|
|
63
|
+
return Object.fromEntries(Object.entries(value).map(([key, inner]) => [key, key === '$ref' && typeof inner === 'string' ? reference(inner, target) : rewrite(inner, target)]));
|
|
64
|
+
};
|
|
65
|
+
export const relocate = (schema, name, place) => {
|
|
66
|
+
const inner = Object.entries(isRecord(schema.$defs) ? schema.$defs : {});
|
|
67
|
+
const renamed = new Map();
|
|
68
|
+
for (const [key, value] of inner) {
|
|
69
|
+
const taken = place.defs.has(key) && stable(place.defs.get(key)) !== stable(rewrite(value, { name, base: place.base, renamed: new Map() }));
|
|
70
|
+
renamed.set(key, taken ? `${name}.${key}` : key);
|
|
71
|
+
if (taken)
|
|
72
|
+
place.findings.push(finding('renamed-definition', `${place.base.slice(1)}${escape(name)}`, `two schemas define "${key}" differently; the one ${name} uses is kept as "${name}.${key}"`, 'give one of them another id'));
|
|
73
|
+
}
|
|
74
|
+
const target = { name, base: place.base, renamed };
|
|
75
|
+
for (const [key, value] of inner)
|
|
76
|
+
place.defs.set(renamed.get(key), rewrite(value, target));
|
|
77
|
+
const { $defs: _defs, ...rest } = schema;
|
|
78
|
+
return rewrite(rest, target);
|
|
79
|
+
};
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The type tree the generator builds before printing: the handful of node shapes openapi-typescript
|
|
3
|
+
* creates with `ts.factory`, as plain data. Nothing here knows about TypeScript the compiler.
|
|
4
|
+
*/
|
|
5
|
+
export type KeywordName = 'boolean' | 'never' | 'number' | 'string' | 'undefined' | 'unknown';
|
|
6
|
+
export interface KeywordNode {
|
|
7
|
+
readonly kind: 'keyword';
|
|
8
|
+
readonly name: KeywordName;
|
|
9
|
+
}
|
|
10
|
+
/** `null`, `true`, `false` or a number, already in source form. */
|
|
11
|
+
export interface LiteralNode {
|
|
12
|
+
readonly kind: 'literal';
|
|
13
|
+
readonly text: string;
|
|
14
|
+
}
|
|
15
|
+
/** Text printed as it is: a quoted string literal or a bare name. Never parenthesised, never deduplicated. */
|
|
16
|
+
export interface RawNode {
|
|
17
|
+
readonly kind: 'raw';
|
|
18
|
+
readonly text: string;
|
|
19
|
+
}
|
|
20
|
+
export interface ListNode {
|
|
21
|
+
readonly kind: 'union' | 'intersection' | 'tuple';
|
|
22
|
+
readonly types: readonly TypeNode[];
|
|
23
|
+
}
|
|
24
|
+
export interface ObjectNode {
|
|
25
|
+
readonly kind: 'object';
|
|
26
|
+
readonly members: readonly Member[];
|
|
27
|
+
}
|
|
28
|
+
/** `T[]`, `readonly T` and `...T`. */
|
|
29
|
+
export interface WrapNode {
|
|
30
|
+
readonly kind: 'array' | 'readonly' | 'rest';
|
|
31
|
+
readonly type: TypeNode;
|
|
32
|
+
}
|
|
33
|
+
export interface ReferenceNode {
|
|
34
|
+
readonly kind: 'reference';
|
|
35
|
+
readonly name: string;
|
|
36
|
+
readonly args: readonly TypeNode[];
|
|
37
|
+
}
|
|
38
|
+
/** `T[index]`, where `index` is already in source form. */
|
|
39
|
+
export interface AccessNode {
|
|
40
|
+
readonly kind: 'access';
|
|
41
|
+
readonly type: TypeNode;
|
|
42
|
+
readonly index: string;
|
|
43
|
+
}
|
|
44
|
+
export type TypeNode = KeywordNode | LiteralNode | RawNode | ListNode | ObjectNode | WrapNode | ReferenceNode | AccessNode;
|
|
45
|
+
export interface Property {
|
|
46
|
+
readonly kind: 'property';
|
|
47
|
+
readonly readonly: boolean;
|
|
48
|
+
/** In source form: an identifier, a number or a quoted string. */
|
|
49
|
+
readonly name: string;
|
|
50
|
+
readonly optional: boolean;
|
|
51
|
+
readonly type: TypeNode;
|
|
52
|
+
/** The comment's text between its opening and closing marks, when it has one. */
|
|
53
|
+
readonly comment: string | undefined;
|
|
54
|
+
}
|
|
55
|
+
export interface IndexSignature {
|
|
56
|
+
readonly kind: 'index';
|
|
57
|
+
readonly readonly: boolean;
|
|
58
|
+
readonly parameter: string;
|
|
59
|
+
/** In source form: `string`, or a template literal type. */
|
|
60
|
+
readonly parameterType: string;
|
|
61
|
+
readonly type: TypeNode;
|
|
62
|
+
}
|
|
63
|
+
export type Member = Property | IndexSignature;
|
|
64
|
+
export interface EnumMember {
|
|
65
|
+
readonly name: string;
|
|
66
|
+
readonly value: string | number;
|
|
67
|
+
readonly comment: string | undefined;
|
|
68
|
+
}
|
|
69
|
+
export interface InterfaceStatement {
|
|
70
|
+
readonly kind: 'interface';
|
|
71
|
+
readonly name: string;
|
|
72
|
+
/** Grows while operations are hoisted into it. */
|
|
73
|
+
readonly members: Member[];
|
|
74
|
+
}
|
|
75
|
+
export interface AliasStatement {
|
|
76
|
+
readonly kind: 'alias';
|
|
77
|
+
readonly name: string;
|
|
78
|
+
readonly type: TypeNode;
|
|
79
|
+
}
|
|
80
|
+
export interface EnumStatement {
|
|
81
|
+
readonly kind: 'enum';
|
|
82
|
+
readonly name: string;
|
|
83
|
+
readonly members: readonly EnumMember[];
|
|
84
|
+
}
|
|
85
|
+
/** `export const name: type = [values];` */
|
|
86
|
+
export interface ValuesStatement {
|
|
87
|
+
readonly kind: 'values';
|
|
88
|
+
readonly name: string;
|
|
89
|
+
readonly type: TypeNode;
|
|
90
|
+
readonly values: readonly (string | number)[];
|
|
91
|
+
}
|
|
92
|
+
/** A fixed helper type, printed as written. */
|
|
93
|
+
export interface HelperStatement {
|
|
94
|
+
readonly kind: 'helper';
|
|
95
|
+
readonly name: string;
|
|
96
|
+
readonly text: string;
|
|
97
|
+
}
|
|
98
|
+
export type Statement = InterfaceStatement | AliasStatement | EnumStatement | ValuesStatement | HelperStatement;
|
|
99
|
+
export declare const BOOLEAN: KeywordNode;
|
|
100
|
+
export declare const NEVER: KeywordNode;
|
|
101
|
+
export declare const NUMBER: KeywordNode;
|
|
102
|
+
export declare const STRING: KeywordNode;
|
|
103
|
+
export declare const UNKNOWN: KeywordNode;
|
|
104
|
+
export declare const NULL: LiteralNode;
|
|
105
|
+
export declare const TRUE: LiteralNode;
|
|
106
|
+
export declare const FALSE: LiteralNode;
|
|
107
|
+
export declare const raw: (text: string) => RawNode;
|
|
108
|
+
export declare const object: (members: readonly Member[]) => ObjectNode;
|
|
109
|
+
export declare const tuple: (types: readonly TypeNode[]) => ListNode;
|
|
110
|
+
export declare const array: (type: TypeNode) => WrapNode;
|
|
111
|
+
export declare const readonlyOf: (type: TypeNode) => WrapNode;
|
|
112
|
+
export declare const rest: (type: TypeNode) => WrapNode;
|
|
113
|
+
export declare const access: (type: TypeNode, index: string) => AccessNode;
|
|
114
|
+
export declare const reference: (name: string, args?: readonly TypeNode[]) => ReferenceNode;
|
|
115
|
+
export declare const record: (key: TypeNode, value: TypeNode) => ReferenceNode;
|
|
116
|
+
export interface PropertyInit {
|
|
117
|
+
readonly name: string;
|
|
118
|
+
readonly type: TypeNode;
|
|
119
|
+
readonly readonly?: boolean;
|
|
120
|
+
readonly optional?: boolean;
|
|
121
|
+
readonly comment?: string | undefined;
|
|
122
|
+
}
|
|
123
|
+
export declare const property: (init: PropertyInit) => Property;
|
|
124
|
+
export declare const alias: (name: string, type: TypeNode) => AliasStatement;
|
|
125
|
+
/** Whether a node is one of the types upstream calls primitive: `unknown`, `true` and `false` are not. */
|
|
126
|
+
export declare const isPrimitive: (type: TypeNode) => boolean;
|
|
127
|
+
/**
|
|
128
|
+
* Drop repeated primitives, keeping the first. Literals with text (strings, numbers), `true`,
|
|
129
|
+
* `false`, `unknown` and every compound type are kept however often they appear.
|
|
130
|
+
*/
|
|
131
|
+
export declare const dedupe: (types: readonly TypeNode[]) => TypeNode[];
|
|
132
|
+
export declare const union: (types: readonly TypeNode[]) => TypeNode;
|
|
133
|
+
export declare const intersection: (types: readonly TypeNode[]) => TypeNode;
|
|
134
|
+
/** `T | null`, never deduplicated. */
|
|
135
|
+
export declare const nullable: (type: TypeNode) => ListNode;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
const keyword = (name) => ({ kind: 'keyword', name });
|
|
2
|
+
export const BOOLEAN = keyword('boolean');
|
|
3
|
+
export const NEVER = keyword('never');
|
|
4
|
+
export const NUMBER = keyword('number');
|
|
5
|
+
export const STRING = keyword('string');
|
|
6
|
+
export const UNKNOWN = keyword('unknown');
|
|
7
|
+
export const NULL = { kind: 'literal', text: 'null' };
|
|
8
|
+
export const TRUE = { kind: 'literal', text: 'true' };
|
|
9
|
+
export const FALSE = { kind: 'literal', text: 'false' };
|
|
10
|
+
export const raw = (text) => ({ kind: 'raw', text });
|
|
11
|
+
export const object = (members) => ({ kind: 'object', members });
|
|
12
|
+
export const tuple = (types) => ({ kind: 'tuple', types });
|
|
13
|
+
export const array = (type) => ({ kind: 'array', type });
|
|
14
|
+
export const readonlyOf = (type) => ({ kind: 'readonly', type });
|
|
15
|
+
export const rest = (type) => ({ kind: 'rest', type });
|
|
16
|
+
export const access = (type, index) => ({ kind: 'access', type, index });
|
|
17
|
+
export const reference = (name, args = []) => ({
|
|
18
|
+
kind: 'reference',
|
|
19
|
+
name,
|
|
20
|
+
args,
|
|
21
|
+
});
|
|
22
|
+
export const record = (key, value) => reference('Record', [key, value]);
|
|
23
|
+
export const property = (init) => ({
|
|
24
|
+
kind: 'property',
|
|
25
|
+
readonly: init.readonly ?? false,
|
|
26
|
+
name: init.name,
|
|
27
|
+
optional: init.optional ?? false,
|
|
28
|
+
type: init.type,
|
|
29
|
+
comment: init.comment,
|
|
30
|
+
});
|
|
31
|
+
export const alias = (name, type) => ({ kind: 'alias', name, type });
|
|
32
|
+
export const isPrimitive = (type) => type === NULL || (type.kind === 'keyword' && type !== UNKNOWN);
|
|
33
|
+
export const dedupe = (types) => {
|
|
34
|
+
const seen = new Set();
|
|
35
|
+
return types.filter((type) => {
|
|
36
|
+
if (!isPrimitive(type))
|
|
37
|
+
return true;
|
|
38
|
+
if (seen.has(type))
|
|
39
|
+
return false;
|
|
40
|
+
seen.add(type);
|
|
41
|
+
return true;
|
|
42
|
+
});
|
|
43
|
+
};
|
|
44
|
+
const list = (kind, types) => {
|
|
45
|
+
const [first] = types;
|
|
46
|
+
if (first === undefined)
|
|
47
|
+
return NEVER;
|
|
48
|
+
return types.length === 1 ? first : { kind, types: dedupe(types) };
|
|
49
|
+
};
|
|
50
|
+
export const union = (types) => list('union', types);
|
|
51
|
+
export const intersection = (types) => list('intersection', types);
|
|
52
|
+
export const nullable = (type) => ({ kind: 'union', types: [type, NULL] });
|
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
2
|
+
import { basename, dirname, extname, relative, resolve } from 'node:path';
|
|
3
|
+
import { parse as parseYaml } from 'seniority/yaml';
|
|
4
|
+
import { dict, isDict } from './gen-ts-dict.js';
|
|
5
|
+
import { GenerateError } from './gen-ts-index.js';
|
|
6
|
+
import { childPath, pointerOf } from './gen-ts-ref.js';
|
|
7
|
+
const CHILDREN = {
|
|
8
|
+
root: { paths: '*pathItem', webhooks: '*pathItem', components: 'components', definitions: '*schema', parameters: '*parameter', responses: '*response' },
|
|
9
|
+
components: {
|
|
10
|
+
schemas: '*schema',
|
|
11
|
+
parameters: '*parameter',
|
|
12
|
+
responses: '*response',
|
|
13
|
+
requestBodies: '*requestBody',
|
|
14
|
+
headers: '*header',
|
|
15
|
+
examples: '*example',
|
|
16
|
+
links: '*link',
|
|
17
|
+
callbacks: '*callback',
|
|
18
|
+
pathItems: '*pathItem',
|
|
19
|
+
securitySchemes: '*securityScheme',
|
|
20
|
+
},
|
|
21
|
+
pathItem: { get: 'operation', put: 'operation', post: 'operation', delete: 'operation', options: 'operation', head: 'operation', patch: 'operation', trace: 'operation', parameters: 'parameter' },
|
|
22
|
+
operation: { parameters: 'parameter', requestBody: 'requestBody', responses: '*response', callbacks: '*callback' },
|
|
23
|
+
callback: { '*': 'pathItem' },
|
|
24
|
+
parameter: { schema: 'schema', items: 'schema', content: '*mediaType', examples: '*example' },
|
|
25
|
+
header: { schema: 'schema', content: '*mediaType', examples: '*example' },
|
|
26
|
+
requestBody: { content: '*mediaType' },
|
|
27
|
+
response: { schema: 'schema', headers: '*header', content: '*mediaType', links: '*link' },
|
|
28
|
+
mediaType: { schema: 'schema', examples: '*example', encoding: '*encoding' },
|
|
29
|
+
encoding: { headers: '*header' },
|
|
30
|
+
schema: {
|
|
31
|
+
properties: '*schema',
|
|
32
|
+
patternProperties: '*schema',
|
|
33
|
+
dependentSchemas: '*schema',
|
|
34
|
+
$defs: '*schema',
|
|
35
|
+
definitions: '*schema',
|
|
36
|
+
allOf: 'schema',
|
|
37
|
+
anyOf: 'schema',
|
|
38
|
+
oneOf: 'schema',
|
|
39
|
+
prefixItems: 'schema',
|
|
40
|
+
items: 'schema',
|
|
41
|
+
additionalItems: 'schema',
|
|
42
|
+
additionalProperties: 'schema',
|
|
43
|
+
unevaluatedItems: 'schema',
|
|
44
|
+
unevaluatedProperties: 'schema',
|
|
45
|
+
propertyNames: 'schema',
|
|
46
|
+
contains: 'schema',
|
|
47
|
+
not: 'schema',
|
|
48
|
+
if: 'schema',
|
|
49
|
+
then: 'schema',
|
|
50
|
+
else: 'schema',
|
|
51
|
+
},
|
|
52
|
+
};
|
|
53
|
+
const GROUPS_3 = {
|
|
54
|
+
schema: 'schemas',
|
|
55
|
+
parameter: 'parameters',
|
|
56
|
+
response: 'responses',
|
|
57
|
+
requestBody: 'requestBodies',
|
|
58
|
+
header: 'headers',
|
|
59
|
+
example: 'examples',
|
|
60
|
+
link: 'links',
|
|
61
|
+
callback: 'callbacks',
|
|
62
|
+
securityScheme: 'securitySchemes',
|
|
63
|
+
};
|
|
64
|
+
const GROUPS_2 = { schema: 'definitions', parameter: 'parameters', response: 'responses' };
|
|
65
|
+
const DATA_MEMBERS = new Set(['example', 'default', 'enum', 'const']);
|
|
66
|
+
const isData = (key) => DATA_MEMBERS.has(key) || key.startsWith('x-');
|
|
67
|
+
const hasExternalRef = (node) => {
|
|
68
|
+
if (typeof node !== 'object' || node === null)
|
|
69
|
+
return false;
|
|
70
|
+
const { $ref } = node;
|
|
71
|
+
if (typeof $ref === 'string' && !$ref.startsWith('#'))
|
|
72
|
+
return true;
|
|
73
|
+
return Object.values(node).some((member) => hasExternalRef(member));
|
|
74
|
+
};
|
|
75
|
+
const escape = (token) => token.replaceAll('~', '~0').replaceAll('/', '~1');
|
|
76
|
+
const follow = (node, fragment) => {
|
|
77
|
+
let at = node;
|
|
78
|
+
for (const token of pointerOf(`#${fragment}`)) {
|
|
79
|
+
if (typeof at !== 'object' || at === null || !Object.hasOwn(at, token))
|
|
80
|
+
return undefined;
|
|
81
|
+
at = at[token];
|
|
82
|
+
}
|
|
83
|
+
return at;
|
|
84
|
+
};
|
|
85
|
+
const same = (left, right) => JSON.stringify(left) === JSON.stringify(right);
|
|
86
|
+
const REMOTE = /^[a-z][\d+.a-z-]*:/iu;
|
|
87
|
+
const targetOf = (ref, from) => {
|
|
88
|
+
const [address = '', fragment = ''] = ref.split('#');
|
|
89
|
+
return { path: address === '' ? from : resolve(dirname(from), decodeURI(address)), fragment };
|
|
90
|
+
};
|
|
91
|
+
const kindOf = (kind, key) => {
|
|
92
|
+
if (kind.startsWith('*'))
|
|
93
|
+
return kind.slice(1);
|
|
94
|
+
if (isData(key))
|
|
95
|
+
return undefined;
|
|
96
|
+
const children = new Map(Object.entries(CHILDREN[kind] ?? {}));
|
|
97
|
+
return children.get(key) ?? children.get('*') ?? 'other';
|
|
98
|
+
};
|
|
99
|
+
const keyOf = ({ from, at }) => `${from}${at}`;
|
|
100
|
+
export const bundle = (document, file) => {
|
|
101
|
+
if (!isDict(document) || !hasExternalRef(document))
|
|
102
|
+
return document;
|
|
103
|
+
const openapi = document.swagger === undefined;
|
|
104
|
+
const groups = new Map(Object.entries(openapi ? GROUPS_3 : GROUPS_2));
|
|
105
|
+
const own = openapi ? dict(document.components) : document;
|
|
106
|
+
const shown = (path) => relative(dirname(file), path).replaceAll('\\', '/');
|
|
107
|
+
const where = ({ from, at }) => (from === file ? at : `${shown(from)}${at}`);
|
|
108
|
+
const files = new Map([[file, document]]);
|
|
109
|
+
const load = (path, ref, site) => {
|
|
110
|
+
const cached = files.get(path);
|
|
111
|
+
if (cached !== undefined)
|
|
112
|
+
return cached;
|
|
113
|
+
if (!existsSync(path)) {
|
|
114
|
+
throw new GenerateError(`Can't resolve $ref at ${where(site)}: \`${ref}\` names ${shown(path)}, which does not exist.`, `add ${shown(path)} next to ${basename(file)}, or replace the \`$ref\` with what it names`);
|
|
115
|
+
}
|
|
116
|
+
const text = readFileSync(path, 'utf8');
|
|
117
|
+
let parsed;
|
|
118
|
+
try {
|
|
119
|
+
parsed = path.endsWith('.json') ? JSON.parse(text) : parseYaml(text);
|
|
120
|
+
}
|
|
121
|
+
catch (error) {
|
|
122
|
+
throw new GenerateError(`${shown(path)} is not JSON or YAML (${error.message}).`, `correct ${shown(path)}`);
|
|
123
|
+
}
|
|
124
|
+
files.set(path, parsed);
|
|
125
|
+
return parsed;
|
|
126
|
+
};
|
|
127
|
+
const names = new Map();
|
|
128
|
+
const hoisted = new Map();
|
|
129
|
+
const waiting = new Map();
|
|
130
|
+
const inlining = new Set();
|
|
131
|
+
const nameFor = (group, path, fragment, content) => {
|
|
132
|
+
const existing = new Map(Object.entries(dict(own[group])));
|
|
133
|
+
const taken = hoisted.get(group) ?? new Map();
|
|
134
|
+
const fits = (name) => {
|
|
135
|
+
if (taken.has(name))
|
|
136
|
+
return same(taken.get(name), content);
|
|
137
|
+
if (!existing.has(name))
|
|
138
|
+
return true;
|
|
139
|
+
const member = dict(existing.get(name));
|
|
140
|
+
if (typeof member.$ref !== 'string')
|
|
141
|
+
return same(member, content);
|
|
142
|
+
const other = targetOf(member.$ref, file);
|
|
143
|
+
return other.path === path && other.fragment === fragment;
|
|
144
|
+
};
|
|
145
|
+
let name = '';
|
|
146
|
+
for (const token of pointerOf(`#${fragment}`).toReversed()) {
|
|
147
|
+
name = name === '' ? token : `${token}-${name}`;
|
|
148
|
+
if (fits(name))
|
|
149
|
+
return name;
|
|
150
|
+
}
|
|
151
|
+
const base = basename(path, extname(path));
|
|
152
|
+
name = name === '' ? base : `${base}_${name}`;
|
|
153
|
+
let candidate = name;
|
|
154
|
+
for (let serial = 2; !fits(candidate); serial += 1)
|
|
155
|
+
candidate = `${name}-${String(serial)}`;
|
|
156
|
+
return candidate;
|
|
157
|
+
};
|
|
158
|
+
const local = (group, name) => (openapi ? `#/components/${group}/${escape(name)}` : `#/${group}/${escape(name)}`);
|
|
159
|
+
const inline = (value, ref, site, into) => {
|
|
160
|
+
const key = keyOf(into);
|
|
161
|
+
if (inlining.has(key)) {
|
|
162
|
+
throw new GenerateError(`Can't bundle $ref at ${where(site)}: \`${ref}\` contains itself, and is written in place.`, 'move the part that repeats into a schema, which is hoisted and named');
|
|
163
|
+
}
|
|
164
|
+
inlining.add(key);
|
|
165
|
+
const out = walk(value, into);
|
|
166
|
+
inlining.delete(key);
|
|
167
|
+
return out;
|
|
168
|
+
};
|
|
169
|
+
const hoist = (node, group, value, into) => {
|
|
170
|
+
const key = keyOf(into);
|
|
171
|
+
const known = names.get(key);
|
|
172
|
+
if (known !== undefined)
|
|
173
|
+
return { ...node, $ref: local(group, known) };
|
|
174
|
+
const out = { ...node };
|
|
175
|
+
const pending = waiting.get(key);
|
|
176
|
+
if (pending !== undefined) {
|
|
177
|
+
pending.push(out);
|
|
178
|
+
return out;
|
|
179
|
+
}
|
|
180
|
+
const references = [out];
|
|
181
|
+
waiting.set(key, references);
|
|
182
|
+
const content = walk(value, into);
|
|
183
|
+
const name = nameFor(group, into.from, into.at.slice(1), content);
|
|
184
|
+
names.set(key, name);
|
|
185
|
+
const members = hoisted.get(group) ?? new Map();
|
|
186
|
+
hoisted.set(group, members);
|
|
187
|
+
members.set(name, content);
|
|
188
|
+
for (const each of references)
|
|
189
|
+
each.$ref = local(group, name);
|
|
190
|
+
return out;
|
|
191
|
+
};
|
|
192
|
+
const reference = (node, ref, site) => {
|
|
193
|
+
if (ref.startsWith('#') && site.from === file)
|
|
194
|
+
return node;
|
|
195
|
+
if (REMOTE.test(ref)) {
|
|
196
|
+
throw new GenerateError(`Can't resolve $ref at ${where(site)}: \`${ref}\` is a remote address, and this package does not fetch.`, `download it next to ${basename(file)} and point the \`$ref\` at the copy`);
|
|
197
|
+
}
|
|
198
|
+
const { path, fragment } = targetOf(ref, site.from);
|
|
199
|
+
const value = follow(load(path, ref, site), fragment);
|
|
200
|
+
if (value === undefined) {
|
|
201
|
+
throw new GenerateError(`Can't resolve $ref at ${where(site)}: \`${ref}\` leads nowhere in ${shown(path)}.`, `point the \`$ref\` at a member ${shown(path)} has`);
|
|
202
|
+
}
|
|
203
|
+
const into = { kind: site.kind, from: path, at: `#${fragment}` };
|
|
204
|
+
const group = groups.get(site.kind);
|
|
205
|
+
if (group !== undefined)
|
|
206
|
+
return hoist(node, group, value, into);
|
|
207
|
+
const inlined = inline(value, ref, site, into);
|
|
208
|
+
if (site.kind !== 'pathItem')
|
|
209
|
+
return inlined;
|
|
210
|
+
const { $ref: _, ...siblings } = node;
|
|
211
|
+
return { ...dict(inlined), ...dict(walk(siblings, site)) };
|
|
212
|
+
};
|
|
213
|
+
const walk = (node, site) => {
|
|
214
|
+
if (Array.isArray(node))
|
|
215
|
+
return node.map((member, index) => walk(member, { ...site, at: childPath(site.at, String(index)) }));
|
|
216
|
+
if (!isDict(node))
|
|
217
|
+
return node;
|
|
218
|
+
const { $ref } = node;
|
|
219
|
+
if (!site.kind.startsWith('*') && typeof $ref === 'string')
|
|
220
|
+
return reference(node, $ref, site);
|
|
221
|
+
return Object.fromEntries(Object.entries(node).map(([key, member]) => {
|
|
222
|
+
const kind = kindOf(site.kind, key);
|
|
223
|
+
return [key, kind === undefined ? member : walk(member, { kind, from: site.from, at: childPath(site.at, key) })];
|
|
224
|
+
}));
|
|
225
|
+
};
|
|
226
|
+
let root = dict(walk(document, { kind: 'root', from: file, at: '#' }));
|
|
227
|
+
for (const [group, members] of hoisted) {
|
|
228
|
+
const holder = openapi ? dict(root.components) : root;
|
|
229
|
+
const merged = { [group]: { ...dict(holder[group]), ...Object.fromEntries(members) } };
|
|
230
|
+
root = openapi ? { ...root, components: { ...holder, ...merged } } : { ...root, ...merged };
|
|
231
|
+
}
|
|
232
|
+
return root;
|
|
233
|
+
};
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { isDict } from './gen-ts-dict.js';
|
|
2
|
+
const LINE_BREAK = /\r?\n/g;
|
|
3
|
+
const CONTINUATION = '\n * ';
|
|
4
|
+
const JSON_INDENT = 2;
|
|
5
|
+
const inline = (value) => {
|
|
6
|
+
const text = isDict(value) || Array.isArray(value) ? JSON.stringify(value, null, JSON_INDENT) : value;
|
|
7
|
+
return String(text).trim().replaceAll(LINE_BREAK, CONTINUATION);
|
|
8
|
+
};
|
|
9
|
+
const heading = (value) => typeof value === 'string' && value !== '' ? [value.trim().replaceAll(LINE_BREAK, CONTINUATION)] : [];
|
|
10
|
+
const tagged = (schema, field) => {
|
|
11
|
+
const value = schema[field];
|
|
12
|
+
if (value === undefined || (value === '' && field === 'description'))
|
|
13
|
+
return [];
|
|
14
|
+
return [`@${field} ${inline(value)}`];
|
|
15
|
+
};
|
|
16
|
+
const examples = (schema) => Array.isArray(schema.examples) ? schema.examples.map((example) => `@example ${inline(example)}`) : [];
|
|
17
|
+
const enumTag = (schema) => {
|
|
18
|
+
if (!schema.enum)
|
|
19
|
+
return [];
|
|
20
|
+
let type = 'unknown';
|
|
21
|
+
if (Array.isArray(schema.type))
|
|
22
|
+
type = schema.type.join('|');
|
|
23
|
+
else if (typeof schema.type === 'string')
|
|
24
|
+
type = schema.type;
|
|
25
|
+
return [`@enum {${type}${schema.nullable ? '|null' : ''}}`];
|
|
26
|
+
};
|
|
27
|
+
const lines = (schema) => [
|
|
28
|
+
...heading(schema.title),
|
|
29
|
+
...heading(schema.summary),
|
|
30
|
+
...(schema.format ? [`Format: ${String(schema.format)}`] : []),
|
|
31
|
+
...(schema.deprecated ? ['@deprecated'] : []),
|
|
32
|
+
...tagged(schema, 'description'),
|
|
33
|
+
...tagged(schema, 'default'),
|
|
34
|
+
...tagged(schema, 'example'),
|
|
35
|
+
...examples(schema),
|
|
36
|
+
...('const' in schema ? ['@constant'] : []),
|
|
37
|
+
...enumTag(schema),
|
|
38
|
+
];
|
|
39
|
+
export const jsDoc = (schema) => {
|
|
40
|
+
if (!isDict(schema))
|
|
41
|
+
return undefined;
|
|
42
|
+
const output = lines(schema);
|
|
43
|
+
const [only] = output;
|
|
44
|
+
if (only === undefined)
|
|
45
|
+
return undefined;
|
|
46
|
+
const text = output.length === 1 && !only.includes('\n') ? `* ${only} ` : `*\n * ${output.join('\n * ')}\n `;
|
|
47
|
+
return text.replaceAll('*/', String.raw `*\/`);
|
|
48
|
+
};
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/** The options a run is generated under, and the state the transforms share while it runs. */
|
|
2
|
+
import { type EnumStatement, type Statement } from './gen-ts-ast.js';
|
|
3
|
+
import { type NameState } from './gen-ts-text.js';
|
|
4
|
+
/** openapi-typescript's data-only options, under the same names and defaults. */
|
|
5
|
+
export interface GenerateOptions {
|
|
6
|
+
/** Treat every object as open: add `[key: string]: unknown`. */
|
|
7
|
+
readonly additionalProperties?: boolean;
|
|
8
|
+
/** Sort members by name instead of keeping document order. */
|
|
9
|
+
readonly alphabetize?: boolean;
|
|
10
|
+
/** Turn `minItems`/`maxItems` into tuple types. */
|
|
11
|
+
readonly arrayLength?: boolean;
|
|
12
|
+
/** A property with a `default` is not optional. On by default. */
|
|
13
|
+
readonly defaultNonNullable?: boolean;
|
|
14
|
+
/** An object with no members is `Record<string, unknown>` instead of `Record<string, never>`. */
|
|
15
|
+
readonly emptyObjectsUnknown?: boolean;
|
|
16
|
+
/** Emit TypeScript enums instead of string unions. */
|
|
17
|
+
readonly enum?: boolean;
|
|
18
|
+
/** Emit each enum's values as an exported array. */
|
|
19
|
+
readonly enumValues?: boolean;
|
|
20
|
+
/** With `enum`, only convert schemas that carry `x-enum-*` metadata. */
|
|
21
|
+
readonly conditionalEnums?: boolean;
|
|
22
|
+
/** With `enum`, emit each distinct set of members once. */
|
|
23
|
+
readonly dedupeEnums?: boolean;
|
|
24
|
+
/** Leave deprecated members out. */
|
|
25
|
+
readonly excludeDeprecated?: boolean;
|
|
26
|
+
/** Emit `type` aliases instead of interfaces. */
|
|
27
|
+
readonly exportType?: boolean;
|
|
28
|
+
/** Mark every member and array `readonly`. */
|
|
29
|
+
readonly immutable?: boolean;
|
|
30
|
+
/** Emit an alias per component at the top level. */
|
|
31
|
+
readonly rootTypes?: boolean;
|
|
32
|
+
/** With `rootTypes`, drop the `Schema` prefix. */
|
|
33
|
+
readonly rootTypesNoSchemaPrefix?: boolean;
|
|
34
|
+
/** With `rootTypes`, keep schema names as written. */
|
|
35
|
+
readonly rootTypesKeepCasing?: boolean;
|
|
36
|
+
/** Key parameterised paths by a template literal type. */
|
|
37
|
+
readonly pathParamsAsTypes?: boolean;
|
|
38
|
+
/** A schema without `required` requires every property. */
|
|
39
|
+
readonly propertiesRequiredByDefault?: boolean;
|
|
40
|
+
/** Emit an `ApiPaths` enum of every path. */
|
|
41
|
+
readonly makePathsEnum?: boolean;
|
|
42
|
+
/** Add the path parameters a URL names but its operation does not declare. */
|
|
43
|
+
readonly generatePathParams?: boolean;
|
|
44
|
+
/** Wrap `readOnly`/`writeOnly` properties in `$Read<>`/`$Write<>`. */
|
|
45
|
+
readonly readWriteMarkers?: boolean;
|
|
46
|
+
/** TypeScript written above the generated types, as given: upstream parses and re-prints it. */
|
|
47
|
+
readonly inject?: string;
|
|
48
|
+
}
|
|
49
|
+
export interface Discriminator {
|
|
50
|
+
readonly propertyName: string;
|
|
51
|
+
readonly mapping?: Readonly<Record<string, string>>;
|
|
52
|
+
}
|
|
53
|
+
export interface Discriminators {
|
|
54
|
+
/** Every discriminator in the document, by the pointer of the schema that declares or inherits it. */
|
|
55
|
+
readonly objects: Record<string, Discriminator>;
|
|
56
|
+
/** Schemas whose discriminator property was already written into them. */
|
|
57
|
+
readonly refsHandled: string[];
|
|
58
|
+
}
|
|
59
|
+
export interface Context extends Required<GenerateOptions> {
|
|
60
|
+
readonly discriminators: Discriminators;
|
|
61
|
+
/** Statements hoisted to the end of the file: operations, enums, value arrays, helper types. */
|
|
62
|
+
readonly injectFooter: Statement[];
|
|
63
|
+
/** What a `$ref` points at, or `undefined`. */
|
|
64
|
+
readonly resolve: (ref: string) => unknown;
|
|
65
|
+
/** The reference a chain of `$ref`s starting at `ref` ends on. */
|
|
66
|
+
readonly lastRef: (ref: string) => string;
|
|
67
|
+
}
|
|
68
|
+
/** Where a transform stands: the pointer of the node it was handed, and the run it belongs to. */
|
|
69
|
+
export interface TransformOptions {
|
|
70
|
+
readonly path?: string;
|
|
71
|
+
readonly ctx: Context;
|
|
72
|
+
/** Inside an object's property or index signature, where a type may name itself: tsc defers it. */
|
|
73
|
+
readonly behindObject?: boolean;
|
|
74
|
+
}
|
|
75
|
+
/** State upstream keeps in module scope, kept per run here so one run cannot change the next. */
|
|
76
|
+
export interface RunState {
|
|
77
|
+
readonly names: NameState;
|
|
78
|
+
readonly enums: Map<string, EnumStatement>;
|
|
79
|
+
}
|
|
80
|
+
export declare const stateOf: (ctx: Context) => RunState;
|
|
81
|
+
export declare const DEFAULTS: Required<GenerateOptions>;
|
|
82
|
+
/** A context for `document` with every option at its default unless given. */
|
|
83
|
+
export declare const createContext: (document: unknown, options?: GenerateOptions, discriminators?: Discriminators) => Context;
|