skyward-api 0.2.0 → 0.4.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 +4 -4
- data/README.md +49 -45
- data/lib/skyward/api/assembly/boot.rb +214 -66
- data/lib/skyward/api/cli/app_file.rb +9 -5
- data/lib/skyward/api/cli/check.rb +12 -29
- data/lib/skyward/api/cli/serve/reloader.rb +44 -3
- data/lib/skyward/api/cli/serve.rb +9 -2
- data/lib/skyward/api/cli/serve.ru +16 -1
- data/lib/skyward/api/commands.rb +18 -11
- data/lib/skyward/api/endpoints/declarations.rb +5 -1
- data/lib/skyward/api/endpoints/route.rb +3 -1
- data/lib/skyward/api/inputs/coercer.rb +37 -4
- data/lib/skyward/api/openapi.rb +2 -0
- data/lib/skyward/api/serializer/writers.rb +12 -1
- data/lib/skyward/api/serializer.rb +25 -7
- data/lib/skyward/api/version.rb +1 -1
- data/lib/skyward/api.rb +1 -0
- data/sig/generated/skyward/api/assembly/boot.rbs +99 -29
- data/sig/generated/skyward/api/cli/app_file.rbs +5 -4
- data/sig/generated/skyward/api/cli/check.rbs +2 -12
- data/sig/generated/skyward/api/cli/serve/reloader.rbs +28 -7
- data/sig/generated/skyward/api/cli/serve.rbs +4 -1
- 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/serializer/writers.rbs +6 -0
- data/sig/generated/skyward/api/serializer.rbs +13 -1
- metadata +17 -6
- data/lib/skyward/api/cli/executable.rb +0 -15
- data/sig/generated/skyward/api/cli/executable.rbs +0 -11
- data/sig/manual/skyward/api/cli/executable.rbs +0 -3
|
@@ -23,6 +23,12 @@ module Skyward::API::Assembly::Boot
|
|
|
23
23
|
# untyped, which is what rbs-inline writes for a method with no annotations.
|
|
24
24
|
ANY = RBS::Types::Bases::Any.new(location: nil) #: RBS::Types::Bases::Any
|
|
25
25
|
|
|
26
|
+
Refusal = Data.define(
|
|
27
|
+
:problem, #: String
|
|
28
|
+
:spots, #: Array[Skyward::Core::Diagnostics::Spot]
|
|
29
|
+
:hint #: String?
|
|
30
|
+
)
|
|
31
|
+
|
|
26
32
|
# @rbs (Array[Skyward::API::Assembly::Builder::Mount] mounts,
|
|
27
33
|
# providers: Hash[Symbol, Skyward::API::_Provider & Object], reader: Skyward::Core::Types::Reader,
|
|
28
34
|
# max_body_size: Integer, strict: bool) ->
|
|
@@ -32,12 +38,13 @@ module Skyward::API::Assembly::Boot
|
|
|
32
38
|
raise Skyward::Core::BootError, single if single
|
|
33
39
|
|
|
34
40
|
provided = providers.to_h { |name, callable| [name, provider(name, callable, reader)] }
|
|
35
|
-
none = [] #: Array[Skyward::API::Routing::Router::Endpoint]
|
|
36
|
-
|
|
41
|
+
none = [] #: Array[[Skyward::API::Endpoints::Route, Skyward::API::Routing::Router::Endpoint]]
|
|
42
|
+
routed = mounts.reduce(none) do |booted, mount|
|
|
37
43
|
mount.api.class.routes.reduce(booted) do |so_far, route|
|
|
38
|
-
[*so_far, boot(mount, route, so_far, provided, reader, max_body_size)]
|
|
44
|
+
[*so_far, [route, boot(mount, route, so_far, provided, reader, max_body_size)]]
|
|
39
45
|
end
|
|
40
46
|
end
|
|
47
|
+
built = routed.map(&:last)
|
|
41
48
|
return built unless strict
|
|
42
49
|
|
|
43
50
|
refused = built.filter_map { |endpoint| untyped(endpoint, reader) }
|
|
@@ -82,7 +89,7 @@ module Skyward::API::Assembly::Boot
|
|
|
82
89
|
|
|
83
90
|
# @rbs mount: Skyward::API::Assembly::Builder::Mount
|
|
84
91
|
# @rbs route: Skyward::API::Endpoints::Route
|
|
85
|
-
# @rbs built: Array[Skyward::API::Routing::Router::Endpoint]
|
|
92
|
+
# @rbs built: Array[[Skyward::API::Endpoints::Route, Skyward::API::Routing::Router::Endpoint]]
|
|
86
93
|
# @rbs providers: Hash[Symbol, Skyward::API::Routing::Router::Provider]
|
|
87
94
|
# @rbs reader: Skyward::Core::Types::Reader
|
|
88
95
|
# @rbs max_body_size: Integer
|
|
@@ -92,20 +99,61 @@ module Skyward::API::Assembly::Boot
|
|
|
92
99
|
verb = route.verb.upcase.name
|
|
93
100
|
pattern = pattern(path)
|
|
94
101
|
label = "#{mount.api.class}##{route.to} (#{verb} #{path})"
|
|
95
|
-
result = summary(route) || shadowing(verb, pattern, built) ||
|
|
102
|
+
result = summary(route) || shadowing(route, verb, pattern, built) ||
|
|
96
103
|
endpoint(mount.api, route, verb, pattern, label, providers, reader, max_body_size)
|
|
97
104
|
case result
|
|
98
|
-
when
|
|
105
|
+
when Refusal then raise Skyward::Core::BootError, explained(label, result)
|
|
99
106
|
else result
|
|
100
107
|
end
|
|
101
108
|
end
|
|
102
109
|
|
|
110
|
+
# Core's own errors carry their excerpt in their message, already indented,
|
|
111
|
+
# so every line but an excerpt's is indented under the label.
|
|
112
|
+
# @rbs label: String
|
|
113
|
+
# @rbs refusal: Refusal
|
|
114
|
+
# @rbs return: String
|
|
115
|
+
def explained(label, refusal)
|
|
116
|
+
excerpt = Skyward::Core::Diagnostics::Excerpt.render(refusal.spots)
|
|
117
|
+
body = [[refusal.problem, excerpt].compact.join("\n\n"), refusal.hint].compact.join("\n")
|
|
118
|
+
lines = body.lines(chomp: true).map do |line|
|
|
119
|
+
line.empty? || line.start_with?(Skyward::Core::Diagnostics::Excerpt::INDENT) ? line : " #{line}"
|
|
120
|
+
end
|
|
121
|
+
[label, *lines].join("\n")
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
# @rbs problem: String
|
|
125
|
+
# @rbs spots: Array[Skyward::Core::Diagnostics::Spot?]
|
|
126
|
+
# @rbs hint: String?
|
|
127
|
+
# @rbs return: Refusal
|
|
128
|
+
def refused(problem, spots: [], hint: nil) = Refusal.new(problem:, spots: spots.compact, hint:)
|
|
129
|
+
|
|
103
130
|
# @rbs route: Skyward::API::Endpoints::Route
|
|
104
|
-
# @rbs
|
|
131
|
+
# @rbs key: Symbol
|
|
132
|
+
# @rbs return: Skyward::Core::Diagnostics::Spot?
|
|
133
|
+
def argument(route, key)
|
|
134
|
+
file = route.file
|
|
135
|
+
line = route.line
|
|
136
|
+
Skyward::Core::Diagnostics::Spots.keyword_value(file, line, call: route.verb, key:) if file && line
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
# @rbs route: Skyward::API::Endpoints::Route
|
|
140
|
+
# @rbs part: String
|
|
141
|
+
# @rbs return: Skyward::Core::Diagnostics::Spot?
|
|
142
|
+
def in_path(route, part)
|
|
143
|
+
file = route.file
|
|
144
|
+
line = route.line
|
|
145
|
+
Skyward::Core::Diagnostics::Spots.string(file, line, call: route.verb, part:) if file && line
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
# @rbs route: Skyward::API::Endpoints::Route
|
|
149
|
+
# @rbs return: Refusal?
|
|
105
150
|
def summary(route)
|
|
106
151
|
return unless route.summary&.match?(/\R/)
|
|
107
152
|
|
|
108
|
-
|
|
153
|
+
refused(
|
|
154
|
+
"Its summary: has a line break, but a summary is one line. Put the rest in description:.",
|
|
155
|
+
spots: [argument(route, :summary)]
|
|
156
|
+
)
|
|
109
157
|
end
|
|
110
158
|
|
|
111
159
|
# @rbs prefix: String
|
|
@@ -122,21 +170,25 @@ module Skyward::API::Assembly::Boot
|
|
|
122
170
|
path.split("/", -1).map { |segment| segment.start_with?(":") ? segment.delete_prefix(":").to_sym : segment }
|
|
123
171
|
end
|
|
124
172
|
|
|
173
|
+
# @rbs route: Skyward::API::Endpoints::Route
|
|
125
174
|
# @rbs verb: String
|
|
126
175
|
# @rbs pattern: Array[String | Symbol]
|
|
127
|
-
# @rbs built: Array[Skyward::API::Routing::Router::Endpoint]
|
|
128
|
-
# @rbs return:
|
|
129
|
-
def shadowing(verb, pattern, built)
|
|
130
|
-
|
|
131
|
-
return unless
|
|
176
|
+
# @rbs built: Array[[Skyward::API::Endpoints::Route, Skyward::API::Routing::Router::Endpoint]]
|
|
177
|
+
# @rbs return: Refusal?
|
|
178
|
+
def shadowing(route, verb, pattern, built)
|
|
179
|
+
found = built.find { |_, endpoint| endpoint.verb == verb && covers?(endpoint.pattern, pattern) }
|
|
180
|
+
return unless found
|
|
132
181
|
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
182
|
+
earlier_route, earlier = found
|
|
183
|
+
problem =
|
|
184
|
+
if covers?(pattern, earlier.pattern)
|
|
185
|
+
"#{earlier.label} is tried first and matches exactly the same paths, so this route is never reached. " \
|
|
186
|
+
"Remove one of them, or change a path."
|
|
187
|
+
else
|
|
188
|
+
"#{earlier.label} is tried first and matches every path this route does, so this route is never reached. " \
|
|
189
|
+
"Declare the static route first."
|
|
190
|
+
end
|
|
191
|
+
refused(problem, spots: [earlier_route, route].map { |declared| in_path(declared, declared.path) })
|
|
140
192
|
end
|
|
141
193
|
|
|
142
194
|
# A path parameter matches any segment but an empty one, which only the root path has.
|
|
@@ -156,36 +208,46 @@ module Skyward::API::Assembly::Boot
|
|
|
156
208
|
# @rbs providers: Hash[Symbol, Skyward::API::Routing::Router::Provider]
|
|
157
209
|
# @rbs reader: Skyward::Core::Types::Reader
|
|
158
210
|
# @rbs max_body_size: Integer
|
|
159
|
-
# @rbs return: Skyward::API::Routing::Router::Endpoint |
|
|
211
|
+
# @rbs return: Skyward::API::Routing::Router::Endpoint | Refusal
|
|
160
212
|
def endpoint(api, route, verb, pattern, label, providers, reader, max_body_size)
|
|
161
|
-
method_type = method_type(api.class, route
|
|
162
|
-
return method_type if method_type.is_a?(
|
|
213
|
+
method_type = method_type(api.class, route, reader)
|
|
214
|
+
return method_type if method_type.is_a?(Refusal)
|
|
163
215
|
|
|
216
|
+
method = api.class.instance_method(route.to)
|
|
164
217
|
function = method_type&.type
|
|
165
|
-
signed = signed(
|
|
166
|
-
return signed if signed.is_a?(
|
|
218
|
+
signed = signed(method, function)
|
|
219
|
+
return signed if signed.is_a?(Refusal)
|
|
167
220
|
|
|
168
|
-
|
|
221
|
+
path_parameters = pattern.grep(Symbol) #: Array[Symbol]
|
|
222
|
+
unnamed = path_parameters.find { |name| signed.none? { |keyword, _, _| keyword == name } }
|
|
169
223
|
if unnamed
|
|
170
|
-
|
|
171
|
-
|
|
224
|
+
query = signed.filter_map { |name, _, _| name if source(name, pattern) == :query && !providers.key?(name) }
|
|
225
|
+
return no_keyword(route, method, unnamed, query)
|
|
172
226
|
end
|
|
173
227
|
|
|
174
|
-
both =
|
|
228
|
+
both = path_parameters.find { |name| providers.key?(name) }
|
|
175
229
|
if both
|
|
176
|
-
|
|
177
|
-
|
|
230
|
+
problem = "The route has a path parameter :#{both}, but :#{both} is provided too, so `#{both}:` can't be both. " \
|
|
231
|
+
"Rename the path parameter, or provide another name."
|
|
232
|
+
return refused(
|
|
233
|
+
problem,
|
|
234
|
+
spots: [in_path(route, ":#{both}"), Skyward::Core::Diagnostics::Spots.keyword_parameter(method, both)]
|
|
235
|
+
)
|
|
178
236
|
end
|
|
179
237
|
|
|
180
|
-
read = signed.
|
|
181
|
-
problem = read.find { |keyword| keyword.is_a?(
|
|
182
|
-
|
|
238
|
+
read = signed.to_h { |name, required, type| [name, keyword(name, required, type, pattern, providers, reader)] }
|
|
239
|
+
name, problem = read.find { |_, keyword| keyword.is_a?(Refusal) }
|
|
240
|
+
if name && problem.is_a?(Refusal)
|
|
241
|
+
return problem.with(spots: [Skyward::Core::Diagnostics::Spots.keyword_parameter(method, name)].compact)
|
|
242
|
+
end
|
|
183
243
|
|
|
184
|
-
keywords = read.grep(Skyward::API::Routing::Router::Keyword) #: Array[Skyward::API::Routing::Router::Keyword]
|
|
185
|
-
provided = read.grep(Skyward::API::Routing::Router::Provider) #: Array[Skyward::API::Routing::Router::Provider]
|
|
244
|
+
keywords = read.values.grep(Skyward::API::Routing::Router::Keyword) #: Array[Skyward::API::Routing::Router::Keyword]
|
|
245
|
+
provided = read.values.grep(Skyward::API::Routing::Router::Provider) #: Array[Skyward::API::Routing::Router::Provider]
|
|
186
246
|
|
|
187
247
|
returns = returns(function&.return_type || ANY, reader)
|
|
188
|
-
|
|
248
|
+
if returns.is_a?(Refusal)
|
|
249
|
+
return returns.with(spots: [Skyward::Core::Diagnostics::Spots.method_name(method)].compact)
|
|
250
|
+
end
|
|
189
251
|
|
|
190
252
|
inputs = Skyward::API::Inputs.new(keywords, providers: provided, max_body_size:)
|
|
191
253
|
serializer = Skyward::API::Serializer.new(returns, label:)
|
|
@@ -207,19 +269,38 @@ module Skyward::API::Assembly::Boot
|
|
|
207
269
|
# The reader raises for a type it can't read at all, such as one that refers
|
|
208
270
|
# to itself, and that's this route's problem too.
|
|
209
271
|
rescue Skyward::Core::BootError => e
|
|
210
|
-
e.message
|
|
272
|
+
refused(e.message)
|
|
211
273
|
end
|
|
212
274
|
|
|
213
275
|
# @rbs klass: Class
|
|
214
|
-
# @rbs
|
|
276
|
+
# @rbs route: Skyward::API::Endpoints::Route
|
|
215
277
|
# @rbs reader: Skyward::Core::Types::Reader
|
|
216
|
-
# @rbs return: RBS::MethodType |
|
|
217
|
-
def method_type(klass,
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
278
|
+
# @rbs return: RBS::MethodType | Refusal | nil
|
|
279
|
+
def method_type(klass, route, reader)
|
|
280
|
+
to = route.to
|
|
281
|
+
return signature(klass, to, reader) if klass.public_method_defined?(to)
|
|
282
|
+
|
|
283
|
+
refused(
|
|
284
|
+
"#{klass} has no public method `#{to}`. Define it as a public method, or change to:.",
|
|
285
|
+
spots: [argument(route, :to)],
|
|
286
|
+
hint: unrouted(klass, to)
|
|
287
|
+
)
|
|
288
|
+
end
|
|
289
|
+
|
|
290
|
+
# Only the class's own methods, so that a method it inherits, as from
|
|
291
|
+
# Skyward::API or Kernel, is never suggested.
|
|
292
|
+
# @rbs klass: Class
|
|
293
|
+
# @rbs to: Symbol
|
|
294
|
+
# @rbs return: String?
|
|
295
|
+
def unrouted(klass, to)
|
|
296
|
+
hidden =
|
|
297
|
+
if klass.private_method_defined?(to, false) then "private"
|
|
298
|
+
elsif klass.protected_method_defined?(to, false) then "protected"
|
|
299
|
+
end
|
|
300
|
+
return "#{to} is #{hidden}. Move it above `#{hidden}`." if hidden
|
|
221
301
|
|
|
222
|
-
|
|
302
|
+
closest = Skyward::Core::Diagnostics::Suggestion.closest(to.to_s, klass.public_instance_methods(false).map(&:to_s))
|
|
303
|
+
"Did you mean to: :#{closest}?" if closest
|
|
223
304
|
end
|
|
224
305
|
|
|
225
306
|
# @rbs owner: Module
|
|
@@ -231,13 +312,17 @@ module Skyward::API::Assembly::Boot
|
|
|
231
312
|
reader.method_type(type_name, name) if type_name
|
|
232
313
|
end
|
|
233
314
|
|
|
234
|
-
# @rbs
|
|
315
|
+
# @rbs method: UnboundMethod
|
|
235
316
|
# @rbs type: RBS::Types::function?
|
|
236
|
-
# @rbs return: Array[[Symbol, bool, RBS::Types::t]] |
|
|
237
|
-
def signed(
|
|
317
|
+
# @rbs return: Array[[Symbol, bool, RBS::Types::t]] | Refusal
|
|
318
|
+
def signed(method, type)
|
|
319
|
+
parameters = method.parameters
|
|
238
320
|
if parameters.any? { |kind, _| UNNAMED.include?(kind) }
|
|
239
|
-
return
|
|
240
|
-
|
|
321
|
+
return refused(
|
|
322
|
+
"The method takes positional arguments or **rest, but Skyward API passes only named keywords. " \
|
|
323
|
+
"Take each input as a keyword, such as `id:`.",
|
|
324
|
+
spots: [Skyward::Core::Diagnostics::Spots.parameters(method)]
|
|
325
|
+
)
|
|
241
326
|
end
|
|
242
327
|
|
|
243
328
|
keywords = parameters.filter_map { |kind, name| [name, kind == :keyreq] if KEYWORD.include?(kind) }.to_h
|
|
@@ -268,11 +353,13 @@ module Skyward::API::Assembly::Boot
|
|
|
268
353
|
|
|
269
354
|
# @rbs keywords: Hash[Symbol, bool]
|
|
270
355
|
# @rbs function: RBS::Types::function
|
|
271
|
-
# @rbs return:
|
|
356
|
+
# @rbs return: Refusal
|
|
272
357
|
def stale(keywords, function)
|
|
273
358
|
taken = keywords.map { |name, required| "#{"?" unless required}#{name}:" }
|
|
274
|
-
|
|
359
|
+
refused(
|
|
360
|
+
"The method takes (#{taken.join(", ")}), but its signature takes (#{written(function.param_to_s)}), " \
|
|
275
361
|
"so the signature is stale. Run rbs-inline, or update the signature."
|
|
362
|
+
)
|
|
276
363
|
end
|
|
277
364
|
|
|
278
365
|
# @rbs function: RBS::Types::function
|
|
@@ -297,13 +384,61 @@ module Skyward::API::Assembly::Boot
|
|
|
297
384
|
function.required_keywords.transform_values { true }.merge(function.optional_keywords.transform_values { false })
|
|
298
385
|
end
|
|
299
386
|
|
|
387
|
+
# @rbs route: Skyward::API::Endpoints::Route
|
|
388
|
+
# @rbs method: UnboundMethod
|
|
389
|
+
# @rbs parameter: Symbol
|
|
390
|
+
# @rbs query: Array[Symbol]
|
|
391
|
+
# @rbs return: Refusal
|
|
392
|
+
def no_keyword(route, method, parameter, query)
|
|
393
|
+
problem = "The route has a path parameter :#{parameter}, but the method has no `#{parameter}:` keyword. " \
|
|
394
|
+
"Add `#{parameter}:` to the method, or rename the path parameter."
|
|
395
|
+
meant = meant(parameter, query)
|
|
396
|
+
keyword = Skyward::Core::Diagnostics::Spots.keyword_parameter(method, meant) if meant
|
|
397
|
+
parameters = Skyward::Core::Diagnostics::Spots.parameters(method)
|
|
398
|
+
hint = rename_hint(method, parameters, keyword, parameter) if parameters && keyword
|
|
399
|
+
refused(problem, spots: [argument(route, :to)&.with(columns: nil), keyword || parameters], hint:)
|
|
400
|
+
end
|
|
401
|
+
|
|
402
|
+
# A nested resource's renamed parameter, as `todo_id:` for `/:id`, isn't a
|
|
403
|
+
# misspelling DidYouMean would find, so a keyword that ends in the parameter,
|
|
404
|
+
# or that the parameter ends in, is meant too, when it's the only one.
|
|
405
|
+
# @rbs parameter: Symbol
|
|
406
|
+
# @rbs query: Array[Symbol]
|
|
407
|
+
# @rbs return: Symbol?
|
|
408
|
+
def meant(parameter, query)
|
|
409
|
+
closest = Skyward::Core::Diagnostics::Suggestion.closest(parameter.to_s, query.map(&:to_s))
|
|
410
|
+
return closest.to_sym if closest
|
|
411
|
+
|
|
412
|
+
suffixed = query.select { |name| name.end_with?("_#{parameter}") || parameter.end_with?("_#{name}") }
|
|
413
|
+
suffixed.first if suffixed.one?
|
|
414
|
+
end
|
|
415
|
+
|
|
416
|
+
# Core's spot of a parameter list that runs onto another line stops at the end
|
|
417
|
+
# of its first, so only a list written whole on the `def` line is rewritten.
|
|
418
|
+
# @rbs method: UnboundMethod
|
|
419
|
+
# @rbs parameters: Skyward::Core::Diagnostics::Spot
|
|
420
|
+
# @rbs keyword: Skyward::Core::Diagnostics::Spot
|
|
421
|
+
# @rbs parameter: Symbol
|
|
422
|
+
# @rbs return: String?
|
|
423
|
+
def rename_hint(method, parameters, keyword, parameter)
|
|
424
|
+
list = parameters.columns
|
|
425
|
+
columns = keyword.columns
|
|
426
|
+
text = parameters.text
|
|
427
|
+
line = method.source_location&.last
|
|
428
|
+
return unless list && columns && parameters.line == line && text[list.end] == ")" && keyword.line == line
|
|
429
|
+
|
|
430
|
+
renamed = text[columns].to_s.sub(/\A[^:]+/, parameter.to_s)
|
|
431
|
+
rewritten = "#{text[list.begin...columns.begin]}#{renamed}#{text[columns.end...list.end]}"
|
|
432
|
+
"Did you mean `def #{method.original_name}(#{rewritten})`?"
|
|
433
|
+
end
|
|
434
|
+
|
|
300
435
|
# @rbs name: Symbol
|
|
301
436
|
# @rbs required: bool
|
|
302
437
|
# @rbs type: RBS::Types::t
|
|
303
438
|
# @rbs pattern: Array[String | Symbol]
|
|
304
439
|
# @rbs providers: Hash[Symbol, Skyward::API::Routing::Router::Provider]
|
|
305
440
|
# @rbs reader: Skyward::Core::Types::Reader
|
|
306
|
-
# @rbs return: Skyward::API::Routing::Router::Keyword | Skyward::API::Routing::Router::Provider |
|
|
441
|
+
# @rbs return: Skyward::API::Routing::Router::Keyword | Skyward::API::Routing::Router::Provider | Refusal
|
|
307
442
|
def keyword(name, required, type, pattern, providers, reader)
|
|
308
443
|
source = source(name, pattern)
|
|
309
444
|
return request(required, type) if source == :request
|
|
@@ -332,11 +467,13 @@ module Skyward::API::Assembly::Boot
|
|
|
332
467
|
|
|
333
468
|
# @rbs required: bool
|
|
334
469
|
# @rbs type: RBS::Types::t
|
|
335
|
-
# @rbs return: Skyward::API::Routing::Router::Keyword |
|
|
470
|
+
# @rbs return: Skyward::API::Routing::Router::Keyword | Refusal
|
|
336
471
|
def request(required, type)
|
|
337
472
|
unless [REQUEST, ANY].include?(type)
|
|
338
|
-
return
|
|
339
|
-
|
|
473
|
+
return refused(
|
|
474
|
+
"`request:` is always the Skyward::API::Request, but its signature types it as " \
|
|
475
|
+
"#{written(type.to_s)}. Type it as Skyward::API::Request, or rename the keyword."
|
|
476
|
+
)
|
|
340
477
|
end
|
|
341
478
|
|
|
342
479
|
Skyward::API::Routing::Router::Keyword.new(name: :request, source: :request, required:, type: REQUEST_TYPE)
|
|
@@ -347,14 +484,16 @@ module Skyward::API::Assembly::Boot
|
|
|
347
484
|
# @rbs name: Symbol
|
|
348
485
|
# @rbs type: RBS::Types::t
|
|
349
486
|
# @rbs provider: Skyward::API::Routing::Router::Provider
|
|
350
|
-
# @rbs return: Skyward::API::Routing::Router::Provider |
|
|
487
|
+
# @rbs return: Skyward::API::Routing::Router::Provider | Refusal
|
|
351
488
|
def provided(name, type, provider)
|
|
352
489
|
value = provider.value
|
|
353
490
|
return provider if value.nil? || untyped_type?(type) || same?(parts(type), value)
|
|
354
491
|
|
|
355
492
|
returned = value.empty? ? "only responses" : "#{written(value.join(" | "))}, besides any responses"
|
|
356
|
-
|
|
493
|
+
refused(
|
|
494
|
+
"`#{name}:` is typed as #{written(type.to_s)}, but the :#{name} provider returns #{returned}. " \
|
|
357
495
|
"Give them the same type."
|
|
496
|
+
)
|
|
358
497
|
end
|
|
359
498
|
|
|
360
499
|
# @rbs type: RBS::Types::t
|
|
@@ -370,20 +509,24 @@ module Skyward::API::Assembly::Boot
|
|
|
370
509
|
# @rbs name: Symbol
|
|
371
510
|
# @rbs source: :path | :query | :body | :request
|
|
372
511
|
# @rbs type: Skyward::Core::Types::t
|
|
373
|
-
# @rbs return:
|
|
512
|
+
# @rbs return: Refusal?
|
|
374
513
|
def carried(name, source, type)
|
|
375
514
|
case source
|
|
376
515
|
when :path
|
|
377
516
|
uncarried = uncarried(type, :path)
|
|
378
517
|
if uncarried
|
|
379
|
-
|
|
518
|
+
refused(
|
|
519
|
+
"`#{name}:` comes from the path, which can't carry #{uncarried}. " \
|
|
380
520
|
"Give it a type such as String, Integer or an enum."
|
|
521
|
+
)
|
|
381
522
|
end
|
|
382
523
|
when :query
|
|
383
524
|
uncarried = uncarried(type, :query)
|
|
384
525
|
if uncarried
|
|
385
|
-
|
|
526
|
+
refused(
|
|
527
|
+
"`#{name}:` comes from the query string, which can't carry #{uncarried}. " \
|
|
386
528
|
"Give it a type such as String, Integer or an array of them, or take it in `body:`."
|
|
529
|
+
)
|
|
387
530
|
end
|
|
388
531
|
end
|
|
389
532
|
end
|
|
@@ -410,7 +553,7 @@ module Skyward::API::Assembly::Boot
|
|
|
410
553
|
# Response values are found in the signature, because the reader can't read them.
|
|
411
554
|
# @rbs type: RBS::Types::t
|
|
412
555
|
# @rbs reader: Skyward::Core::Types::Reader
|
|
413
|
-
# @rbs return: Array[Skyward::API::Routing::Router::returned] |
|
|
556
|
+
# @rbs return: Array[Skyward::API::Routing::Router::returned] | Refusal
|
|
414
557
|
def returns(type, reader)
|
|
415
558
|
read = parts(type).map { |part| response(part, reader) || reader.read(part) }
|
|
416
559
|
unreadable = read.find { |member| member.is_a?(Skyward::Core::Types::Unreadable) }
|
|
@@ -457,7 +600,8 @@ module Skyward::API::Assembly::Boot
|
|
|
457
600
|
# @rbs return: Skyward::API::Routing::Router::Created | Skyward::Core::Types::Unreadable
|
|
458
601
|
def created(type, reader)
|
|
459
602
|
unless type.args.size == 1
|
|
460
|
-
|
|
603
|
+
instead = "Skyward::API::Responses::Created[T]"
|
|
604
|
+
return Skyward::Core::Types::Unreadable.new(type: written(type.to_s), at: nil, instead:, suggestion: nil)
|
|
461
605
|
end
|
|
462
606
|
|
|
463
607
|
read = reader.read(type.args.fetch(0))
|
|
@@ -521,10 +665,14 @@ module Skyward::API::Assembly::Boot
|
|
|
521
665
|
|
|
522
666
|
# @rbs subject: String
|
|
523
667
|
# @rbs type: Skyward::Core::Types::Unreadable
|
|
524
|
-
# @rbs return:
|
|
668
|
+
# @rbs return: Refusal
|
|
525
669
|
def unreadable(subject, type)
|
|
526
|
-
|
|
527
|
-
|
|
670
|
+
suggestion = type.suggestion
|
|
671
|
+
refused(
|
|
672
|
+
"#{subject} can't be read: Skyward API doesn't read #{type.type}#{", at #{type.at}" if type.at}. " \
|
|
673
|
+
"Use #{type.instead} instead.",
|
|
674
|
+
hint: ("Did you mean #{suggestion}?" if suggestion)
|
|
675
|
+
)
|
|
528
676
|
end
|
|
529
677
|
|
|
530
678
|
# @rbs text: String
|
|
@@ -7,10 +7,10 @@ module Skyward::API::CLI::AppFile
|
|
|
7
7
|
|
|
8
8
|
ASSIGNMENT = "App = Skyward.app { ... }" #: String
|
|
9
9
|
|
|
10
|
-
# @rbs (String directory, out: _Writer, kernel: singleton(Kernel)
|
|
11
|
-
# -> void
|
|
12
|
-
def run(directory, out:, kernel:, &)
|
|
13
|
-
app = load(directory)
|
|
10
|
+
# @rbs (String directory, out: _Writer, kernel: singleton(Kernel), generate: ^() -> String?)
|
|
11
|
+
# { (Skyward::API::Assembly::Application) -> String } -> void
|
|
12
|
+
def run(directory, out:, kernel:, generate:, &)
|
|
13
|
+
app = load(directory, generate)
|
|
14
14
|
case app
|
|
15
15
|
when String then kernel.abort(app)
|
|
16
16
|
else out.write(yield app)
|
|
@@ -22,13 +22,17 @@ module Skyward::API::CLI::AppFile
|
|
|
22
22
|
end
|
|
23
23
|
|
|
24
24
|
# @rbs directory: String
|
|
25
|
+
# @rbs generate: ^() -> String?
|
|
25
26
|
# @rbs return: Skyward::API::Assembly::Application | String
|
|
26
|
-
def load(directory)
|
|
27
|
+
def load(directory, generate)
|
|
27
28
|
path = File.join(directory, FILE)
|
|
28
29
|
unless File.file?(path)
|
|
29
30
|
return "skyward: no #{FILE} in #{directory}. Run skyward where your app's #{FILE} assigns #{ASSIGNMENT}."
|
|
30
31
|
end
|
|
31
32
|
|
|
33
|
+
failure = generate.call
|
|
34
|
+
return failure if failure
|
|
35
|
+
|
|
32
36
|
require path
|
|
33
37
|
app = Object.const_get(:App) if Object.const_defined?(:App)
|
|
34
38
|
return app if app.is_a?(Skyward::API::Assembly::Application)
|
|
@@ -1,11 +1,9 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require "tmpdir"
|
|
4
|
-
|
|
5
3
|
module Skyward::API::CLI::Check
|
|
6
4
|
extend self
|
|
7
5
|
|
|
8
|
-
# @rbs! type capture =
|
|
6
|
+
# @rbs! type capture = Skyward::Core::CLI::Executable::capture
|
|
9
7
|
|
|
10
8
|
Outcome = Data.define(
|
|
11
9
|
:passed, #: bool
|
|
@@ -14,19 +12,17 @@ module Skyward::API::CLI::Check
|
|
|
14
12
|
|
|
15
13
|
PASSED = Outcome.new(passed: true, detail: "") #: Outcome
|
|
16
14
|
|
|
17
|
-
GENERATED =
|
|
15
|
+
GENERATED = Skyward::Core::Signatures::Inline::OUTPUT #: String
|
|
18
16
|
|
|
19
17
|
STEEPFILE = "Steepfile" #: String
|
|
20
18
|
|
|
21
|
-
# None holds the app's contract: its tests, its installed gems and scratch.
|
|
22
|
-
UNCHECKED = %w[test spec vendor tmp].freeze #: Array[String]
|
|
23
|
-
|
|
24
19
|
# The strict boot's own Ruby runs it, so the forced strict mode, the app's
|
|
25
20
|
# side effects and an exit or crash stay out of skyward check. The require
|
|
26
21
|
# lives here because a `-r` on the command line runs before RUBYOPT's
|
|
27
22
|
# `-rbundler/setup` on newer Rubies, before the bundle's gems are loadable.
|
|
23
|
+
# skyward check writes nothing, so the boot doesn't regenerate sig/generated.
|
|
28
24
|
BOOT = 'require "skyward/api"; ' \
|
|
29
|
-
'Skyward::API::CLI::AppFile.run(Dir.pwd, out: $stdout, kernel: Kernel) { "" }' #: String
|
|
25
|
+
'Skyward::API::CLI::AppFile.run(Dir.pwd, out: $stdout, kernel: Kernel, generate: -> {}) { "" }' #: String
|
|
30
26
|
|
|
31
27
|
INSTALL_RBS_INLINE = "rbs-inline isn't installed. Add `gem \"rbs-inline\", require: false` to the Gemfile " \
|
|
32
28
|
"and run `bundle install`.\n" #: String
|
|
@@ -56,19 +52,20 @@ module Skyward::API::CLI::Check
|
|
|
56
52
|
def signatures(directory, capture:, rbs_inline:)
|
|
57
53
|
return failed(INSTALL_RBS_INLINE) unless rbs_inline
|
|
58
54
|
|
|
59
|
-
|
|
55
|
+
inline = Skyward::Core::Signatures::Inline
|
|
56
|
+
paths = inline.sources(directory)
|
|
60
57
|
generate = "bundle exec rbs-inline --output=#{GENERATED} #{paths.join(" ")}"
|
|
61
58
|
generated = File.join(directory, GENERATED)
|
|
62
59
|
unless File.directory?(generated)
|
|
63
60
|
return failed("There's no #{GENERATED}. Run `#{generate}` and commit the result.\n")
|
|
64
61
|
end
|
|
65
62
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
stale = differences(rbs_files(generated),
|
|
71
|
-
|
|
63
|
+
expected = inline.run(directory, rbs_inline:, capture:)
|
|
64
|
+
case expected
|
|
65
|
+
when String then failed(expected)
|
|
66
|
+
else
|
|
67
|
+
stale = differences(inline.rbs_files(generated), expected)
|
|
68
|
+
return PASSED if stale.empty?
|
|
72
69
|
|
|
73
70
|
failed(
|
|
74
71
|
[
|
|
@@ -113,20 +110,6 @@ module Skyward::API::CLI::Check
|
|
|
113
110
|
# @rbs return: Outcome
|
|
114
111
|
def failed(detail) = Outcome.new(passed: false, detail:)
|
|
115
112
|
|
|
116
|
-
# @rbs directory: String
|
|
117
|
-
# @rbs return: Array[String]
|
|
118
|
-
def sources(directory)
|
|
119
|
-
roots = Dir.glob("*/", base: directory).map { |root| root.delete_suffix("/") } - UNCHECKED
|
|
120
|
-
files = Dir.glob("*.rb", base: directory)
|
|
121
|
-
[*files, *roots.reject { |root| Dir.glob("#{root}/**/*.rb", base: directory).empty? }].sort
|
|
122
|
-
end
|
|
123
|
-
|
|
124
|
-
# @rbs directory: String
|
|
125
|
-
# @rbs return: Hash[String, String]
|
|
126
|
-
def rbs_files(directory)
|
|
127
|
-
Dir.glob("**/*.rbs", base: directory).to_h { |file| [file, File.read(File.join(directory, file))] }
|
|
128
|
-
end
|
|
129
|
-
|
|
130
113
|
# @rbs current: Hash[String, String]
|
|
131
114
|
# @rbs expected: Hash[String, String]
|
|
132
115
|
# @rbs return: Array[String]
|
|
@@ -7,22 +7,29 @@ class Skyward::API::CLI::Serve::Reloader
|
|
|
7
7
|
|
|
8
8
|
# @rbs @watch: ^() -> Hash[String, Time]
|
|
9
9
|
# @rbs @load: ^() -> Skyward::API::_RackApp
|
|
10
|
+
# @rbs @generate: ^() -> String?
|
|
10
11
|
# @rbs @clock: ^() -> Float
|
|
11
12
|
# @rbs @mutex: Thread::Mutex
|
|
12
13
|
# @rbs @checked_at: Float
|
|
13
14
|
# @rbs @files: Hash[String, Time]
|
|
15
|
+
# @rbs @sources: Hash[String, Time]
|
|
16
|
+
# @rbs @failure: String?
|
|
14
17
|
# @rbs @app: Skyward::API::_RackApp
|
|
15
18
|
|
|
16
19
|
# @rbs watch: ^() -> Hash[String, Time]
|
|
17
20
|
# @rbs load: ^() -> Skyward::API::_RackApp
|
|
21
|
+
# @rbs generate: ^() -> String?
|
|
18
22
|
# @rbs clock: ^() -> Float
|
|
19
23
|
# @rbs return: void
|
|
20
|
-
def initialize(watch:, load:, clock:)
|
|
24
|
+
def initialize(watch:, load:, generate:, clock:)
|
|
21
25
|
@watch = watch
|
|
22
26
|
@load = load
|
|
27
|
+
@generate = generate
|
|
23
28
|
@clock = clock
|
|
24
29
|
@mutex = Thread::Mutex.new
|
|
25
30
|
@checked_at = clock.call
|
|
31
|
+
@sources = {}
|
|
32
|
+
@failure = nil
|
|
26
33
|
@files = watch.call
|
|
27
34
|
@app = attempt
|
|
28
35
|
end
|
|
@@ -33,6 +40,17 @@ class Skyward::API::CLI::Serve::Reloader
|
|
|
33
40
|
# @rbs return: Skyward::API::rack_response
|
|
34
41
|
def call(env) = @mutex.synchronize { current }.call(env)
|
|
35
42
|
|
|
43
|
+
# Leaves the reload to the next request, which still sees the change.
|
|
44
|
+
# @rbs return: void
|
|
45
|
+
def regenerate
|
|
46
|
+
@mutex.synchronize { generate(@watch.call) }
|
|
47
|
+
# Runs on a thread of its own, which an exception would end. A file removed
|
|
48
|
+
# between listing and reading it raises, and a generate that raised is run
|
|
49
|
+
# again by the next reload, which answers with its error.
|
|
50
|
+
rescue StandardError
|
|
51
|
+
nil
|
|
52
|
+
end
|
|
53
|
+
|
|
36
54
|
private
|
|
37
55
|
|
|
38
56
|
# @rbs return: Skyward::API::_RackApp
|
|
@@ -50,10 +68,32 @@ class Skyward::API::CLI::Serve::Reloader
|
|
|
50
68
|
|
|
51
69
|
# @rbs return: Skyward::API::_RackApp
|
|
52
70
|
def attempt
|
|
71
|
+
failure = generate(@files)
|
|
72
|
+
return failed(Skyward::Core::BootError.new(failure)) if failure
|
|
73
|
+
|
|
53
74
|
@load.call
|
|
54
75
|
# A SyntaxError or LoadError is a ScriptError, not a StandardError.
|
|
55
76
|
rescue StandardError, ScriptError => e
|
|
56
|
-
|
|
77
|
+
failed(e)
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
# Runs rbs-inline once for the .rb files as they are, whether regenerate or
|
|
81
|
+
# a reload sees them first.
|
|
82
|
+
# @rbs files: Hash[String, Time]
|
|
83
|
+
# @rbs return: String?
|
|
84
|
+
def generate(files)
|
|
85
|
+
sources = files.select { |file, _time| file.end_with?(".rb") }
|
|
86
|
+
return @failure if sources == @sources
|
|
87
|
+
|
|
88
|
+
@failure = @generate.call
|
|
89
|
+
@sources = sources
|
|
90
|
+
@failure
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
# @rbs error: Exception
|
|
94
|
+
# @rbs return: Skyward::API::_RackApp
|
|
95
|
+
def failed(error)
|
|
96
|
+
->(env) { failure(error, env) } #: ^(Hash[String, untyped]) -> Skyward::API::rack_response
|
|
57
97
|
end
|
|
58
98
|
|
|
59
99
|
# The development 500 that Skyward::API::Middleware::Rescue answers, without a request
|
|
@@ -69,7 +109,8 @@ class Skyward::API::CLI::Serve::Reloader
|
|
|
69
109
|
title: "Internal Server Error",
|
|
70
110
|
status: 500,
|
|
71
111
|
detail: "#{error.class}: #{message}",
|
|
72
|
-
backtrace
|
|
112
|
+
# rbs-inline's failure is never raised, so has no backtrace.
|
|
113
|
+
backtrace: error.backtrace || []
|
|
73
114
|
}
|
|
74
115
|
body = env["REQUEST_METHOD"] == "HEAD" ? [] : [JSON.generate(problem)] #: Array[String]
|
|
75
116
|
[500, { "content-type" => "application/problem+json" }, body]
|