Class: ZodRails::Generation::TypescriptEmitter

Inherits:
Object
  • Object
show all
Defined in:
lib/zod_rails/generation/typescript_emitter.rb

Instance Method Summary collapse

Instance Method Details

#emit(schema_name:, schema_body:, type_name: nil) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/zod_rails/generation/typescript_emitter.rb', line 6

def emit(schema_name:, schema_body:, type_name: nil)
  type_name ||= derive_type_name(schema_name)

  "    import { z } from \"zod\";\n\n    export const \#{schema_name} = \#{schema_body};\n\n    export type \#{type_name} = z.infer<typeof \#{schema_name}>;\n  TYPESCRIPT\nend\n"

#emit_combined(response:, input:) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/zod_rails/generation/typescript_emitter.rb', line 18

def emit_combined(response:, input:)
  response_type = response[:type_name] || derive_type_name(response[:name])
  input_type = input[:type_name] || derive_type_name(input[:name])

  "    import { z } from \"zod\";\n\n    export const \#{response[:name]} = \#{response[:body]};\n\n    export type \#{response_type} = z.infer<typeof \#{response[:name]}>;\n\n    export const \#{input[:name]} = \#{input[:body]};\n\n    export type \#{input_type} = z.infer<typeof \#{input[:name]}>;\n  TYPESCRIPT\nend\n"