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:) ⇒ 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 = derive_type_name(schema_name)

  <<~TYPESCRIPT
    import { z } from "zod";

    export const #{schema_name} = #{schema_body};

    export type #{type_name} = z.infer<typeof #{schema_name}>;
  TYPESCRIPT
end

#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 = derive_type_name(response[:name])
  input_type = derive_type_name(input[:name])

  <<~TYPESCRIPT
    import { z } from "zod";

    export const #{response[:name]} = #{response[:body]};

    export type #{response_type} = z.infer<typeof #{response[:name]}>;

    export const #{input[:name]} = #{input[:body]};

    export type #{input_type} = z.infer<typeof #{input[:name]}>;
  TYPESCRIPT
end