Skip to content

Commit 786bae2

Browse files
committed
refactor: create static type-def for zod inference overhead removal
1 parent 1139ee0 commit 786bae2

File tree

1 file changed

+22
-35
lines changed

1 file changed

+22
-35
lines changed

packages/create_freestyle_fetch/src/schema_generator.ts

Lines changed: 22 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@ import type { OpenAPIV3_1 } from 'openapi-types'
22
import { SchemaValidationError } from './errors' // Assumed existing
33
import { toPascalCase } from './utils' // Assumed existing
44

5-
function isReferenceObject(obj: any): obj is OpenAPIV3_1.ReferenceObject {
6-
return obj && '$ref' in obj
7-
}
8-
95
export class SchemaGenerator {
106
private spec: OpenAPIV3_1.Document
117

@@ -19,6 +15,10 @@ export class SchemaGenerator {
1915
this.spec = spec
2016
}
2117

18+
private isReferenceObject(obj: any): obj is OpenAPIV3_1.ReferenceObject {
19+
return obj && '$ref' in obj
20+
}
21+
2222
private initializeSchemaNames() {
2323
if (!this.spec.components?.schemas) return
2424

@@ -74,42 +74,29 @@ export class SchemaGenerator {
7474

7575
const modelStrings: string[] = [`import { z } from 'zod';`]
7676

77-
// 4. Generate Types for Cyclic Schemas FIRST
78-
// Use type
79-
if (this.cyclicSchemas.size > 0) {
80-
modelStrings.push('// Helper types for recursive schemas')
81-
for (const name of this.cyclicSchemas) {
82-
const pascalName = this.getSchemaName(name)
83-
const typeDef = this.mapSchemaObjectToType(name)
84-
modelStrings.push(
85-
`export type ${pascalName}Model = ${typeDef};`
86-
)
87-
}
88-
modelStrings.push('')
89-
}
90-
91-
// 5. Sort schemas topologically
77+
// 4. Sort schemas topologically
9278
const sortedSchemaNames = this.sortSchemas()
9379

80+
// 5. Generate Types for ALL schemas for performance
81+
modelStrings.push('// Helper types for schemas')
82+
for (const name of sortedSchemaNames) {
83+
const pascalName = this.getSchemaName(name)
84+
const typeDef = this.mapSchemaObjectToType(name)
85+
modelStrings.push(`export type ${pascalName}Model = ${typeDef};`)
86+
}
87+
modelStrings.push('')
88+
9489
// 6. Generate Zod Schemas
9590
for (const name of sortedSchemaNames) {
9691
const zodSchema =
9792
this.processedSchemas.get(name) ||
9893
this.mapSchemaObjectToZod(name)
9994
const pascalName = this.getSchemaName(name)
10095

101-
if (this.cyclicSchemas.has(name)) {
102-
// For cyclic schemas, use the explicitly generated type
103-
modelStrings.push(
104-
`export const ${pascalName}: z.ZodType<${pascalName}Model> = ${zodSchema};`
105-
)
106-
} else {
107-
// Standard generation
108-
modelStrings.push(`export const ${pascalName} = ${zodSchema};`)
109-
modelStrings.push(
110-
`export type ${pascalName}Model = z.infer<typeof ${pascalName}>;`
111-
)
112-
}
96+
// Use explicit type for perf.
97+
modelStrings.push(
98+
`export const ${pascalName}: z.ZodType<${pascalName}Model> = ${zodSchema};`
99+
)
113100
}
114101

115102
return modelStrings.join('\n\n')
@@ -122,7 +109,7 @@ export class SchemaGenerator {
122109
const schema = schemaObject || this.spec.components?.schemas?.[name]
123110
if (!schema) return 'any'
124111

125-
if (isReferenceObject(schema)) {
112+
if (this.isReferenceObject(schema)) {
126113
const { name: refName } = this.resolveRef(schema.$ref)
127114
return `${this.getSchemaName(refName)}Model`
128115
}
@@ -193,14 +180,14 @@ export class SchemaGenerator {
193180
}
194181

195182
if (schema.additionalProperties) {
196-
const addlType =
183+
const additionalTypes =
197184
typeof schema.additionalProperties === 'object'
198185
? this.mapSchemaObjectToType(
199186
name,
200187
schema.additionalProperties
201188
)
202189
: 'any'
203-
props.push(` [key: string]: ${addlType};`)
190+
props.push(` [key: string]: ${additionalTypes};`)
204191
}
205192
return `{\n${props.join('\n')}\n}`
206193
}
@@ -271,7 +258,7 @@ export class SchemaGenerator {
271258
}
272259

273260
// Handle References
274-
if (isReferenceObject(schema)) {
261+
if (this.isReferenceObject(schema)) {
275262
const { name: refName } = this.resolveRef(schema.$ref)
276263

277264
// Track dependency

0 commit comments

Comments
 (0)