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,11 +1,11 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require
|
|
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 =>
|
|
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[
|
|
48
|
-
path = env[
|
|
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 ==
|
|
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, {
|
|
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,
|
|
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,
|
|
100
|
+
return problem(404, 'Not Found') if allowed.empty?
|
|
101
101
|
|
|
102
|
-
allow = {
|
|
103
|
-
verb ==
|
|
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:
|
|
113
|
-
json(status, {
|
|
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
|
|
@@ -1,32 +1,32 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require
|
|
3
|
+
require 'time'
|
|
4
4
|
|
|
5
5
|
class Skyward::API::Assembly::Static
|
|
6
6
|
TYPES = {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
7
|
+
'.avif' => 'image/avif',
|
|
8
|
+
'.css' => 'text/css',
|
|
9
|
+
'.csv' => 'text/csv',
|
|
10
|
+
'.gif' => 'image/gif',
|
|
11
|
+
'.htm' => 'text/html',
|
|
12
|
+
'.html' => 'text/html',
|
|
13
|
+
'.ico' => 'image/vnd.microsoft.icon',
|
|
14
|
+
'.jpeg' => 'image/jpeg',
|
|
15
|
+
'.jpg' => 'image/jpeg',
|
|
16
|
+
'.js' => 'text/javascript',
|
|
17
|
+
'.json' => 'application/json',
|
|
18
|
+
'.map' => 'application/json',
|
|
19
|
+
'.mjs' => 'text/javascript',
|
|
20
|
+
'.pdf' => 'application/pdf',
|
|
21
|
+
'.png' => 'image/png',
|
|
22
|
+
'.svg' => 'image/svg+xml',
|
|
23
|
+
'.txt' => 'text/plain',
|
|
24
|
+
'.wasm' => 'application/wasm',
|
|
25
|
+
'.webmanifest' => 'application/manifest+json',
|
|
26
|
+
'.webp' => 'image/webp',
|
|
27
|
+
'.woff' => 'font/woff',
|
|
28
|
+
'.woff2' => 'font/woff2',
|
|
29
|
+
'.xml' => 'application/xml'
|
|
30
30
|
}.freeze #: Hash[String, String]
|
|
31
31
|
|
|
32
32
|
# @rbs @statics: Array[Skyward::API::Assembly::Builder::Static]
|
|
@@ -34,15 +34,15 @@ class Skyward::API::Assembly::Static
|
|
|
34
34
|
# @rbs statics: Array[Skyward::API::Assembly::Builder::Static]
|
|
35
35
|
# @rbs return: void
|
|
36
36
|
def initialize(statics)
|
|
37
|
-
@statics = statics.map { |static| static.with(prefix: static.prefix.delete_suffix(
|
|
37
|
+
@statics = statics.map { |static| static.with(prefix: static.prefix.delete_suffix('/')) }
|
|
38
38
|
end
|
|
39
39
|
|
|
40
40
|
# @rbs env: Hash[String, untyped]
|
|
41
41
|
# @rbs return: Skyward::API::Assembly::Dispatcher::response?
|
|
42
42
|
def call(env)
|
|
43
|
-
return unless %w[GET HEAD].include?(env[
|
|
43
|
+
return unless %w[GET HEAD].include?(env['REQUEST_METHOD'])
|
|
44
44
|
|
|
45
|
-
file = file(env[
|
|
45
|
+
file = file(env['PATH_INFO'])
|
|
46
46
|
serve(file, env) if file
|
|
47
47
|
end
|
|
48
48
|
|
|
@@ -59,11 +59,11 @@ class Skyward::API::Assembly::Static
|
|
|
59
59
|
return unless path == static.prefix || path.start_with?("#{static.prefix}/")
|
|
60
60
|
|
|
61
61
|
rest = path.delete_prefix(static.prefix)
|
|
62
|
-
rest = "#{rest}index.html" if rest.empty? || rest.end_with?(
|
|
63
|
-
names = rest.split(
|
|
62
|
+
rest = "#{rest}index.html" if rest.empty? || rest.end_with?('/')
|
|
63
|
+
names = rest.split('/').map { |segment| Skyward::API::Routing::PercentEncoding.decode(segment) }
|
|
64
64
|
# Each segment is decoded after splitting, so an escaped / stays inside a
|
|
65
65
|
# name, where joining would make it a separator. File raises on a NUL.
|
|
66
|
-
return if names.any? { |name| name ==
|
|
66
|
+
return if names.any? { |name| name == '..' || name.include?('/') || name.include?("\0") }
|
|
67
67
|
|
|
68
68
|
# File.file? follows a symlink under from: whoever deployed it put it there.
|
|
69
69
|
file = File.join(static.from, *names)
|
|
@@ -76,11 +76,11 @@ class Skyward::API::Assembly::Static
|
|
|
76
76
|
def serve(file, env)
|
|
77
77
|
stat = File.stat(file)
|
|
78
78
|
etag = etag(stat)
|
|
79
|
-
validators = {
|
|
79
|
+
validators = { 'last-modified' => stat.mtime.httpdate, 'etag' => etag, 'cache-control' => 'no-cache' }
|
|
80
80
|
return [304, validators, []] if fresh?(env, etag, stat.mtime)
|
|
81
81
|
|
|
82
|
-
type = TYPES.fetch(File.extname(file).downcase(:ascii),
|
|
83
|
-
headers = {
|
|
82
|
+
type = TYPES.fetch(File.extname(file).downcase(:ascii), 'application/octet-stream')
|
|
83
|
+
headers = { 'content-type' => type, 'content-length' => stat.size.to_s, **validators }
|
|
84
84
|
[200, headers, [File.binread(file)]]
|
|
85
85
|
end
|
|
86
86
|
|
|
@@ -98,10 +98,10 @@ class Skyward::API::Assembly::Static
|
|
|
98
98
|
# @rbs modified: Time
|
|
99
99
|
# @rbs return: bool
|
|
100
100
|
def fresh?(env, etag, modified)
|
|
101
|
-
tags = env[
|
|
102
|
-
return tags.b.split(
|
|
101
|
+
tags = env['HTTP_IF_NONE_MATCH']
|
|
102
|
+
return tags.b.split(',').any? { |tag| ['*', etag].include?(tag.strip.delete_prefix('W/')) } if tags
|
|
103
103
|
|
|
104
|
-
since = http_date(env[
|
|
104
|
+
since = http_date(env['HTTP_IF_MODIFIED_SINCE'])
|
|
105
105
|
since ? since.to_i >= modified.to_i : false
|
|
106
106
|
end
|
|
107
107
|
|
|
@@ -3,9 +3,9 @@
|
|
|
3
3
|
module Skyward::API::CLI::AppFile
|
|
4
4
|
extend self
|
|
5
5
|
|
|
6
|
-
FILE =
|
|
6
|
+
FILE = 'app.rb' #: String
|
|
7
7
|
|
|
8
|
-
ASSIGNMENT =
|
|
8
|
+
ASSIGNMENT = 'App = Skyward.app { ... }' #: String
|
|
9
9
|
|
|
10
10
|
# @rbs (String directory, out: _Writer, kernel: singleton(Kernel), generate: ^() -> String?)
|
|
11
11
|
# { (Skyward::API::Assembly::Application) -> String } -> void
|
|
@@ -10,11 +10,11 @@ module Skyward::API::CLI::Check
|
|
|
10
10
|
:detail #: String
|
|
11
11
|
)
|
|
12
12
|
|
|
13
|
-
PASSED = Outcome.new(passed: true, detail:
|
|
13
|
+
PASSED = Outcome.new(passed: true, detail: '') #: Outcome
|
|
14
14
|
|
|
15
15
|
GENERATED = Skyward::Core::Signatures::Inline::OUTPUT #: String
|
|
16
16
|
|
|
17
|
-
STEEPFILE =
|
|
17
|
+
STEEPFILE = 'Steepfile' #: String
|
|
18
18
|
|
|
19
19
|
# The strict boot's own Ruby runs it, so the forced strict mode, the app's
|
|
20
20
|
# side effects and an exit or crash stay out of skyward check. The require
|
|
@@ -33,13 +33,13 @@ module Skyward::API::CLI::Check
|
|
|
33
33
|
# @rbs (String directory, out: _Writer, capture: capture, rbs_inline: String?, steep: String?) -> Integer
|
|
34
34
|
def run(directory, out:, capture:, rbs_inline:, steep:)
|
|
35
35
|
checks = {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
36
|
+
'Signatures' => -> { signatures(directory, capture:, rbs_inline:) },
|
|
37
|
+
'Strict boot' => -> { strict_boot(directory, capture:) },
|
|
38
|
+
'Steep' => -> { type_check(directory, capture:, steep:) }
|
|
39
39
|
}
|
|
40
40
|
passed = checks.map do |name, check|
|
|
41
41
|
outcome = check.call
|
|
42
|
-
out.write("#{name}: #{outcome.passed ?
|
|
42
|
+
out.write("#{name}: #{outcome.passed ? 'passed' : 'failed'}\n", outcome.detail)
|
|
43
43
|
outcome.passed
|
|
44
44
|
end
|
|
45
45
|
passed.all? ? 0 : 1
|
|
@@ -54,7 +54,7 @@ module Skyward::API::CLI::Check
|
|
|
54
54
|
|
|
55
55
|
inline = Skyward::Core::Signatures::Inline
|
|
56
56
|
paths = inline.sources(directory)
|
|
57
|
-
generate = "bundle exec rbs-inline --output=#{GENERATED} #{paths.join(
|
|
57
|
+
generate = "bundle exec rbs-inline --output=#{GENERATED} #{paths.join(' ')}"
|
|
58
58
|
generated = File.join(directory, GENERATED)
|
|
59
59
|
unless File.directory?(generated)
|
|
60
60
|
return failed("There's no #{GENERATED}. Run `#{generate}` and commit the result.\n")
|
|
@@ -81,7 +81,7 @@ module Skyward::API::CLI::Check
|
|
|
81
81
|
# @rbs capture: capture
|
|
82
82
|
# @rbs return: Outcome
|
|
83
83
|
def strict_boot(directory, capture:)
|
|
84
|
-
output, ok = capture.call({
|
|
84
|
+
output, ok = capture.call({ 'SKYWARD_STRICT' => '1' }, [Gem.ruby, '-e', BOOT], directory)
|
|
85
85
|
Outcome.new(passed: ok, detail: output)
|
|
86
86
|
end
|
|
87
87
|
|
|
@@ -100,7 +100,7 @@ module Skyward::API::CLI::Check
|
|
|
100
100
|
|
|
101
101
|
# Steep otherwise checks through any language server it finds running,
|
|
102
102
|
# which can report stale results.
|
|
103
|
-
output, ok = capture.call({}, [Gem.ruby, steep,
|
|
103
|
+
output, ok = capture.call({}, [Gem.ruby, steep, 'check', '--no-daemon'], directory)
|
|
104
104
|
Outcome.new(passed: ok, detail: output)
|
|
105
105
|
end
|
|
106
106
|
|
|
@@ -9,7 +9,7 @@ module Skyward::API::CLI::Routes
|
|
|
9
9
|
# @rbs return: String
|
|
10
10
|
def table(endpoints)
|
|
11
11
|
rows = endpoints.map do |endpoint|
|
|
12
|
-
[endpoint.verb, path(endpoint.pattern), handler(endpoint), endpoint.signature.gsub(/\s*\R\s*/,
|
|
12
|
+
[endpoint.verb, path(endpoint.pattern), handler(endpoint), endpoint.signature.gsub(/\s*\R\s*/, ' ')]
|
|
13
13
|
end
|
|
14
14
|
widths = rows.transpose.map { |column| (column.map(&:length).max || 0) + GAP }
|
|
15
15
|
rows.map do |row|
|
|
@@ -22,7 +22,7 @@ module Skyward::API::CLI::Routes
|
|
|
22
22
|
|
|
23
23
|
# @rbs pattern: Array[String | Symbol]
|
|
24
24
|
# @rbs return: String
|
|
25
|
-
def path(pattern) = pattern.map { |part| part.is_a?(Symbol) ? ":#{part}" : part }.join(
|
|
25
|
+
def path(pattern) = pattern.map { |part| part.is_a?(Symbol) ? ":#{part}" : part }.join('/')
|
|
26
26
|
|
|
27
27
|
# @rbs endpoint: Skyward::API::Routing::Router::Endpoint
|
|
28
28
|
# @rbs return: String
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require
|
|
3
|
+
require 'json'
|
|
4
4
|
|
|
5
5
|
class Skyward::API::CLI::Serve::Reloader
|
|
6
6
|
INTERVAL = 0.5 #: Float
|
|
@@ -82,7 +82,7 @@ class Skyward::API::CLI::Serve::Reloader
|
|
|
82
82
|
# @rbs files: Hash[String, Time]
|
|
83
83
|
# @rbs return: String?
|
|
84
84
|
def generate(files)
|
|
85
|
-
sources = files.select { |file, _time| file.end_with?(
|
|
85
|
+
sources = files.select { |file, _time| file.end_with?('.rb') }
|
|
86
86
|
return @failure if sources == @sources
|
|
87
87
|
|
|
88
88
|
@failure = @generate.call
|
|
@@ -105,14 +105,14 @@ class Skyward::API::CLI::Serve::Reloader
|
|
|
105
105
|
# A message can quote a source line's bytes, and JSON.generate raises on invalid UTF-8.
|
|
106
106
|
message = String.new(error.message, encoding: Encoding::UTF_8).scrub
|
|
107
107
|
problem = {
|
|
108
|
-
type:
|
|
109
|
-
title:
|
|
108
|
+
type: 'about:blank',
|
|
109
|
+
title: 'Internal Server Error',
|
|
110
110
|
status: 500,
|
|
111
111
|
detail: "#{error.class}: #{message}",
|
|
112
112
|
# rbs-inline's failure is never raised, so has no backtrace.
|
|
113
113
|
backtrace: error.backtrace || []
|
|
114
114
|
}
|
|
115
|
-
body = env[
|
|
116
|
-
[500, {
|
|
115
|
+
body = env['REQUEST_METHOD'] == 'HEAD' ? [] : [JSON.generate(problem)] #: Array[String]
|
|
116
|
+
[500, { 'content-type' => 'application/problem+json' }, body]
|
|
117
117
|
end
|
|
118
118
|
end
|
|
@@ -5,7 +5,7 @@ module Skyward::API::CLI::Serve
|
|
|
5
5
|
|
|
6
6
|
RACKUP = "#{__dir__}/serve.ru".freeze #: String
|
|
7
7
|
|
|
8
|
-
CONFIG =
|
|
8
|
+
CONFIG = 'config.ru' #: String
|
|
9
9
|
|
|
10
10
|
# Neither watched nor reloaded, and nor is any hidden directory: the app's
|
|
11
11
|
# gems can be installed under vendor/ or .bundle/.
|
|
@@ -26,20 +26,20 @@ module Skyward::API::CLI::Serve
|
|
|
26
26
|
unless falcon
|
|
27
27
|
return kernel.abort(
|
|
28
28
|
"skyward: Falcon isn't installed. Add `gem \"falcon\"` to the Gemfile and run `bundle install`, " \
|
|
29
|
-
|
|
29
|
+
'or run config.ru under any Rack server, such as Puma.'
|
|
30
30
|
)
|
|
31
31
|
end
|
|
32
32
|
|
|
33
33
|
kernel.exec(
|
|
34
|
-
{
|
|
34
|
+
{ 'RACK_ENV' => env.fetch('RACK_ENV', 'development') },
|
|
35
35
|
Gem.ruby,
|
|
36
36
|
falcon,
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
"http://localhost:#{env.fetch(
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
37
|
+
'serve',
|
|
38
|
+
'--bind',
|
|
39
|
+
"http://localhost:#{env.fetch('PORT', '9292')}",
|
|
40
|
+
'--count',
|
|
41
|
+
'1',
|
|
42
|
+
'--config',
|
|
43
43
|
RACKUP,
|
|
44
44
|
chdir: directory
|
|
45
45
|
)
|
|
@@ -73,7 +73,7 @@ module Skyward::API::CLI::Serve
|
|
|
73
73
|
# @rbs directory: String
|
|
74
74
|
# @rbs return: Hash[String, Time]
|
|
75
75
|
def watch(directory)
|
|
76
|
-
roots = Dir.glob(
|
|
76
|
+
roots = Dir.glob('*/', base: directory).map { |root| root.delete_suffix('/') }.reject { |root| ignored?(root) }
|
|
77
77
|
files = Dir.glob([WATCHED, *roots.map { |root| "#{root}/**/#{WATCHED}" }], base: directory)
|
|
78
78
|
files.to_h { |file| [file, File.mtime(File.join(directory, file))] }
|
|
79
79
|
end
|
|
@@ -93,8 +93,8 @@ module Skyward::API::CLI::Serve
|
|
|
93
93
|
stuck = app_loaders.find { |loader| !loader.reloading_enabled? }
|
|
94
94
|
if stuck
|
|
95
95
|
raise Skyward::Core::BootError,
|
|
96
|
-
"skyward serve can't reload the Zeitwerk loader for #{stuck.dirs.join(
|
|
97
|
-
|
|
96
|
+
"skyward serve can't reload the Zeitwerk loader for #{stuck.dirs.join(', ')}. Call " \
|
|
97
|
+
'`loader.enable_reloading` before `loader.setup` in development, then restart skyward serve.'
|
|
98
98
|
end
|
|
99
99
|
|
|
100
100
|
apis.select { |api| reloaded_api?(directory, api) }.each(&:clear_declarations)
|
|
@@ -120,7 +120,7 @@ module Skyward::API::CLI::Serve
|
|
|
120
120
|
def reloaded_api?(directory, api)
|
|
121
121
|
name = api.name
|
|
122
122
|
# const_source_location refuses the name of a class under an anonymous module.
|
|
123
|
-
return false if name.nil? || name.start_with?(
|
|
123
|
+
return false if name.nil? || name.start_with?('#')
|
|
124
124
|
# A class left over from an earlier load keeps its name after its namespace
|
|
125
125
|
# is gone, and const_source_location raises for a missing namespace.
|
|
126
126
|
return false unless Object.const_defined?(name)
|
|
@@ -135,10 +135,10 @@ module Skyward::API::CLI::Serve
|
|
|
135
135
|
def reloaded?(directory, path)
|
|
136
136
|
return false unless path == directory || path.start_with?("#{directory}/")
|
|
137
137
|
|
|
138
|
-
!ignored?(path.delete_prefix(directory).delete_prefix(
|
|
138
|
+
!ignored?(path.delete_prefix(directory).delete_prefix('/').partition('/').first)
|
|
139
139
|
end
|
|
140
140
|
|
|
141
141
|
# @rbs root: String
|
|
142
142
|
# @rbs return: bool
|
|
143
|
-
def ignored?(root) = root.start_with?(
|
|
143
|
+
def ignored?(root) = root.start_with?('.') || IGNORED.include?(root)
|
|
144
144
|
end
|
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
|
|
3
3
|
# skyward serve's composition root. Rack is reached for only here: Skyward API
|
|
4
4
|
# doesn't depend on it, and Falcon brings it.
|
|
5
|
-
require
|
|
5
|
+
require 'skyward/api'
|
|
6
6
|
|
|
7
|
-
rbs_inline = Skyward::Core::CLI::Executable.find(
|
|
7
|
+
rbs_inline = Skyward::Core::CLI::Executable.find('rbs-inline', feature: 'rbs/inline')
|
|
8
8
|
|
|
9
9
|
capture = Skyward::Core::CLI::Executable::CAPTURE
|
|
10
10
|
generate = rbs_inline && -> { Skyward::Core::Signatures::Inline.generate(Dir.pwd, rbs_inline:, capture:) }
|
data/lib/skyward/api/commands.rb
CHANGED
|
@@ -1,44 +1,44 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require
|
|
4
|
-
require
|
|
3
|
+
require 'json'
|
|
4
|
+
require 'skyward/api'
|
|
5
5
|
|
|
6
6
|
generate = lambda do
|
|
7
7
|
Skyward::Core::Signatures::Inline.generate(
|
|
8
8
|
Dir.pwd,
|
|
9
|
-
rbs_inline: Skyward::Core::CLI::Executable.find(
|
|
9
|
+
rbs_inline: Skyward::Core::CLI::Executable.find('rbs-inline', feature: 'rbs/inline'),
|
|
10
10
|
capture: Skyward::Core::CLI::Executable::CAPTURE
|
|
11
11
|
)
|
|
12
12
|
end
|
|
13
13
|
|
|
14
|
-
Skyward::Core::CLI.register(
|
|
14
|
+
Skyward::Core::CLI.register('serve', 'Serve config.ru with Falcon in development, on PORT (default 9292)') do |_args|
|
|
15
15
|
Skyward::API::CLI::Serve.run(
|
|
16
16
|
Dir.pwd,
|
|
17
17
|
env: ENV.to_h,
|
|
18
|
-
falcon: Skyward::Core::CLI::Executable.find(
|
|
18
|
+
falcon: Skyward::Core::CLI::Executable.find('falcon'),
|
|
19
19
|
kernel: Kernel
|
|
20
20
|
)
|
|
21
21
|
end
|
|
22
22
|
|
|
23
|
-
Skyward::Core::CLI.register(
|
|
23
|
+
Skyward::Core::CLI.register('check', 'Check signatures, boot app.rb in strict mode and run Steep') do |_args|
|
|
24
24
|
# So CI shows each check as it finishes, not all of them at exit.
|
|
25
25
|
$stdout.sync = true
|
|
26
26
|
exit Skyward::API::CLI::Check.run(
|
|
27
27
|
Dir.pwd,
|
|
28
28
|
out: $stdout,
|
|
29
29
|
capture: Skyward::Core::CLI::Executable::CAPTURE,
|
|
30
|
-
rbs_inline: Skyward::Core::CLI::Executable.find(
|
|
31
|
-
steep: Skyward::Core::CLI::Executable.find(
|
|
30
|
+
rbs_inline: Skyward::Core::CLI::Executable.find('rbs-inline', feature: 'rbs/inline'),
|
|
31
|
+
steep: Skyward::Core::CLI::Executable.find('steep')
|
|
32
32
|
)
|
|
33
33
|
end
|
|
34
34
|
|
|
35
|
-
Skyward::Core::CLI.register(
|
|
35
|
+
Skyward::Core::CLI.register('routes', 'Print every route with its handler and signature') do |_args|
|
|
36
36
|
Skyward::API::CLI::AppFile.run(Dir.pwd, out: $stdout, kernel: Kernel, generate:) do |app|
|
|
37
37
|
Skyward::API::CLI::Routes.table(app.endpoints)
|
|
38
38
|
end
|
|
39
39
|
end
|
|
40
40
|
|
|
41
|
-
Skyward::Core::CLI.register(
|
|
41
|
+
Skyward::Core::CLI.register('openapi', 'Write the OpenAPI document to stdout') do |_args|
|
|
42
42
|
Skyward::API::CLI::AppFile.run(Dir.pwd, out: $stdout, kernel: Kernel, generate:) do |app|
|
|
43
43
|
"#{JSON.pretty_generate(app.openapi)}\n"
|
|
44
44
|
end
|
|
@@ -114,9 +114,9 @@ module Skyward::API::Endpoints::Declarations
|
|
|
114
114
|
# @rbs to: untyped
|
|
115
115
|
# @rbs return: String?
|
|
116
116
|
def problem_with(path, to)
|
|
117
|
-
return
|
|
117
|
+
return 'the path must be a String that starts with /' unless path.is_a?(String) && path.start_with?('/')
|
|
118
118
|
|
|
119
|
-
names = path.split(
|
|
119
|
+
names = path.split('/').filter_map { |segment| segment.delete_prefix(':') if segment.start_with?(':') }
|
|
120
120
|
invalid = names.find { |name| !PARAMETER_NAME.match?(name) }
|
|
121
121
|
return "path parameter #{invalid.inspect} isn't a valid keyword name" if invalid
|
|
122
122
|
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require
|
|
4
|
-
require
|
|
3
|
+
require 'date'
|
|
4
|
+
require 'json'
|
|
5
5
|
|
|
6
6
|
module Skyward::API::Inputs::Coercer
|
|
7
7
|
extend self
|
|
@@ -36,7 +36,7 @@ module Skyward::API::Inputs::Coercer
|
|
|
36
36
|
|
|
37
37
|
# Stands for every problem a union's member finds, since the union reports
|
|
38
38
|
# none of them.
|
|
39
|
-
MISFIT = Invalid.new(errors: [Error.new(at:
|
|
39
|
+
MISFIT = Invalid.new(errors: [Error.new(at: '', message: "doesn't fit")]) #: Invalid
|
|
40
40
|
|
|
41
41
|
# INTEGER, DECIMAL, DATE and TIME match a String's bytes, because a path
|
|
42
42
|
# parameter can be any bytes, and Regexp#match raises on a String that isn't
|
|
@@ -47,7 +47,7 @@ module Skyward::API::Inputs::Coercer
|
|
|
47
47
|
# an exponent it's a billion digits long.
|
|
48
48
|
DECIMAL = /\A-?\d+(?:\.\d+)?\z/ #: Regexp
|
|
49
49
|
|
|
50
|
-
BOOLEANS = {
|
|
50
|
+
BOOLEANS = { 'true' => true, 'false' => false, '1' => true, '0' => false }.freeze #: Hash[String, bool]
|
|
51
51
|
|
|
52
52
|
DATE = /\A(\d{4})-(\d{2})-(\d{2})\z/ #: Regexp
|
|
53
53
|
|
|
@@ -59,19 +59,19 @@ module Skyward::API::Inputs::Coercer
|
|
|
59
59
|
/x #: Regexp
|
|
60
60
|
|
|
61
61
|
NAMES = {
|
|
62
|
-
integer:
|
|
63
|
-
float:
|
|
64
|
-
string:
|
|
65
|
-
bool:
|
|
66
|
-
date:
|
|
67
|
-
time:
|
|
68
|
-
decimal:
|
|
69
|
-
untyped:
|
|
62
|
+
integer: 'Integer',
|
|
63
|
+
float: 'Float',
|
|
64
|
+
string: 'String',
|
|
65
|
+
bool: 'bool',
|
|
66
|
+
date: 'Date',
|
|
67
|
+
time: 'Time',
|
|
68
|
+
decimal: 'BigDecimal',
|
|
69
|
+
untyped: 'untyped'
|
|
70
70
|
}.freeze #: Hash[Skyward::Core::Types::scalar, String]
|
|
71
71
|
|
|
72
72
|
FORMATS = {
|
|
73
|
-
date:
|
|
74
|
-
time:
|
|
73
|
+
date: 'Date (YYYY-MM-DD)',
|
|
74
|
+
time: 'Time (ISO 8601)',
|
|
75
75
|
decimal: 'BigDecimal (a decimal string, such as "12.50")'
|
|
76
76
|
}.freeze #: Hash[Skyward::Core::Types::scalar, String]
|
|
77
77
|
|
|
@@ -99,7 +99,7 @@ module Skyward::API::Inputs::Coercer
|
|
|
99
99
|
return unless required
|
|
100
100
|
return Ok.new(value: nil) if type.is_a?(Skyward::Core::Types::Nullable)
|
|
101
101
|
|
|
102
|
-
Invalid.new(errors: [Error.new(at:, message:
|
|
102
|
+
Invalid.new(errors: [Error.new(at:, message: 'is required')])
|
|
103
103
|
end
|
|
104
104
|
|
|
105
105
|
# @rbs results: Hash[Symbol, result]
|
|
@@ -435,16 +435,16 @@ module Skyward::API::Inputs::Coercer
|
|
|
435
435
|
case type
|
|
436
436
|
when Skyward::Core::Types::Scalar then NAMES.fetch(type.name)
|
|
437
437
|
when Skyward::Core::Types::Literal then type.value.inspect
|
|
438
|
-
when Skyward::Core::Types::Enum then type.symbols.map(&:inspect).join(
|
|
438
|
+
when Skyward::Core::Types::Enum then type.symbols.map(&:inspect).join(' | ')
|
|
439
439
|
when Skyward::Core::Types::Nullable then nullable_written(type.type)
|
|
440
440
|
when Skyward::Core::Types::ArrayOf then "Array[#{written(type.type)}]"
|
|
441
441
|
when Skyward::Core::Types::HashOf then "Hash[String, #{written(type.type)}]"
|
|
442
442
|
when Skyward::Core::Types::Record
|
|
443
|
-
fields = type.fields.map { |field| "#{
|
|
444
|
-
"{ #{fields.join(
|
|
443
|
+
fields = type.fields.map { |field| "#{'?' unless field.required}#{field.name}: #{written(field.type)}" }
|
|
444
|
+
"{ #{fields.join(', ')} }"
|
|
445
445
|
when Skyward::Core::Types::Named then type.name
|
|
446
446
|
when Skyward::Core::Types::Refined then written(type.type)
|
|
447
|
-
else type.types.map { |member| written(member) }.join(
|
|
447
|
+
else type.types.map { |member| written(member) }.join(' | ')
|
|
448
448
|
end
|
|
449
449
|
end
|
|
450
450
|
|
data/lib/skyward/api/inputs.rb
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require
|
|
4
|
-
require
|
|
3
|
+
require 'json'
|
|
4
|
+
require 'uri'
|
|
5
5
|
|
|
6
6
|
class Skyward::API::Inputs
|
|
7
7
|
# @rbs!
|
|
@@ -11,11 +11,11 @@ class Skyward::API::Inputs
|
|
|
11
11
|
JSON_TYPE = %r{\A(?:application/json|[^/;\s]+/[^/;\s]+\+json)\s*(?:;|\z)}i #: Regexp
|
|
12
12
|
|
|
13
13
|
MALFORMED_QUERY = Coercer::Invalid.new(
|
|
14
|
-
errors: [Coercer::Error.new(at:
|
|
14
|
+
errors: [Coercer::Error.new(at: 'query', message: 'is not a valid query string')]
|
|
15
15
|
) #: Coercer::Invalid
|
|
16
16
|
|
|
17
17
|
INVALID_JSON = Coercer::Invalid.new(
|
|
18
|
-
errors: [Coercer::Error.new(at:
|
|
18
|
+
errors: [Coercer::Error.new(at: 'body', message: 'is not valid JSON')]
|
|
19
19
|
) #: Coercer::Invalid
|
|
20
20
|
|
|
21
21
|
# @rbs @keywords: Array[[Skyward::API::Routing::Router::Keyword, Coercer::t]]
|
|
@@ -52,7 +52,7 @@ class Skyward::API::Inputs
|
|
|
52
52
|
body = body(env) if @body
|
|
53
53
|
case body
|
|
54
54
|
when Symbol then body
|
|
55
|
-
else arguments(params, query(env[
|
|
55
|
+
else arguments(params, query(env['QUERY_STRING'].to_s), body, given)
|
|
56
56
|
end
|
|
57
57
|
end
|
|
58
58
|
|
|
@@ -100,7 +100,7 @@ class Skyward::API::Inputs
|
|
|
100
100
|
# @rbs return: [Symbol, Coercer::result]?
|
|
101
101
|
def argument(keyword, coercer, params, query, body)
|
|
102
102
|
name = keyword.name
|
|
103
|
-
at = keyword.source == :body ?
|
|
103
|
+
at = keyword.source == :body ? 'body' : "#{keyword.source}.#{name}"
|
|
104
104
|
raw =
|
|
105
105
|
case keyword.source
|
|
106
106
|
when :path then Coercer::Ok.new(value: params.fetch(name))
|
|
@@ -140,13 +140,13 @@ class Skyward::API::Inputs
|
|
|
140
140
|
# @rbs env: Hash[String, untyped]
|
|
141
141
|
# @rbs return: body
|
|
142
142
|
def body(env)
|
|
143
|
-
return :content_too_large if env[
|
|
143
|
+
return :content_too_large if env['CONTENT_LENGTH'].to_i > @max_body_size
|
|
144
144
|
|
|
145
145
|
# One byte past the limit shows the body runs past it, without reading the rest.
|
|
146
|
-
text = env[
|
|
146
|
+
text = env['rack.input']&.read(@max_body_size + 1).to_s
|
|
147
147
|
return :content_too_large if text.bytesize > @max_body_size
|
|
148
148
|
return if text.empty?
|
|
149
|
-
return :unsupported_media_type unless JSON_TYPE.match?(env[
|
|
149
|
+
return :unsupported_media_type unless JSON_TYPE.match?(env['CONTENT_TYPE'].to_s)
|
|
150
150
|
|
|
151
151
|
parse(String.new(text, encoding: Encoding::UTF_8))
|
|
152
152
|
end
|
|
@@ -17,10 +17,10 @@ class Skyward::API::Middleware::RequestId
|
|
|
17
17
|
# @rbs env: Hash[String, untyped]
|
|
18
18
|
# @rbs return: Skyward::API::rack_response
|
|
19
19
|
def call(env)
|
|
20
|
-
incoming = env[
|
|
20
|
+
incoming = env['HTTP_X_REQUEST_ID']
|
|
21
21
|
# Matched as binary, because Regexp#match? raises on a String that isn't valid UTF-8.
|
|
22
22
|
id = VALID.match?(incoming&.b) ? incoming : @generate.call
|
|
23
|
-
status, headers, body = @app.call(env.merge(
|
|
24
|
-
[status, headers.merge(
|
|
23
|
+
status, headers, body = @app.call(env.merge('skyward.request_id' => id))
|
|
24
|
+
[status, headers.merge('x-request-id' => id), body]
|
|
25
25
|
end
|
|
26
26
|
end
|