Class: JekyllOpenAPI::SchemaRenderer::TypeScript

Inherits:
Object
  • Object
show all
Defined in:
lib/jekyll_openapi/schema_renderer/typescript.rb

Overview

Renders a JSON-Schema as a simplified TypeScript-like textual representation. Traversal, type inference and $ref following live in JekyllOpenAPI::Schema; this class only decides how each shape prints.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger: JekyllOpenAPI.logger) ⇒ TypeScript

Returns a new instance of TypeScript.



11
12
13
# File 'lib/jekyll_openapi/schema_renderer/typescript.rb', line 11

def initialize(logger: JekyllOpenAPI.logger)
  @logger = logger
end

Class Method Details

.render(schema, logger: JekyllOpenAPI.logger) ⇒ Object



15
16
17
# File 'lib/jekyll_openapi/schema_renderer/typescript.rb', line 15

def self.render(schema, logger: JekyllOpenAPI.logger)
  new(logger: logger).render(schema)
end

Instance Method Details

#render(input, prefix = "") ⇒ String

Parameters:

  • input (Hash, Reference, Schema)

    a JSON-Schema

  • prefix (String) (defaults to: "")

    current indentation

Returns:

  • (String)


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/jekyll_openapi/schema_renderer/typescript.rb', line 22

def render(input, prefix = "")
  schema = Schema.wrap(input)

  if schema.reference?
    followed = schema.follow
    # A cycle prints the schema's name (e.g. "Node") instead of looping.
    return schema.name.to_s if followed == Schema::CYCLE
    return "" if followed.nil?
    return render(followed, prefix)
  end

  unless schema.mapping?
    @logger.warn("not a valid schema object: #{schema.raw.inspect}")
    return ""
  end

  str = base(schema, prefix)
  return "" if str.nil?
  annotate(str, schema)
end