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.
Files changed (39) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE +21 -0
  3. data/README.md +60 -56
  4. data/lib/skyward/api/assembly/boot.rb +239 -91
  5. data/lib/skyward/api/assembly/builder.rb +2 -2
  6. data/lib/skyward/api/assembly/dispatcher.rb +12 -12
  7. data/lib/skyward/api/assembly/static.rb +36 -36
  8. data/lib/skyward/api/cli/app_file.rb +2 -2
  9. data/lib/skyward/api/cli/check.rb +9 -9
  10. data/lib/skyward/api/cli/routes.rb +2 -2
  11. data/lib/skyward/api/cli/serve/reloader.rb +6 -6
  12. data/lib/skyward/api/cli/serve.rb +15 -15
  13. data/lib/skyward/api/cli/serve.ru +2 -2
  14. data/lib/skyward/api/commands.rb +10 -10
  15. data/lib/skyward/api/endpoints/declarations.rb +7 -3
  16. data/lib/skyward/api/endpoints/route.rb +3 -1
  17. data/lib/skyward/api/inputs/coercer.rb +53 -20
  18. data/lib/skyward/api/inputs.rb +9 -9
  19. data/lib/skyward/api/middleware/request_id.rb +3 -3
  20. data/lib/skyward/api/middleware/request_log.rb +5 -5
  21. data/lib/skyward/api/middleware/rescue.rb +7 -7
  22. data/lib/skyward/api/openapi/docs.rb +5 -5
  23. data/lib/skyward/api/openapi.rb +52 -50
  24. data/lib/skyward/api/request.rb +8 -8
  25. data/lib/skyward/api/routing/percent_encoding.rb +1 -1
  26. data/lib/skyward/api/routing/router.rb +68 -27
  27. data/lib/skyward/api/serializer/writers.rb +27 -16
  28. data/lib/skyward/api/serializer.rb +39 -21
  29. data/lib/skyward/api/test/client.rb +25 -25
  30. data/lib/skyward/api/version.rb +1 -1
  31. data/lib/skyward/api.rb +15 -14
  32. data/sig/generated/skyward/api/assembly/boot.rbs +99 -29
  33. data/sig/generated/skyward/api/endpoints/route.rbs +8 -4
  34. data/sig/generated/skyward/api/inputs/coercer.rbs +20 -2
  35. data/sig/generated/skyward/api/routing/router.rbs +52 -9
  36. data/sig/generated/skyward/api/serializer/writers.rbs +6 -0
  37. data/sig/generated/skyward/api/serializer.rbs +13 -1
  38. metadata +19 -4
  39. /data/sig/{manual/zeitwerk/loader.rbs → _private/zeitwerk.rbs} +0 -0
@@ -7,14 +7,14 @@ module Skyward::API::Assembly::Boot
7
7
 
8
8
  KEYWORD = %i[keyreq key].freeze #: Array[Symbol]
9
9
 
10
- RACK = ["::Skyward::API::rack", "[::Integer, ::Hash[::String, ::String], untyped]"].map do |type|
10
+ RACK = ['::Skyward::API::rack', '[::Integer, ::Hash[::String, ::String], untyped]'].map do |type|
11
11
  RBS::Parser.parse_type(type)
12
12
  end.freeze #: Array[RBS::Types::t?]
13
13
 
14
- REQUEST = RBS::Parser.parse_type("::Skyward::API::Request") #: RBS::Types::t?
14
+ REQUEST = RBS::Parser.parse_type('::Skyward::API::Request') #: RBS::Types::t?
15
15
 
16
16
  REQUEST_TYPE = Skyward::Core::Types::Named.new(
17
- name: "Skyward::API::Request",
17
+ name: 'Skyward::API::Request',
18
18
  klass: Skyward::API::Request,
19
19
  fields: []
20
20
  ) #: Skyward::Core::Types::Named
@@ -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
- built = mounts.reduce(none) do |booted, mount|
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,51 +99,96 @@ 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 String then raise Skyward::Core::BootError, "#{label}\n #{result}"
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 return: String?
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
- "Its summary: has a line break, but a summary is one line. Put the rest in description:."
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
112
160
  # @rbs path: String
113
161
  # @rbs return: String
114
162
  def full_path(prefix, path)
115
- parts = [prefix, path].map { |part| part.delete_prefix("/").delete_suffix("/") }
116
- "/#{parts.reject(&:empty?).join("/")}"
163
+ parts = [prefix, path].map { |part| part.delete_prefix('/').delete_suffix('/') }
164
+ "/#{parts.reject(&:empty?).join('/')}"
117
165
  end
118
166
 
119
167
  # @rbs path: String
120
168
  # @rbs return: Array[String | Symbol]
121
169
  def pattern(path)
122
- path.split("/", -1).map { |segment| segment.start_with?(":") ? segment.delete_prefix(":").to_sym : segment }
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: String?
129
- def shadowing(verb, pattern, built)
130
- earlier = built.find { |endpoint| endpoint.verb == verb && covers?(endpoint.pattern, pattern) }
131
- return unless earlier
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
- if covers?(pattern, earlier.pattern)
134
- "#{earlier.label} is tried first and matches exactly the same paths, so this route is never reached. " \
135
- "Remove one of them, or change a path."
136
- else
137
- "#{earlier.label} is tried first and matches every path this route does, so this route is never reached. " \
138
- "Declare the static route first."
139
- end
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.
@@ -145,7 +197,7 @@ module Skyward::API::Assembly::Boot
145
197
  # @rbs return: bool
146
198
  def covers?(pattern, other)
147
199
  pattern.length == other.length &&
148
- pattern.zip(other).all? { |part, other_part| part == other_part || (part.is_a?(Symbol) && other_part != "") }
200
+ pattern.zip(other).all? { |part, other_part| part == other_part || (part.is_a?(Symbol) && other_part != '') }
149
201
  end
150
202
 
151
203
  # @rbs api: Skyward::API
@@ -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 | String
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.to, reader)
162
- return method_type if method_type.is_a?(String)
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(api.class.instance_method(route.to).parameters, function)
166
- return signed if signed.is_a?(String)
218
+ signed = signed(method, function)
219
+ return signed if signed.is_a?(Refusal)
167
220
 
168
- unnamed = pattern.grep(Symbol).find { |name| signed.none? { |keyword, _, _| keyword == name } }
221
+ path_parameters = pattern.grep(Symbol) #: Array[Symbol]
222
+ unnamed = path_parameters.find { |name| signed.none? { |keyword, _, _| keyword == name } }
169
223
  if unnamed
170
- return "The route has a path parameter :#{unnamed}, but the method has no `#{unnamed}:` keyword. " \
171
- "Add `#{unnamed}:` to the method, or rename the path parameter."
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 = pattern.grep(Symbol).find { |name| providers.key?(name) }
228
+ both = path_parameters.find { |name| providers.key?(name) }
175
229
  if both
176
- return "The route has a path parameter :#{both}, but :#{both} is provided too, so `#{both}:` can't be both. " \
177
- "Rename the path parameter, or provide another name."
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.map { |name, required, type| keyword(name, required, type, pattern, providers, reader) }
181
- problem = read.find { |keyword| keyword.is_a?(String) }
182
- return problem if problem.is_a?(String)
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
- return returns if returns.is_a?(String)
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 to: Symbol
276
+ # @rbs route: Skyward::API::Endpoints::Route
215
277
  # @rbs reader: Skyward::Core::Types::Reader
216
- # @rbs return: RBS::MethodType | String | nil
217
- def method_type(klass, to, reader)
218
- unless klass.public_method_defined?(to)
219
- return "#{klass} has no public method `#{to}`. Define it as a public method, or change to:."
220
- end
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
- signature(klass, to, reader)
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 parameters: Method::param_types
315
+ # @rbs method: UnboundMethod
235
316
  # @rbs type: RBS::Types::function?
236
- # @rbs return: Array[[Symbol, bool, RBS::Types::t]] | String
237
- def signed(parameters, type)
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 "The method takes positional arguments or **rest, but Skyward API passes only named keywords. " \
240
- "Take each input as a keyword, such as `id:`."
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: String
356
+ # @rbs return: Refusal
272
357
  def stale(keywords, function)
273
- taken = keywords.map { |name, required| "#{"?" unless required}#{name}:" }
274
- "The method takes (#{taken.join(", ")}), but its signature takes (#{written(function.param_to_s)}), " \
275
- "so the signature is stale. Run rbs-inline, or update the signature."
358
+ taken = keywords.map { |name, required| "#{'?' unless required}#{name}:" }
359
+ refused(
360
+ "The method takes (#{taken.join(', ')}), but its signature takes (#{written(function.param_to_s)}), " \
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 | String
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 | String
470
+ # @rbs return: Skyward::API::Routing::Router::Keyword | Refusal
336
471
  def request(required, type)
337
472
  unless [REQUEST, ANY].include?(type)
338
- return "`request:` is always the Skyward::API::Request, but its signature types it as #{written(type.to_s)}. " \
339
- "Type it as Skyward::API::Request, or rename the keyword."
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 | String
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
- returned = value.empty? ? "only responses" : "#{written(value.join(" | "))}, besides any responses"
356
- "`#{name}:` is typed as #{written(type.to_s)}, but the :#{name} provider returns #{returned}. " \
357
- "Give them the same type."
492
+ returned = value.empty? ? 'only responses' : "#{written(value.join(' | '))}, besides any responses"
493
+ refused(
494
+ "`#{name}:` is typed as #{written(type.to_s)}, but the :#{name} provider returns #{returned}. " \
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: String?
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
- "`#{name}:` comes from the path, which can't carry #{uncarried}. " \
380
- "Give it a type such as String, Integer or an enum."
518
+ refused(
519
+ "`#{name}:` comes from the path, which can't carry #{uncarried}. " \
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
- "`#{name}:` comes from the query string, which can't carry #{uncarried}. " \
386
- "Give it a type such as String, Integer or an array of them, or take it in `body:`."
526
+ refused(
527
+ "`#{name}:` comes from the query string, which can't carry #{uncarried}. " \
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
@@ -393,14 +536,14 @@ module Skyward::API::Assembly::Boot
393
536
  # @rbs return: String?
394
537
  def uncarried(type, within)
395
538
  case type
396
- when Skyward::Core::Types::Record then "a record"
397
- when Skyward::Core::Types::HashOf then "a hash"
539
+ when Skyward::Core::Types::Record then 'a record'
540
+ when Skyward::Core::Types::HashOf then 'a hash'
398
541
  when Skyward::Core::Types::Named then "#{type.name}, a class"
399
542
  when Skyward::Core::Types::ArrayOf
400
543
  case within
401
- when :path then "an array"
544
+ when :path then 'an array'
402
545
  when :query then uncarried(type.type, :array)
403
- else "an array of arrays"
546
+ else 'an array of arrays'
404
547
  end
405
548
  when Skyward::Core::Types::Nullable then uncarried(type.type, within)
406
549
  when Skyward::Core::Types::Union then type.types.lazy.filter_map { |member| uncarried(member, within) }.first
@@ -410,11 +553,11 @@ 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] | String
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) }
417
- return unreadable("The return type", unreadable) if unreadable.is_a?(Skyward::Core::Types::Unreadable)
560
+ return unreadable('The return type', unreadable) if unreadable.is_a?(Skyward::Core::Types::Unreadable)
418
561
 
419
562
  readable = read.grep_v(Skyward::Core::Types::Unreadable) #: Array[Skyward::API::Routing::Router::returned]
420
563
  readable.flat_map { |member| members(member) }.uniq
@@ -436,7 +579,7 @@ module Skyward::API::Assembly::Boot
436
579
  # | Skyward::Core::Types::Unreadable | nil
437
580
  def response(type, reader)
438
581
  return Skyward::API::Routing::Router::Rack.new if RACK.include?(type)
439
- if type.is_a?(RBS::Types::ClassInstance) && type.name.to_s == "::Skyward::API::Responses::Created"
582
+ if type.is_a?(RBS::Types::ClassInstance) && type.name.to_s == '::Skyward::API::Responses::Created'
440
583
  return created(type, reader)
441
584
  end
442
585
 
@@ -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
- return Skyward::Core::Types::Unreadable.new(type: written(type.to_s), at: nil, instead: "Skyward::API::Responses::Created[T]")
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))
@@ -487,8 +631,8 @@ module Skyward::API::Assembly::Boot
487
631
  if function
488
632
  untyped_parts(endpoint, function)
489
633
  else
490
- "It has no RBS signature, but strict: true refuses an untyped endpoint. " \
491
- "Annotate it and run rbs-inline, or check `signatures:`."
634
+ 'It has no RBS signature, but strict: true refuses an untyped endpoint. ' \
635
+ 'Annotate it and run rbs-inline, or check `signatures:`.'
492
636
  end
493
637
  "#{endpoint.label}\n #{problem}" if problem
494
638
  end
@@ -510,24 +654,28 @@ module Skyward::API::Assembly::Boot
510
654
  "`#{provider.name}:`" if untyped_type?(params.fetch(provider.name).type)
511
655
  end
512
656
  named = [*keywords, *provided]
513
- found = endpoint.returns.include?(any) ? [*named, "the return type"] : named
657
+ found = endpoint.returns.include?(any) ? [*named, 'the return type'] : named
514
658
  return if found.empty?
515
659
 
516
660
  *rest, last = found
517
- listed = rest.empty? ? last : "#{rest.join(", ")} and #{last}"
661
+ listed = rest.empty? ? last : "#{rest.join(', ')} and #{last}"
518
662
  "Its signature leaves #{listed} untyped, but strict: true refuses an untyped endpoint. " \
519
- "Give #{found.one? ? "it" : "each"} a type."
663
+ "Give #{found.one? ? 'it' : 'each'} a type."
520
664
  end
521
665
 
522
666
  # @rbs subject: String
523
667
  # @rbs type: Skyward::Core::Types::Unreadable
524
- # @rbs return: String
668
+ # @rbs return: Refusal
525
669
  def unreadable(subject, type)
526
- "#{subject} can't be read: Skyward API doesn't read #{type.type}#{", at #{type.at}" if type.at}. " \
527
- "Use #{type.instead} instead."
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
531
679
  # @rbs return: String
532
- def written(text) = text.gsub(/(?<!\w)::/, "")
680
+ def written(text) = text.gsub(/(?<!\w)::/, '')
533
681
  end
@@ -41,7 +41,7 @@ class Skyward::API::Assembly::Builder
41
41
  # @rbs api: Skyward::API
42
42
  # @rbs at: String
43
43
  # @rbs return: void
44
- def mount(api, at: "/")
44
+ def mount(api, at: '/')
45
45
  # Steep proves a typed caller passes an API, but an untyped one can pass anything.
46
46
  unless api.is_a?(Skyward::API) # steep:ignore UnreachableBranch
47
47
  raise Skyward::Core::BootError, "mount takes a Skyward::API instance, got #{api.inspect}"
@@ -72,7 +72,7 @@ class Skyward::API::Assembly::Builder
72
72
  if %i[body request].include?(name)
73
73
  raise Skyward::Core::BootError,
74
74
  "provide #{name.inspect}: Skyward API fills `#{name}:` itself, so it can't be provided. " \
75
- "Provide another name."
75
+ 'Provide another name.'
76
76
  end
77
77
  if @providers.key?(name)
78
78
  raise Skyward::Core::BootError, "provide #{name.inspect}: it's already provided. Provide each name once."
@@ -1,11 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "json"
3
+ require 'json'
4
4
 
5
5
  class Skyward::API::Assembly::Dispatcher
6
6
  # @rbs! type response = [Integer, Hash[String, String], Array[String]]
7
7
 
8
- INPUT_TITLES = { 413 => "Content Too Large", 415 => "Unsupported Media Type" }.freeze #: Hash[Integer, String]
8
+ INPUT_TITLES = { 413 => 'Content Too Large', 415 => 'Unsupported Media Type' }.freeze #: Hash[Integer, String]
9
9
 
10
10
  Document = Data.define(
11
11
  :type, #: String
@@ -44,11 +44,11 @@ class Skyward::API::Assembly::Dispatcher
44
44
  # @rbs env: Hash[String, untyped]
45
45
  # @rbs return: Skyward::API::rack
46
46
  def route(env)
47
- verb = env["REQUEST_METHOD"]
48
- path = env["PATH_INFO"]
47
+ verb = env['REQUEST_METHOD']
48
+ path = env['PATH_INFO']
49
49
  # Boot refuses a route or a file at a document's path, but a path parameter can still match it, so it comes first.
50
50
  status, headers, body = served(verb, path) || routed(env, verb, path)
51
- [status, headers, verb == "HEAD" ? dropped(body) : body]
51
+ [status, headers, verb == 'HEAD' ? dropped(body) : body]
52
52
  end
53
53
 
54
54
  # @rbs verb: String
@@ -58,7 +58,7 @@ class Skyward::API::Assembly::Dispatcher
58
58
  document = @documents[path]
59
59
  return unless document && %w[GET HEAD].include?(verb)
60
60
 
61
- [200, { "content-type" => document.type }, [document.body]]
61
+ [200, { 'content-type' => document.type }, [document.body]]
62
62
  end
63
63
 
64
64
  # @rbs env: Hash[String, untyped]
@@ -78,7 +78,7 @@ class Skyward::API::Assembly::Dispatcher
78
78
  case inputs
79
79
  when Skyward::API::Inputs::Coercer::Ok then match.serializer.call(match.api.public_send(match.to, **inputs.value))
80
80
  when Skyward::API::Inputs::Coercer::Invalid
81
- problem(422, "Unprocessable Content", errors: inputs.errors.map(&:to_h))
81
+ problem(422, 'Unprocessable Content', errors: inputs.errors.map(&:to_h))
82
82
  when :content_too_large then problem(413, INPUT_TITLES.fetch(413))
83
83
  when Array then inputs
84
84
  else problem(415, INPUT_TITLES.fetch(415))
@@ -97,10 +97,10 @@ class Skyward::API::Assembly::Dispatcher
97
97
  # @rbs allowed: Array[String]
98
98
  # @rbs return: response
99
99
  def unmatched(verb, allowed)
100
- return problem(404, "Not Found") if allowed.empty?
100
+ return problem(404, 'Not Found') if allowed.empty?
101
101
 
102
- allow = { "allow" => allowed.join(", ") }
103
- verb == "OPTIONS" ? [204, allow, []] : problem(405, "Method Not Allowed", allow)
102
+ allow = { 'allow' => allowed.join(', ') }
103
+ verb == 'OPTIONS' ? [204, allow, []] : problem(405, 'Method Not Allowed', allow)
104
104
  end
105
105
 
106
106
  # @rbs status: Integer
@@ -109,8 +109,8 @@ class Skyward::API::Assembly::Dispatcher
109
109
  # @rbs **members: untyped
110
110
  # @rbs return: response
111
111
  def problem(status, title, headers = {}, **members)
112
- body = { type: "about:blank", title:, status:, **members }
113
- json(status, { "content-type" => "application/problem+json", **headers }, body)
112
+ body = { type: 'about:blank', title:, status:, **members }
113
+ json(status, { 'content-type' => 'application/problem+json', **headers }, body)
114
114
  end
115
115
 
116
116
  # @rbs status: Integer