Module: OpenAPIGenerateTypeScriptFetch

Defined in:
lib/openapi_generate_typescript_fetch.rb,
lib/openapi_generate_typescript_fetch/tasks.rb,
lib/openapi_generate_typescript_fetch/schema.rb,
lib/openapi_generate_typescript_fetch/version.rb,
lib/openapi_generate_typescript_fetch/taskinfo.rb

Overview

Copyright © 2025 Ismo Kärkkäinen Licensed under Universal Permissive License. See LICENSE.txt.

Defined Under Namespace

Classes: ExportedNames, ObjectSchema, ObjectSchemaProperty, TaskInfo

Constant Summary collapse

"Copyright #{Date.today.year} by the respective holders.
This is a default copyright notice. Substitute with your own in configuration.
(Use 'copyright' configuration key.)".freeze
NAME =
'openapi_generate_typescript_fetch'
VERSION =
'0.1.0'

Class Method Summary collapse

Class Method Details

.config_defaults(cfg) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/openapi_generate_typescript_fetch.rb', line 20

def self.config_defaults(cfg)
  ds = {
    'subdir' => '.',
    'copy' => [],
    'copyright' => COPYRIGHT_DEFAULT,
    'ts_indentation' => {
      'indent_character' => ' ',
      'indent_step' => 2,
      'tab' => "\t",
      'tab_replaces_count' => 0
    }
  }
  ds.each do |key, value|
    cfg[key] = value unless cfg.key?(key)
  end
  cfg
end

.full_template_name(template_name) ⇒ Object



36
37
38
# File 'lib/openapi_generate_typescript_fetch/tasks.rb', line 36

def self.full_template_name(template_name)
  File.join(template_directory, template_name)
end

.gitignoreObject



56
57
58
59
60
61
62
63
# File 'lib/openapi_generate_typescript_fetch.rb', line 56

def self.gitignore
  <<EOB
coverage
coverage-reports
node_modules
pkg
EOB
end

.licenseObject



40
41
42
# File 'lib/openapi_generate_typescript_fetch/tasks.rb', line 40

def self.license
  File.read(File.join(template_directory, '..', 'LICENSE.txt'))
end

.obtain_content(root, info) ⇒ Object

Raises:

  • (StandardError)


75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/openapi_generate_typescript_fetch.rb', line 75

def self.obtain_content(root, info)
  # Expects 'target' and either 'content' or 'source'.
  content = info['content']
  return content unless content.nil?
  src = info['source']
  raise StandardError, "No copy file content or source name provided, copy target: #{info['target']}" if src.nil?
  begin
    File.binread(File.join(root, src))
  rescue Exception => e
    $stderr.puts("Failed to read copy file from source: '#{src}', copy target: #{info['target']}")
    raise e
  end
end

.setup_copies(copies, subdir) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/openapi_generate_typescript_fetch.rb', line 89

def self.setup_copies(copies, subdir)
  own_copies = [{
    'target' => 'mcr.config.json',
    'content' => '{"reports":["text","v8","v8-json","raw"],"entryFilter":"**/pkg/src/**"}'
  }]
  # Order allows easy overwrites from user's config.
  copies = [].concat(own_copies, copies)
  copies.size.times do |k|
    info = copies[k]
    name = info['target']
    raise StandardError, "No copy file target name provided, copy index: #{k - own_copies.size}" if name.nil?
    content = obtain_content(Gen.wd, info)
    Gen.add_write_content(name: File.join(subdir, name), content: content)
  end
end

.setup_tasksObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/openapi_generate_typescript_fetch.rb', line 38

def self.setup_tasks
  # Configurations are read at this point to catch errors early.
  cfg = config_defaults(Gen.load_config(Gen.config || OpenAPIGenerateTypeScriptFetch::NAME))
  Gen.x = TaskInfo.new(cfg, OpenAPIArrangement::Schema.alphabetical(Gen.doc))
  cr = cfg['copyright'].lines.map! { |x| "// #{x}".rstrip }
  Gen.x.generator_info = <<EOB
#{cr.join("\n")}
//
// Generated code. Do not edit.
//
// Generated from #{Gen.doc.dig('info', 'title')} version #{Gen.doc.dig('info', 'version')}
// Gem name: #{OpenAPIGenerateTypeScriptFetch::NAME}
// Gem version: #{OpenAPIGenerateTypeScriptFetch::VERSION}
EOB
  setup_templates(OpenAPIGenerateTypeScriptFetch.template_names, cfg['subdir'])
  setup_copies(cfg['copy'], cfg['subdir'])
end

.setup_templates(names, subdir) ⇒ Object



65
66
67
68
69
70
71
72
73
# File 'lib/openapi_generate_typescript_fetch.rb', line 65

def self.setup_templates(names, subdir)
  names.each do |template_name|
    f = OpenAPIGenerateTypeScriptFetch.full_template_name(template_name)
    template = File.read(f)
    name = File.join(subdir, template_name[0..-5]) # Drop '.erb'
    executable = name.upcase.end_with?('.SH')
    Gen.add(source: Gen.doc, template:, template_name:, name:, executable:)
  end
end

.template_namesObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/openapi_generate_typescript_fetch/tasks.rb', line 17

def self.template_names
  [ # Consider import order when ordering these files.
    'package.json.erb',
    'tsconfig.json.erb',
    'src/helpers.ts.erb',
    'src/servers.ts.erb',
    'src/schemas.ts.erb',
    'src/shared.ts.erb',
    'src/callclasses.ts.erb',
    'src/index.ts.erb',
    'test/helpers.ts.erb',
    'test/makers.ts.erb',
    'test/schemas.ts.erb',
    'test/shared.ts.erb',
    'test/servers.ts.erb',
    'test/callclasses.ts.erb'
  ]
end