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.
@@ -1,9 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "json"
3
+ require 'json'
4
4
 
5
5
  class Skyward::API::Serializer
6
- JSON_TYPE = { "content-type" => "application/json" }.freeze #: Hash[String, String]
6
+ JSON_TYPE = { 'content-type' => 'application/json' }.freeze #: Hash[String, String]
7
7
 
8
8
  Problem = Data.define(
9
9
  :status, #: Integer
@@ -11,12 +11,12 @@ class Skyward::API::Serializer
11
11
  )
12
12
 
13
13
  PROBLEMS = {
14
- Skyward::API::Responses::BadRequest => Problem.new(status: 400, title: "Bad Request"),
15
- Skyward::API::Responses::Unauthorized => Problem.new(status: 401, title: "Unauthorized"),
16
- Skyward::API::Responses::Forbidden => Problem.new(status: 403, title: "Forbidden"),
17
- Skyward::API::Responses::NotFound => Problem.new(status: 404, title: "Not Found"),
18
- Skyward::API::Responses::Conflict => Problem.new(status: 409, title: "Conflict"),
19
- Skyward::API::Responses::Unprocessable => Problem.new(status: 422, title: "Unprocessable Content")
14
+ Skyward::API::Responses::BadRequest => Problem.new(status: 400, title: 'Bad Request'),
15
+ Skyward::API::Responses::Unauthorized => Problem.new(status: 401, title: 'Unauthorized'),
16
+ Skyward::API::Responses::Forbidden => Problem.new(status: 403, title: 'Forbidden'),
17
+ Skyward::API::Responses::NotFound => Problem.new(status: 404, title: 'Not Found'),
18
+ Skyward::API::Responses::Conflict => Problem.new(status: 409, title: 'Conflict'),
19
+ Skyward::API::Responses::Unprocessable => Problem.new(status: 422, title: 'Unprocessable Content')
20
20
  }.freeze #: Hash[Class, Problem]
21
21
 
22
22
  # Every response value but Skyward::API::Responses::Created, which carries a type of its own.
@@ -44,7 +44,7 @@ class Skyward::API::Serializer
44
44
  # @rbs value: untyped
45
45
  # @rbs return: Skyward::API::rack
46
46
  def call(value)
47
- response = @writer.write.call(value, "")
47
+ response = @writer.write.call(value, '')
48
48
  case response
49
49
  when Writers::Misfit then raise Skyward::API::ReturnTypeError, message(response)
50
50
  else response
@@ -70,7 +70,7 @@ class Skyward::API::Serializer
70
70
  def untyped
71
71
  writer = Writers.writer(UNTYPED)
72
72
  responses = [created(writer), *RESPONSES.map { |klass| response(klass) }, untyped_rack, ok(writer)]
73
- Writers.union(responses).with(name: "untyped")
73
+ Writers.union(responses).with(name: 'untyped')
74
74
  end
75
75
 
76
76
  # @rbs writer: Writers::Writer
@@ -97,7 +97,7 @@ class Skyward::API::Serializer
97
97
  problem = PROBLEMS.fetch(klass, nil)
98
98
  answer =
99
99
  if problem then ->(given, _) { problem_response(problem, given) }
100
- elsif klass == Skyward::API::Responses::Redirect then ->(given, _) { [given.status, { "location" => given.to, **given.headers }, []] }
100
+ elsif klass == Skyward::API::Responses::Redirect then ->(given, _) { [given.status, { 'location' => given.to, **given.headers }, []] }
101
101
  else ->(given, _) { [204, given.headers.dup, []] }
102
102
  end #: ^(untyped, String) -> Skyward::API::Assembly::Dispatcher::response
103
103
  Writers.checked(klass.name.to_s, ->(given) { given.is_a?(klass) }, answer)
@@ -117,7 +117,7 @@ class Skyward::API::Serializer
117
117
  # @rbs fits: ^(untyped) -> bool
118
118
  # @rbs return: Writers::Writer
119
119
  def rack_writer(fits)
120
- Writers.checked("Skyward::API::rack", fits, ->(given, _) { given })
120
+ Writers.checked('Skyward::API::rack', fits, ->(given, _) { given })
121
121
  end
122
122
 
123
123
  # @rbs given: untyped
@@ -134,8 +134,8 @@ class Skyward::API::Serializer
134
134
  # @rbs given: untyped
135
135
  # @rbs return: Skyward::API::Assembly::Dispatcher::response
136
136
  def problem_response(problem, given)
137
- body = { type: "about:blank", title: problem.title, status: problem.status, detail: given.detail }.compact
138
- [problem.status, { "content-type" => "application/problem+json", **given.headers }, [JSON.generate(body)]]
137
+ body = { type: 'about:blank', title: problem.title, status: problem.status, detail: given.detail }.compact
138
+ [problem.status, { 'content-type' => 'application/problem+json', **given.headers }, [JSON.generate(body)]]
139
139
  end
140
140
 
141
141
  # @rbs written: untyped
@@ -151,7 +151,7 @@ class Skyward::API::Serializer
151
151
  def message(misfit)
152
152
  return "#{@label} returned #{misfit.got}, which isn't #{misfit.expected}" if misfit.at.empty?
153
153
 
154
- "#{@label} returned #{misfit.got} at #{misfit.at.delete_prefix(".")}, which isn't #{misfit.expected}, " \
154
+ "#{@label} returned #{misfit.got} at #{misfit.at.delete_prefix('.')}, which isn't #{misfit.expected}, " \
155
155
  "in its return type #{@writer.name}"
156
156
  end
157
157
  end
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "json"
4
- require "stringio"
3
+ require 'json'
4
+ require 'stringio'
5
5
 
6
6
  class Skyward::API::Test::Client
7
7
  Response = Data.define(
@@ -30,7 +30,7 @@ class Skyward::API::Test::Client
30
30
  # @rbs cookies: Hash[String, String]
31
31
  # @rbs return: Response
32
32
  def get(path, json: nil, headers: {}, cookies: {})
33
- request("GET", path, json, headers, cookies)
33
+ request('GET', path, json, headers, cookies)
34
34
  end
35
35
 
36
36
  # @rbs path: String
@@ -39,7 +39,7 @@ class Skyward::API::Test::Client
39
39
  # @rbs cookies: Hash[String, String]
40
40
  # @rbs return: Response
41
41
  def post(path, json: nil, headers: {}, cookies: {})
42
- request("POST", path, json, headers, cookies)
42
+ request('POST', path, json, headers, cookies)
43
43
  end
44
44
 
45
45
  # @rbs path: String
@@ -48,7 +48,7 @@ class Skyward::API::Test::Client
48
48
  # @rbs cookies: Hash[String, String]
49
49
  # @rbs return: Response
50
50
  def put(path, json: nil, headers: {}, cookies: {})
51
- request("PUT", path, json, headers, cookies)
51
+ request('PUT', path, json, headers, cookies)
52
52
  end
53
53
 
54
54
  # @rbs path: String
@@ -57,7 +57,7 @@ class Skyward::API::Test::Client
57
57
  # @rbs cookies: Hash[String, String]
58
58
  # @rbs return: Response
59
59
  def patch(path, json: nil, headers: {}, cookies: {})
60
- request("PATCH", path, json, headers, cookies)
60
+ request('PATCH', path, json, headers, cookies)
61
61
  end
62
62
 
63
63
  # @rbs path: String
@@ -66,7 +66,7 @@ class Skyward::API::Test::Client
66
66
  # @rbs cookies: Hash[String, String]
67
67
  # @rbs return: Response
68
68
  def delete(path, json: nil, headers: {}, cookies: {})
69
- request("DELETE", path, json, headers, cookies)
69
+ request('DELETE', path, json, headers, cookies)
70
70
  end
71
71
 
72
72
  # @rbs path: String
@@ -75,7 +75,7 @@ class Skyward::API::Test::Client
75
75
  # @rbs cookies: Hash[String, String]
76
76
  # @rbs return: Response
77
77
  def head(path, json: nil, headers: {}, cookies: {})
78
- request("HEAD", path, json, headers, cookies)
78
+ request('HEAD', path, json, headers, cookies)
79
79
  end
80
80
 
81
81
  # @rbs path: String
@@ -84,7 +84,7 @@ class Skyward::API::Test::Client
84
84
  # @rbs cookies: Hash[String, String]
85
85
  # @rbs return: Response
86
86
  def options(path, json: nil, headers: {}, cookies: {})
87
- request("OPTIONS", path, json, headers, cookies)
87
+ request('OPTIONS', path, json, headers, cookies)
88
88
  end
89
89
 
90
90
  private
@@ -96,23 +96,23 @@ class Skyward::API::Test::Client
96
96
  # @rbs cookies: Hash[String, String]
97
97
  # @rbs return: Response
98
98
  def request(method, path, json, headers, cookies)
99
- body = json.nil? ? "" : JSON.generate(json)
99
+ body = json.nil? ? '' : JSON.generate(json)
100
100
  sent = body_headers(body).merge(cookie_header(cookies), headers)
101
101
  status, response_headers, response_body = @app.call(env(method, path, body, sent))
102
102
  text = read(response_body)
103
- Response.new(status:, headers: response_headers, body: text, json: parse(response_headers["content-type"], text))
103
+ Response.new(status:, headers: response_headers, body: text, json: parse(response_headers['content-type'], text))
104
104
  end
105
105
 
106
106
  # @rbs body: String
107
107
  # @rbs return: Hash[String, String]
108
108
  def body_headers(body)
109
- body.empty? ? {} : { "content-type" => "application/json", "content-length" => body.bytesize.to_s }
109
+ body.empty? ? {} : { 'content-type' => 'application/json', 'content-length' => body.bytesize.to_s }
110
110
  end
111
111
 
112
112
  # @rbs cookies: Hash[String, String]
113
113
  # @rbs return: Hash[String, String]
114
114
  def cookie_header(cookies)
115
- cookies.empty? ? {} : { "cookie" => cookies.map { |name, value| "#{name}=#{value}" }.join("; ") }
115
+ cookies.empty? ? {} : { 'cookie' => cookies.map { |name, value| "#{name}=#{value}" }.join('; ') }
116
116
  end
117
117
 
118
118
  # @rbs method: String
@@ -121,25 +121,25 @@ class Skyward::API::Test::Client
121
121
  # @rbs headers: Hash[String, String]
122
122
  # @rbs return: Hash[String, untyped]
123
123
  def env(method, path, body, headers)
124
- path_info, _, query = path.partition("?")
124
+ path_info, _, query = path.partition('?')
125
125
  {
126
- "REQUEST_METHOD" => method,
127
- "SCRIPT_NAME" => "",
128
- "PATH_INFO" => path_info,
129
- "QUERY_STRING" => query,
130
- "SERVER_NAME" => "localhost",
131
- "SERVER_PORT" => "80",
132
- "SERVER_PROTOCOL" => "HTTP/1.1",
133
- "rack.url_scheme" => "http",
134
- "rack.input" => StringIO.new(body.b),
135
- "rack.errors" => StringIO.new
126
+ 'REQUEST_METHOD' => method,
127
+ 'SCRIPT_NAME' => '',
128
+ 'PATH_INFO' => path_info,
129
+ 'QUERY_STRING' => query,
130
+ 'SERVER_NAME' => 'localhost',
131
+ 'SERVER_PORT' => '80',
132
+ 'SERVER_PROTOCOL' => 'HTTP/1.1',
133
+ 'rack.url_scheme' => 'http',
134
+ 'rack.input' => StringIO.new(body.b),
135
+ 'rack.errors' => StringIO.new
136
136
  }.merge(headers.to_h { |name, value| [env_key(name), value] })
137
137
  end
138
138
 
139
139
  # @rbs name: String
140
140
  # @rbs return: String
141
141
  def env_key(name)
142
- key = name.upcase.tr("-", "_")
142
+ key = name.upcase.tr('-', '_')
143
143
  UNPREFIXED.include?(key) ? key : "HTTP_#{key}"
144
144
  end
145
145
 
@@ -4,7 +4,7 @@ module Skyward
4
4
  class API
5
5
  # x-release-please-start-version
6
6
 
7
- VERSION = "0.4.0" #: String
7
+ VERSION = '0.4.1' #: String
8
8
 
9
9
  # x-release-please-end
10
10
  end
data/lib/skyward/api.rb CHANGED
@@ -1,20 +1,20 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "bigdecimal"
4
- require "securerandom"
5
- require "skyward/core"
6
- require "zeitwerk"
7
- require_relative "api/version"
3
+ require 'bigdecimal'
4
+ require 'securerandom'
5
+ require 'skyward/core'
6
+ require 'zeitwerk'
7
+ require_relative 'api/version'
8
8
 
9
9
  loader = Zeitwerk::Loader.for_gem_extension(Skyward)
10
- loader.inflector.inflect("api" => "API", "cli" => "CLI", "openapi" => "OpenAPI")
10
+ loader.inflector.inflect('api' => 'API', 'cli' => 'CLI', 'openapi' => 'OpenAPI')
11
11
  # The skyward executable loads it, and it defines no constant.
12
12
  loader.ignore("#{__dir__}/api/commands.rb")
13
13
  loader.setup
14
14
 
15
15
  # An app's signatures subclass Skyward::API, so RBS needs its declaration.
16
16
  dir = __dir__ #: String
17
- Skyward::Core::Signatures.register(File.expand_path("../../sig", dir))
17
+ Skyward::Core::Signatures.register(File.expand_path('../../sig', dir))
18
18
 
19
19
  class Skyward::API
20
20
  # @rbs! type rack_response = [Integer, Hash[String, untyped], untyped]
@@ -29,11 +29,11 @@ module Skyward
29
29
  # ?schema_names: Hash[Class, String], ?scalar_url: String, ?logger: API::_Logger?, ?on_error: API::_OnError?,
30
30
  # ?max_body_size: Integer) { (API::Assembly::Builder) -> void } -> API::Assembly::Application
31
31
  def self.app(
32
- signatures: "sig",
32
+ signatures: 'sig',
33
33
  strict: false,
34
34
  docs: false,
35
- title: "API",
36
- version: "0.1.0",
35
+ title: 'API',
36
+ version: '0.1.0',
37
37
  description: nil,
38
38
  schema_names: {},
39
39
  scalar_url: API::OpenAPI::Docs::SCALAR_URL,
@@ -47,13 +47,13 @@ module Skyward
47
47
  builder = API::Assembly::Builder.new
48
48
  yield builder
49
49
  builder.freeze
50
- reader = Core::Types::Reader.new(Core::Signatures.load(signatures))
51
50
  endpoints = API::Assembly::Boot.endpoints(
52
51
  builder.mounts,
53
52
  providers: builder.providers,
54
- reader:,
53
+ # Not a local, which every lambda built here would close over, keeping the signatures loaded.
54
+ reader: Core::Types::Reader.new(Core::Signatures.load(signatures)),
55
55
  max_body_size:,
56
- strict: strict || ENV.fetch("SKYWARD_STRICT", nil) == "1"
56
+ strict: strict || ENV.fetch('SKYWARD_STRICT', nil) == '1'
57
57
  )
58
58
  static = API::Assembly::Static.new(builder.statics)
59
59
  openapi = -> { API::OpenAPI.document(endpoints, title:, version:, description:, schema_names:) }
@@ -73,7 +73,7 @@ module Skyward
73
73
  API::Assembly::Dispatcher.new(API::Routing::Router.new(endpoints), static, builder.middleware, documents),
74
74
  logger:,
75
75
  on_error:,
76
- development: ENV.fetch("RACK_ENV") { ENV.fetch("APP_ENV", nil) } == "development"
76
+ development: ENV.fetch('RACK_ENV') { ENV.fetch('APP_ENV', nil) } == 'development'
77
77
  )
78
78
  logged =
79
79
  if logger
@@ -116,7 +116,7 @@ module Skyward
116
116
  def self.redirect(to, status: 303, headers: {})
117
117
  statuses = API::Responses::Redirect::STATUSES
118
118
  unless statuses.include?(status)
119
- raise ArgumentError, "A redirect's status must be one of #{statuses.join(", ")}, not #{status}"
119
+ raise ArgumentError, "A redirect's status must be one of #{statuses.join(', ')}, not #{status}"
120
120
  end
121
121
 
122
122
  # A line break in the location would let it smuggle in headers of its own.
@@ -126,9 +126,42 @@ class Skyward::API::Routing::Router
126
126
  def members: () -> [ :api, :to, :inputs, :params, :serializer ]
127
127
  end
128
128
 
129
+ class Target < Data
130
+ attr_reader index(): Integer
131
+
132
+ attr_reader endpoint(): Endpoint
133
+
134
+ attr_reader params(): Array[[ Symbol, Integer ]]
135
+
136
+ def self.new: (Integer index, Endpoint endpoint, Array[[ Symbol, Integer ]] params) -> instance
137
+ | (index: Integer, endpoint: Endpoint, params: Array[[ Symbol, Integer ]]) -> instance
138
+
139
+ def self.members: () -> [ :index, :endpoint, :params ]
140
+
141
+ def members: () -> [ :index, :endpoint, :params ]
142
+ end
143
+
144
+ # 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.
145
+ class Node < Data
146
+ attr_reader literals(): Hash[String, Node]
147
+
148
+ attr_reader param(): Node?
149
+
150
+ attr_reader target(): Target?
151
+
152
+ attr_reader first(): Integer
153
+
154
+ def self.new: (Hash[String, Node] literals, Node? param, Target? target, Integer first) -> instance
155
+ | (literals: Hash[String, Node], param: Node?, target: Target?, first: Integer) -> instance
156
+
157
+ def self.members: () -> [ :literals, :param, :target, :first ]
158
+
159
+ def members: () -> [ :literals, :param, :target, :first ]
160
+ end
161
+
129
162
  METHODS: Array[String]
130
163
 
131
- @endpoints: Array[Endpoint]
164
+ @trees: Hash[String, Node]
132
165
 
133
166
  # @rbs endpoints: Array[Endpoint]
134
167
  # @rbs return: void
@@ -145,14 +178,24 @@ class Skyward::API::Routing::Router
145
178
 
146
179
  private
147
180
 
148
- # @rbs endpoint: Endpoint
149
- # @rbs verb: String
150
- # @rbs segments: Array[String]
151
- # @rbs return: Match?
152
- def match_endpoint: (Endpoint endpoint, String verb, Array[String] segments) -> Match?
153
-
154
181
  # @rbs pattern: Array[String | Symbol]
182
+ # @rbs return: Array[[Symbol, Integer]]
183
+ def params_of: (Array[String | Symbol] pattern) -> Array[[ Symbol, Integer ]]
184
+
185
+ # @rbs targets: Array[Target]
186
+ # @rbs depth: Integer
187
+ # @rbs return: Node
188
+ def build: (Array[Target] targets, Integer depth) -> Node
189
+
190
+ # @rbs node: Node?
155
191
  # @rbs segments: Array[String]
156
- # @rbs return: Hash[Symbol, String]?
157
- def params_for: (Array[String | Symbol] pattern, Array[String] segments) -> Hash[Symbol, String]?
192
+ # @rbs depth: Integer
193
+ # @rbs best: Target?
194
+ # @rbs return: Target?
195
+ def search: (Node? node, Array[String] segments, Integer depth, Target? best) -> Target?
196
+
197
+ # @rbs target: Target?
198
+ # @rbs best: Target?
199
+ # @rbs return: Target?
200
+ def earlier: (Target? target, Target? best) -> Target?
158
201
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: skyward-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jake Bottrall
@@ -15,14 +15,14 @@ dependencies:
15
15
  requirements:
16
16
  - - '='
17
17
  - !ruby/object:Gem::Version
18
- version: 0.4.0
18
+ version: 0.4.1
19
19
  type: :runtime
20
20
  prerelease: false
21
21
  version_requirements: !ruby/object:Gem::Requirement
22
22
  requirements:
23
23
  - - '='
24
24
  - !ruby/object:Gem::Version
25
- version: 0.4.0
25
+ version: 0.4.1
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: bigdecimal
28
28
  requirement: !ruby/object:Gem::Requirement
@@ -57,6 +57,7 @@ executables: []
57
57
  extensions: []
58
58
  extra_rdoc_files: []
59
59
  files:
60
+ - LICENSE
60
61
  - README.md
61
62
  - lib/skyward/api.rb
62
63
  - lib/skyward/api/assembly.rb
@@ -106,6 +107,7 @@ files:
106
107
  - lib/skyward/api/test.rb
107
108
  - lib/skyward/api/test/client.rb
108
109
  - lib/skyward/api/version.rb
110
+ - sig/_private/zeitwerk.rbs
109
111
  - sig/generated/skyward/api.rbs
110
112
  - sig/generated/skyward/api/assembly.rbs
111
113
  - sig/generated/skyward/api/assembly/application.rbs
@@ -165,7 +167,6 @@ files:
165
167
  - sig/manual/skyward/api/routing/percent_encoding.rbs
166
168
  - sig/manual/skyward/api/serializer/writers.rbs
167
169
  - sig/manual/skyward/api/test.rbs
168
- - sig/manual/zeitwerk/loader.rbs
169
170
  homepage: https://github.com/bottrall/skyward
170
171
  licenses:
171
172
  - MIT