skyward-api 0.4.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 +48 -48
- data/lib/skyward/api/assembly/boot.rb +37 -37
- 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 +2 -2
- data/lib/skyward/api/inputs/coercer.rb +19 -19
- 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 +51 -51
- 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 +19 -19
- data/lib/skyward/api/serializer.rb +15 -15
- data/lib/skyward/api/test/client.rb +25 -25
- data/lib/skyward/api/version.rb +1 -1
- data/lib/skyward/api.rb +15 -15
- data/sig/generated/skyward/api/routing/router.rbs +52 -9
- metadata +5 -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
|
class Skyward::API::Middleware::RequestLog
|
|
8
8
|
# @rbs @app: Skyward::API::_RackApp
|
|
@@ -24,9 +24,9 @@ class Skyward::API::Middleware::RequestLog
|
|
|
24
24
|
def call(env)
|
|
25
25
|
time = @now.call.getutc.iso8601(3)
|
|
26
26
|
# Read before the app runs, because middleware can rewrite the env in place.
|
|
27
|
-
method = env[
|
|
27
|
+
method = env['REQUEST_METHOD']
|
|
28
28
|
# Rack's path is raw bytes from the client, and JSON.generate raises on invalid UTF-8.
|
|
29
|
-
path = String.new("#{env[
|
|
29
|
+
path = String.new("#{env['SCRIPT_NAME']}#{env['PATH_INFO']}", encoding: Encoding::UTF_8).scrub
|
|
30
30
|
started = @clock_ms.call
|
|
31
31
|
response = @app.call(env)
|
|
32
32
|
@logger.info(
|
|
@@ -37,7 +37,7 @@ class Skyward::API::Middleware::RequestLog
|
|
|
37
37
|
path:,
|
|
38
38
|
status: response.first,
|
|
39
39
|
duration_ms: (@clock_ms.call - started).round(2),
|
|
40
|
-
request_id: env[
|
|
40
|
+
request_id: env['skyward.request_id']
|
|
41
41
|
}
|
|
42
42
|
)
|
|
43
43
|
)
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require
|
|
3
|
+
require 'json'
|
|
4
4
|
|
|
5
5
|
class Skyward::API::Middleware::Rescue
|
|
6
6
|
# @rbs @app: Skyward::API::_RackApp
|
|
@@ -22,14 +22,14 @@ class Skyward::API::Middleware::Rescue
|
|
|
22
22
|
# @rbs return: Skyward::API::rack_response
|
|
23
23
|
def call(env)
|
|
24
24
|
# Read before the app runs, because middleware can rewrite the env in place.
|
|
25
|
-
verb = env[
|
|
25
|
+
verb = env['REQUEST_METHOD']
|
|
26
26
|
@app.call(env)
|
|
27
27
|
rescue StandardError => e
|
|
28
|
-
request_id = env[
|
|
28
|
+
request_id = env['skyward.request_id']
|
|
29
29
|
log(e, request_id)
|
|
30
30
|
report(e, env, request_id)
|
|
31
|
-
body = verb ==
|
|
32
|
-
[500, {
|
|
31
|
+
body = verb == 'HEAD' ? [] : [JSON.generate(problem(e, request_id))] #: Array[String]
|
|
32
|
+
[500, { 'content-type' => 'application/problem+json' }, body]
|
|
33
33
|
end
|
|
34
34
|
|
|
35
35
|
private
|
|
@@ -41,7 +41,7 @@ class Skyward::API::Middleware::Rescue
|
|
|
41
41
|
@logger&.info(
|
|
42
42
|
JSON.generate(
|
|
43
43
|
{
|
|
44
|
-
level:
|
|
44
|
+
level: 'error',
|
|
45
45
|
request_id:,
|
|
46
46
|
class: exception.class.name,
|
|
47
47
|
message: message(exception),
|
|
@@ -65,7 +65,7 @@ class Skyward::API::Middleware::Rescue
|
|
|
65
65
|
# @rbs request_id: String?
|
|
66
66
|
# @rbs return: Hash[Symbol, untyped]
|
|
67
67
|
def problem(exception, request_id)
|
|
68
|
-
problem = { type:
|
|
68
|
+
problem = { type: 'about:blank', title: 'Internal Server Error', status: 500, request_id: }
|
|
69
69
|
# An exception's message can carry data, so only development shows it.
|
|
70
70
|
return problem unless @development
|
|
71
71
|
|
|
@@ -1,18 +1,18 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require
|
|
3
|
+
require 'cgi/escape'
|
|
4
4
|
|
|
5
5
|
module Skyward::API::OpenAPI::Docs
|
|
6
6
|
extend self
|
|
7
7
|
|
|
8
|
-
PATH =
|
|
8
|
+
PATH = '/docs' #: String
|
|
9
9
|
|
|
10
|
-
HTML_TYPE =
|
|
10
|
+
HTML_TYPE = 'text/html; charset=utf-8' #: String
|
|
11
11
|
|
|
12
|
-
SCALAR_URL =
|
|
12
|
+
SCALAR_URL = 'https://cdn.jsdelivr.net/npm/@scalar/api-reference@1.75.1/dist/browser/standalone.js' #: String
|
|
13
13
|
|
|
14
14
|
# Recompute from the file whenever SCALAR_URL's version changes.
|
|
15
|
-
SCALAR_INTEGRITY =
|
|
15
|
+
SCALAR_INTEGRITY = 'sha384-G+2Q2i9l3PPfmYkk0b4BAIUrs7xb/DNaTL1wCYYab4c9dIKlCyiqzC0Qx3eq9Wf6' #: String
|
|
16
16
|
|
|
17
17
|
# @rbs title: String
|
|
18
18
|
# @rbs scalar_url: String
|
data/lib/skyward/api/openapi.rb
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require
|
|
3
|
+
require 'json'
|
|
4
4
|
|
|
5
5
|
module Skyward::API::OpenAPI
|
|
6
6
|
extend self
|
|
@@ -15,47 +15,47 @@ module Skyward::API::OpenAPI
|
|
|
15
15
|
:schema #: schema?
|
|
16
16
|
)
|
|
17
17
|
|
|
18
|
-
PATH =
|
|
18
|
+
PATH = '/openapi.json' #: String
|
|
19
19
|
|
|
20
20
|
SERVED = {
|
|
21
|
-
PATH =>
|
|
22
|
-
Docs::PATH =>
|
|
21
|
+
PATH => 'the OpenAPI document',
|
|
22
|
+
Docs::PATH => 'the API reference'
|
|
23
23
|
}.freeze #: Hash[String, String]
|
|
24
24
|
|
|
25
25
|
# OpenAPI's pattern for the name of a component.
|
|
26
26
|
NAME = /\A[A-Za-z0-9._-]+\z/ #: Regexp
|
|
27
27
|
|
|
28
|
-
JSON_TYPE =
|
|
28
|
+
JSON_TYPE = 'application/json' #: String
|
|
29
29
|
|
|
30
|
-
PROBLEM_TYPE =
|
|
30
|
+
PROBLEM_TYPE = 'application/problem+json' #: String
|
|
31
31
|
|
|
32
32
|
SCALARS = {
|
|
33
|
-
integer: { type:
|
|
34
|
-
float: { type:
|
|
35
|
-
string: { type:
|
|
36
|
-
bool: { type:
|
|
37
|
-
date: { type:
|
|
38
|
-
time: { type:
|
|
39
|
-
decimal: { type:
|
|
33
|
+
integer: { type: 'integer' },
|
|
34
|
+
float: { type: 'number' },
|
|
35
|
+
string: { type: 'string' },
|
|
36
|
+
bool: { type: 'boolean' },
|
|
37
|
+
date: { type: 'string', format: 'date' },
|
|
38
|
+
time: { type: 'string', format: 'date-time' },
|
|
39
|
+
decimal: { type: 'string', format: 'decimal' },
|
|
40
40
|
untyped: {} #: schema
|
|
41
41
|
}.freeze #: Hash[Skyward::Core::Types::scalar, schema]
|
|
42
42
|
|
|
43
|
-
NULL = { type:
|
|
43
|
+
NULL = { type: 'null' }.freeze #: schema
|
|
44
44
|
|
|
45
|
-
PROBLEM = {
|
|
45
|
+
PROBLEM = { '$ref': '#/components/schemas/Problem' }.freeze #: schema
|
|
46
46
|
|
|
47
47
|
PROBLEM_SCHEMA = {
|
|
48
|
-
type:
|
|
48
|
+
type: 'object',
|
|
49
49
|
properties: {
|
|
50
|
-
type: { type:
|
|
51
|
-
title: { type:
|
|
52
|
-
status: { type:
|
|
53
|
-
detail: { type:
|
|
50
|
+
type: { type: 'string' },
|
|
51
|
+
title: { type: 'string' },
|
|
52
|
+
status: { type: 'integer' },
|
|
53
|
+
detail: { type: 'string' },
|
|
54
54
|
errors: {
|
|
55
|
-
type:
|
|
55
|
+
type: 'array',
|
|
56
56
|
items: {
|
|
57
|
-
type:
|
|
58
|
-
properties: { at: { type:
|
|
57
|
+
type: 'object',
|
|
58
|
+
properties: { at: { type: 'string' }, message: { type: 'string' } },
|
|
59
59
|
required: %w[at message]
|
|
60
60
|
}
|
|
61
61
|
}
|
|
@@ -63,30 +63,30 @@ module Skyward::API::OpenAPI
|
|
|
63
63
|
required: %w[type title status]
|
|
64
64
|
}.freeze #: schema
|
|
65
65
|
|
|
66
|
-
NO_CONTENT = Answer.new(status:
|
|
66
|
+
NO_CONTENT = Answer.new(status: '204', media_type: nil, schema: nil) #: Answer
|
|
67
67
|
|
|
68
68
|
# A redirect's status is chosen when it's made, so it's documented as the range.
|
|
69
|
-
REDIRECT = Answer.new(status:
|
|
69
|
+
REDIRECT = Answer.new(status: '3XX', media_type: nil, schema: nil) #: Answer
|
|
70
70
|
|
|
71
|
-
RACK = Answer.new(status:
|
|
71
|
+
RACK = Answer.new(status: 'default', media_type: nil, schema: nil) #: Answer
|
|
72
72
|
|
|
73
|
-
INVALID = Answer.new(status:
|
|
73
|
+
INVALID = Answer.new(status: '422', media_type: PROBLEM_TYPE, schema: PROBLEM) #: Answer
|
|
74
74
|
|
|
75
75
|
REFUSED = [
|
|
76
|
-
Answer.new(status:
|
|
77
|
-
Answer.new(status:
|
|
76
|
+
Answer.new(status: '413', media_type: PROBLEM_TYPE, schema: PROBLEM),
|
|
77
|
+
Answer.new(status: '415', media_type: PROBLEM_TYPE, schema: PROBLEM)
|
|
78
78
|
].freeze #: Array[Answer]
|
|
79
79
|
|
|
80
|
-
LOCATION = { location: { schema: { type:
|
|
80
|
+
LOCATION = { location: { schema: { type: 'string' } } }.freeze #: Hash[Symbol, untyped]
|
|
81
81
|
|
|
82
82
|
DESCRIPTIONS = {
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
83
|
+
'200' => 'OK',
|
|
84
|
+
'201' => 'Created',
|
|
85
|
+
'204' => 'No Content',
|
|
86
|
+
'3XX' => 'Redirect',
|
|
87
87
|
**Skyward::API::Serializer::PROBLEMS.values.to_h { |problem| [problem.status.to_s, problem.title] },
|
|
88
88
|
**Skyward::API::Assembly::Dispatcher::INPUT_TITLES.transform_keys(&:to_s),
|
|
89
|
-
|
|
89
|
+
'default' => "A Rack response, whose status and content are known only when it's sent"
|
|
90
90
|
}.freeze #: Hash[String, String]
|
|
91
91
|
|
|
92
92
|
# Takes the document unbuilt, so a route or static file at /openapi.json or /docs is refused first.
|
|
@@ -183,7 +183,7 @@ module Skyward::API::OpenAPI
|
|
|
183
183
|
first = same.fetch(0)
|
|
184
184
|
second = same.fetch(1)
|
|
185
185
|
"#{second.label}\n Its OpenAPI operation ID, #{id}, is #{first.label}'s too, " \
|
|
186
|
-
|
|
186
|
+
'but each operation needs its own. With docs: true, route each method once, and mount each class once.'
|
|
187
187
|
end
|
|
188
188
|
|
|
189
189
|
# @rbs types: Array[Skyward::Core::Types::Named]
|
|
@@ -194,16 +194,16 @@ module Skyward::API::OpenAPI
|
|
|
194
194
|
invalid, name = chosen.find { |_, named| !NAME.match?(named) }
|
|
195
195
|
if invalid
|
|
196
196
|
return "#{invalid}'s schema name, #{name.inspect}, isn't one OpenAPI allows: use only letters, digits, " \
|
|
197
|
-
|
|
197
|
+
'., - and _. Name it with Skyward.app(schema_names:).'
|
|
198
198
|
end
|
|
199
199
|
|
|
200
|
-
taken = [[
|
|
200
|
+
taken = [['Problem', "Skyward API's problem+json"], *chosen.map { |klass, named| [named, klass.to_s] }]
|
|
201
201
|
same = taken.group_by(&:first).values.find { |shared| shared.size > 1 }
|
|
202
202
|
return chosen unless same
|
|
203
203
|
|
|
204
204
|
(name, first), (_, second) = same.fetch(0), same.fetch(1)
|
|
205
205
|
"#{first} and #{second} both have the schema name #{name}, but each schema needs its own. " \
|
|
206
|
-
|
|
206
|
+
'Name one of them with Skyward.app(schema_names:).'
|
|
207
207
|
end
|
|
208
208
|
|
|
209
209
|
# @rbs endpoints: Array[Skyward::API::Routing::Router::Endpoint]
|
|
@@ -213,7 +213,7 @@ module Skyward::API::OpenAPI
|
|
|
213
213
|
# @rbs return: schema
|
|
214
214
|
def assembled(endpoints, types, info, names)
|
|
215
215
|
{
|
|
216
|
-
openapi:
|
|
216
|
+
openapi: '3.1.0',
|
|
217
217
|
info:,
|
|
218
218
|
tags: endpoints.uniq { |endpoint| endpoint.api.class }.map do |endpoint|
|
|
219
219
|
{ name: tag(endpoint), description: endpoint.api.class.class_description }.compact
|
|
@@ -288,10 +288,10 @@ module Skyward::API::OpenAPI
|
|
|
288
288
|
def answer(member, names)
|
|
289
289
|
case member
|
|
290
290
|
when Skyward::API::Routing::Router::Created
|
|
291
|
-
Answer.new(status:
|
|
291
|
+
Answer.new(status: '201', media_type: JSON_TYPE, schema: schema(member.type, :response, names))
|
|
292
292
|
when Skyward::API::Routing::Router::Response then response_value(member.klass)
|
|
293
293
|
when Skyward::API::Routing::Router::Rack then RACK
|
|
294
|
-
else Answer.new(status:
|
|
294
|
+
else Answer.new(status: '200', media_type: JSON_TYPE, schema: schema(member, :response, names))
|
|
295
295
|
end
|
|
296
296
|
end
|
|
297
297
|
|
|
@@ -329,7 +329,7 @@ module Skyward::API::OpenAPI
|
|
|
329
329
|
requested = endpoints.flat_map { |endpoint| described(endpoint).flat_map { |keyword| named(keyword.type) } }
|
|
330
330
|
types.to_h do |type|
|
|
331
331
|
[names.fetch(type.klass), object(type.fields, requested.include?(type) ? :request : :response, names)]
|
|
332
|
-
end.merge(
|
|
332
|
+
end.merge('Problem' => PROBLEM_SCHEMA).sort_by(&:first).to_h
|
|
333
333
|
end
|
|
334
334
|
|
|
335
335
|
# @rbs type: Skyward::Core::Types::t
|
|
@@ -340,12 +340,12 @@ module Skyward::API::OpenAPI
|
|
|
340
340
|
case type
|
|
341
341
|
when Skyward::Core::Types::Scalar then SCALARS.fetch(type.name)
|
|
342
342
|
when Skyward::Core::Types::Literal then type.value.nil? ? NULL : { const: type.value }
|
|
343
|
-
when Skyward::Core::Types::Enum then { type:
|
|
343
|
+
when Skyward::Core::Types::Enum then { type: 'string', enum: type.symbols.map(&:name) }
|
|
344
344
|
when Skyward::Core::Types::Nullable then nullable(schema(type.type, form, names))
|
|
345
|
-
when Skyward::Core::Types::ArrayOf then { type:
|
|
346
|
-
when Skyward::Core::Types::HashOf then { type:
|
|
345
|
+
when Skyward::Core::Types::ArrayOf then { type: 'array', items: schema(type.type, form, names) }
|
|
346
|
+
when Skyward::Core::Types::HashOf then { type: 'object', additionalProperties: schema(type.type, form, names) }
|
|
347
347
|
when Skyward::Core::Types::Record then object(type.fields, form, names)
|
|
348
|
-
when Skyward::Core::Types::Named then {
|
|
348
|
+
when Skyward::Core::Types::Named then { '$ref': "#/components/schemas/#{names.fetch(type.klass)}" }
|
|
349
349
|
when Skyward::Core::Types::Refined then schema(type.type, form, names)
|
|
350
350
|
else { oneOf: type.types.map { |member| schema(member, form, names) } }
|
|
351
351
|
end
|
|
@@ -355,7 +355,7 @@ module Skyward::API::OpenAPI
|
|
|
355
355
|
# @rbs return: schema
|
|
356
356
|
def nullable(schema)
|
|
357
357
|
type = schema[:type]
|
|
358
|
-
type.is_a?(String) && !schema.key?(:enum) ? schema.merge(type: [type,
|
|
358
|
+
type.is_a?(String) && !schema.key?(:enum) ? schema.merge(type: [type, 'null']) : { anyOf: [schema, NULL] }
|
|
359
359
|
end
|
|
360
360
|
|
|
361
361
|
# @rbs fields: Array[Skyward::Core::Types::Field]
|
|
@@ -364,7 +364,7 @@ module Skyward::API::OpenAPI
|
|
|
364
364
|
# @rbs return: schema
|
|
365
365
|
def object(fields, form, names)
|
|
366
366
|
{
|
|
367
|
-
type:
|
|
367
|
+
type: 'object',
|
|
368
368
|
properties: fields.to_h { |field| [field.name, schema(field.type, form, names)] },
|
|
369
369
|
required: fields.filter_map { |field| field.name if required?(field.required, field.type, form) }
|
|
370
370
|
}
|
|
@@ -379,7 +379,7 @@ module Skyward::API::OpenAPI
|
|
|
379
379
|
|
|
380
380
|
# @rbs pattern: Array[String | Symbol]
|
|
381
381
|
# @rbs return: String
|
|
382
|
-
def path(pattern) = pattern.map { |part| part.is_a?(Symbol) ? "{#{part}}" : part }.join(
|
|
382
|
+
def path(pattern) = pattern.map { |part| part.is_a?(Symbol) ? "{#{part}}" : part }.join('/')
|
|
383
383
|
|
|
384
384
|
# @rbs endpoint: Skyward::API::Routing::Router::Endpoint
|
|
385
385
|
# @rbs return: String
|
|
@@ -391,5 +391,5 @@ module Skyward::API::OpenAPI
|
|
|
391
391
|
|
|
392
392
|
# @rbs name: String
|
|
393
393
|
# @rbs return: String
|
|
394
|
-
def dotted(name) = name.gsub(
|
|
394
|
+
def dotted(name) = name.gsub('::', '.')
|
|
395
395
|
end
|
data/lib/skyward/api/request.rb
CHANGED
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
|
|
3
3
|
class Skyward::API::Request
|
|
4
4
|
# Rack passes these two headers without the HTTP_ prefix the others get.
|
|
5
|
-
UNPREFIXED = {
|
|
5
|
+
UNPREFIXED = { 'CONTENT_TYPE' => 'content-type', 'CONTENT_LENGTH' => 'content-length' }.freeze #: Hash[String, String]
|
|
6
6
|
|
|
7
|
-
OCTET =
|
|
7
|
+
OCTET = '(?:25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])' #: String
|
|
8
8
|
|
|
9
9
|
# Rack's trusted proxies: loopback, the private ranges and Unix sockets.
|
|
10
10
|
TRUSTED_PROXY = Regexp.union(
|
|
@@ -66,9 +66,9 @@ class Skyward::API::Request
|
|
|
66
66
|
def initialize(env)
|
|
67
67
|
@env = env
|
|
68
68
|
@headers = read_headers(env)
|
|
69
|
-
@cookies = read_cookies(env[
|
|
70
|
-
@ip = client_ip(addresses(env[
|
|
71
|
-
@request_id = env.fetch(
|
|
69
|
+
@cookies = read_cookies(env['HTTP_COOKIE'])
|
|
70
|
+
@ip = client_ip(addresses(env['REMOTE_ADDR']), addresses(env['HTTP_X_FORWARDED_FOR']).map { |entry| bare(entry) })
|
|
71
|
+
@request_id = env.fetch('skyward.request_id')
|
|
72
72
|
freeze
|
|
73
73
|
end
|
|
74
74
|
|
|
@@ -78,7 +78,7 @@ class Skyward::API::Request
|
|
|
78
78
|
# @rbs return: Hash[String, String]
|
|
79
79
|
def read_headers(env)
|
|
80
80
|
env.filter_map do |key, value|
|
|
81
|
-
name = UNPREFIXED[key] || (key.delete_prefix(
|
|
81
|
+
name = UNPREFIXED[key] || (key.delete_prefix('HTTP_').downcase.tr('_', '-') if key.start_with?('HTTP_'))
|
|
82
82
|
[name, value] if name
|
|
83
83
|
end.to_h.freeze
|
|
84
84
|
end
|
|
@@ -89,7 +89,7 @@ class Skyward::API::Request
|
|
|
89
89
|
# Split as bytes, because String#split raises on a String that isn't valid
|
|
90
90
|
# UTF-8, and a client can send one.
|
|
91
91
|
pairs = header.to_s.b.split(/; */).reject(&:empty?).map do |cookie|
|
|
92
|
-
name, value = cookie.split(
|
|
92
|
+
name, value = cookie.split('=', 2)
|
|
93
93
|
[String.new(name.to_s, encoding: Encoding::UTF_8), Skyward::API::Routing::PercentEncoding.decode(value.to_s)]
|
|
94
94
|
end
|
|
95
95
|
# The first of two cookies with one name wins, as in Rack.
|
|
@@ -104,7 +104,7 @@ class Skyward::API::Request
|
|
|
104
104
|
# @rbs entry: String
|
|
105
105
|
# @rbs return: String?
|
|
106
106
|
def bare(entry)
|
|
107
|
-
wrapped = !entry.start_with?(
|
|
107
|
+
wrapped = !entry.start_with?('[') && entry.count(':') > 1 ? "[#{entry}]" : entry
|
|
108
108
|
wrapped[AUTHORITY, :address]
|
|
109
109
|
end
|
|
110
110
|
|
|
@@ -10,6 +10,6 @@ module Skyward::API::Routing::PercentEncoding
|
|
|
10
10
|
# @rbs text: String
|
|
11
11
|
# @rbs return: String
|
|
12
12
|
def decode(text)
|
|
13
|
-
text.b.gsub(ESCAPE) { |escape| escape.delete_prefix(
|
|
13
|
+
text.b.gsub(ESCAPE) { |escape| escape.delete_prefix('%').hex.chr }.force_encoding(Encoding::UTF_8)
|
|
14
14
|
end
|
|
15
15
|
end
|
|
@@ -54,62 +54,103 @@ class Skyward::API::Routing::Router
|
|
|
54
54
|
:serializer #: Skyward::API::Serializer
|
|
55
55
|
)
|
|
56
56
|
|
|
57
|
+
Target = Data.define(
|
|
58
|
+
:index, #: Integer
|
|
59
|
+
:endpoint, #: Endpoint
|
|
60
|
+
:params #: Array[[Symbol, Integer]]
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
# First is the lowest index in or below the node, so a search can skip a node that can't beat what it has found.
|
|
64
|
+
Node = Data.define(
|
|
65
|
+
:literals, #: Hash[String, Node]
|
|
66
|
+
:param, #: Node?
|
|
67
|
+
:target, #: Target?
|
|
68
|
+
:first #: Integer
|
|
69
|
+
)
|
|
70
|
+
|
|
57
71
|
METHODS = %w[GET HEAD POST PUT PATCH DELETE OPTIONS].freeze #: Array[String]
|
|
58
72
|
|
|
59
|
-
# @rbs @
|
|
73
|
+
# @rbs @trees: Hash[String, Node]
|
|
60
74
|
|
|
61
75
|
# @rbs endpoints: Array[Endpoint]
|
|
62
76
|
# @rbs return: void
|
|
63
77
|
def initialize(endpoints)
|
|
64
|
-
|
|
78
|
+
targets = endpoints.each_with_index.map do |endpoint, index|
|
|
79
|
+
Target.new(index:, endpoint:, params: params_of(endpoint.pattern).freeze)
|
|
80
|
+
end
|
|
81
|
+
@trees = targets.group_by { |target| target.endpoint.verb }.transform_values { |group| build(group, 0) }.freeze
|
|
65
82
|
end
|
|
66
83
|
|
|
67
84
|
# @rbs verb: String
|
|
68
85
|
# @rbs path: String
|
|
69
86
|
# @rbs return: Match?
|
|
70
87
|
def match(verb, path)
|
|
71
|
-
segments = path.split(
|
|
72
|
-
|
|
73
|
-
|
|
88
|
+
segments = path.split('/', -1)
|
|
89
|
+
target = search(@trees.fetch(verb == 'HEAD' ? 'GET' : verb, nil), segments, 0, nil)
|
|
90
|
+
return unless target
|
|
91
|
+
|
|
92
|
+
endpoint = target.endpoint
|
|
93
|
+
params = target.params.to_h do |name, position|
|
|
94
|
+
[name, Skyward::API::Routing::PercentEncoding.decode(segments[position])]
|
|
95
|
+
end
|
|
96
|
+
Match.new(api: endpoint.api, to: endpoint.to, inputs: endpoint.inputs, params:, serializer: endpoint.serializer)
|
|
74
97
|
end
|
|
75
98
|
|
|
76
99
|
# @rbs path: String
|
|
77
100
|
# @rbs return: Array[String]
|
|
78
101
|
def allowed(path)
|
|
79
|
-
segments = path.split(
|
|
80
|
-
verbs = @
|
|
102
|
+
segments = path.split('/', -1)
|
|
103
|
+
verbs = @trees.filter_map { |verb, tree| verb if search(tree, segments, 0, nil) }
|
|
81
104
|
return [] if verbs.empty?
|
|
82
105
|
|
|
83
|
-
implied = verbs.include?(
|
|
106
|
+
implied = verbs.include?('GET') ? %w[HEAD OPTIONS] : %w[OPTIONS]
|
|
84
107
|
METHODS & (verbs + implied)
|
|
85
108
|
end
|
|
86
109
|
|
|
87
110
|
private
|
|
88
111
|
|
|
89
|
-
# @rbs
|
|
90
|
-
# @rbs
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
return unless endpoint.verb == verb
|
|
95
|
-
|
|
96
|
-
params = params_for(endpoint.pattern, segments)
|
|
97
|
-
return unless params
|
|
112
|
+
# @rbs pattern: Array[String | Symbol]
|
|
113
|
+
# @rbs return: Array[[Symbol, Integer]]
|
|
114
|
+
def params_of(pattern)
|
|
115
|
+
pattern.each_with_index.filter_map { |part, position| [part, position] if part.is_a?(Symbol) }
|
|
116
|
+
end
|
|
98
117
|
|
|
99
|
-
|
|
118
|
+
# @rbs targets: Array[Target]
|
|
119
|
+
# @rbs depth: Integer
|
|
120
|
+
# @rbs return: Node
|
|
121
|
+
def build(targets, depth)
|
|
122
|
+
ending, deeper = targets.partition { |target| target.endpoint.pattern.length == depth }
|
|
123
|
+
params, literals = deeper.partition { |target| target.endpoint.pattern.fetch(depth).is_a?(Symbol) }
|
|
124
|
+
by_segment = literals.group_by { |target| target.endpoint.pattern.fetch(depth).to_s }
|
|
125
|
+
# Targets come in index order, so a node's first target is its earliest.
|
|
126
|
+
Node.new(
|
|
127
|
+
literals: by_segment.transform_values { |group| build(group, depth + 1) }.freeze,
|
|
128
|
+
param: params.empty? ? nil : build(params, depth + 1),
|
|
129
|
+
target: ending.first,
|
|
130
|
+
first: targets.fetch(0).index
|
|
131
|
+
)
|
|
100
132
|
end
|
|
101
133
|
|
|
102
|
-
# @rbs
|
|
134
|
+
# @rbs node: Node?
|
|
103
135
|
# @rbs segments: Array[String]
|
|
104
|
-
# @rbs
|
|
105
|
-
|
|
106
|
-
|
|
136
|
+
# @rbs depth: Integer
|
|
137
|
+
# @rbs best: Target?
|
|
138
|
+
# @rbs return: Target?
|
|
139
|
+
def search(node, segments, depth, best)
|
|
140
|
+
return best unless node && (best.nil? || node.first < best.index)
|
|
141
|
+
return earlier(node.target, best) if depth == segments.length
|
|
142
|
+
|
|
143
|
+
segment = segments[depth]
|
|
144
|
+
found = search(node.literals[segment], segments, depth + 1, best)
|
|
145
|
+
segment.empty? ? found : search(node.param, segments, depth + 1, found)
|
|
146
|
+
end
|
|
107
147
|
|
|
108
|
-
|
|
109
|
-
|
|
148
|
+
# @rbs target: Target?
|
|
149
|
+
# @rbs best: Target?
|
|
150
|
+
# @rbs return: Target?
|
|
151
|
+
def earlier(target, best)
|
|
152
|
+
return best unless target && (best.nil? || target.index < best.index)
|
|
110
153
|
|
|
111
|
-
|
|
112
|
-
[part, Skyward::API::Routing::PercentEncoding.decode(segment)] if part.is_a?(Symbol)
|
|
113
|
-
end.to_h
|
|
154
|
+
target
|
|
114
155
|
end
|
|
115
156
|
end
|
|
@@ -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
|
|
@@ -39,7 +39,7 @@ module Skyward::API::Serializer::Writers
|
|
|
39
39
|
# @rbs members: Array[Writer]
|
|
40
40
|
# @rbs return: Writer
|
|
41
41
|
def union(members)
|
|
42
|
-
name = members.map(&:name).join(
|
|
42
|
+
name = members.map(&:name).join(' | ')
|
|
43
43
|
Writer.new(
|
|
44
44
|
name:,
|
|
45
45
|
fits: ->(value) { members.any? { |member| member.fits.call(value) } },
|
|
@@ -83,15 +83,15 @@ module Skyward::API::Serializer::Writers
|
|
|
83
83
|
# @rbs return: Writer
|
|
84
84
|
def scalar(name)
|
|
85
85
|
case name
|
|
86
|
-
when :integer then checked(
|
|
87
|
-
when :float then checked(
|
|
88
|
-
when :string then checked(
|
|
89
|
-
when :bool then checked(
|
|
90
|
-
when :date then checked(
|
|
91
|
-
when :time then checked(
|
|
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
92
|
when :decimal
|
|
93
|
-
checked(
|
|
94
|
-
when :untyped then Writer.new(name:
|
|
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) })
|
|
95
95
|
end
|
|
96
96
|
end
|
|
97
97
|
|
|
@@ -104,7 +104,7 @@ module Skyward::API::Serializer::Writers
|
|
|
104
104
|
# @rbs symbols: Array[Symbol]
|
|
105
105
|
# @rbs return: Writer
|
|
106
106
|
def enum(symbols)
|
|
107
|
-
checked(symbols.map(&:inspect).join(
|
|
107
|
+
checked(symbols.map(&:inspect).join(' | '), ->(value) { symbols.include?(value) }, ->(value, _) { value.name })
|
|
108
108
|
end
|
|
109
109
|
|
|
110
110
|
# @rbs type: Skyward::Core::Types::t
|
|
@@ -146,9 +146,9 @@ module Skyward::API::Serializer::Writers
|
|
|
146
146
|
# @rbs return: Writer
|
|
147
147
|
def record(fields)
|
|
148
148
|
writers = fields.map { |field| [field, writer(field.type)] } #: Array[[Skyward::Core::Types::Field, Writer]]
|
|
149
|
-
declared = writers.map { |field, writer| "#{
|
|
149
|
+
declared = writers.map { |field, writer| "#{'?' unless field.required}#{field.name}: #{writer.name}" }
|
|
150
150
|
checked(
|
|
151
|
-
"{ #{declared.join(
|
|
151
|
+
"{ #{declared.join(', ')} }",
|
|
152
152
|
->(value) { value.is_a?(Hash) },
|
|
153
153
|
->(value, at) { object(writers.filter_map { |field, writer| entry(field, writer, value, at) }) }
|
|
154
154
|
)
|
|
@@ -162,7 +162,7 @@ module Skyward::API::Serializer::Writers
|
|
|
162
162
|
def entry(field, writer, hash, at)
|
|
163
163
|
name = field.name
|
|
164
164
|
if hash.key?(name) then [name, writer.write.call(hash[name], "#{at}.#{name}")]
|
|
165
|
-
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')]
|
|
166
166
|
end
|
|
167
167
|
end
|
|
168
168
|
|
|
@@ -184,7 +184,7 @@ module Skyward::API::Serializer::Writers
|
|
|
184
184
|
# strftime rather than iso8601, which writes a DateTime with its time.
|
|
185
185
|
# @rbs value: Date
|
|
186
186
|
# @rbs return: String
|
|
187
|
-
def date(value) = value.strftime(
|
|
187
|
+
def date(value) = value.strftime('%Y-%m-%d')
|
|
188
188
|
|
|
189
189
|
# Six fractional digits, so a time read back from JSON equals the one written.
|
|
190
190
|
# @rbs value: Time
|
|
@@ -195,7 +195,7 @@ module Skyward::API::Serializer::Writers
|
|
|
195
195
|
# and "F" so it never has an exponent.
|
|
196
196
|
# @rbs value: BigDecimal
|
|
197
197
|
# @rbs return: String
|
|
198
|
-
def decimal(value) = value.to_s(
|
|
198
|
+
def decimal(value) = value.to_s('F')
|
|
199
199
|
|
|
200
200
|
# @rbs value: untyped
|
|
201
201
|
# @rbs at: String
|
|
@@ -206,10 +206,10 @@ module Skyward::API::Serializer::Writers
|
|
|
206
206
|
when Symbol then value.name
|
|
207
207
|
when Date then date(value)
|
|
208
208
|
when Time then time(value)
|
|
209
|
-
when BigDecimal then value.finite? ? decimal(value) : misfit(
|
|
209
|
+
when BigDecimal then value.finite? ? decimal(value) : misfit('BigDecimal', value, at)
|
|
210
210
|
when Hash then object(value.map { |key, item| [key, untyped(item, "#{at}.#{key}")] })
|
|
211
211
|
when Array then items(value.each_with_index.map { |item, index| untyped(item, "#{at}[#{index}]") })
|
|
212
|
-
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)
|
|
213
213
|
end
|
|
214
214
|
end
|
|
215
215
|
|