@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.
Files changed (42) hide show
  1. package/README.md +122 -2
  2. package/dist/cli.d.ts +3 -0
  3. package/dist/cli.d.ts.map +1 -0
  4. package/dist/cli.js +141 -0
  5. package/dist/cli.js.map +1 -0
  6. package/dist/compile.d.ts +152 -0
  7. package/dist/compile.d.ts.map +1 -0
  8. package/dist/compile.js +11 -0
  9. package/dist/compile.js.map +1 -0
  10. package/dist/index.d.ts +43 -0
  11. package/dist/index.d.ts.map +1 -0
  12. package/dist/index.js +39 -0
  13. package/dist/index.js.map +1 -0
  14. package/dist/jsonSchemaExtract.d.ts +40 -0
  15. package/dist/jsonSchemaExtract.d.ts.map +1 -0
  16. package/dist/jsonSchemaExtract.js +144 -0
  17. package/dist/jsonSchemaExtract.js.map +1 -0
  18. package/dist/odk.d.ts +169 -0
  19. package/dist/odk.d.ts.map +1 -0
  20. package/dist/odk.js +186 -0
  21. package/dist/odk.js.map +1 -0
  22. package/dist/odk.test.d.ts +2 -0
  23. package/dist/odk.test.d.ts.map +1 -0
  24. package/dist/odk.test.js +36 -0
  25. package/dist/odk.test.js.map +1 -0
  26. package/dist/standardSchemaAdapter.d.ts +16 -0
  27. package/dist/standardSchemaAdapter.d.ts.map +1 -0
  28. package/dist/standardSchemaAdapter.js +277 -0
  29. package/dist/standardSchemaAdapter.js.map +1 -0
  30. package/dist/typespecBridge.d.ts +27 -0
  31. package/dist/typespecBridge.d.ts.map +1 -0
  32. package/dist/typespecBridge.js +274 -0
  33. package/dist/typespecBridge.js.map +1 -0
  34. package/package.json +63 -4
  35. package/src/cli.ts +160 -0
  36. package/src/compile.ts +172 -0
  37. package/src/index.ts +63 -0
  38. package/src/jsonSchemaExtract.ts +226 -0
  39. package/src/odk.test.ts +49 -0
  40. package/src/odk.ts +219 -0
  41. package/src/standardSchemaAdapter.ts +355 -0
  42. package/src/typespecBridge.ts +340 -0
@@ -0,0 +1,144 @@
1
+ /**
2
+ * JSON Schema extraction from different Standard Schema libraries.
3
+ * Each library has its own method for converting to JSON Schema.
4
+ */
5
+ /**
6
+ * Detect which schema library the schema comes from and extract JSON Schema.
7
+ */
8
+ export function extractJsonSchema(schema) {
9
+ // Zod detection
10
+ if (isZodSchema(schema)) {
11
+ return extractFromZod(schema);
12
+ }
13
+ // ArkType detection
14
+ if (isArkTypeSchema(schema)) {
15
+ return extractFromArkType(schema);
16
+ }
17
+ // Valibot detection
18
+ if (isValibotSchema(schema)) {
19
+ return extractFromValibot(schema);
20
+ }
21
+ // Check if it's already a Standard Schema with ~standard property
22
+ if (isStandardSchema(schema)) {
23
+ // Try to extract via library-specific methods first
24
+ // Fall back to inferring from the standard schema interface
25
+ return inferFromStandardSchema(schema);
26
+ }
27
+ throw new Error("Unknown schema library. Please use Zod, ArkType, or Valibot.");
28
+ }
29
+ // Zod 4 schema detection (has toJSONSchema method and ~standard.vendor === "zod")
30
+ function isZod4Schema(schema) {
31
+ return (schema !== null &&
32
+ typeof schema === "object" &&
33
+ "toJSONSchema" in schema &&
34
+ typeof schema.toJSONSchema === "function" &&
35
+ "~standard" in schema &&
36
+ schema["~standard"]?.vendor ===
37
+ "zod");
38
+ }
39
+ // Zod 3 schema detection (has _def property)
40
+ function isZod3Schema(schema) {
41
+ return (schema !== null &&
42
+ typeof schema === "object" &&
43
+ "_def" in schema &&
44
+ typeof schema._def === "object" &&
45
+ !("toJSONSchema" in schema) // Not Zod 4
46
+ );
47
+ }
48
+ function isZodSchema(schema) {
49
+ return isZod4Schema(schema) || isZod3Schema(schema);
50
+ }
51
+ function extractFromZod(schema) {
52
+ // Zod 4: Use native toJSONSchema()
53
+ if (isZod4Schema(schema)) {
54
+ // Use unrepresentable: 'any' to avoid throwing on Date, Function, etc.
55
+ // These will become empty schemas {} which we handle gracefully
56
+ return schema.toJSONSchema({ unrepresentable: "any" });
57
+ }
58
+ // Zod 3: Use zod-to-json-schema library
59
+ if (isZod3Schema(schema)) {
60
+ try {
61
+ // eslint-disable-next-line @typescript-eslint/no-require-imports
62
+ const { zodToJsonSchema } = require("zod-to-json-schema");
63
+ return zodToJsonSchema(schema, { $refStrategy: "none" });
64
+ }
65
+ catch {
66
+ throw new Error("zod-to-json-schema is required for Zod 3 schemas. Install it with: bun add zod-to-json-schema");
67
+ }
68
+ }
69
+ throw new Error("Unknown Zod schema version");
70
+ }
71
+ // ============ ArkType ============
72
+ function isArkTypeSchema(schema) {
73
+ // ArkType schemas are functions (callable validators), not plain objects
74
+ if (schema === null ||
75
+ (typeof schema !== "object" && typeof schema !== "function"))
76
+ return false;
77
+ // Check for ~standard.vendor === "arktype"
78
+ if ("~standard" in schema &&
79
+ schema["~standard"]?.vendor ===
80
+ "arktype") {
81
+ return true;
82
+ }
83
+ // Fallback: check for toJsonSchema method (lowercase, on prototype)
84
+ return (typeof schema.toJsonSchema === "function");
85
+ }
86
+ function extractFromArkType(schema) {
87
+ const s = schema;
88
+ return s.toJsonSchema();
89
+ }
90
+ // ============ Valibot ============
91
+ function isValibotSchema(schema) {
92
+ return (schema !== null &&
93
+ typeof schema === "object" &&
94
+ "kind" in schema &&
95
+ schema.kind === "schema");
96
+ }
97
+ function extractFromValibot(schema) {
98
+ try {
99
+ // eslint-disable-next-line @typescript-eslint/no-require-imports
100
+ const { toJsonSchema } = require("@valibot/to-json-schema");
101
+ return toJsonSchema(schema);
102
+ }
103
+ catch {
104
+ throw new Error("@valibot/to-json-schema is required for Valibot schemas. Install it with: bun add @valibot/to-json-schema");
105
+ }
106
+ }
107
+ // ============ Standard Schema Fallback ============
108
+ function isStandardSchema(schema) {
109
+ return (schema !== null &&
110
+ typeof schema === "object" &&
111
+ "~standard" in schema &&
112
+ typeof schema["~standard"] === "object");
113
+ }
114
+ function inferFromStandardSchema(schema) {
115
+ // Standard Schema spec doesn't mandate JSON Schema output,
116
+ // so we can only infer basic structure from the types property
117
+ const standard = schema["~standard"];
118
+ // If the schema library provides vendor-specific JSON Schema, use it
119
+ if ("toJsonSchema" in schema && typeof schema.toJsonSchema === "function") {
120
+ return schema.toJsonSchema();
121
+ }
122
+ // Otherwise, return a minimal schema with unknown type
123
+ // The adapter will warn about this
124
+ return {
125
+ type: "object",
126
+ description: `Standard Schema (vendor: ${standard.vendor})`,
127
+ };
128
+ }
129
+ /**
130
+ * Get the library name from a schema for error messages.
131
+ */
132
+ export function getSchemaLibrary(schema) {
133
+ if (isZod4Schema(schema) || isZod3Schema(schema))
134
+ return "zod";
135
+ if (isArkTypeSchema(schema))
136
+ return "arktype";
137
+ if (isValibotSchema(schema))
138
+ return "valibot";
139
+ if (isStandardSchema(schema)) {
140
+ return schema["~standard"].vendor;
141
+ }
142
+ return "unknown";
143
+ }
144
+ //# sourceMappingURL=jsonSchemaExtract.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"jsonSchemaExtract.js","sourceRoot":"","sources":["../src/jsonSchemaExtract.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAkCH;;GAEG;AACH,MAAM,UAAU,iBAAiB,CAAC,MAAe;IAC/C,gBAAgB;IAChB,IAAI,WAAW,CAAC,MAAM,CAAC,EAAE,CAAC;QACxB,OAAO,cAAc,CAAC,MAAM,CAAC,CAAC;IAChC,CAAC;IAED,oBAAoB;IACpB,IAAI,eAAe,CAAC,MAAM,CAAC,EAAE,CAAC;QAC5B,OAAO,kBAAkB,CAAC,MAAM,CAAC,CAAC;IACpC,CAAC;IAED,oBAAoB;IACpB,IAAI,eAAe,CAAC,MAAM,CAAC,EAAE,CAAC;QAC5B,OAAO,kBAAkB,CAAC,MAAM,CAAC,CAAC;IACpC,CAAC;IAED,kEAAkE;IAClE,IAAI,gBAAgB,CAAC,MAAM,CAAC,EAAE,CAAC;QAC7B,oDAAoD;QACpD,4DAA4D;QAC5D,OAAO,uBAAuB,CAAC,MAAM,CAAC,CAAC;IACzC,CAAC;IAED,MAAM,IAAI,KAAK,CACb,8DAA8D,CAC/D,CAA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,WAAW,CAAC,KAAK,QAAQ,CACrE,CAAC;AACJ,CAAC;AAED,SAAS,uBAAuB,CAAC,MAAwB;IACvD,2DAA2D;IAC3D,+DAA+D;IAC/D,MAAM,QAAQ,GAAG,MAAM,CAAC,WAAW,CAAC,CAAC;IAErC,qEAAqE;IACrE,IAAI,cAAc,IAAI,MAAM,IAAI,OAAO,MAAM,CAAC,YAAY,KAAK,UAAU,EAAE,CAAC;QAC1E,OAAQ,MAA6C,CAAC,YAAY,EAAE,CAAC;IACvE,CAAC;IAED,uDAAuD;IACvD,mCAAmC;IACnC,OAAO;QACL,IAAI,EAAE,QAAQ;QACd,WAAW,EAAE,4BAA4B,QAAQ,CAAC,MAAM,GAAG;KAC5D,CAAC;AACJ,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,gBAAgB,CAAC,MAAe;IAC9C,IAAI,YAAY,CAAC,MAAM,CAAC,IAAI,YAAY,CAAC,MAAM,CAAC;QAAE,OAAO,KAAK,CAAC;IAC/D,IAAI,eAAe,CAAC,MAAM,CAAC;QAAE,OAAO,SAAS,CAAC;IAC9C,IAAI,eAAe,CAAC,MAAM,CAAC;QAAE,OAAO,SAAS,CAAC;IAC9C,IAAI,gBAAgB,CAAC,MAAM,CAAC,EAAE,CAAC;QAC7B,OAAQ,MAA2B,CAAC,WAAW,CAAC,CAAC,MAAM,CAAC;IAC1D,CAAC;IACD,OAAO,SAAS,CAAC;AACnB,CAAC"}
package/dist/odk.d.ts ADDED
@@ -0,0 +1,169 @@
1
+ /**
2
+ * ODK - Omni Development Kit
3
+ *
4
+ * Main API for defining schemas and emitting to various targets.
5
+ *
6
+ * @example
7
+ * ```typescript
8
+ * import { odk } from "@omnidotdev/odk";
9
+ * import { z } from "zod";
10
+ *
11
+ * const User = z.object({
12
+ * id: z.string().uuid(),
13
+ * email: z.string().email(),
14
+ * name: z.string().optional(),
15
+ * });
16
+ *
17
+ * const schema = odk.schema("MyAPI", { User });
18
+ *
19
+ * await odk.emit(schema, {
20
+ * targets: ["@typespec/openapi3"],
21
+ * outDir: "./generated",
22
+ * });
23
+ * ```
24
+ *
25
+ * @example Escape hatches for advanced use cases
26
+ * ```typescript
27
+ * // Add raw TypeSpec code
28
+ * const schema = odk.schema("MyAPI", { User });
29
+ * odk.raw(schema, `
30
+ * @route("/api")
31
+ * interface CustomApi {
32
+ * @get op health(): void;
33
+ * }
34
+ * `);
35
+ *
36
+ * // Add custom imports
37
+ * odk.addImport(schema, "@typespec/http");
38
+ * ```
39
+ */
40
+ import type { CompiledSchema } from "./compile.js";
41
+ import type { AdapterOptions } from "./standardSchemaAdapter.js";
42
+ import type { EmitOptions } from "./typespecBridge.js";
43
+ export interface SchemaOptions extends AdapterOptions {
44
+ /** Description for the schema namespace */
45
+ description?: string;
46
+ /** TypeSpec imports to include */
47
+ imports?: string[];
48
+ }
49
+ export interface ODKSchema {
50
+ /** The compiled schema representation */
51
+ compiled: CompiledSchema;
52
+ /** The original types passed to schema() */
53
+ types: Record<string, unknown>;
54
+ }
55
+ /**
56
+ * Create an ODK schema from Standard Schema-compliant types.
57
+ *
58
+ * @param name - Name for the schema namespace
59
+ * @param types - Record of named types (Zod, ArkType, or Valibot schemas)
60
+ * @param options - Optional configuration
61
+ * @returns ODK schema object
62
+ *
63
+ * @example
64
+ * ```typescript
65
+ * const User = z.object({ id: z.string(), name: z.string() });
66
+ * const Post = z.object({ title: z.string(), authorId: z.string() });
67
+ *
68
+ * const schema = odk.schema("Blog", { User, Post });
69
+ * ```
70
+ */
71
+ declare function schema(name: string, types: Record<string, unknown>, options?: SchemaOptions): ODKSchema;
72
+ /**
73
+ * Add a raw TypeSpec block to the schema.
74
+ *
75
+ * Use this escape hatch when you need to write TypeSpec code that
76
+ * ODK's schema API doesn't support.
77
+ *
78
+ * @param schema - ODK schema to modify
79
+ * @param content - Raw TypeSpec code
80
+ * @param description - Optional description/comment
81
+ *
82
+ * @example
83
+ * ```typescript
84
+ * odk.raw(schema, `
85
+ * @route("/api")
86
+ * interface CustomApi {
87
+ * @get op health(): void;
88
+ * }
89
+ * `);
90
+ * ```
91
+ */
92
+ declare function raw(schema: ODKSchema, content: string, description?: string): void;
93
+ /**
94
+ * Add a TypeSpec import to the schema.
95
+ *
96
+ * @param schema - ODK schema to modify
97
+ * @param importPath - Import path (e.g., "@typespec/http")
98
+ *
99
+ * @example
100
+ * ```typescript
101
+ * odk.addImport(schema, "@typespec/http");
102
+ * odk.addImport(schema, "@omnidotdev/typespec-drizzle");
103
+ * ```
104
+ */
105
+ declare function addImport(schema: ODKSchema, importPath: string): void;
106
+ /**
107
+ * Emit an ODK schema to target formats.
108
+ *
109
+ * @param schema - ODK schema created with odk.schema()
110
+ * @param options - Emission options (targets, output directory)
111
+ *
112
+ * @example
113
+ * ```typescript
114
+ * await odk.emit(schema, {
115
+ * targets: ["@typespec/openapi3", "@typespec/protobuf"],
116
+ * outDir: "./generated",
117
+ * });
118
+ * ```
119
+ */
120
+ declare function emit(schema: ODKSchema, options: EmitOptions): Promise<void>;
121
+ /**
122
+ * Generate TypeSpec source code from an ODK schema.
123
+ *
124
+ * @param schema - ODK schema created with odk.schema()
125
+ * @param namespace - Optional namespace override
126
+ * @returns TypeSpec source code as a string
127
+ *
128
+ * @example
129
+ * ```typescript
130
+ * const tspSource = odk.toTypeSpec(schema);
131
+ * console.log(tspSource);
132
+ * ```
133
+ */
134
+ declare function toTypeSpec(schema: ODKSchema, namespace?: string): string;
135
+ /**
136
+ * Get the compiled schema representation (useful for the Rust CLI).
137
+ *
138
+ * @param schema - ODK schema created with odk.schema()
139
+ * @returns CompiledSchema object
140
+ */
141
+ declare function getCompiled(schema: ODKSchema): CompiledSchema;
142
+ /**
143
+ * Serialize an ODK schema to JSON (for the Rust CLI).
144
+ *
145
+ * @param schema - ODK schema created with odk.schema()
146
+ * @returns JSON string of the compiled schema
147
+ *
148
+ * @example
149
+ * ```typescript
150
+ * const json = odk.toJSON(schema);
151
+ * await Bun.write("schema.json", json);
152
+ * // Then: tspgen schema.json -o output.tsp
153
+ * ```
154
+ */
155
+ declare function toJSON(schema: ODKSchema): string;
156
+ /**
157
+ * ODK namespace - main entry point for the library.
158
+ */
159
+ export declare const odk: {
160
+ readonly schema: typeof schema;
161
+ readonly emit: typeof emit;
162
+ readonly toTypeSpec: typeof toTypeSpec;
163
+ readonly toJSON: typeof toJSON;
164
+ readonly getCompiled: typeof getCompiled;
165
+ readonly raw: typeof raw;
166
+ readonly addImport: typeof addImport;
167
+ };
168
+ export {};
169
+ //# sourceMappingURL=odk.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"odk.d.ts","sourceRoot":"","sources":["../src/odk.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsCG;AAKH,OAAO,KAAK,EAAE,cAAc,EAAoB,MAAM,cAAc,CAAC;AACrE,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,4BAA4B,CAAC;AACjE,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AAEvD,MAAM,WAAW,aAAc,SAAQ,cAAc;IACnD,2CAA2C;IAC3C,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,kCAAkC;IAClC,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC;CACpB;AAED,MAAM,WAAW,SAAS;IACxB,yCAAyC;IACzC,QAAQ,EAAE,cAAc,CAAC;IACzB,4CAA4C;IAC5C,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CAChC;AAED;;;;;;;;;;;;;;;GAeG;AACH,iBAAS,MAAM,CACb,IAAI,EAAE,MAAM,EACZ,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC9B,OAAO,GAAE,aAAkB,GAC1B,SAAS,CASX;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,iBAAS,GAAG,CAAC,MAAM,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,EAAE,WAAW,CAAC,EAAE,MAAM,GAAG,IAAI,CAU3E;AAED;;;;;;;;;;;GAWG;AACH,iBAAS,SAAS,CAAC,MAAM,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,GAAG,IAAI,CAO9D;AAED;;;;;;;;;;;;;GAaG;AACH,iBAAe,IAAI,CAAC,MAAM,EAAE,SAAS,EAAE,OAAO,EAAE,WAAW,GAAG,OAAO,CAAC,IAAI,CAAC,CAE1E;AAED;;;;;;;;;;;;GAYG;AACH,iBAAS,UAAU,CAAC,MAAM,EAAE,SAAS,EAAE,SAAS,CAAC,EAAE,MAAM,GAAG,MAAM,CAEjE;AAED;;;;;GAKG;AACH,iBAAS,WAAW,CAAC,MAAM,EAAE,SAAS,GAAG,cAAc,CAEtD;AAED;;;;;;;;;;;;GAYG;AACH,iBAAS,MAAM,CAAC,MAAM,EAAE,SAAS,GAAG,MAAM,CAEzC;AAED;;GAEG;AACH,eAAO,MAAM,GAAG;;;;;;;;CAQN,CAAC"}
package/dist/odk.js ADDED
@@ -0,0 +1,186 @@
1
+ /**
2
+ * ODK - Omni Development Kit
3
+ *
4
+ * Main API for defining schemas and emitting to various targets.
5
+ *
6
+ * @example
7
+ * ```typescript
8
+ * import { odk } from "@omnidotdev/odk";
9
+ * import { z } from "zod";
10
+ *
11
+ * const User = z.object({
12
+ * id: z.string().uuid(),
13
+ * email: z.string().email(),
14
+ * name: z.string().optional(),
15
+ * });
16
+ *
17
+ * const schema = odk.schema("MyAPI", { User });
18
+ *
19
+ * await odk.emit(schema, {
20
+ * targets: ["@typespec/openapi3"],
21
+ * outDir: "./generated",
22
+ * });
23
+ * ```
24
+ *
25
+ * @example Escape hatches for advanced use cases
26
+ * ```typescript
27
+ * // Add raw TypeSpec code
28
+ * const schema = odk.schema("MyAPI", { User });
29
+ * odk.raw(schema, `
30
+ * @route("/api")
31
+ * interface CustomApi {
32
+ * @get op health(): void;
33
+ * }
34
+ * `);
35
+ *
36
+ * // Add custom imports
37
+ * odk.addImport(schema, "@typespec/http");
38
+ * ```
39
+ */
40
+ import { standardSchemaToCompiled } from "./standardSchemaAdapter.js";
41
+ import { compiledSchemaToTypeSpec, emitToTargets } from "./typespecBridge.js";
42
+ /**
43
+ * Create an ODK schema from Standard Schema-compliant types.
44
+ *
45
+ * @param name - Name for the schema namespace
46
+ * @param types - Record of named types (Zod, ArkType, or Valibot schemas)
47
+ * @param options - Optional configuration
48
+ * @returns ODK schema object
49
+ *
50
+ * @example
51
+ * ```typescript
52
+ * const User = z.object({ id: z.string(), name: z.string() });
53
+ * const Post = z.object({ title: z.string(), authorId: z.string() });
54
+ *
55
+ * const schema = odk.schema("Blog", { User, Post });
56
+ * ```
57
+ */
58
+ function schema(name, types, options = {}) {
59
+ const compiled = standardSchemaToCompiled(name, types, options);
60
+ // Add imports from options
61
+ if (options.imports) {
62
+ compiled.imports = [...(compiled.imports ?? []), ...options.imports];
63
+ }
64
+ return { compiled, types };
65
+ }
66
+ /**
67
+ * Add a raw TypeSpec block to the schema.
68
+ *
69
+ * Use this escape hatch when you need to write TypeSpec code that
70
+ * ODK's schema API doesn't support.
71
+ *
72
+ * @param schema - ODK schema to modify
73
+ * @param content - Raw TypeSpec code
74
+ * @param description - Optional description/comment
75
+ *
76
+ * @example
77
+ * ```typescript
78
+ * odk.raw(schema, `
79
+ * @route("/api")
80
+ * interface CustomApi {
81
+ * @get op health(): void;
82
+ * }
83
+ * `);
84
+ * ```
85
+ */
86
+ function raw(schema, content, description) {
87
+ const block = { content };
88
+ if (description) {
89
+ block.description = description;
90
+ }
91
+ if (!schema.compiled.rawBlocks) {
92
+ schema.compiled.rawBlocks = [];
93
+ }
94
+ schema.compiled.rawBlocks.push(block);
95
+ }
96
+ /**
97
+ * Add a TypeSpec import to the schema.
98
+ *
99
+ * @param schema - ODK schema to modify
100
+ * @param importPath - Import path (e.g., "@typespec/http")
101
+ *
102
+ * @example
103
+ * ```typescript
104
+ * odk.addImport(schema, "@typespec/http");
105
+ * odk.addImport(schema, "@omnidotdev/typespec-drizzle");
106
+ * ```
107
+ */
108
+ function addImport(schema, importPath) {
109
+ if (!schema.compiled.imports) {
110
+ schema.compiled.imports = [];
111
+ }
112
+ if (!schema.compiled.imports.includes(importPath)) {
113
+ schema.compiled.imports.push(importPath);
114
+ }
115
+ }
116
+ /**
117
+ * Emit an ODK schema to target formats.
118
+ *
119
+ * @param schema - ODK schema created with odk.schema()
120
+ * @param options - Emission options (targets, output directory)
121
+ *
122
+ * @example
123
+ * ```typescript
124
+ * await odk.emit(schema, {
125
+ * targets: ["@typespec/openapi3", "@typespec/protobuf"],
126
+ * outDir: "./generated",
127
+ * });
128
+ * ```
129
+ */
130
+ async function emit(schema, options) {
131
+ await emitToTargets(schema.compiled, options);
132
+ }
133
+ /**
134
+ * Generate TypeSpec source code from an ODK schema.
135
+ *
136
+ * @param schema - ODK schema created with odk.schema()
137
+ * @param namespace - Optional namespace override
138
+ * @returns TypeSpec source code as a string
139
+ *
140
+ * @example
141
+ * ```typescript
142
+ * const tspSource = odk.toTypeSpec(schema);
143
+ * console.log(tspSource);
144
+ * ```
145
+ */
146
+ function toTypeSpec(schema, namespace) {
147
+ return compiledSchemaToTypeSpec(schema.compiled, namespace);
148
+ }
149
+ /**
150
+ * Get the compiled schema representation (useful for the Rust CLI).
151
+ *
152
+ * @param schema - ODK schema created with odk.schema()
153
+ * @returns CompiledSchema object
154
+ */
155
+ function getCompiled(schema) {
156
+ return schema.compiled;
157
+ }
158
+ /**
159
+ * Serialize an ODK schema to JSON (for the Rust CLI).
160
+ *
161
+ * @param schema - ODK schema created with odk.schema()
162
+ * @returns JSON string of the compiled schema
163
+ *
164
+ * @example
165
+ * ```typescript
166
+ * const json = odk.toJSON(schema);
167
+ * await Bun.write("schema.json", json);
168
+ * // Then: tspgen schema.json -o output.tsp
169
+ * ```
170
+ */
171
+ function toJSON(schema) {
172
+ return JSON.stringify(schema.compiled, null, 2);
173
+ }
174
+ /**
175
+ * ODK namespace - main entry point for the library.
176
+ */
177
+ export const odk = {
178
+ schema,
179
+ emit,
180
+ toTypeSpec,
181
+ toJSON,
182
+ getCompiled,
183
+ raw,
184
+ addImport,
185
+ };
186
+ //# sourceMappingURL=odk.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"odk.js","sourceRoot":"","sources":["../src/odk.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsCG;AAEH,OAAO,EAAE,wBAAwB,EAAE,MAAM,4BAA4B,CAAC;AACtE,OAAO,EAAE,wBAAwB,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAoB9E;;;;;;;;;;;;;;;GAeG;AACH,SAAS,MAAM,CACb,IAAY,EACZ,KAA8B,EAC9B,UAAyB,EAAE;IAE3B,MAAM,QAAQ,GAAG,wBAAwB,CAAC,IAAI,EAAE,KAAK,EAAE,OAAO,CAAC,CAAC;IAEhE,2BAA2B;IAC3B,IAAI,OAAO,CAAC,OAAO,EAAE,CAAC;QACpB,QAAQ,CAAC,OAAO,GAAG,CAAC,GAAG,CAAC,QAAQ,CAAC,OAAO,IAAI,EAAE,CAAC,EAAE,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;IACvE,CAAC;IAED,OAAO,EAAE,QAAQ,EAAE,KAAK,EAAE,CAAC;AAC7B,CAAC;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,SAAS,GAAG,CAAC,MAAiB,EAAE,OAAe,EAAE,WAAoB;IACnE,MAAM,KAAK,GAAqB,EAAE,OAAO,EAAE,CAAC;IAC5C,IAAI,WAAW,EAAE,CAAC;QAChB,KAAK,CAAC,WAAW,GAAG,WAAW,CAAC;IAClC,CAAC;IAED,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,SAAS,EAAE,CAAC;QAC/B,MAAM,CAAC,QAAQ,CAAC,SAAS,GAAG,EAAE,CAAC;IACjC,CAAC;IACD,MAAM,CAAC,QAAQ,CAAC,SAAS,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;AACxC,CAAC;AAED;;;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-0,0 +1,36 @@
1
+ import { describe, expect, test } from "bun:test";
2
+ import { z } from "zod";
3
+ import { odk } from "./index.js";
4
+ const User = z.object({
5
+ id: z.string(),
6
+ email: z.string(),
7
+ name: z.string(),
8
+ });
9
+ describe("odk", () => {
10
+ test("schema() compiles Standard Schema types into a named model", () => {
11
+ const schema = odk.schema("TestAPI", { User });
12
+ expect(schema.compiled.name).toBe("TestAPI");
13
+ expect(schema.types.User).toBe(User);
14
+ const user = schema.compiled.models.find((model) => model.name === "User");
15
+ const fieldNames = user?.fields.map((field) => field.name) ?? [];
16
+ expect(fieldNames).toContain("id");
17
+ expect(fieldNames).toContain("email");
18
+ expect(fieldNames).toContain("name");
19
+ });
20
+ test("toJSON() round-trips the compiled schema", () => {
21
+ const schema = odk.schema("TestAPI", { User });
22
+ expect(JSON.parse(odk.toJSON(schema))).toEqual(odk.getCompiled(schema));
23
+ });
24
+ test("toTypeSpec() renders the namespace and model", () => {
25
+ const tsp = odk.toTypeSpec(odk.schema("TestAPI", { User }));
26
+ expect(tsp).toContain("TestAPI");
27
+ expect(tsp).toContain("User");
28
+ });
29
+ test("addImport() adds an import without duplicating it", () => {
30
+ const schema = odk.schema("TestAPI", { User });
31
+ odk.addImport(schema, "@typespec/http");
32
+ odk.addImport(schema, "@typespec/http");
33
+ expect(schema.compiled.imports?.filter((i) => i === "@typespec/http")).toEqual(["@typespec/http"]);
34
+ });
35
+ });
36
+ //# sourceMappingURL=odk.test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"odk.test.js","sourceRoot":"","sources":["../src/odk.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,UAAU,CAAC;AAElD,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,OAAO,EAAE,GAAG,EAAE,MAAM,YAAY,CAAC;AAEjC,MAAM,IAAI,GAAG,CAAC,CAAC,MAAM,CAAC;IACpB,EAAE,EAAE,CAA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AG,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC,CAAC;IAC1E,CAAC,CAAC,CAAC;IAEH,IAAI,CAAC,8CAA8C,EAAE,GAAG,EAAE;QACxD,MAAM,GAAG,GAAG,GAAG,CAAC,UAAU,CAAC,GAAG,CAAC,MAAM,CAAC,SAAS,EAAE,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC;QAE5D,MAAM,CAAC,GAAG,CAAC,CAAC,SAAS,CAAC,SAAS,CAAC,CAAC;QACjC,MAAM,CAAC,GAAG,CAAC,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC;IAChC,CAAC,CAAC,CAAC;IAEH,IAAI,CAAC,mDAAmD,EAAE,GAAG,EAAE;QAC7D,MAAM,MAAM,GAAG,GAAG,CAAC,MAAM,CAAC,SAAS,EAAE,EAAE,IAAI,EAAE,CAAC,CAAC;QAC/C,GAAG,CAAC,SAAS,CAAC,MAAM,EAAE,gBAAgB,CAAC,CAAC;QACxC,GAAG,CAAC,SAAS,CAAC,MAAM,EAAE,gBAAgB,CAAC,CAAC;QAExC,MAAM,CACJ,MAAM,CAAC,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,KAAK,gBAAgB,CAAC,CAC/D,CAAC,OAAO,CAAC,CAAC,gBAAgB,CAAC,CAAC,CAAC;IAChC,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Standard Schema Adapter
3
+ *
4
+ * Converts any Standard Schema-compliant library (Zod, ArkType, Valibot)
5
+ * to ODK's CompiledSchema IR format.
6
+ */
7
+ import type { CompiledSchema } from "./compile.js";
8
+ export interface AdapterOptions {
9
+ /** Suppress warnings about non-serializable constraints */
10
+ suppressWarnings?: boolean;
11
+ }
12
+ /**
13
+ * Convert a collection of Standard Schema types to CompiledSchema.
14
+ */
15
+ export declare function standardSchemaToCompiled(name: string, types: Record<string, unknown>, options?: AdapterOptions): CompiledSchema;
16
+ //# sourceMappingURL=standardSchemaAdapter.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"standardSchemaAdapter.d.ts","sourceRoot":"","sources":["../src/standardSchemaAdapter.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAIH,OAAO,KAAK,EAGV,cAAc,EAKf,MAAM,cAAc,CAAC;AAGtB,MAAM,WAAW,cAAc;IAC7B,2DAA2D;IAC3D,gBAAgB,CAAC,EAAE,OAAO,CAAC;CAC5B;AAED;;GAEG;AACH,wBAAgB,wBAAwB,CACtC,IAAI,EAAE,MAAM,EACZ,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC9B,OAAO,GAAE,cAAmB,GAC3B,cAAc,CAsChB"}