apiwork 0.3.1 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (54) hide show
  1. checksums.yaml +4 -4
  2. data/lib/apiwork/adapter/serializer/resource/base.rb +15 -0
  3. data/lib/apiwork/adapter/serializer/resource/default/contract_builder.rb +4 -3
  4. data/lib/apiwork/adapter/standard/capability/writing/contract_builder.rb +13 -9
  5. data/lib/apiwork/api/base.rb +105 -17
  6. data/lib/apiwork/api/element.rb +35 -4
  7. data/lib/apiwork/api/object.rb +72 -7
  8. data/lib/apiwork/api/router.rb +16 -0
  9. data/lib/apiwork/configuration/validatable.rb +1 -0
  10. data/lib/apiwork/configuration.rb +2 -0
  11. data/lib/apiwork/contract/element.rb +19 -4
  12. data/lib/apiwork/contract/object/coercer.rb +31 -2
  13. data/lib/apiwork/contract/object/deserializer.rb +5 -1
  14. data/lib/apiwork/contract/object/transformer.rb +15 -2
  15. data/lib/apiwork/contract/object/validator.rb +49 -11
  16. data/lib/apiwork/contract/object.rb +79 -9
  17. data/lib/apiwork/element.rb +34 -1
  18. data/lib/apiwork/export/base.rb +1 -4
  19. data/lib/apiwork/export/builder_mapper.rb +184 -0
  20. data/lib/apiwork/export/open_api.rb +9 -2
  21. data/lib/apiwork/export/sorbus.rb +5 -1
  22. data/lib/apiwork/export/sorbus_mapper.rb +3 -7
  23. data/lib/apiwork/export/type_analysis.rb +20 -6
  24. data/lib/apiwork/export/type_script.rb +4 -1
  25. data/lib/apiwork/export/type_script_mapper.rb +25 -2
  26. data/lib/apiwork/export/zod.rb +9 -0
  27. data/lib/apiwork/export/zod_mapper.rb +22 -1
  28. data/lib/apiwork/introspection/api.rb +18 -0
  29. data/lib/apiwork/introspection/dump/action.rb +1 -1
  30. data/lib/apiwork/introspection/dump/api.rb +2 -0
  31. data/lib/apiwork/introspection/dump/param.rb +36 -20
  32. data/lib/apiwork/introspection/dump/resource.rb +7 -4
  33. data/lib/apiwork/introspection/dump/type.rb +31 -25
  34. data/lib/apiwork/introspection/param/array.rb +26 -0
  35. data/lib/apiwork/introspection/param/base.rb +15 -25
  36. data/lib/apiwork/introspection/param/binary.rb +36 -0
  37. data/lib/apiwork/introspection/param/boolean.rb +36 -0
  38. data/lib/apiwork/introspection/param/date.rb +36 -0
  39. data/lib/apiwork/introspection/param/date_time.rb +36 -0
  40. data/lib/apiwork/introspection/param/decimal.rb +26 -0
  41. data/lib/apiwork/introspection/param/integer.rb +26 -0
  42. data/lib/apiwork/introspection/param/number.rb +26 -0
  43. data/lib/apiwork/introspection/param/record.rb +71 -0
  44. data/lib/apiwork/introspection/param/string.rb +26 -0
  45. data/lib/apiwork/introspection/param/time.rb +36 -0
  46. data/lib/apiwork/introspection/param/uuid.rb +36 -0
  47. data/lib/apiwork/introspection/param.rb +1 -0
  48. data/lib/apiwork/introspection.rb +17 -4
  49. data/lib/apiwork/object.rb +246 -4
  50. data/lib/apiwork/representation/attribute.rb +2 -2
  51. data/lib/apiwork/representation/base.rb +107 -2
  52. data/lib/apiwork/representation/element.rb +15 -5
  53. data/lib/apiwork/version.rb +1 -1
  54. metadata +6 -4
@@ -20,6 +20,30 @@ module Apiwork
20
20
  # param.enum_reference? # => false
21
21
  # end
22
22
  class Boolean < Base
23
+ # @api public
24
+ # The default for this param.
25
+ #
26
+ # @return [Object, nil]
27
+ def default
28
+ @dump[:default]
29
+ end
30
+
31
+ # @api public
32
+ # The example for this param.
33
+ #
34
+ # @return [Object, nil]
35
+ def example
36
+ @dump[:example]
37
+ end
38
+
39
+ # @api public
40
+ # Whether this param is concrete.
41
+ #
42
+ # @return [Boolean]
43
+ def concrete?
44
+ true
45
+ end
46
+
23
47
  # @api public
24
48
  # Whether this param is scalar.
25
49
  #
@@ -67,6 +91,18 @@ module Apiwork
67
91
  def formattable?
68
92
  false
69
93
  end
94
+
95
+ # @api public
96
+ # Converts this param to a hash.
97
+ #
98
+ # @return [Hash]
99
+ def to_h
100
+ result = super
101
+ result[:default] = default
102
+ result[:enum] = enum if enum?
103
+ result[:example] = example
104
+ result
105
+ end
70
106
  end
71
107
  end
72
108
  end
@@ -20,6 +20,30 @@ module Apiwork
20
20
  # param.enum_reference? # => false
21
21
  # end
22
22
  class Date < Base
23
+ # @api public
24
+ # The default for this param.
25
+ #
26
+ # @return [Object, nil]
27
+ def default
28
+ @dump[:default]
29
+ end
30
+
31
+ # @api public
32
+ # The example for this param.
33
+ #
34
+ # @return [Object, nil]
35
+ def example
36
+ @dump[:example]
37
+ end
38
+
39
+ # @api public
40
+ # Whether this param is concrete.
41
+ #
42
+ # @return [Boolean]
43
+ def concrete?
44
+ true
45
+ end
46
+
23
47
  # @api public
24
48
  # Whether this param is scalar.
25
49
  #
@@ -67,6 +91,18 @@ module Apiwork
67
91
  def formattable?
68
92
  false
69
93
  end
94
+
95
+ # @api public
96
+ # Converts this param to a hash.
97
+ #
98
+ # @return [Hash]
99
+ def to_h
100
+ result = super
101
+ result[:default] = default
102
+ result[:enum] = enum if enum?
103
+ result[:example] = example
104
+ result
105
+ end
70
106
  end
71
107
  end
72
108
  end
@@ -20,6 +20,30 @@ module Apiwork
20
20
  # param.enum_reference? # => false
21
21
  # end
22
22
  class DateTime < Base
23
+ # @api public
24
+ # The default for this param.
25
+ #
26
+ # @return [Object, nil]
27
+ def default
28
+ @dump[:default]
29
+ end
30
+
31
+ # @api public
32
+ # The example for this param.
33
+ #
34
+ # @return [Object, nil]
35
+ def example
36
+ @dump[:example]
37
+ end
38
+
39
+ # @api public
40
+ # Whether this param is concrete.
41
+ #
42
+ # @return [Boolean]
43
+ def concrete?
44
+ true
45
+ end
46
+
23
47
  # @api public
24
48
  # Whether this param is scalar.
25
49
  #
@@ -67,6 +91,18 @@ module Apiwork
67
91
  def formattable?
68
92
  false
69
93
  end
94
+
95
+ # @api public
96
+ # Converts this param to a hash.
97
+ #
98
+ # @return [Hash]
99
+ def to_h
100
+ result = super
101
+ result[:default] = default
102
+ result[:enum] = enum if enum?
103
+ result[:example] = example
104
+ result
105
+ end
70
106
  end
71
107
  end
72
108
  end
@@ -24,6 +24,22 @@ module Apiwork
24
24
  # param.enum_reference? # => false
25
25
  # end
26
26
  class Decimal < Base
27
+ # @api public
28
+ # The default for this param.
29
+ #
30
+ # @return [Object, nil]
31
+ def default
32
+ @dump[:default]
33
+ end
34
+
35
+ # @api public
36
+ # The example for this param.
37
+ #
38
+ # @return [Object, nil]
39
+ def example
40
+ @dump[:example]
41
+ end
42
+
27
43
  # @api public
28
44
  # The minimum for this param.
29
45
  #
@@ -40,6 +56,14 @@ module Apiwork
40
56
  @dump[:max]
41
57
  end
42
58
 
59
+ # @api public
60
+ # Whether this param is concrete.
61
+ #
62
+ # @return [Boolean]
63
+ def concrete?
64
+ true
65
+ end
66
+
43
67
  # @api public
44
68
  # Whether this param is scalar.
45
69
  #
@@ -110,7 +134,9 @@ module Apiwork
110
134
  # @return [Hash]
111
135
  def to_h
112
136
  result = super
137
+ result[:default] = default
113
138
  result[:enum] = enum if enum?
139
+ result[:example] = example
114
140
  result[:max] = max
115
141
  result[:min] = min
116
142
  result
@@ -25,6 +25,22 @@ module Apiwork
25
25
  # param.enum_reference? # => false
26
26
  # end
27
27
  class Integer < Base
28
+ # @api public
29
+ # The default for this param.
30
+ #
31
+ # @return [Object, nil]
32
+ def default
33
+ @dump[:default]
34
+ end
35
+
36
+ # @api public
37
+ # The example for this param.
38
+ #
39
+ # @return [Object, nil]
40
+ def example
41
+ @dump[:example]
42
+ end
43
+
28
44
  # @api public
29
45
  # The minimum for this param.
30
46
  #
@@ -49,6 +65,14 @@ module Apiwork
49
65
  @dump[:format]
50
66
  end
51
67
 
68
+ # @api public
69
+ # Whether this param is concrete.
70
+ #
71
+ # @return [Boolean]
72
+ def concrete?
73
+ true
74
+ end
75
+
52
76
  # @api public
53
77
  # Whether this param is scalar.
54
78
  #
@@ -119,7 +143,9 @@ module Apiwork
119
143
  # @return [Hash]
120
144
  def to_h
121
145
  result = super
146
+ result[:default] = default
122
147
  result[:enum] = enum if enum?
148
+ result[:example] = example
123
149
  result[:format] = format
124
150
  result[:max] = max
125
151
  result[:min] = min
@@ -24,6 +24,22 @@ module Apiwork
24
24
  # param.enum_reference? # => false
25
25
  # end
26
26
  class Number < Base
27
+ # @api public
28
+ # The default for this param.
29
+ #
30
+ # @return [Object, nil]
31
+ def default
32
+ @dump[:default]
33
+ end
34
+
35
+ # @api public
36
+ # The example for this param.
37
+ #
38
+ # @return [Object, nil]
39
+ def example
40
+ @dump[:example]
41
+ end
42
+
27
43
  # @api public
28
44
  # The minimum for this param.
29
45
  #
@@ -40,6 +56,14 @@ module Apiwork
40
56
  @dump[:max]
41
57
  end
42
58
 
59
+ # @api public
60
+ # Whether this param is concrete.
61
+ #
62
+ # @return [Boolean]
63
+ def concrete?
64
+ true
65
+ end
66
+
43
67
  # @api public
44
68
  # Whether this param is scalar.
45
69
  #
@@ -110,7 +134,9 @@ module Apiwork
110
134
  # @return [Hash]
111
135
  def to_h
112
136
  result = super
137
+ result[:default] = default
113
138
  result[:enum] = enum if enum?
139
+ result[:example] = example
114
140
  result[:max] = max
115
141
  result[:min] = min
116
142
  result
@@ -0,0 +1,71 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Apiwork
4
+ module Introspection
5
+ module Param
6
+ # @api public
7
+ # Record param representing key-value maps with typed values.
8
+ #
9
+ # @example Basic usage
10
+ # param.type # => :record
11
+ # param.record? # => true
12
+ # param.scalar? # => false
13
+ #
14
+ # @example Value type
15
+ # param.of # => Param (value type) or nil
16
+ class Record < Base
17
+ # @api public
18
+ # The default for this param.
19
+ #
20
+ # @return [Object, nil]
21
+ def default
22
+ @dump[:default]
23
+ end
24
+
25
+ # @api public
26
+ # The example for this param.
27
+ #
28
+ # @return [Object, nil]
29
+ def example
30
+ @dump[:example]
31
+ end
32
+
33
+ # @api public
34
+ # The value type for this record.
35
+ #
36
+ # @return [Param::Base, nil]
37
+ def of
38
+ @of ||= @dump[:of] ? Param.build(@dump[:of]) : nil
39
+ end
40
+
41
+ # @api public
42
+ # Whether this param is a record.
43
+ #
44
+ # @return [Boolean]
45
+ def record?
46
+ true
47
+ end
48
+
49
+ # @api public
50
+ # Whether this param is concrete.
51
+ #
52
+ # @return [Boolean]
53
+ def concrete?
54
+ true
55
+ end
56
+
57
+ # @api public
58
+ # Converts this param to a hash.
59
+ #
60
+ # @return [Hash]
61
+ def to_h
62
+ result = super
63
+ result[:default] = default
64
+ result[:example] = example
65
+ result[:of] = of&.to_h
66
+ result
67
+ end
68
+ end
69
+ end
70
+ end
71
+ end
@@ -24,6 +24,22 @@ module Apiwork
24
24
  # param.enum_reference? # => false
25
25
  # end
26
26
  class String < Base
27
+ # @api public
28
+ # The default for this param.
29
+ #
30
+ # @return [Object, nil]
31
+ def default
32
+ @dump[:default]
33
+ end
34
+
35
+ # @api public
36
+ # The example for this param.
37
+ #
38
+ # @return [Object, nil]
39
+ def example
40
+ @dump[:example]
41
+ end
42
+
27
43
  # @api public
28
44
  # The format for this param.
29
45
  #
@@ -48,6 +64,14 @@ module Apiwork
48
64
  @dump[:max]
49
65
  end
50
66
 
67
+ # @api public
68
+ # Whether this param is concrete.
69
+ #
70
+ # @return [Boolean]
71
+ def concrete?
72
+ true
73
+ end
74
+
51
75
  # @api public
52
76
  # Whether this param is scalar.
53
77
  #
@@ -110,7 +134,9 @@ module Apiwork
110
134
  # @return [Hash]
111
135
  def to_h
112
136
  result = super
137
+ result[:default] = default
113
138
  result[:enum] = enum if enum?
139
+ result[:example] = example
114
140
  result[:format] = format
115
141
  result[:max] = max
116
142
  result[:min] = min
@@ -20,6 +20,30 @@ module Apiwork
20
20
  # param.enum_reference? # => false
21
21
  # end
22
22
  class Time < Base
23
+ # @api public
24
+ # The default for this param.
25
+ #
26
+ # @return [Object, nil]
27
+ def default
28
+ @dump[:default]
29
+ end
30
+
31
+ # @api public
32
+ # The example for this param.
33
+ #
34
+ # @return [Object, nil]
35
+ def example
36
+ @dump[:example]
37
+ end
38
+
39
+ # @api public
40
+ # Whether this param is concrete.
41
+ #
42
+ # @return [Boolean]
43
+ def concrete?
44
+ true
45
+ end
46
+
23
47
  # @api public
24
48
  # Whether this param is scalar.
25
49
  #
@@ -67,6 +91,18 @@ module Apiwork
67
91
  def formattable?
68
92
  false
69
93
  end
94
+
95
+ # @api public
96
+ # Converts this param to a hash.
97
+ #
98
+ # @return [Hash]
99
+ def to_h
100
+ result = super
101
+ result[:default] = default
102
+ result[:enum] = enum if enum?
103
+ result[:example] = example
104
+ result
105
+ end
70
106
  end
71
107
  end
72
108
  end
@@ -20,6 +20,30 @@ module Apiwork
20
20
  # param.enum_reference? # => false
21
21
  # end
22
22
  class UUID < Base
23
+ # @api public
24
+ # The default for this param.
25
+ #
26
+ # @return [Object, nil]
27
+ def default
28
+ @dump[:default]
29
+ end
30
+
31
+ # @api public
32
+ # The example for this param.
33
+ #
34
+ # @return [Object, nil]
35
+ def example
36
+ @dump[:example]
37
+ end
38
+
39
+ # @api public
40
+ # Whether this param is concrete.
41
+ #
42
+ # @return [Boolean]
43
+ def concrete?
44
+ true
45
+ end
46
+
23
47
  # @api public
24
48
  # Whether this param is scalar.
25
49
  #
@@ -67,6 +91,18 @@ module Apiwork
67
91
  def formattable?
68
92
  false
69
93
  end
94
+
95
+ # @api public
96
+ # Converts this param to a hash.
97
+ #
98
+ # @return [Hash]
99
+ def to_h
100
+ result = super
101
+ result[:default] = default
102
+ result[:enum] = enum if enum?
103
+ result[:example] = example
104
+ result
105
+ end
70
106
  end
71
107
  end
72
108
  end
@@ -18,6 +18,7 @@ module Apiwork
18
18
  when :binary then Binary.new(dump)
19
19
  when :unknown then Unknown.new(dump)
20
20
  when :array then Array.new(dump)
21
+ when :record then Record.new(dump)
21
22
  when :object then Object.new(dump)
22
23
  when :union then Union.new(dump)
23
24
  when :literal then Literal.new(dump)
@@ -4,23 +4,36 @@ module Apiwork
4
4
  module Introspection
5
5
  class << self
6
6
  def api(api_class, locale: nil)
7
+ validate_locale(api_class, locale)
7
8
  with_locale(locale) { API.new(Dump.api(api_class)) }
8
9
  end
9
10
 
10
11
  def contract(contract_class, expand: false, locale: nil)
12
+ validate_locale(contract_class.api_class, locale)
11
13
  with_locale(locale) { Contract.new(Dump.contract(contract_class, expand:)) }
12
14
  end
13
15
 
14
16
  private
15
17
 
16
- def with_locale(locale, &block)
17
- return yield unless locale
18
+ def validate_locale(api_class, locale)
19
+ return unless locale
20
+ return unless api_class
18
21
 
19
- unless I18n.available_locales.include?(locale)
22
+ if api_class.locales.empty?
20
23
  raise ConfigurationError,
21
- "locale must be one of #{I18n.available_locales.inspect}, got #{locale.inspect}"
24
+ "locale :#{locale} was requested but no locales are defined. " \
25
+ "Add `locales #{locale.inspect}` to your API definition."
22
26
  end
23
27
 
28
+ return if api_class.locales.include?(locale)
29
+
30
+ raise ConfigurationError,
31
+ "locale must be one of #{api_class.locales.inspect}, got #{locale.inspect}"
32
+ end
33
+
34
+ def with_locale(locale, &block)
35
+ return yield unless locale
36
+
24
37
  I18n.with_locale(locale, &block)
25
38
  end
26
39
  end