@contractkit/plugin-typescript 0.16.1
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/.turbo/turbo-build$colon$ci.log +35 -0
- package/.turbo/turbo-build.log +15 -0
- package/.turbo/turbo-test$colon$ci.log +81 -0
- package/.turbo/turbo-test.log +19 -0
- package/CHANGELOG.md +151 -0
- package/README.md +153 -0
- package/coverage/base.css +224 -0
- package/coverage/block-navigation.js +87 -0
- package/coverage/clover.xml +1882 -0
- package/coverage/coverage-final.json +9 -0
- package/coverage/favicon.png +0 -0
- package/coverage/index.html +131 -0
- package/coverage/prettify.css +1 -0
- package/coverage/prettify.js +2 -0
- package/coverage/sort-arrow-sprite.png +0 -0
- package/coverage/sorter.js +210 -0
- package/coverage/src/codegen-contract.ts.html +3331 -0
- package/coverage/src/codegen-operation.ts.html +2530 -0
- package/coverage/src/codegen-plain-types.ts.html +901 -0
- package/coverage/src/codegen-sdk.ts.html +2797 -0
- package/coverage/src/index.html +206 -0
- package/coverage/src/index.ts.html +1360 -0
- package/coverage/src/path-utils.ts.html +649 -0
- package/coverage/src/ts-render.ts.html +592 -0
- package/coverage/tests/helpers.ts.html +826 -0
- package/coverage/tests/index.html +116 -0
- package/dist/codegen-contract.d.ts +56 -0
- package/dist/codegen-contract.d.ts.map +1 -0
- package/dist/codegen-operation.d.ts +25 -0
- package/dist/codegen-operation.d.ts.map +1 -0
- package/dist/codegen-plain-types.d.ts +10 -0
- package/dist/codegen-plain-types.d.ts.map +1 -0
- package/dist/codegen-sdk.d.ts +38 -0
- package/dist/codegen-sdk.d.ts.map +1 -0
- package/dist/index.d.ts +77 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3162 -0
- package/dist/index.js.map +1 -0
- package/dist/path-utils.d.ts +15 -0
- package/dist/path-utils.d.ts.map +1 -0
- package/dist/ts-render.d.ts +20 -0
- package/dist/ts-render.d.ts.map +1 -0
- package/eslint.config.js +6 -0
- package/package.json +43 -0
- package/src/codegen-contract.ts +1082 -0
- package/src/codegen-operation.ts +815 -0
- package/src/codegen-plain-types.ts +272 -0
- package/src/codegen-sdk.ts +904 -0
- package/src/index.ts +425 -0
- package/src/path-utils.ts +188 -0
- package/src/ts-render.ts +169 -0
- package/tests/codegen-contract.test.ts +1004 -0
- package/tests/codegen-operation.test.ts +939 -0
- package/tests/codegen-plain-types.test.ts +636 -0
- package/tests/codegen-sdk.test.ts +1500 -0
- package/tests/codegen-server.test.ts +192 -0
- package/tests/helpers.ts +247 -0
- package/tests/pipeline.test.ts +372 -0
- package/tsconfig.json +9 -0
- package/vitest.config.ts +14 -0
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
import { relative, dirname } from 'node:path';
|
|
2
|
+
import type { ContractRootNode, ModelNode, FieldNode } from '@contractkit/core';
|
|
3
|
+
import { computeModelsWithOutput, collectExternalOutputRefs } from '@contractkit/core';
|
|
4
|
+
import type { ContractCodegenContext } from './codegen-contract.js';
|
|
5
|
+
import {
|
|
6
|
+
collectExternalRefs,
|
|
7
|
+
collectExternalInputRefs,
|
|
8
|
+
computeModelsWithInput,
|
|
9
|
+
topoSortModels,
|
|
10
|
+
resolveImportPath,
|
|
11
|
+
rootNeedsScalar,
|
|
12
|
+
} from './codegen-contract.js';
|
|
13
|
+
import { renderTsType, renderInputTsType, renderOutputTsType, quoteKey, JSON_VALUE_TYPE_DECL } from './ts-render.js';
|
|
14
|
+
|
|
15
|
+
// ─── Public entry point ────────────────────────────────────────────────────
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Generate plain TypeScript interfaces/types from a contract AST.
|
|
19
|
+
* Unlike `generateContract()` which produces Zod schemas, this emits
|
|
20
|
+
* vanilla TypeScript `interface` and `type` declarations suitable
|
|
21
|
+
* for SDK consumers that don't need runtime validation.
|
|
22
|
+
*/
|
|
23
|
+
export function generatePlainTypes(root: ContractRootNode, context?: ContractCodegenContext): string {
|
|
24
|
+
const externalRefs = collectExternalRefs(root);
|
|
25
|
+
const lines: string[] = [];
|
|
26
|
+
|
|
27
|
+
// Compute which models have Input variants (local, incl. transitive deps + external)
|
|
28
|
+
const externalModelsWithInput = context?.modelsWithInput ?? new Set<string>();
|
|
29
|
+
const localModelsWithInput = computeModelsWithInput(root.models, externalModelsWithInput);
|
|
30
|
+
const allModelsWithInput = new Set([...localModelsWithInput, ...externalModelsWithInput]);
|
|
31
|
+
|
|
32
|
+
// Compute which models have Output variants (post-transform wire shape)
|
|
33
|
+
const externalModelsWithOutput = context?.modelsWithOutput ?? new Set<string>();
|
|
34
|
+
const localModelsWithOutput = computeModelsWithOutput(root.models, externalModelsWithOutput);
|
|
35
|
+
const allModelsWithOutput = new Set([...localModelsWithOutput, ...externalModelsWithOutput]);
|
|
36
|
+
|
|
37
|
+
// Collect additional external Input/Output refs needed for variant fields
|
|
38
|
+
const externalInputRefs = allModelsWithInput.size > 0 ? collectExternalInputRefs(root, allModelsWithInput) : [];
|
|
39
|
+
const externalOutputRefs = allModelsWithOutput.size > 0 ? collectExternalOutputRefs(root, allModelsWithOutput) : [];
|
|
40
|
+
const allExternalRefs = [...new Set([...externalRefs, ...externalInputRefs, ...externalOutputRefs])].sort();
|
|
41
|
+
|
|
42
|
+
// Type-only imports for external references
|
|
43
|
+
for (const ref of allExternalRefs) {
|
|
44
|
+
const importPath = resolveImportPath(ref, context);
|
|
45
|
+
lines.push(`import type { ${ref} } from '${importPath}';`);
|
|
46
|
+
}
|
|
47
|
+
if (allExternalRefs.length > 0) lines.push('');
|
|
48
|
+
|
|
49
|
+
if (rootNeedsScalar(root, 'json')) {
|
|
50
|
+
if (context?.jsonValueImportPath) {
|
|
51
|
+
lines.push(`import type { JsonValue } from '${context.jsonValueImportPath}';`);
|
|
52
|
+
} else {
|
|
53
|
+
lines.push(JSON_VALUE_TYPE_DECL);
|
|
54
|
+
}
|
|
55
|
+
lines.push('');
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
for (const model of topoSortModels(root.models)) {
|
|
59
|
+
lines.push(...generateModel(model, context?.currentOutPath, allModelsWithInput, allModelsWithOutput));
|
|
60
|
+
lines.push('');
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return lines.join('\n');
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// ─── Model ─────────────────────────────────────────────────────────────────
|
|
67
|
+
|
|
68
|
+
function generateModel(model: ModelNode, outPath?: string, modelsWithInput?: Set<string>, modelsWithOutput?: Set<string>): string[] {
|
|
69
|
+
// Type alias: Name : typeExpression
|
|
70
|
+
if (model.type) {
|
|
71
|
+
return generateTypeAlias(model, outPath, modelsWithInput, modelsWithOutput);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// A model needs Input/read split if it has visibility-modified fields OR if it
|
|
75
|
+
// transitively references models that have Input variants (captured in modelsWithInput).
|
|
76
|
+
const needsInputSplit = model.fields.some(f => f.visibility !== 'normal') || (modelsWithInput?.has(model.name) ?? false);
|
|
77
|
+
|
|
78
|
+
const lines = needsInputSplit ? generateVisibilityModel(model, outPath, modelsWithInput) : generateSimpleModel(model, outPath);
|
|
79
|
+
|
|
80
|
+
if (modelsWithOutput?.has(model.name)) {
|
|
81
|
+
lines.push('');
|
|
82
|
+
lines.push(...generateOutputModel(model, modelsWithOutput));
|
|
83
|
+
}
|
|
84
|
+
return lines;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function generateComments(model: ModelNode, outPath?: string): string[] {
|
|
88
|
+
const lines: string[] = [];
|
|
89
|
+
lines.push('/**');
|
|
90
|
+
if (model.deprecated) {
|
|
91
|
+
lines.push(` * @deprecated`);
|
|
92
|
+
}
|
|
93
|
+
if (model.description) {
|
|
94
|
+
lines.push(` * ${model.description}`);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const relPath = outPath ? relative(dirname(outPath), model.loc.file) : model.loc.file;
|
|
98
|
+
lines.push(` * generated from [${model.name}](file://./${relPath}#L${model.loc.line})`);
|
|
99
|
+
lines.push(' */');
|
|
100
|
+
return lines;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function generateTypeAlias(model: ModelNode, outPath?: string, modelsWithInput?: Set<string>, modelsWithOutput?: Set<string>): string[] {
|
|
104
|
+
const lines: string[] = [];
|
|
105
|
+
lines.push(...generateComments(model, outPath));
|
|
106
|
+
lines.push(`export type ${model.name} = ${renderTsType(model.type!)};`);
|
|
107
|
+
if (modelsWithInput?.has(model.name)) {
|
|
108
|
+
lines.push(`export type ${model.name}Input = ${renderInputTsType(model.type!, modelsWithInput)};`);
|
|
109
|
+
}
|
|
110
|
+
if (modelsWithOutput?.has(model.name)) {
|
|
111
|
+
lines.push(`export type ${model.name}Output = ${renderOutputTsType(model.type!, modelsWithOutput)};`);
|
|
112
|
+
}
|
|
113
|
+
return lines;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** Build the `extends` clause for a multi-base model.
|
|
117
|
+
* Override-marked field names are wrapped in `Omit<Base, 'name1' | 'name2'>` per base so the
|
|
118
|
+
* subclass can legally redeclare them with new types. TypeScript's `Omit<T, K extends keyof any>`
|
|
119
|
+
* tolerates omit keys that don't appear on the base, so we omit unconditionally — no need to know
|
|
120
|
+
* each base's actual field set. */
|
|
121
|
+
function buildExtendsClause(bases: string[], overrideNames: string[], baseNameResolver: (b: string) => string): string {
|
|
122
|
+
if (bases.length === 0) return '';
|
|
123
|
+
if (overrideNames.length === 0) return ` extends ${bases.map(baseNameResolver).join(', ')}`;
|
|
124
|
+
const omitKeys = overrideNames.map(n => `'${n}'`).join(' | ');
|
|
125
|
+
const wrapped = bases.map(b => `Omit<${baseNameResolver(b)}, ${omitKeys}>`);
|
|
126
|
+
return ` extends ${wrapped.join(', ')}`;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function generateSimpleModel(model: ModelNode, outPath?: string): string[] {
|
|
130
|
+
const lines: string[] = [];
|
|
131
|
+
lines.push(...generateComments(model, outPath));
|
|
132
|
+
|
|
133
|
+
const bases = model.bases ?? [];
|
|
134
|
+
const overrideNames = model.fields.filter(f => f.override).map(f => f.name);
|
|
135
|
+
lines.push(`export interface ${model.name}${buildExtendsClause(bases, overrideNames, b => b)} {`);
|
|
136
|
+
|
|
137
|
+
for (const field of model.fields) {
|
|
138
|
+
lines.push(` ${renderField(field)}`);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
lines.push('}');
|
|
142
|
+
return lines;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function generateVisibilityModel(model: ModelNode, outPath?: string, modelsWithInput?: Set<string>): string[] {
|
|
146
|
+
const lines: string[] = [];
|
|
147
|
+
lines.push(...generateComments(model, outPath));
|
|
148
|
+
|
|
149
|
+
const bases = model.bases ?? [];
|
|
150
|
+
const overrideNames = model.fields.filter(f => f.override).map(f => f.name);
|
|
151
|
+
|
|
152
|
+
// Read type — omit writeonly fields
|
|
153
|
+
const readFields = model.fields.filter(f => f.visibility !== 'writeonly');
|
|
154
|
+
lines.push(`export interface ${model.name}${buildExtendsClause(bases, overrideNames, b => b)} {`);
|
|
155
|
+
for (const field of readFields) {
|
|
156
|
+
lines.push(` ${renderField(field)}`);
|
|
157
|
+
}
|
|
158
|
+
lines.push('}');
|
|
159
|
+
lines.push('');
|
|
160
|
+
|
|
161
|
+
// Write type — omit readonly fields (use Input variants for sub-type refs);
|
|
162
|
+
// extends ParentInput if parent has an Input variant, else extends parent read type
|
|
163
|
+
const writeFields = model.fields.filter(f => f.visibility !== 'readonly');
|
|
164
|
+
const inputResolver = (b: string) => (modelsWithInput?.has(b) ? `${b}Input` : b);
|
|
165
|
+
lines.push(`export interface ${model.name}Input${buildExtendsClause(bases, overrideNames, inputResolver)} {`);
|
|
166
|
+
for (const field of writeFields) {
|
|
167
|
+
lines.push(` ${modelsWithInput ? renderInputField(field, modelsWithInput) : renderField(field)}`);
|
|
168
|
+
}
|
|
169
|
+
lines.push('}');
|
|
170
|
+
|
|
171
|
+
return lines;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
// ─── Field rendering ──────────────────────────────────────────────────────
|
|
175
|
+
|
|
176
|
+
function renderField(field: FieldNode): string {
|
|
177
|
+
const opt = field.optional || field.default !== undefined ? '?' : '';
|
|
178
|
+
let typeStr = renderTsType(field.type);
|
|
179
|
+
if (field.nullable) typeStr += ' | null';
|
|
180
|
+
const line = `${quoteKey(field.name)}${opt}: ${typeStr};`;
|
|
181
|
+
const jsdocParts: string[] = [];
|
|
182
|
+
if (field.deprecated) jsdocParts.push('@deprecated');
|
|
183
|
+
if (field.description) jsdocParts.push(field.description);
|
|
184
|
+
if (jsdocParts.length > 0) {
|
|
185
|
+
return `/** ${jsdocParts.join(' ')} */\n ${line}`;
|
|
186
|
+
}
|
|
187
|
+
return line;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
function renderInputField(field: FieldNode, modelsWithInput: Set<string>): string {
|
|
191
|
+
const opt = field.optional || field.default !== undefined ? '?' : '';
|
|
192
|
+
let typeStr = renderInputTsType(field.type, modelsWithInput);
|
|
193
|
+
if (field.nullable) typeStr += ' | null';
|
|
194
|
+
const line = `${quoteKey(field.name)}${opt}: ${typeStr};`;
|
|
195
|
+
const jsdocParts: string[] = [];
|
|
196
|
+
if (field.deprecated) jsdocParts.push('@deprecated');
|
|
197
|
+
if (field.description) jsdocParts.push(field.description);
|
|
198
|
+
if (jsdocParts.length > 0) {
|
|
199
|
+
return `/** ${jsdocParts.join(' ')} */\n ${line}`;
|
|
200
|
+
}
|
|
201
|
+
return line;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
// ─── Output (post-transform wire shape) ──────────────────────────────────
|
|
205
|
+
|
|
206
|
+
function camelToSnake(s: string): string {
|
|
207
|
+
return s.replace(/[A-Z]/g, c => `_${c.toLowerCase()}`);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
function camelToPascal(s: string): string {
|
|
211
|
+
return s.charAt(0).toUpperCase() + s.slice(1);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
function applyOutputCase(name: string, c: 'camel' | 'snake' | 'pascal' | undefined): string {
|
|
215
|
+
if (!c || c === 'camel') return name;
|
|
216
|
+
if (c === 'snake') return camelToSnake(name);
|
|
217
|
+
return camelToPascal(name);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* Emit `${name}Output` for a model in the output transitive set.
|
|
222
|
+
* - Direct hits (model.outputCase set): rename keys per the transform and substitute nested refs.
|
|
223
|
+
* - Transitive hits: keep field names as-is but substitute nested refs with their Output variants.
|
|
224
|
+
*
|
|
225
|
+
* `extends` is dropped for direct-hit models because the Zod schema flattens fields when an
|
|
226
|
+
* ancestor has format(...) (see `flattenFormatChain` in codegen-contract); we mirror that here
|
|
227
|
+
* so the plain interface matches the wire shape produced by the Zod transform.
|
|
228
|
+
*/
|
|
229
|
+
function generateOutputModel(model: ModelNode, modelsWithOutput: Set<string>): string[] {
|
|
230
|
+
const lines: string[] = [];
|
|
231
|
+
const outputCase = model.outputCase && model.outputCase !== 'camel' ? model.outputCase : undefined;
|
|
232
|
+
const readFields = model.fields.filter(f => f.visibility !== 'writeonly');
|
|
233
|
+
|
|
234
|
+
// Transitive-only (no direct outputCase): preserve `extends` and original key names.
|
|
235
|
+
if (!outputCase) {
|
|
236
|
+
const baseExt =
|
|
237
|
+
model.bases?.[0] && modelsWithOutput.has(model.bases?.[0])
|
|
238
|
+
? ` extends ${model.bases?.[0]}Output`
|
|
239
|
+
: model.bases?.[0]
|
|
240
|
+
? ` extends ${model.bases?.[0]}`
|
|
241
|
+
: '';
|
|
242
|
+
lines.push(`export interface ${model.name}Output${baseExt} {`);
|
|
243
|
+
for (const field of readFields) {
|
|
244
|
+
lines.push(` ${renderOutputField(field, model.outputCase, modelsWithOutput)}`);
|
|
245
|
+
}
|
|
246
|
+
lines.push('}');
|
|
247
|
+
return lines;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
// Direct hit: emit a flat interface with renamed keys.
|
|
251
|
+
lines.push(`export interface ${model.name}Output {`);
|
|
252
|
+
for (const field of readFields) {
|
|
253
|
+
lines.push(` ${renderOutputField(field, outputCase, modelsWithOutput)}`);
|
|
254
|
+
}
|
|
255
|
+
lines.push('}');
|
|
256
|
+
return lines;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
function renderOutputField(field: FieldNode, outputCase: 'camel' | 'snake' | 'pascal' | undefined, modelsWithOutput: Set<string>): string {
|
|
260
|
+
const opt = field.optional || field.default !== undefined ? '?' : '';
|
|
261
|
+
const key = applyOutputCase(field.name, outputCase);
|
|
262
|
+
let typeStr = renderOutputTsType(field.type, modelsWithOutput);
|
|
263
|
+
if (field.nullable) typeStr += ' | null';
|
|
264
|
+
const line = `${quoteKey(key)}${opt}: ${typeStr};`;
|
|
265
|
+
const jsdocParts: string[] = [];
|
|
266
|
+
if (field.deprecated) jsdocParts.push('@deprecated');
|
|
267
|
+
if (field.description) jsdocParts.push(field.description);
|
|
268
|
+
if (jsdocParts.length > 0) {
|
|
269
|
+
return `/** ${jsdocParts.join(' ')} */\n ${line}`;
|
|
270
|
+
}
|
|
271
|
+
return line;
|
|
272
|
+
}
|