@omnidotdev/odk 0.0.0-stage → 0.1.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/README.md +122 -2
- package/dist/cli.d.ts +3 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +141 -0
- package/dist/cli.js.map +1 -0
- package/dist/compile.d.ts +152 -0
- package/dist/compile.d.ts.map +1 -0
- package/dist/compile.js +11 -0
- package/dist/compile.js.map +1 -0
- package/dist/index.d.ts +43 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +39 -0
- package/dist/index.js.map +1 -0
- package/dist/jsonSchemaExtract.d.ts +40 -0
- package/dist/jsonSchemaExtract.d.ts.map +1 -0
- package/dist/jsonSchemaExtract.js +144 -0
- package/dist/jsonSchemaExtract.js.map +1 -0
- package/dist/odk.d.ts +169 -0
- package/dist/odk.d.ts.map +1 -0
- package/dist/odk.js +186 -0
- package/dist/odk.js.map +1 -0
- package/dist/odk.test.d.ts +2 -0
- package/dist/odk.test.d.ts.map +1 -0
- package/dist/odk.test.js +36 -0
- package/dist/odk.test.js.map +1 -0
- package/dist/standardSchemaAdapter.d.ts +16 -0
- package/dist/standardSchemaAdapter.d.ts.map +1 -0
- package/dist/standardSchemaAdapter.js +277 -0
- package/dist/standardSchemaAdapter.js.map +1 -0
- package/dist/typespecBridge.d.ts +27 -0
- package/dist/typespecBridge.d.ts.map +1 -0
- package/dist/typespecBridge.js +274 -0
- package/dist/typespecBridge.js.map +1 -0
- package/package.json +63 -4
- package/src/cli.ts +160 -0
- package/src/compile.ts +172 -0
- package/src/index.ts +63 -0
- package/src/jsonSchemaExtract.ts +226 -0
- package/src/odk.test.ts +49 -0
- package/src/odk.ts +219 -0
- package/src/standardSchemaAdapter.ts +355 -0
- package/src/typespecBridge.ts +340 -0
|
@@ -0,0 +1,340 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TypeSpec Bridge
|
|
3
|
+
*
|
|
4
|
+
* Converts CompiledSchema to TypeSpec source code, then uses
|
|
5
|
+
* the TypeSpec compiler to emit to various targets.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import type {
|
|
9
|
+
CompiledField,
|
|
10
|
+
CompiledModel,
|
|
11
|
+
CompiledSchema,
|
|
12
|
+
CompiledType,
|
|
13
|
+
CompiledUnion,
|
|
14
|
+
} from "./compile.js";
|
|
15
|
+
|
|
16
|
+
export interface EmitOptions {
|
|
17
|
+
/** Target emitters (e.g., "@typespec/openapi3", "@typespec/protobuf") */
|
|
18
|
+
targets: string[];
|
|
19
|
+
/** Output directory */
|
|
20
|
+
outDir: string;
|
|
21
|
+
/** Namespace for generated TypeSpec */
|
|
22
|
+
namespace?: string;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Convert CompiledSchema to TypeSpec source code.
|
|
27
|
+
*/
|
|
28
|
+
export function compiledSchemaToTypeSpec(
|
|
29
|
+
schema: CompiledSchema,
|
|
30
|
+
namespace?: string,
|
|
31
|
+
): string {
|
|
32
|
+
const lines: string[] = [];
|
|
33
|
+
const ns = namespace || schema.name;
|
|
34
|
+
|
|
35
|
+
// Imports
|
|
36
|
+
lines.push('import "@typespec/json-schema";');
|
|
37
|
+
lines.push("using TypeSpec.JsonSchema;");
|
|
38
|
+
lines.push("");
|
|
39
|
+
|
|
40
|
+
// Namespace
|
|
41
|
+
lines.push(`namespace ${sanitizeIdentifier(ns)};`);
|
|
42
|
+
lines.push("");
|
|
43
|
+
|
|
44
|
+
// Emit unions first (enums)
|
|
45
|
+
for (const union of schema.unions) {
|
|
46
|
+
lines.push(emitUnion(union));
|
|
47
|
+
lines.push("");
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// Emit models
|
|
51
|
+
for (const model of schema.models) {
|
|
52
|
+
lines.push(emitModel(model));
|
|
53
|
+
lines.push("");
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return lines.join("\n");
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function emitUnion(union: CompiledUnion): string {
|
|
60
|
+
const lines: string[] = [];
|
|
61
|
+
|
|
62
|
+
if (union.description) {
|
|
63
|
+
lines.push(`/** ${union.description} */`);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// Check if it's a string literal enum
|
|
67
|
+
const isStringEnum = union.variants.every(
|
|
68
|
+
(v) => v.type?.kind === "literal" && typeof v.type.value === "string",
|
|
69
|
+
);
|
|
70
|
+
|
|
71
|
+
if (isStringEnum) {
|
|
72
|
+
lines.push(`enum ${sanitizeIdentifier(union.name)} {`);
|
|
73
|
+
for (const variant of union.variants) {
|
|
74
|
+
const value = (variant.type as { kind: "literal"; value: string }).value;
|
|
75
|
+
// If the name is a valid identifier, use it; otherwise quote it
|
|
76
|
+
if (isValidIdentifier(variant.name)) {
|
|
77
|
+
if (variant.name === value) {
|
|
78
|
+
lines.push(` ${variant.name},`);
|
|
79
|
+
} else {
|
|
80
|
+
lines.push(` ${variant.name}: "${value}",`);
|
|
81
|
+
}
|
|
82
|
+
} else {
|
|
83
|
+
lines.push(` "${value}",`);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
lines.push("}");
|
|
87
|
+
} else {
|
|
88
|
+
// Union type
|
|
89
|
+
const variantTypes = union.variants
|
|
90
|
+
.map((v) => (v.type ? typeToString(v.type) : v.name))
|
|
91
|
+
.join(" | ");
|
|
92
|
+
lines.push(`alias ${sanitizeIdentifier(union.name)} = ${variantTypes};`);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return lines.join("\n");
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function emitModel(model: CompiledModel): string {
|
|
99
|
+
const lines: string[] = [];
|
|
100
|
+
|
|
101
|
+
if (model.description) {
|
|
102
|
+
lines.push(`/** ${model.description} */`);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
lines.push("@jsonSchema");
|
|
106
|
+
lines.push(`model ${sanitizeIdentifier(model.name)} {`);
|
|
107
|
+
|
|
108
|
+
for (const field of model.fields) {
|
|
109
|
+
lines.push(emitField(field));
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
lines.push("}");
|
|
113
|
+
|
|
114
|
+
return lines.join("\n");
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function emitField(field: CompiledField): string {
|
|
118
|
+
const decorators: string[] = [];
|
|
119
|
+
|
|
120
|
+
// Add constraint decorators
|
|
121
|
+
if (field.constraints) {
|
|
122
|
+
const c = field.constraints;
|
|
123
|
+
if (c.minLength !== undefined)
|
|
124
|
+
decorators.push(`@minLength(${c.minLength})`);
|
|
125
|
+
if (c.maxLength !== undefined)
|
|
126
|
+
decorators.push(`@maxLength(${c.maxLength})`);
|
|
127
|
+
if (c.minValue !== undefined) decorators.push(`@minValue(${c.minValue})`);
|
|
128
|
+
if (c.maxValue !== undefined) decorators.push(`@maxValue(${c.maxValue})`);
|
|
129
|
+
if (c.pattern !== undefined)
|
|
130
|
+
decorators.push(`@pattern("${escapeString(c.pattern)}")`);
|
|
131
|
+
if (c.multipleOf !== undefined)
|
|
132
|
+
decorators.push(`@multipleOf(${c.multipleOf})`);
|
|
133
|
+
if (c.format !== undefined) {
|
|
134
|
+
// Map common formats to TypeSpec decorators
|
|
135
|
+
switch (c.format) {
|
|
136
|
+
case "uuid":
|
|
137
|
+
decorators.push('@format("uuid")');
|
|
138
|
+
break;
|
|
139
|
+
case "email":
|
|
140
|
+
decorators.push('@format("email")');
|
|
141
|
+
break;
|
|
142
|
+
case "uri":
|
|
143
|
+
case "url":
|
|
144
|
+
decorators.push('@format("uri")');
|
|
145
|
+
break;
|
|
146
|
+
case "date":
|
|
147
|
+
decorators.push('@format("date")');
|
|
148
|
+
break;
|
|
149
|
+
case "date-time":
|
|
150
|
+
decorators.push('@format("date-time")');
|
|
151
|
+
break;
|
|
152
|
+
default:
|
|
153
|
+
decorators.push(`@format("${c.format}")`);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
const decoratorStr = decorators.length > 0 ? `${decorators.join(" ")} ` : "";
|
|
159
|
+
const optionalMark = field.optional ? "?" : "";
|
|
160
|
+
const typeStr = typeToString(field.type);
|
|
161
|
+
const fieldName = sanitizeIdentifier(field.name);
|
|
162
|
+
|
|
163
|
+
return ` ${decoratorStr}${fieldName}${optionalMark}: ${typeStr};`;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
function typeToString(type: CompiledType): string {
|
|
167
|
+
switch (type.kind) {
|
|
168
|
+
case "scalar":
|
|
169
|
+
return scalarToTypeSpec(type.name);
|
|
170
|
+
case "array":
|
|
171
|
+
return `${typeToString(type.items)}[]`;
|
|
172
|
+
case "ref":
|
|
173
|
+
return sanitizeIdentifier(type.name);
|
|
174
|
+
case "literal":
|
|
175
|
+
if (typeof type.value === "string") {
|
|
176
|
+
return `"${escapeString(type.value)}"`;
|
|
177
|
+
}
|
|
178
|
+
return String(type.value);
|
|
179
|
+
case "union":
|
|
180
|
+
return type.variants.map(typeToString).join(" | ");
|
|
181
|
+
case "object": {
|
|
182
|
+
const fields = type.fields
|
|
183
|
+
.map((f) => {
|
|
184
|
+
const opt = f.optional ? "?" : "";
|
|
185
|
+
return `${f.name}${opt}: ${typeToString(f.type)}`;
|
|
186
|
+
})
|
|
187
|
+
.join("; ");
|
|
188
|
+
return `{ ${fields} }`;
|
|
189
|
+
}
|
|
190
|
+
default:
|
|
191
|
+
return "unknown";
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
function scalarToTypeSpec(scalar: string): string {
|
|
196
|
+
switch (scalar) {
|
|
197
|
+
case "string":
|
|
198
|
+
return "string";
|
|
199
|
+
case "int32":
|
|
200
|
+
return "int32";
|
|
201
|
+
case "int64":
|
|
202
|
+
return "int64";
|
|
203
|
+
case "float32":
|
|
204
|
+
return "float32";
|
|
205
|
+
case "float64":
|
|
206
|
+
return "float64";
|
|
207
|
+
case "boolean":
|
|
208
|
+
return "boolean";
|
|
209
|
+
case "date":
|
|
210
|
+
return "plainDate";
|
|
211
|
+
case "datetime":
|
|
212
|
+
return "utcDateTime";
|
|
213
|
+
case "bytes":
|
|
214
|
+
return "bytes";
|
|
215
|
+
default:
|
|
216
|
+
return "unknown";
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
function sanitizeIdentifier(name: string): string {
|
|
221
|
+
// Replace invalid characters with underscores
|
|
222
|
+
let sanitized = name.replace(/[^A-Za-z0-9_]/g, "_");
|
|
223
|
+
// Ensure it doesn't start with a number
|
|
224
|
+
if (/^[0-9]/.test(sanitized)) {
|
|
225
|
+
sanitized = `_${sanitized}`;
|
|
226
|
+
}
|
|
227
|
+
return sanitized || "_";
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
function isValidIdentifier(name: string): boolean {
|
|
231
|
+
return /^[A-Za-z_][A-Za-z0-9_]*$/.test(name);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
function escapeString(s: string): string {
|
|
235
|
+
return s.replace(/\\/g, "\\\\").replace(/"/g, '\\"');
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* Emit CompiledSchema to target formats using TypeSpec compiler.
|
|
240
|
+
*
|
|
241
|
+
* This creates a temporary TypeSpec project with proper package.json
|
|
242
|
+
* to resolve emitter dependencies.
|
|
243
|
+
*/
|
|
244
|
+
export async function emitToTargets(
|
|
245
|
+
schema: CompiledSchema,
|
|
246
|
+
options: EmitOptions,
|
|
247
|
+
): Promise<void> {
|
|
248
|
+
const { targets, outDir, namespace } = options;
|
|
249
|
+
|
|
250
|
+
const fs = await import("node:fs/promises");
|
|
251
|
+
const path = await import("node:path");
|
|
252
|
+
const os = await import("node:os");
|
|
253
|
+
|
|
254
|
+
// Generate TypeSpec source
|
|
255
|
+
const tspSource = compiledSchemaToTypeSpec(schema, namespace);
|
|
256
|
+
|
|
257
|
+
// Create temp directory with a proper TypeSpec project structure
|
|
258
|
+
const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), "odk-"));
|
|
259
|
+
const tspPath = path.join(tempDir, "main.tsp");
|
|
260
|
+
|
|
261
|
+
try {
|
|
262
|
+
// Write the TypeSpec source
|
|
263
|
+
await fs.writeFile(tspPath, tspSource, "utf-8");
|
|
264
|
+
|
|
265
|
+
// Create package.json with dependencies so TypeSpec can find them
|
|
266
|
+
const packageJson = {
|
|
267
|
+
name: "odk-temp-project",
|
|
268
|
+
type: "module",
|
|
269
|
+
dependencies: {
|
|
270
|
+
"@typespec/compiler": "*",
|
|
271
|
+
"@typespec/json-schema": "*",
|
|
272
|
+
...Object.fromEntries(targets.map((t) => [t, "*"])),
|
|
273
|
+
},
|
|
274
|
+
};
|
|
275
|
+
await fs.writeFile(
|
|
276
|
+
path.join(tempDir, "package.json"),
|
|
277
|
+
JSON.stringify(packageJson, null, 2),
|
|
278
|
+
"utf-8",
|
|
279
|
+
);
|
|
280
|
+
|
|
281
|
+
// Symlink node_modules from odk-ts to the temp directory
|
|
282
|
+
// This avoids having to install packages
|
|
283
|
+
const odkNodeModules = path.resolve(import.meta.dirname, "../node_modules");
|
|
284
|
+
const tempNodeModules = path.join(tempDir, "node_modules");
|
|
285
|
+
|
|
286
|
+
try {
|
|
287
|
+
await fs.symlink(odkNodeModules, tempNodeModules, "dir");
|
|
288
|
+
} catch {
|
|
289
|
+
// If symlink fails (e.g., on Windows without admin), copy the approach below
|
|
290
|
+
// For now, just try to compile with the current working directory
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
// Create tspconfig.yaml
|
|
294
|
+
const absoluteOutDir = path.resolve(process.cwd(), outDir);
|
|
295
|
+
const tspConfigContent = `
|
|
296
|
+
emit:
|
|
297
|
+
${targets.map((t) => ` - "${t}"`).join("\n")}
|
|
298
|
+
options:
|
|
299
|
+
${targets.map((t) => ` "${t}":\n output-dir: "${absoluteOutDir}"`).join("\n")}
|
|
300
|
+
`;
|
|
301
|
+
await fs.writeFile(
|
|
302
|
+
path.join(tempDir, "tspconfig.yaml"),
|
|
303
|
+
tspConfigContent,
|
|
304
|
+
"utf-8",
|
|
305
|
+
);
|
|
306
|
+
|
|
307
|
+
// Ensure output directory exists
|
|
308
|
+
await fs.mkdir(absoluteOutDir, { recursive: true });
|
|
309
|
+
|
|
310
|
+
// Run TypeSpec compiler
|
|
311
|
+
const { compile, NodeHost } = await import("@typespec/compiler");
|
|
312
|
+
const program = await compile(NodeHost, tspPath, {
|
|
313
|
+
outputDir: absoluteOutDir,
|
|
314
|
+
emit: targets,
|
|
315
|
+
});
|
|
316
|
+
|
|
317
|
+
if (program.diagnostics.length > 0) {
|
|
318
|
+
for (const diag of program.diagnostics) {
|
|
319
|
+
if (diag.severity === "error") {
|
|
320
|
+
console.error(`TypeSpec error: ${diag.message}`);
|
|
321
|
+
} else {
|
|
322
|
+
console.warn(`TypeSpec warning: ${diag.message}`);
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
const errors = program.diagnostics.filter((d) => d.severity === "error");
|
|
327
|
+
if (errors.length > 0) {
|
|
328
|
+
throw new Error(
|
|
329
|
+
`TypeSpec compilation failed with ${errors.length} errors`,
|
|
330
|
+
);
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
// biome-ignore lint/suspicious/noConsole: intentional emit-completion notice for CLI output
|
|
335
|
+
console.log(`Emitted to ${absoluteOutDir}`);
|
|
336
|
+
} finally {
|
|
337
|
+
// Cleanup temp files
|
|
338
|
+
await fs.rm(tempDir, { recursive: true, force: true });
|
|
339
|
+
}
|
|
340
|
+
}
|