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 +7 -0
- data/LICENSE.txt +17 -0
- data/lib/openapi_generate_typescript_fetch/schema.rb +115 -0
- data/lib/openapi_generate_typescript_fetch/taskinfo.rb +52 -0
- data/lib/openapi_generate_typescript_fetch/tasks.rb +43 -0
- data/lib/openapi_generate_typescript_fetch/version.rb +9 -0
- data/lib/openapi_generate_typescript_fetch.rb +107 -0
- data/template/package.json.erb +48 -0
- data/template/src/callclasses.ts.erb +542 -0
- data/template/src/helpers.ts.erb +191 -0
- data/template/src/index.ts.erb +19 -0
- data/template/src/schemas.ts.erb +1016 -0
- data/template/src/servers.ts.erb +226 -0
- data/template/src/shared.ts.erb +114 -0
- data/template/test/callclasses.ts.erb +844 -0
- data/template/test/helpers.ts.erb +1214 -0
- data/template/test/makers.ts.erb +1237 -0
- data/template/test/schemas.ts.erb +664 -0
- data/template/test/servers.ts.erb +109 -0
- data/template/test/shared.ts.erb +107 -0
- data/template/tsconfig.json.erb +51 -0
- metadata +112 -0
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
<%=
|
|
2
|
+
Gen.output.config = Gen.x.cfg['ts_indentation']
|
|
3
|
+
Gen.x.generator_info
|
|
4
|
+
%>
|
|
5
|
+
|
|
6
|
+
import {expect} from 'chai';
|
|
7
|
+
import type {<%= Gen.x.servers.import_types.join(', ') %>} from '../src/servers.d.ts';
|
|
8
|
+
import {<%= Gen.x.servers.functions.sort.join(', ') %>} from '../src/servers.js';
|
|
9
|
+
|
|
10
|
+
<%=
|
|
11
|
+
out = []
|
|
12
|
+
sos = Gen.x.server_data[:server_objects]
|
|
13
|
+
so2checker = Gen.x.server_data[:checkers]
|
|
14
|
+
so2args = Gen.x.server_data[:args]
|
|
15
|
+
sos.each do |so|
|
|
16
|
+
out.push("describe('#{so2checker[so]}', function () {")
|
|
17
|
+
if so.variables.empty?
|
|
18
|
+
s = <<EOB
|
|
19
|
+
it('Returns server URL', function () {
|
|
20
|
+
expect(#{so2checker[so]}()).to.equal('#{so.url}');
|
|
21
|
+
});
|
|
22
|
+
EOB
|
|
23
|
+
out.push(s)
|
|
24
|
+
else
|
|
25
|
+
v2pass = {}.compare_by_identity
|
|
26
|
+
v2fail = {}.compare_by_identity
|
|
27
|
+
so.variables.each do |svo|
|
|
28
|
+
if svo.enum.empty?
|
|
29
|
+
v2pass[svo] = [ svo.name.upcase ]
|
|
30
|
+
v2fail[svo] = []
|
|
31
|
+
else
|
|
32
|
+
v2pass[svo] = svo.enum
|
|
33
|
+
ml = svo.enum.map(&:size).max
|
|
34
|
+
v2fail[svo] = [ 'X' * (ml + 1) ]
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
s = <<EOB
|
|
38
|
+
it('Returns server URL with all arguments set', function () {
|
|
39
|
+
const args: #{so2args[so]} = {
|
|
40
|
+
#{so.variables.map { |v| "#{v.name}: '#{v2pass[v].first}'" }.join(",\n")}
|
|
41
|
+
};
|
|
42
|
+
const url: string = #{so2checker[so]}(args);
|
|
43
|
+
#{so.variables.map { |v| "expect(url).to.include('#{v2pass[v].first}');" }.join("\n")}
|
|
44
|
+
});
|
|
45
|
+
EOB
|
|
46
|
+
out.push(s)
|
|
47
|
+
s = <<EOB
|
|
48
|
+
it('Returns server URL with no optional arguments set', function () {
|
|
49
|
+
const args: #{so2args[so]} = {
|
|
50
|
+
#{so.variables.reject { |v| v.default }.map { |v| "#{v.name}: '#{v2pass[v].first}'" }.join(",\n") || '// All optional.'}
|
|
51
|
+
};
|
|
52
|
+
const url: string = #{so2checker[so]}(args);
|
|
53
|
+
#{so.variables.reject { |v| v.default }.map { |v| "expect(url).to.include('#{v2pass[v].first}');" }.join("\n") || '// Nothing without default.'}
|
|
54
|
+
#{so.variables.reject { |v| v.default.nil? }.map { |v| "expect(url).to.include('#{v.default}');" }.join("\n") || '// Nothing with default.'}
|
|
55
|
+
});
|
|
56
|
+
EOB
|
|
57
|
+
out.push(s)
|
|
58
|
+
# Pass all valid enums other than first.
|
|
59
|
+
firsts = so.variables.map { |v| "#{v.name}: '#{v2pass[v].first}'" }
|
|
60
|
+
so.variables.size.times do |chosen|
|
|
61
|
+
v = so.variables[chosen]
|
|
62
|
+
v.enum.size.times do |n|
|
|
63
|
+
next unless n.positive?
|
|
64
|
+
value = v.enum[n]
|
|
65
|
+
lines = []
|
|
66
|
+
so.variables.size.times do |k|
|
|
67
|
+
if k == chosen
|
|
68
|
+
lines.push("#{v.name}: '#{value}'")
|
|
69
|
+
else
|
|
70
|
+
lines.push(firsts[k])
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
s = <<EOB
|
|
74
|
+
it('Returns server URL with #{v.name} enum #{value}', function () {
|
|
75
|
+
const args: #{so2args[so]} = {
|
|
76
|
+
#{lines.join(",\n")}
|
|
77
|
+
};
|
|
78
|
+
const url: string = #{so2checker[so]}(args);
|
|
79
|
+
expect(url).to.include('#{value}');
|
|
80
|
+
});
|
|
81
|
+
EOB
|
|
82
|
+
out.push(s)
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
# Fail each in turn, if possible.
|
|
86
|
+
so.variables.size.times do |chosen|
|
|
87
|
+
v = so.variables[chosen]
|
|
88
|
+
fails = v2fail[v]
|
|
89
|
+
next if fails.empty?
|
|
90
|
+
lines = []
|
|
91
|
+
so.variables.size.times do |k|
|
|
92
|
+
value = ((k == chosen) ? fails : v2pass[so.variables[k]]).first
|
|
93
|
+
lines.push("#{so.variables[k].name}: '#{value}'")
|
|
94
|
+
end
|
|
95
|
+
s = <<EOB
|
|
96
|
+
it('Throws when #{v.name} is #{fails.first}', function () {
|
|
97
|
+
const args: #{so2args[so]} = {
|
|
98
|
+
#{lines.join(",\n")}
|
|
99
|
+
};
|
|
100
|
+
expect(() => #{so2checker[so]}(args)).to.throw;
|
|
101
|
+
});
|
|
102
|
+
EOB
|
|
103
|
+
out.push(s)
|
|
104
|
+
end
|
|
105
|
+
end
|
|
106
|
+
out.push('});')
|
|
107
|
+
end
|
|
108
|
+
Gen.output.join(out)
|
|
109
|
+
%>
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
<%=
|
|
2
|
+
Gen.output.config = Gen.x.cfg['ts_indentation']
|
|
3
|
+
Gen.x.generator_info
|
|
4
|
+
%>
|
|
5
|
+
|
|
6
|
+
import {expect} from 'chai';
|
|
7
|
+
import type { AllParameters, Shared } from '../src/shared.d.ts';
|
|
8
|
+
import { shared } from '../src/shared.js';
|
|
9
|
+
import { <%= Gen.x.shared.functions.join(', ') %> } from '../src/shared.js';
|
|
10
|
+
import type {CallExtras, String2String, <%= Gen.x.schemas.import_types.join(', ') %>} from '../src/schemas.d.ts';
|
|
11
|
+
import {<%= Gen.x.schemas.functions.join(', ') %>} from '../src/schemas.js';
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
// Fluffy scripting languages allow this despite types, handy.
|
|
15
|
+
function orderlyGarbage(plain: boolean): AllParameters {
|
|
16
|
+
const out: AllParameters = {
|
|
17
|
+
<%=
|
|
18
|
+
out = []
|
|
19
|
+
Gen.doc[:all_params].each do |name, types|
|
|
20
|
+
ti = types.first
|
|
21
|
+
out.push("#{name}: '#{name}' as any as #{ti[:type]}") unless ti[:majority]
|
|
22
|
+
end
|
|
23
|
+
out.join(",\n")
|
|
24
|
+
%>
|
|
25
|
+
};
|
|
26
|
+
<%=
|
|
27
|
+
out = []
|
|
28
|
+
Gen.doc[:all_params].each do |name, types|
|
|
29
|
+
ti = types.first
|
|
30
|
+
next unless ti[:majority]
|
|
31
|
+
next unless name == ti[:name]
|
|
32
|
+
typed = ti[:shared]
|
|
33
|
+
out.push("if (plain) out.#{name} = '#{name}' as any as #{ti[:type]}; else out.#{typed} = '#{typed}' as any as #{ti[:type]};")
|
|
34
|
+
end
|
|
35
|
+
Gen.output.join(out)
|
|
36
|
+
%>
|
|
37
|
+
return out;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
describe('orderlyGarbage', function () {
|
|
41
|
+
it('majority, unique', function () {
|
|
42
|
+
const r = orderlyGarbage(true);
|
|
43
|
+
const names: Array<string> = Object.keys(r);
|
|
44
|
+
for (const name of names) {
|
|
45
|
+
expect(r).to.have.property(name, name);
|
|
46
|
+
for (const other of names) {
|
|
47
|
+
if (name == other) continue;
|
|
48
|
+
expect(r).to.have.property(other).not.to.equal(name);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
it('no majority, unique', function () {
|
|
53
|
+
const r = orderlyGarbage(false);
|
|
54
|
+
const names: Array<string> = Object.keys(r);
|
|
55
|
+
for (const name of names) {
|
|
56
|
+
expect(r).to.have.property(name, name);
|
|
57
|
+
for (const other of names) {
|
|
58
|
+
if (name == other) continue;
|
|
59
|
+
expect(r).to.have.property(other).not.to.equal(name);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
<%=
|
|
66
|
+
out = []
|
|
67
|
+
Gen.doc['paths'].each do |path, path_item_object|
|
|
68
|
+
oos = OpenAPISourceTools::ApiObjects.operation_objects(path_item_object)
|
|
69
|
+
oos.each do |method, operation_object|
|
|
70
|
+
next if operation_object[:parameterless]
|
|
71
|
+
shared_func = operation_object[:shared]
|
|
72
|
+
related = []
|
|
73
|
+
Gen.doc[:all_params].each_value do |types|
|
|
74
|
+
types.each do |ti|
|
|
75
|
+
related.push(ti) if ti[:ooid] == operation_object.object_id
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
out.push("describe('#{shared_func}', function () {")
|
|
79
|
+
typed = related.map do |ti|
|
|
80
|
+
"expect(r).to.have.property('#{ti[:name]}', '#{ti[:shared]}');"
|
|
81
|
+
end.uniq
|
|
82
|
+
plain = related.map do |ti|
|
|
83
|
+
"expect(r).to.have.property('#{ti[:name]}', '#{ti[ti[:majority] ? :name : :shared]}');"
|
|
84
|
+
end.uniq
|
|
85
|
+
s = <<EOB
|
|
86
|
+
it('no majority', function () {
|
|
87
|
+
shared.params = orderlyGarbage(false);
|
|
88
|
+
const r = #{shared_func}();
|
|
89
|
+
#{typed.join("\n")}
|
|
90
|
+
});
|
|
91
|
+
it('majority', function () {
|
|
92
|
+
shared.params = orderlyGarbage(true);
|
|
93
|
+
const r = #{shared_func}();
|
|
94
|
+
#{plain.join("\n")}
|
|
95
|
+
});
|
|
96
|
+
it('both', function () {
|
|
97
|
+
shared.params = { ...orderlyGarbage(false), ...orderlyGarbage(true) };
|
|
98
|
+
const r = #{shared_func}();
|
|
99
|
+
#{typed.join("\n")}
|
|
100
|
+
});
|
|
101
|
+
EOB
|
|
102
|
+
out.push(s)
|
|
103
|
+
out.push("});")
|
|
104
|
+
end
|
|
105
|
+
end
|
|
106
|
+
Gen.output.join(out)
|
|
107
|
+
%>
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
<%=
|
|
2
|
+
# Default tsconfig.json. You should provide your own.
|
|
3
|
+
defaults = {
|
|
4
|
+
"compilerOptions" => {
|
|
5
|
+
"outDir" => "./pkg",
|
|
6
|
+
"noImplicitAny" => true,
|
|
7
|
+
"forceConsistentCasingInFileNames" => true,
|
|
8
|
+
"erasableSyntaxOnly" => false,
|
|
9
|
+
"declaration" => true,
|
|
10
|
+
"rootDir" => '.',
|
|
11
|
+
'sourceMap' => true
|
|
12
|
+
},
|
|
13
|
+
"include" => [
|
|
14
|
+
"src", "test"
|
|
15
|
+
]
|
|
16
|
+
}
|
|
17
|
+
tgt = Gen.x.cfg.fetch('target', 'browser')
|
|
18
|
+
if tgt == 'node'
|
|
19
|
+
defaults.merge!({
|
|
20
|
+
"extends" => ["@tsconfig/node-lts/tsconfig.json", "@tsconfig/node-ts/tsconfig.json"]
|
|
21
|
+
})
|
|
22
|
+
defaults['compilerOptions'].merge!({
|
|
23
|
+
"module" => "nodenext",
|
|
24
|
+
"moduleResolution" => "nodenext",
|
|
25
|
+
"exactOptionalPropertyTypes" => true,
|
|
26
|
+
"types" => ["node", "mocha"]
|
|
27
|
+
})
|
|
28
|
+
elsif tgt == 'browser'
|
|
29
|
+
defaults.merge!({
|
|
30
|
+
"extends" => ["@tsconfig/recommended/tsconfig.json"],
|
|
31
|
+
})
|
|
32
|
+
defaults['compilerOptions'].merge!({
|
|
33
|
+
"module" => "es6",
|
|
34
|
+
"moduleResolution" => "bundler",
|
|
35
|
+
"exactOptionalPropertyTypes" => true,
|
|
36
|
+
"types" => ["web", "mocha"]
|
|
37
|
+
})
|
|
38
|
+
end
|
|
39
|
+
begin
|
|
40
|
+
cfg = Gen.x.cfg['tsconfig'] || {}
|
|
41
|
+
cfg = JSON.parse(cfg) unless cfg.is_a?(Hash)
|
|
42
|
+
rescue StandardError => e
|
|
43
|
+
raise StandardError, "#{OpenAPISourceToolsFetchTypescript::NAME}: Error parsing tsconfig.json: #{e.message}"
|
|
44
|
+
end
|
|
45
|
+
%w[compilerOptions].each do |key|
|
|
46
|
+
next unless cfg.key?(key) && defaults.key?(key)
|
|
47
|
+
cfg[key].merge!(defaults[key]) { |_key, cfg_value, _default_value| cfg_value }
|
|
48
|
+
end
|
|
49
|
+
cfg.merge!(defaults) { |_key, cfg_value, _default_value| cfg_value }
|
|
50
|
+
Gen.output.pretty_json(cfg)
|
|
51
|
+
%>
|
metadata
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: openapi_generate_typescript_fetch
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Ismo Kärkkäinen
|
|
8
|
+
bindir: bin
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: lucky_case
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - "~>"
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: '1.1'
|
|
19
|
+
- - ">="
|
|
20
|
+
- !ruby/object:Gem::Version
|
|
21
|
+
version: 1.1.0
|
|
22
|
+
type: :runtime
|
|
23
|
+
prerelease: false
|
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
25
|
+
requirements:
|
|
26
|
+
- - "~>"
|
|
27
|
+
- !ruby/object:Gem::Version
|
|
28
|
+
version: '1.1'
|
|
29
|
+
- - ">="
|
|
30
|
+
- !ruby/object:Gem::Version
|
|
31
|
+
version: 1.1.0
|
|
32
|
+
- !ruby/object:Gem::Dependency
|
|
33
|
+
name: openapi-arrangement
|
|
34
|
+
requirement: !ruby/object:Gem::Requirement
|
|
35
|
+
requirements:
|
|
36
|
+
- - "~>"
|
|
37
|
+
- !ruby/object:Gem::Version
|
|
38
|
+
version: 0.1.0
|
|
39
|
+
type: :runtime
|
|
40
|
+
prerelease: false
|
|
41
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
42
|
+
requirements:
|
|
43
|
+
- - "~>"
|
|
44
|
+
- !ruby/object:Gem::Version
|
|
45
|
+
version: 0.1.0
|
|
46
|
+
- !ruby/object:Gem::Dependency
|
|
47
|
+
name: openapi-sourcetools
|
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
|
49
|
+
requirements:
|
|
50
|
+
- - "~>"
|
|
51
|
+
- !ruby/object:Gem::Version
|
|
52
|
+
version: 0.10.0
|
|
53
|
+
type: :runtime
|
|
54
|
+
prerelease: false
|
|
55
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
56
|
+
requirements:
|
|
57
|
+
- - "~>"
|
|
58
|
+
- !ruby/object:Gem::Version
|
|
59
|
+
version: 0.10.0
|
|
60
|
+
description: |-
|
|
61
|
+
This is a processor gem for use with openapi-generate from gem
|
|
62
|
+
openapi-sourcetools.
|
|
63
|
+
|
|
64
|
+
Produces a Fetch API client in TypeScript wrapped into a NPM package.
|
|
65
|
+
email: ismokarkkainen@icloud.com
|
|
66
|
+
executables: []
|
|
67
|
+
extensions: []
|
|
68
|
+
extra_rdoc_files: []
|
|
69
|
+
files:
|
|
70
|
+
- LICENSE.txt
|
|
71
|
+
- lib/openapi_generate_typescript_fetch.rb
|
|
72
|
+
- lib/openapi_generate_typescript_fetch/schema.rb
|
|
73
|
+
- lib/openapi_generate_typescript_fetch/taskinfo.rb
|
|
74
|
+
- lib/openapi_generate_typescript_fetch/tasks.rb
|
|
75
|
+
- lib/openapi_generate_typescript_fetch/version.rb
|
|
76
|
+
- template/package.json.erb
|
|
77
|
+
- template/src/callclasses.ts.erb
|
|
78
|
+
- template/src/helpers.ts.erb
|
|
79
|
+
- template/src/index.ts.erb
|
|
80
|
+
- template/src/schemas.ts.erb
|
|
81
|
+
- template/src/servers.ts.erb
|
|
82
|
+
- template/src/shared.ts.erb
|
|
83
|
+
- template/test/callclasses.ts.erb
|
|
84
|
+
- template/test/helpers.ts.erb
|
|
85
|
+
- template/test/makers.ts.erb
|
|
86
|
+
- template/test/schemas.ts.erb
|
|
87
|
+
- template/test/servers.ts.erb
|
|
88
|
+
- template/test/shared.ts.erb
|
|
89
|
+
- template/tsconfig.json.erb
|
|
90
|
+
homepage: https://xn--ismo-krkkinen-gfbd.fi/openapi_generate_typescript_fetch/index.html
|
|
91
|
+
licenses:
|
|
92
|
+
- UPL-1.0
|
|
93
|
+
metadata:
|
|
94
|
+
rubygems_mfa_required: 'true'
|
|
95
|
+
rdoc_options: []
|
|
96
|
+
require_paths:
|
|
97
|
+
- lib
|
|
98
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
99
|
+
requirements:
|
|
100
|
+
- - ">="
|
|
101
|
+
- !ruby/object:Gem::Version
|
|
102
|
+
version: 3.2.0
|
|
103
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
104
|
+
requirements:
|
|
105
|
+
- - ">="
|
|
106
|
+
- !ruby/object:Gem::Version
|
|
107
|
+
version: '0'
|
|
108
|
+
requirements: []
|
|
109
|
+
rubygems_version: 3.6.9
|
|
110
|
+
specification_version: 4
|
|
111
|
+
summary: Generate Typescript Fetch API client package using OpenAPI format API specification.
|
|
112
|
+
test_files: []
|