openapi_generate_typescript_fetch 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 6c88d2001fe469f8df04f3e11f72debff180b87dbcd66dcf0e9b6f0804a789f7
4
+ data.tar.gz: 6fcc50af48f8b2b82cfeee034b9aa412def76f031931fc1dc657bc733e856e50
5
+ SHA512:
6
+ metadata.gz: 4897d3b2c4f0c6ef83efa29d801f0282e40611d7f8c7ae867ae1ab7ca7aa370845d88f2fe45036293d69cf9e7b2ee214d4e16745c772e45e0ae172cd695c9a88
7
+ data.tar.gz: 3237efee6086d9772723d474fa7653d135134ae68a3b4b2cb97b604db02196d92a57b60817eb0e74fd3550f66e55ae334bb789f29c63d79dceb8fffbeeb0c40a
data/LICENSE.txt ADDED
@@ -0,0 +1,17 @@
1
+ Copyright (c) 2025-2026 Ismo Kärkkäinen
2
+
3
+ The Universal Permissive License (UPL), Version 1.0
4
+
5
+ Subject to the condition set forth below, permission is hereby granted to any person obtaining a copy of this software, associated documentation and/or data (collectively the "Software"), free of charge and under any and all copyright rights in the Software, and any and all patent rights owned or freely licensable by each licensor hereunder covering either (i) the unmodified Software as contributed to or provided by such licensor, or (ii) the Larger Works (as defined below), to deal in both
6
+
7
+ (a) the Software, and
8
+
9
+ (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if one is included with the Software (each a “Larger Work” to which the Software is contributed by such licensors),
10
+
11
+ without restriction, including without limitation the rights to copy, create derivative works of, display, perform, and distribute the Software and make, use, sell, offer for sale, import, export, have made, and have sold the Software and the Larger Work(s), and to sublicense the foregoing rights on either these or other terms.
12
+
13
+ This license is subject to the following condition:
14
+
15
+ The above copyright notice and either this complete permission notice or at a minimum a reference to the UPL must be included in all copies or substantial portions of the Software.
16
+
17
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,115 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright © 2025 Ismo Kärkkäinen
4
+ # Licensed under Universal Permissive License. See LICENSE.txt.
5
+
6
+ require 'lucky_case'
7
+
8
+ # Namespace for functions with supposedly only internal use.
9
+ module OpenAPIGenerateTypeScriptFetch
10
+ # Contains info about a property of an object schema.
11
+ class ObjectSchemaProperty
12
+ include Comparable
13
+
14
+ attr_reader :name, :req, :type, :pattern, :additional, :spec
15
+
16
+ def initialize(type:, spec:, name: nil, req: false, pattern: false, additional: false)
17
+ @name = name
18
+ @req = req
19
+ @type = type
20
+ @pattern = pattern
21
+ @additional = additional
22
+ @spec = spec
23
+ end
24
+
25
+ def <=>(other)
26
+ return -1 if !@name.nil? && other.name.nil?
27
+ return 1 if @name.nil? && !other.name.nil?
28
+ d = @name <=> other.name
29
+ return d unless d.zero?
30
+ d = @pattern <=> other.pattern
31
+ return d unless d.zero?
32
+ d = @additional <=> other.additional
33
+ return d unless d.zero?
34
+ d = @type <=> other.type
35
+ return d unless d.zero?
36
+ @spec <=> other.spec
37
+ end
38
+
39
+ def to_s
40
+ "ObjectSchemaProperty(name: #{@name}, req: #{@req}, type: #{@type}, pattern: #{@pattern}, additional: #{@additional}, spec: #{@spec})"
41
+ end
42
+ end
43
+
44
+ # Common info about schema, used in multiple places so gathered here to shorten templates.
45
+ class ObjectSchema
46
+ attr_reader :props, :additional, :schema, :unknown_names
47
+ def initialize(schema)
48
+ raise ArgumentError, "#{schema['type']} not an object" unless schema['type'] == 'object'
49
+ @schema = schema
50
+ @props = []
51
+ props = schema['properties'] || {}
52
+ pat_props = schema['patternProperties'] || {}
53
+ add_props = schema['additionalProperties']
54
+ reqd = schema['required'] || []
55
+ props.each do |name, spec|
56
+ @props.push(ObjectSchemaProperty.new(
57
+ name: name,
58
+ req: reqd.include?(name),
59
+ type: (Gen.h.category_and_name(spec) || [ spec['type'] ]).last,
60
+ spec: spec
61
+ ))
62
+ end
63
+ pat_props.each do |pattern, spec|
64
+ @props.push(ObjectSchemaProperty.new(
65
+ name: pattern,
66
+ type: (Gen.h.category_and_name(spec) || [ spec['type'] ]).last,
67
+ pattern: true,
68
+ spec: spec
69
+ ))
70
+ end
71
+ if add_props.is_a?(Hash)
72
+ @additional = true
73
+ @props.push(ObjectSchemaProperty.new(
74
+ type: (Gen.h.category_and_name(add_props) || [ add_props['type'] ]).last,
75
+ additional: true,
76
+ spec: add_props
77
+ ))
78
+ elsif add_props.nil? || add_props == true
79
+ @additional = true
80
+ @props.push(ObjectSchemaProperty.new(
81
+ type: nil,
82
+ additional: true,
83
+ spec: add_props
84
+ ))
85
+ else
86
+ @additional = false
87
+ end
88
+ @props.sort!
89
+ return if @additional
90
+ if !pat_props.empty?
91
+ # All patterns must have same failure cases.
92
+ tps = Gen.x.cfg.dig(*%w[tests patterns])
93
+ return if tps.nil?
94
+ cands = nil
95
+ @props.select(&:pattern).each do |p|
96
+ pt = tps.index { |pc| pc['pattern'] == p.name }
97
+ return if pt.nil?
98
+ fails = tps[pt]['fail']
99
+ return if fails.nil?
100
+ cands = cands.nil? ? Set.new(fails) : cands & Set.new(fails)
101
+ return if cands.empty?
102
+ end
103
+ cands -= Set.new(@props.reject(&:pattern).map(&:name))
104
+ @unknown_names = cands.empty? ? nil : cands.to_a.sort!
105
+ elsif !props.empty?
106
+ fixed = @props.reject(&:pattern).map(&:name).map(&:size)
107
+ @unknown_names = [ 'a' * (fixed.max + 1) ]
108
+ @unknown_names.push('a') if fixed.min > 1
109
+ else
110
+ # No properties, pattern properties, no additional properties.
111
+ @unknown_names = [ 'a' ]
112
+ end
113
+ end
114
+ end
115
+ end
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright © 2025 Ismo Kärkkäinen
4
+ # Licensed under Universal Permissive License. See LICENSE.txt.
5
+
6
+ require_relative 'version'
7
+
8
+ module OpenAPIGenerateTypeScriptFetch
9
+ # For storing names for imports.
10
+ class ExportedNames
11
+ attr_reader :basename, :types, :functions, :consts, :interfaces, :enums, :classes
12
+
13
+ def initialize(basename)
14
+ @basename = basename
15
+ @types = []
16
+ @functions = []
17
+ @consts = []
18
+ @interfaces = []
19
+ @enums = []
20
+ @classes = []
21
+ end
22
+
23
+ def imports
24
+ [].concat(@functions, @consts, @interfaces, @enums, @classes).sort!
25
+ end
26
+
27
+ def import_types
28
+ @types.sort
29
+ end
30
+ end
31
+
32
+ # For use as Gen.x.
33
+ class TaskInfo
34
+ attr_reader :cfg, :order, :gem_name, :gem_version
35
+ attr_accessor :generator_info
36
+ attr_reader :schemas, :servers, :shared, :callclasses, :makers
37
+ attr_accessor :server_data
38
+
39
+ def initialize(config, order)
40
+ @cfg = config
41
+ @order = order
42
+ @gem_name = OpenAPIGenerateTypeScriptFetch::NAME
43
+ @gem_version = OpenAPIGenerateTypeScriptFetch::VERSION
44
+ @schemas = ExportedNames.new('schemas')
45
+ @servers = ExportedNames.new('servers')
46
+ @shared = ExportedNames.new('shared')
47
+ @callclasses = ExportedNames.new('callclasses')
48
+ @makers = ExportedNames.new('makers')
49
+ @server_data = nil
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright © 2024-2026 Ismo Kärkkäinen
4
+ # Licensed under Universal Permissive License. See LICENSE.txt.
5
+
6
+ require_relative 'schema' # For templates.
7
+ require 'lucky_case'
8
+ require 'uri'
9
+
10
+ # Namespace for functions with supposedly only internal use.
11
+ module OpenAPIGenerateTypeScriptFetch
12
+ def self.template_directory
13
+ File.join(File.dirname(__FILE__), '..', '..', 'template')
14
+ end
15
+ private_class_method :template_directory
16
+
17
+ def self.template_names
18
+ [ # Consider import order when ordering these files.
19
+ 'package.json.erb',
20
+ 'tsconfig.json.erb',
21
+ 'src/helpers.ts.erb',
22
+ 'src/servers.ts.erb',
23
+ 'src/schemas.ts.erb',
24
+ 'src/shared.ts.erb',
25
+ 'src/callclasses.ts.erb',
26
+ 'src/index.ts.erb',
27
+ 'test/helpers.ts.erb',
28
+ 'test/makers.ts.erb',
29
+ 'test/schemas.ts.erb',
30
+ 'test/shared.ts.erb',
31
+ 'test/servers.ts.erb',
32
+ 'test/callclasses.ts.erb'
33
+ ]
34
+ end
35
+
36
+ def self.full_template_name(template_name)
37
+ File.join(template_directory, template_name)
38
+ end
39
+
40
+ def self.license
41
+ File.read(File.join(template_directory, '..', 'LICENSE.txt'))
42
+ end
43
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright © 2025 Ismo Kärkkäinen
4
+ # Licensed under Universal Permissive License. See LICENSE.txt.
5
+
6
+ module OpenAPIGenerateTypeScriptFetch
7
+ NAME = 'openapi_generate_typescript_fetch'
8
+ VERSION = '0.1.0'
9
+ end
@@ -0,0 +1,107 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright © 2024-2026 Ismo Kärkkäinen
4
+ # Licensed under Universal Permissive License. See LICENSE.txt.
5
+
6
+ require_relative 'openapi_generate_typescript_fetch/tasks'
7
+ require_relative 'openapi_generate_typescript_fetch/version'
8
+ require_relative 'openapi_generate_typescript_fetch/taskinfo'
9
+ require 'openapi/sourcetools'
10
+ require 'openapi/arrangement'
11
+ require 'date'
12
+ require 'base64'
13
+
14
+ # Task initialization and helper code.
15
+ module OpenAPIGenerateTypeScriptFetch
16
+ COPYRIGHT_DEFAULT = "Copyright #{Date.today.year} by the respective holders.
17
+ This is a default copyright notice. Substitute with your own in configuration.
18
+ (Use 'copyright' configuration key.)".freeze
19
+
20
+ def self.config_defaults(cfg)
21
+ ds = {
22
+ 'subdir' => '.',
23
+ 'copy' => [],
24
+ 'copyright' => COPYRIGHT_DEFAULT,
25
+ 'ts_indentation' => {
26
+ 'indent_character' => ' ',
27
+ 'indent_step' => 2,
28
+ 'tab' => "\t",
29
+ 'tab_replaces_count' => 0
30
+ }
31
+ }
32
+ ds.each do |key, value|
33
+ cfg[key] = value unless cfg.key?(key)
34
+ end
35
+ cfg
36
+ end
37
+
38
+ def self.setup_tasks
39
+ # Configurations are read at this point to catch errors early.
40
+ cfg = config_defaults(Gen.load_config(Gen.config || OpenAPIGenerateTypeScriptFetch::NAME))
41
+ Gen.x = TaskInfo.new(cfg, OpenAPIArrangement::Schema.alphabetical(Gen.doc))
42
+ cr = cfg['copyright'].lines.map! { |x| "// #{x}".rstrip }
43
+ Gen.x.generator_info = <<EOB
44
+ #{cr.join("\n")}
45
+ //
46
+ // Generated code. Do not edit.
47
+ //
48
+ // Generated from #{Gen.doc.dig('info', 'title')} version #{Gen.doc.dig('info', 'version')}
49
+ // Gem name: #{OpenAPIGenerateTypeScriptFetch::NAME}
50
+ // Gem version: #{OpenAPIGenerateTypeScriptFetch::VERSION}
51
+ EOB
52
+ setup_templates(OpenAPIGenerateTypeScriptFetch.template_names, cfg['subdir'])
53
+ setup_copies(cfg['copy'], cfg['subdir'])
54
+ end
55
+
56
+ def self.gitignore
57
+ <<EOB
58
+ coverage
59
+ coverage-reports
60
+ node_modules
61
+ pkg
62
+ EOB
63
+ end
64
+
65
+ def self.setup_templates(names, subdir)
66
+ names.each do |template_name|
67
+ f = OpenAPIGenerateTypeScriptFetch.full_template_name(template_name)
68
+ template = File.read(f)
69
+ name = File.join(subdir, template_name[0..-5]) # Drop '.erb'
70
+ executable = name.upcase.end_with?('.SH')
71
+ Gen.add(source: Gen.doc, template:, template_name:, name:, executable:)
72
+ end
73
+ end
74
+
75
+ def self.obtain_content(root, info)
76
+ # Expects 'target' and either 'content' or 'source'.
77
+ content = info['content']
78
+ return content unless content.nil?
79
+ src = info['source']
80
+ raise StandardError, "No copy file content or source name provided, copy target: #{info['target']}" if src.nil?
81
+ begin
82
+ File.binread(File.join(root, src))
83
+ rescue Exception => e
84
+ $stderr.puts("Failed to read copy file from source: '#{src}', copy target: #{info['target']}")
85
+ raise e
86
+ end
87
+ end
88
+
89
+ def self.setup_copies(copies, subdir)
90
+ own_copies = [{
91
+ 'target' => 'mcr.config.json',
92
+ 'content' => '{"reports":["text","v8","v8-json","raw"],"entryFilter":"**/pkg/src/**"}'
93
+ }]
94
+ # Order allows easy overwrites from user's config.
95
+ copies = [].concat(own_copies, copies)
96
+ copies.size.times do |k|
97
+ info = copies[k]
98
+ name = info['target']
99
+ raise StandardError, "No copy file target name provided, copy index: #{k - own_copies.size}" if name.nil?
100
+ content = obtain_content(Gen.wd, info)
101
+ Gen.add_write_content(name: File.join(subdir, name), content: content)
102
+ end
103
+ end
104
+ end
105
+
106
+ # Runs when the gem is loaded the first time from openapi-generate.
107
+ OpenAPIGenerateTypeScriptFetch.setup_tasks if defined?(Gen)
@@ -0,0 +1,48 @@
1
+ <%=
2
+ tgt = Gen.x.cfg.fetch('target', 'browser')
3
+ defaults = {
4
+ "main" => "pkg/src/index.js",
5
+ "files" => [
6
+ "pkg/src/*.js",
7
+ "pkg/src/*.d.ts"
8
+ ],
9
+ "type" => "module",
10
+ "scripts" => {
11
+ "build" => "tsc",
12
+ "test" => "tsc && mcr mocha pkg/test"
13
+ }
14
+ }
15
+ devDeps = {
16
+ "chai" => "6.2.2",
17
+ "mocha" => "11.7.6",
18
+ "monocart-coverage-reports" => "2.12.12",
19
+ "typescript" => "7.0.2",
20
+ "@types/chai" => "5.2.3",
21
+ "@types/mocha" => "10.0.10"
22
+ }
23
+ if tgt == 'node'
24
+ devDeps.merge!({
25
+ "@types/node" => "24.12.4",
26
+ "@tsconfig/node-lts" => "24.0.0",
27
+ "@tsconfig/node-ts" => "23.6.4"
28
+ })
29
+ elsif tgt == 'browser'
30
+ devDeps.merge!({
31
+ "@types/web" => "0.0.354",
32
+ "@tsconfig/recommended" => "1.0.13"
33
+ })
34
+ end
35
+ defaults['devDependencies'] = devDeps
36
+ begin
37
+ cfg = Gen.x.cfg['package'] || {}
38
+ cfg = JSON.parse(cfg) unless cfg.is_a?(Hash)
39
+ rescue StandardError => e
40
+ raise StandardError, "#{OpenAPIGenerateTypeScriptFetch::NAME}: Error parsing package.json: #{e.message}"
41
+ end
42
+ %w[scripts devDependencies].each do |key|
43
+ next unless cfg.key?(key) && defaults.key?(key)
44
+ cfg[key].merge!(defaults[key]) { |_key, cfg_value, _default_value| cfg_value }
45
+ end
46
+ cfg.merge!(defaults) { |_key, cfg_value, _default_value| cfg_value }
47
+ Gen.output.pretty_json(cfg)
48
+ %>