skyward-api 0.3.0 → 0.4.1
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 +4 -4
- data/LICENSE +21 -0
- data/README.md +60 -56
- data/lib/skyward/api/assembly/boot.rb +239 -91
- data/lib/skyward/api/assembly/builder.rb +2 -2
- data/lib/skyward/api/assembly/dispatcher.rb +12 -12
- data/lib/skyward/api/assembly/static.rb +36 -36
- data/lib/skyward/api/cli/app_file.rb +2 -2
- data/lib/skyward/api/cli/check.rb +9 -9
- data/lib/skyward/api/cli/routes.rb +2 -2
- data/lib/skyward/api/cli/serve/reloader.rb +6 -6
- data/lib/skyward/api/cli/serve.rb +15 -15
- data/lib/skyward/api/cli/serve.ru +2 -2
- data/lib/skyward/api/commands.rb +10 -10
- data/lib/skyward/api/endpoints/declarations.rb +7 -3
- data/lib/skyward/api/endpoints/route.rb +3 -1
- data/lib/skyward/api/inputs/coercer.rb +53 -20
- data/lib/skyward/api/inputs.rb +9 -9
- data/lib/skyward/api/middleware/request_id.rb +3 -3
- data/lib/skyward/api/middleware/request_log.rb +5 -5
- data/lib/skyward/api/middleware/rescue.rb +7 -7
- data/lib/skyward/api/openapi/docs.rb +5 -5
- data/lib/skyward/api/openapi.rb +52 -50
- data/lib/skyward/api/request.rb +8 -8
- data/lib/skyward/api/routing/percent_encoding.rb +1 -1
- data/lib/skyward/api/routing/router.rb +68 -27
- data/lib/skyward/api/serializer/writers.rb +27 -16
- data/lib/skyward/api/serializer.rb +39 -21
- data/lib/skyward/api/test/client.rb +25 -25
- data/lib/skyward/api/version.rb +1 -1
- data/lib/skyward/api.rb +15 -14
- data/sig/generated/skyward/api/assembly/boot.rbs +99 -29
- data/sig/generated/skyward/api/endpoints/route.rbs +8 -4
- data/sig/generated/skyward/api/inputs/coercer.rbs +20 -2
- data/sig/generated/skyward/api/routing/router.rbs +52 -9
- data/sig/generated/skyward/api/serializer/writers.rbs +6 -0
- data/sig/generated/skyward/api/serializer.rbs +13 -1
- metadata +19 -4
- /data/sig/{manual/zeitwerk/loader.rbs → _private/zeitwerk.rbs} +0 -0
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require
|
|
3
|
+
require 'json'
|
|
4
4
|
# Time#iso8601 is core only from Ruby 3.4.
|
|
5
|
-
require
|
|
5
|
+
require 'time'
|
|
6
6
|
|
|
7
7
|
module Skyward::API::Serializer::Writers
|
|
8
8
|
extend self
|
|
@@ -32,13 +32,14 @@ module Skyward::API::Serializer::Writers
|
|
|
32
32
|
when Skyward::Core::Types::Record then record(type.fields)
|
|
33
33
|
when Skyward::Core::Types::Named then named(type)
|
|
34
34
|
when Skyward::Core::Types::Union then union(type.types.map { |member| writer(member) })
|
|
35
|
+
when Skyward::Core::Types::Refined then writer(type.type)
|
|
35
36
|
end
|
|
36
37
|
end
|
|
37
38
|
|
|
38
39
|
# @rbs members: Array[Writer]
|
|
39
40
|
# @rbs return: Writer
|
|
40
41
|
def union(members)
|
|
41
|
-
name = members.map(&:name).join(
|
|
42
|
+
name = members.map(&:name).join(' | ')
|
|
42
43
|
Writer.new(
|
|
43
44
|
name:,
|
|
44
45
|
fits: ->(value) { members.any? { |member| member.fits.call(value) } },
|
|
@@ -82,13 +83,15 @@ module Skyward::API::Serializer::Writers
|
|
|
82
83
|
# @rbs return: Writer
|
|
83
84
|
def scalar(name)
|
|
84
85
|
case name
|
|
85
|
-
when :integer then checked(
|
|
86
|
-
when :float then checked(
|
|
87
|
-
when :string then checked(
|
|
88
|
-
when :bool then checked(
|
|
89
|
-
when :date then checked(
|
|
90
|
-
when :time then checked(
|
|
91
|
-
when :
|
|
86
|
+
when :integer then checked('Integer', ->(value) { value.is_a?(Integer) }, ->(value, _) { value })
|
|
87
|
+
when :float then checked('Float', ->(value) { value.is_a?(Float) && value.finite? }, ->(value, _) { value })
|
|
88
|
+
when :string then checked('String', ->(value) { value.is_a?(String) }, ->(value, _) { value })
|
|
89
|
+
when :bool then checked('bool', ->(value) { [true, false].include?(value) }, ->(value, _) { value })
|
|
90
|
+
when :date then checked('Date', ->(value) { value.is_a?(Date) }, ->(value, _) { date(value) })
|
|
91
|
+
when :time then checked('Time', ->(value) { value.is_a?(Time) }, ->(value, _) { time(value) })
|
|
92
|
+
when :decimal
|
|
93
|
+
checked('BigDecimal', ->(value) { value.is_a?(BigDecimal) && value.finite? }, ->(value, _) { decimal(value) })
|
|
94
|
+
when :untyped then Writer.new(name: 'untyped', fits: ->(_) { true }, write: ->(value, at) { untyped(value, at) })
|
|
92
95
|
end
|
|
93
96
|
end
|
|
94
97
|
|
|
@@ -101,7 +104,7 @@ module Skyward::API::Serializer::Writers
|
|
|
101
104
|
# @rbs symbols: Array[Symbol]
|
|
102
105
|
# @rbs return: Writer
|
|
103
106
|
def enum(symbols)
|
|
104
|
-
checked(symbols.map(&:inspect).join(
|
|
107
|
+
checked(symbols.map(&:inspect).join(' | '), ->(value) { symbols.include?(value) }, ->(value, _) { value.name })
|
|
105
108
|
end
|
|
106
109
|
|
|
107
110
|
# @rbs type: Skyward::Core::Types::t
|
|
@@ -143,9 +146,9 @@ module Skyward::API::Serializer::Writers
|
|
|
143
146
|
# @rbs return: Writer
|
|
144
147
|
def record(fields)
|
|
145
148
|
writers = fields.map { |field| [field, writer(field.type)] } #: Array[[Skyward::Core::Types::Field, Writer]]
|
|
146
|
-
declared = writers.map { |field, writer| "#{
|
|
149
|
+
declared = writers.map { |field, writer| "#{'?' unless field.required}#{field.name}: #{writer.name}" }
|
|
147
150
|
checked(
|
|
148
|
-
"{ #{declared.join(
|
|
151
|
+
"{ #{declared.join(', ')} }",
|
|
149
152
|
->(value) { value.is_a?(Hash) },
|
|
150
153
|
->(value, at) { object(writers.filter_map { |field, writer| entry(field, writer, value, at) }) }
|
|
151
154
|
)
|
|
@@ -159,7 +162,7 @@ module Skyward::API::Serializer::Writers
|
|
|
159
162
|
def entry(field, writer, hash, at)
|
|
160
163
|
name = field.name
|
|
161
164
|
if hash.key?(name) then [name, writer.write.call(hash[name], "#{at}.#{name}")]
|
|
162
|
-
elsif field.required then [name, Misfit.new(at: "#{at}.#{name}", expected: writer.name, got:
|
|
165
|
+
elsif field.required then [name, Misfit.new(at: "#{at}.#{name}", expected: writer.name, got: 'nothing')]
|
|
163
166
|
end
|
|
164
167
|
end
|
|
165
168
|
|
|
@@ -181,13 +184,19 @@ module Skyward::API::Serializer::Writers
|
|
|
181
184
|
# strftime rather than iso8601, which writes a DateTime with its time.
|
|
182
185
|
# @rbs value: Date
|
|
183
186
|
# @rbs return: String
|
|
184
|
-
def date(value) = value.strftime(
|
|
187
|
+
def date(value) = value.strftime('%Y-%m-%d')
|
|
185
188
|
|
|
186
189
|
# Six fractional digits, so a time read back from JSON equals the one written.
|
|
187
190
|
# @rbs value: Time
|
|
188
191
|
# @rbs return: String
|
|
189
192
|
def time(value) = value.getutc.iso8601(6)
|
|
190
193
|
|
|
194
|
+
# A String, so a client that reads JSON numbers as doubles keeps every digit,
|
|
195
|
+
# and "F" so it never has an exponent.
|
|
196
|
+
# @rbs value: BigDecimal
|
|
197
|
+
# @rbs return: String
|
|
198
|
+
def decimal(value) = value.to_s('F')
|
|
199
|
+
|
|
191
200
|
# @rbs value: untyped
|
|
192
201
|
# @rbs at: String
|
|
193
202
|
# @rbs return: untyped
|
|
@@ -197,9 +206,10 @@ module Skyward::API::Serializer::Writers
|
|
|
197
206
|
when Symbol then value.name
|
|
198
207
|
when Date then date(value)
|
|
199
208
|
when Time then time(value)
|
|
209
|
+
when BigDecimal then value.finite? ? decimal(value) : misfit('BigDecimal', value, at)
|
|
200
210
|
when Hash then object(value.map { |key, item| [key, untyped(item, "#{at}.#{key}")] })
|
|
201
211
|
when Array then items(value.each_with_index.map { |item, index| untyped(item, "#{at}[#{index}]") })
|
|
202
|
-
else value.respond_to?(:to_h) ? untyped(value.to_h, at) : misfit(
|
|
212
|
+
else value.respond_to?(:to_h) ? untyped(value.to_h, at) : misfit('something with to_h', value, at)
|
|
203
213
|
end
|
|
204
214
|
end
|
|
205
215
|
|
|
@@ -223,6 +233,7 @@ module Skyward::API::Serializer::Writers
|
|
|
223
233
|
got =
|
|
224
234
|
case value
|
|
225
235
|
when nil, true, false, Symbol, Integer, Float then value.inspect
|
|
236
|
+
when BigDecimal then decimal(value)
|
|
226
237
|
else value.class.inspect
|
|
227
238
|
end
|
|
228
239
|
Misfit.new(at:, expected:, got:)
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require
|
|
3
|
+
require 'json'
|
|
4
4
|
|
|
5
5
|
class Skyward::API::Serializer
|
|
6
|
-
JSON_TYPE = {
|
|
6
|
+
JSON_TYPE = { 'content-type' => 'application/json' }.freeze #: Hash[String, String]
|
|
7
7
|
|
|
8
8
|
Problem = Data.define(
|
|
9
9
|
:status, #: Integer
|
|
@@ -11,12 +11,12 @@ class Skyward::API::Serializer
|
|
|
11
11
|
)
|
|
12
12
|
|
|
13
13
|
PROBLEMS = {
|
|
14
|
-
Skyward::API::Responses::BadRequest => Problem.new(status: 400, title:
|
|
15
|
-
Skyward::API::Responses::Unauthorized => Problem.new(status: 401, title:
|
|
16
|
-
Skyward::API::Responses::Forbidden => Problem.new(status: 403, title:
|
|
17
|
-
Skyward::API::Responses::NotFound => Problem.new(status: 404, title:
|
|
18
|
-
Skyward::API::Responses::Conflict => Problem.new(status: 409, title:
|
|
19
|
-
Skyward::API::Responses::Unprocessable => Problem.new(status: 422, title:
|
|
14
|
+
Skyward::API::Responses::BadRequest => Problem.new(status: 400, title: 'Bad Request'),
|
|
15
|
+
Skyward::API::Responses::Unauthorized => Problem.new(status: 401, title: 'Unauthorized'),
|
|
16
|
+
Skyward::API::Responses::Forbidden => Problem.new(status: 403, title: 'Forbidden'),
|
|
17
|
+
Skyward::API::Responses::NotFound => Problem.new(status: 404, title: 'Not Found'),
|
|
18
|
+
Skyward::API::Responses::Conflict => Problem.new(status: 409, title: 'Conflict'),
|
|
19
|
+
Skyward::API::Responses::Unprocessable => Problem.new(status: 422, title: 'Unprocessable Content')
|
|
20
20
|
}.freeze #: Hash[Class, Problem]
|
|
21
21
|
|
|
22
22
|
# Every response value but Skyward::API::Responses::Created, which carries a type of its own.
|
|
@@ -44,7 +44,7 @@ class Skyward::API::Serializer
|
|
|
44
44
|
# @rbs value: untyped
|
|
45
45
|
# @rbs return: Skyward::API::rack
|
|
46
46
|
def call(value)
|
|
47
|
-
response = @writer.write.call(value,
|
|
47
|
+
response = @writer.write.call(value, '')
|
|
48
48
|
case response
|
|
49
49
|
when Writers::Misfit then raise Skyward::API::ReturnTypeError, message(response)
|
|
50
50
|
else response
|
|
@@ -65,12 +65,12 @@ class Skyward::API::Serializer
|
|
|
65
65
|
end
|
|
66
66
|
end
|
|
67
67
|
|
|
68
|
-
# Untyped code can return any response value.
|
|
68
|
+
# Untyped code can return any response value, or a Rack triple.
|
|
69
69
|
# @rbs return: Writers::Writer
|
|
70
70
|
def untyped
|
|
71
71
|
writer = Writers.writer(UNTYPED)
|
|
72
|
-
responses = [created(writer), *RESPONSES.map { |klass| response(klass) }, ok(writer)]
|
|
73
|
-
Writers.union(responses).with(name:
|
|
72
|
+
responses = [created(writer), *RESPONSES.map { |klass| response(klass) }, untyped_rack, ok(writer)]
|
|
73
|
+
Writers.union(responses).with(name: 'untyped')
|
|
74
74
|
end
|
|
75
75
|
|
|
76
76
|
# @rbs writer: Writers::Writer
|
|
@@ -97,7 +97,7 @@ class Skyward::API::Serializer
|
|
|
97
97
|
problem = PROBLEMS.fetch(klass, nil)
|
|
98
98
|
answer =
|
|
99
99
|
if problem then ->(given, _) { problem_response(problem, given) }
|
|
100
|
-
elsif klass == Skyward::API::Responses::Redirect then ->(given, _) { [given.status, {
|
|
100
|
+
elsif klass == Skyward::API::Responses::Redirect then ->(given, _) { [given.status, { 'location' => given.to, **given.headers }, []] }
|
|
101
101
|
else ->(given, _) { [204, given.headers.dup, []] }
|
|
102
102
|
end #: ^(untyped, String) -> Skyward::API::Assembly::Dispatcher::response
|
|
103
103
|
Writers.checked(klass.name.to_s, ->(given) { given.is_a?(klass) }, answer)
|
|
@@ -105,19 +105,37 @@ class Skyward::API::Serializer
|
|
|
105
105
|
|
|
106
106
|
# @rbs return: Writers::Writer
|
|
107
107
|
def rack
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
108
|
+
rack_writer(->(given) { given.is_a?(Array) && given.size == 3 && given[0].is_a?(Integer) && given[1].is_a?(Hash) })
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
# Nothing typed says an untyped value is a triple, so it must have a status HTTP has and a body Rack can send.
|
|
112
|
+
# @rbs return: Writers::Writer
|
|
113
|
+
def untyped_rack
|
|
114
|
+
rack_writer(->(given) { triple?(given) })
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
# @rbs fits: ^(untyped) -> bool
|
|
118
|
+
# @rbs return: Writers::Writer
|
|
119
|
+
def rack_writer(fits)
|
|
120
|
+
Writers.checked('Skyward::API::rack', fits, ->(given, _) { given })
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
# @rbs given: untyped
|
|
124
|
+
# @rbs return: bool
|
|
125
|
+
def triple?(given)
|
|
126
|
+
return false unless given.is_a?(Array) && given.size == 3
|
|
127
|
+
|
|
128
|
+
status, headers, body = given
|
|
129
|
+
status.is_a?(Integer) && status.between?(100, 599) && headers.is_a?(Hash) &&
|
|
130
|
+
(body.respond_to?(:each) || body.respond_to?(:call))
|
|
113
131
|
end
|
|
114
132
|
|
|
115
133
|
# @rbs problem: Problem
|
|
116
134
|
# @rbs given: untyped
|
|
117
135
|
# @rbs return: Skyward::API::Assembly::Dispatcher::response
|
|
118
136
|
def problem_response(problem, given)
|
|
119
|
-
body = { type:
|
|
120
|
-
[problem.status, {
|
|
137
|
+
body = { type: 'about:blank', title: problem.title, status: problem.status, detail: given.detail }.compact
|
|
138
|
+
[problem.status, { 'content-type' => 'application/problem+json', **given.headers }, [JSON.generate(body)]]
|
|
121
139
|
end
|
|
122
140
|
|
|
123
141
|
# @rbs written: untyped
|
|
@@ -133,7 +151,7 @@ class Skyward::API::Serializer
|
|
|
133
151
|
def message(misfit)
|
|
134
152
|
return "#{@label} returned #{misfit.got}, which isn't #{misfit.expected}" if misfit.at.empty?
|
|
135
153
|
|
|
136
|
-
"#{@label} returned #{misfit.got} at #{misfit.at.delete_prefix(
|
|
154
|
+
"#{@label} returned #{misfit.got} at #{misfit.at.delete_prefix('.')}, which isn't #{misfit.expected}, " \
|
|
137
155
|
"in its return type #{@writer.name}"
|
|
138
156
|
end
|
|
139
157
|
end
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require
|
|
4
|
-
require
|
|
3
|
+
require 'json'
|
|
4
|
+
require 'stringio'
|
|
5
5
|
|
|
6
6
|
class Skyward::API::Test::Client
|
|
7
7
|
Response = Data.define(
|
|
@@ -30,7 +30,7 @@ class Skyward::API::Test::Client
|
|
|
30
30
|
# @rbs cookies: Hash[String, String]
|
|
31
31
|
# @rbs return: Response
|
|
32
32
|
def get(path, json: nil, headers: {}, cookies: {})
|
|
33
|
-
request(
|
|
33
|
+
request('GET', path, json, headers, cookies)
|
|
34
34
|
end
|
|
35
35
|
|
|
36
36
|
# @rbs path: String
|
|
@@ -39,7 +39,7 @@ class Skyward::API::Test::Client
|
|
|
39
39
|
# @rbs cookies: Hash[String, String]
|
|
40
40
|
# @rbs return: Response
|
|
41
41
|
def post(path, json: nil, headers: {}, cookies: {})
|
|
42
|
-
request(
|
|
42
|
+
request('POST', path, json, headers, cookies)
|
|
43
43
|
end
|
|
44
44
|
|
|
45
45
|
# @rbs path: String
|
|
@@ -48,7 +48,7 @@ class Skyward::API::Test::Client
|
|
|
48
48
|
# @rbs cookies: Hash[String, String]
|
|
49
49
|
# @rbs return: Response
|
|
50
50
|
def put(path, json: nil, headers: {}, cookies: {})
|
|
51
|
-
request(
|
|
51
|
+
request('PUT', path, json, headers, cookies)
|
|
52
52
|
end
|
|
53
53
|
|
|
54
54
|
# @rbs path: String
|
|
@@ -57,7 +57,7 @@ class Skyward::API::Test::Client
|
|
|
57
57
|
# @rbs cookies: Hash[String, String]
|
|
58
58
|
# @rbs return: Response
|
|
59
59
|
def patch(path, json: nil, headers: {}, cookies: {})
|
|
60
|
-
request(
|
|
60
|
+
request('PATCH', path, json, headers, cookies)
|
|
61
61
|
end
|
|
62
62
|
|
|
63
63
|
# @rbs path: String
|
|
@@ -66,7 +66,7 @@ class Skyward::API::Test::Client
|
|
|
66
66
|
# @rbs cookies: Hash[String, String]
|
|
67
67
|
# @rbs return: Response
|
|
68
68
|
def delete(path, json: nil, headers: {}, cookies: {})
|
|
69
|
-
request(
|
|
69
|
+
request('DELETE', path, json, headers, cookies)
|
|
70
70
|
end
|
|
71
71
|
|
|
72
72
|
# @rbs path: String
|
|
@@ -75,7 +75,7 @@ class Skyward::API::Test::Client
|
|
|
75
75
|
# @rbs cookies: Hash[String, String]
|
|
76
76
|
# @rbs return: Response
|
|
77
77
|
def head(path, json: nil, headers: {}, cookies: {})
|
|
78
|
-
request(
|
|
78
|
+
request('HEAD', path, json, headers, cookies)
|
|
79
79
|
end
|
|
80
80
|
|
|
81
81
|
# @rbs path: String
|
|
@@ -84,7 +84,7 @@ class Skyward::API::Test::Client
|
|
|
84
84
|
# @rbs cookies: Hash[String, String]
|
|
85
85
|
# @rbs return: Response
|
|
86
86
|
def options(path, json: nil, headers: {}, cookies: {})
|
|
87
|
-
request(
|
|
87
|
+
request('OPTIONS', path, json, headers, cookies)
|
|
88
88
|
end
|
|
89
89
|
|
|
90
90
|
private
|
|
@@ -96,23 +96,23 @@ class Skyward::API::Test::Client
|
|
|
96
96
|
# @rbs cookies: Hash[String, String]
|
|
97
97
|
# @rbs return: Response
|
|
98
98
|
def request(method, path, json, headers, cookies)
|
|
99
|
-
body = json.nil? ?
|
|
99
|
+
body = json.nil? ? '' : JSON.generate(json)
|
|
100
100
|
sent = body_headers(body).merge(cookie_header(cookies), headers)
|
|
101
101
|
status, response_headers, response_body = @app.call(env(method, path, body, sent))
|
|
102
102
|
text = read(response_body)
|
|
103
|
-
Response.new(status:, headers: response_headers, body: text, json: parse(response_headers[
|
|
103
|
+
Response.new(status:, headers: response_headers, body: text, json: parse(response_headers['content-type'], text))
|
|
104
104
|
end
|
|
105
105
|
|
|
106
106
|
# @rbs body: String
|
|
107
107
|
# @rbs return: Hash[String, String]
|
|
108
108
|
def body_headers(body)
|
|
109
|
-
body.empty? ? {} : {
|
|
109
|
+
body.empty? ? {} : { 'content-type' => 'application/json', 'content-length' => body.bytesize.to_s }
|
|
110
110
|
end
|
|
111
111
|
|
|
112
112
|
# @rbs cookies: Hash[String, String]
|
|
113
113
|
# @rbs return: Hash[String, String]
|
|
114
114
|
def cookie_header(cookies)
|
|
115
|
-
cookies.empty? ? {} : {
|
|
115
|
+
cookies.empty? ? {} : { 'cookie' => cookies.map { |name, value| "#{name}=#{value}" }.join('; ') }
|
|
116
116
|
end
|
|
117
117
|
|
|
118
118
|
# @rbs method: String
|
|
@@ -121,25 +121,25 @@ class Skyward::API::Test::Client
|
|
|
121
121
|
# @rbs headers: Hash[String, String]
|
|
122
122
|
# @rbs return: Hash[String, untyped]
|
|
123
123
|
def env(method, path, body, headers)
|
|
124
|
-
path_info, _, query = path.partition(
|
|
124
|
+
path_info, _, query = path.partition('?')
|
|
125
125
|
{
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
126
|
+
'REQUEST_METHOD' => method,
|
|
127
|
+
'SCRIPT_NAME' => '',
|
|
128
|
+
'PATH_INFO' => path_info,
|
|
129
|
+
'QUERY_STRING' => query,
|
|
130
|
+
'SERVER_NAME' => 'localhost',
|
|
131
|
+
'SERVER_PORT' => '80',
|
|
132
|
+
'SERVER_PROTOCOL' => 'HTTP/1.1',
|
|
133
|
+
'rack.url_scheme' => 'http',
|
|
134
|
+
'rack.input' => StringIO.new(body.b),
|
|
135
|
+
'rack.errors' => StringIO.new
|
|
136
136
|
}.merge(headers.to_h { |name, value| [env_key(name), value] })
|
|
137
137
|
end
|
|
138
138
|
|
|
139
139
|
# @rbs name: String
|
|
140
140
|
# @rbs return: String
|
|
141
141
|
def env_key(name)
|
|
142
|
-
key = name.upcase.tr(
|
|
142
|
+
key = name.upcase.tr('-', '_')
|
|
143
143
|
UNPREFIXED.include?(key) ? key : "HTTP_#{key}"
|
|
144
144
|
end
|
|
145
145
|
|
data/lib/skyward/api/version.rb
CHANGED
data/lib/skyward/api.rb
CHANGED
|
@@ -1,19 +1,20 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require
|
|
4
|
-
require
|
|
5
|
-
require
|
|
6
|
-
|
|
3
|
+
require 'bigdecimal'
|
|
4
|
+
require 'securerandom'
|
|
5
|
+
require 'skyward/core'
|
|
6
|
+
require 'zeitwerk'
|
|
7
|
+
require_relative 'api/version'
|
|
7
8
|
|
|
8
9
|
loader = Zeitwerk::Loader.for_gem_extension(Skyward)
|
|
9
|
-
loader.inflector.inflect(
|
|
10
|
+
loader.inflector.inflect('api' => 'API', 'cli' => 'CLI', 'openapi' => 'OpenAPI')
|
|
10
11
|
# The skyward executable loads it, and it defines no constant.
|
|
11
12
|
loader.ignore("#{__dir__}/api/commands.rb")
|
|
12
13
|
loader.setup
|
|
13
14
|
|
|
14
15
|
# An app's signatures subclass Skyward::API, so RBS needs its declaration.
|
|
15
16
|
dir = __dir__ #: String
|
|
16
|
-
Skyward::Core::Signatures.register(File.expand_path(
|
|
17
|
+
Skyward::Core::Signatures.register(File.expand_path('../../sig', dir))
|
|
17
18
|
|
|
18
19
|
class Skyward::API
|
|
19
20
|
# @rbs! type rack_response = [Integer, Hash[String, untyped], untyped]
|
|
@@ -28,11 +29,11 @@ module Skyward
|
|
|
28
29
|
# ?schema_names: Hash[Class, String], ?scalar_url: String, ?logger: API::_Logger?, ?on_error: API::_OnError?,
|
|
29
30
|
# ?max_body_size: Integer) { (API::Assembly::Builder) -> void } -> API::Assembly::Application
|
|
30
31
|
def self.app(
|
|
31
|
-
signatures:
|
|
32
|
+
signatures: 'sig',
|
|
32
33
|
strict: false,
|
|
33
34
|
docs: false,
|
|
34
|
-
title:
|
|
35
|
-
version:
|
|
35
|
+
title: 'API',
|
|
36
|
+
version: '0.1.0',
|
|
36
37
|
description: nil,
|
|
37
38
|
schema_names: {},
|
|
38
39
|
scalar_url: API::OpenAPI::Docs::SCALAR_URL,
|
|
@@ -46,13 +47,13 @@ module Skyward
|
|
|
46
47
|
builder = API::Assembly::Builder.new
|
|
47
48
|
yield builder
|
|
48
49
|
builder.freeze
|
|
49
|
-
reader = Core::Types::Reader.new(Core::Signatures.load(signatures))
|
|
50
50
|
endpoints = API::Assembly::Boot.endpoints(
|
|
51
51
|
builder.mounts,
|
|
52
52
|
providers: builder.providers,
|
|
53
|
-
|
|
53
|
+
# Not a local, which every lambda built here would close over, keeping the signatures loaded.
|
|
54
|
+
reader: Core::Types::Reader.new(Core::Signatures.load(signatures)),
|
|
54
55
|
max_body_size:,
|
|
55
|
-
strict: strict || ENV.fetch(
|
|
56
|
+
strict: strict || ENV.fetch('SKYWARD_STRICT', nil) == '1'
|
|
56
57
|
)
|
|
57
58
|
static = API::Assembly::Static.new(builder.statics)
|
|
58
59
|
openapi = -> { API::OpenAPI.document(endpoints, title:, version:, description:, schema_names:) }
|
|
@@ -72,7 +73,7 @@ module Skyward
|
|
|
72
73
|
API::Assembly::Dispatcher.new(API::Routing::Router.new(endpoints), static, builder.middleware, documents),
|
|
73
74
|
logger:,
|
|
74
75
|
on_error:,
|
|
75
|
-
development: ENV.fetch(
|
|
76
|
+
development: ENV.fetch('RACK_ENV') { ENV.fetch('APP_ENV', nil) } == 'development'
|
|
76
77
|
)
|
|
77
78
|
logged =
|
|
78
79
|
if logger
|
|
@@ -115,7 +116,7 @@ module Skyward
|
|
|
115
116
|
def self.redirect(to, status: 303, headers: {})
|
|
116
117
|
statuses = API::Responses::Redirect::STATUSES
|
|
117
118
|
unless statuses.include?(status)
|
|
118
|
-
raise ArgumentError, "A redirect's status must be one of #{statuses.join(
|
|
119
|
+
raise ArgumentError, "A redirect's status must be one of #{statuses.join(', ')}, not #{status}"
|
|
119
120
|
end
|
|
120
121
|
|
|
121
122
|
# A line break in the location would let it smuggle in headers of its own.
|