apiwork 0.4.0 → 0.6.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 (52) hide show
  1. checksums.yaml +4 -4
  2. data/lib/apiwork/adapter/serializer/error/default/api_builder.rb +4 -4
  3. data/lib/apiwork/adapter/serializer/resource/base.rb +15 -0
  4. data/lib/apiwork/adapter/serializer/resource/default/contract_builder.rb +3 -2
  5. data/lib/apiwork/adapter/standard/capability/writing/contract_builder.rb +2 -2
  6. data/lib/apiwork/api/base.rb +67 -17
  7. data/lib/apiwork/api/element.rb +33 -2
  8. data/lib/apiwork/api/object.rb +70 -5
  9. data/lib/apiwork/api/router.rb +16 -0
  10. data/lib/apiwork/configuration/validatable.rb +1 -0
  11. data/lib/apiwork/configuration.rb +2 -0
  12. data/lib/apiwork/contract/element.rb +17 -2
  13. data/lib/apiwork/contract/object/coercer.rb +24 -2
  14. data/lib/apiwork/contract/object/deserializer.rb +5 -1
  15. data/lib/apiwork/contract/object/transformer.rb +15 -2
  16. data/lib/apiwork/contract/object/validator.rb +46 -3
  17. data/lib/apiwork/contract/object.rb +85 -7
  18. data/lib/apiwork/controller.rb +15 -2
  19. data/lib/apiwork/element.rb +33 -0
  20. data/lib/apiwork/export/base.rb +1 -4
  21. data/lib/apiwork/export/builder_mapper.rb +184 -0
  22. data/lib/apiwork/export/open_api.rb +9 -2
  23. data/lib/apiwork/export/sorbus.rb +5 -1
  24. data/lib/apiwork/export/sorbus_mapper.rb +3 -7
  25. data/lib/apiwork/export/type_analysis.rb +20 -6
  26. data/lib/apiwork/export/type_script.rb +4 -1
  27. data/lib/apiwork/export/type_script_mapper.rb +25 -2
  28. data/lib/apiwork/export/zod.rb +9 -0
  29. data/lib/apiwork/export/zod_mapper.rb +22 -1
  30. data/lib/apiwork/introspection/dump/action.rb +1 -1
  31. data/lib/apiwork/introspection/dump/param.rb +36 -20
  32. data/lib/apiwork/introspection/dump/type.rb +31 -25
  33. data/lib/apiwork/introspection/param/array.rb +26 -0
  34. data/lib/apiwork/introspection/param/base.rb +16 -18
  35. data/lib/apiwork/introspection/param/binary.rb +36 -0
  36. data/lib/apiwork/introspection/param/boolean.rb +36 -0
  37. data/lib/apiwork/introspection/param/date.rb +36 -0
  38. data/lib/apiwork/introspection/param/date_time.rb +36 -0
  39. data/lib/apiwork/introspection/param/decimal.rb +26 -0
  40. data/lib/apiwork/introspection/param/integer.rb +26 -0
  41. data/lib/apiwork/introspection/param/number.rb +26 -0
  42. data/lib/apiwork/introspection/param/record.rb +71 -0
  43. data/lib/apiwork/introspection/param/string.rb +26 -0
  44. data/lib/apiwork/introspection/param/time.rb +36 -0
  45. data/lib/apiwork/introspection/param/uuid.rb +36 -0
  46. data/lib/apiwork/introspection/param.rb +1 -0
  47. data/lib/apiwork/object.rb +252 -2
  48. data/lib/apiwork/representation/attribute.rb +1 -1
  49. data/lib/apiwork/representation/base.rb +105 -0
  50. data/lib/apiwork/representation/element.rb +15 -5
  51. data/lib/apiwork/version.rb +1 -1
  52. metadata +4 -2
@@ -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)
@@ -1129,6 +1129,103 @@ module Apiwork
1129
1129
  )
1130
1130
  end
1131
1131
 
1132
+ # @api public
1133
+ # Defines an unknown.
1134
+ #
1135
+ # @param name [Symbol]
1136
+ # The name.
1137
+ # @param as [Symbol, nil] (nil)
1138
+ # The target attribute name.
1139
+ # @param default [Object, nil] (nil)
1140
+ # The default value.
1141
+ # @param deprecated [Boolean] (false)
1142
+ # Whether deprecated. Metadata included in exports.
1143
+ # @param description [String, nil] (nil)
1144
+ # The description. Metadata included in exports.
1145
+ # @param example [Object, nil] (nil)
1146
+ # The example value. Metadata included in exports.
1147
+ # @param nullable [Boolean] (false)
1148
+ # Whether the value can be `null`.
1149
+ # @param optional [Boolean] (false)
1150
+ # Whether the param is optional.
1151
+ # @param required [Boolean] (false)
1152
+ # Whether the param is required.
1153
+ # @return [void]
1154
+ #
1155
+ # @example Opaque metadata
1156
+ # unknown :metadata
1157
+ def unknown(
1158
+ name,
1159
+ as: nil,
1160
+ default: nil,
1161
+ deprecated: false,
1162
+ description: nil,
1163
+ example: nil,
1164
+ nullable: false,
1165
+ optional: false,
1166
+ required: false
1167
+ )
1168
+ param(
1169
+ name,
1170
+ as:,
1171
+ default:,
1172
+ deprecated:,
1173
+ description:,
1174
+ example:,
1175
+ nullable:,
1176
+ optional:,
1177
+ required:,
1178
+ type: :unknown,
1179
+ )
1180
+ end
1181
+
1182
+ # @api public
1183
+ # Defines an optional unknown.
1184
+ #
1185
+ # @param name [Symbol]
1186
+ # The name.
1187
+ # @param as [Symbol, nil] (nil)
1188
+ # The target attribute name.
1189
+ # @param default [Object, nil] (nil)
1190
+ # The default value.
1191
+ # @param deprecated [Boolean] (false)
1192
+ # Whether deprecated. Metadata included in exports.
1193
+ # @param description [String, nil] (nil)
1194
+ # The description. Metadata included in exports.
1195
+ # @param example [Object, nil] (nil)
1196
+ # The example value. Metadata included in exports.
1197
+ # @param nullable [Boolean] (false)
1198
+ # Whether the value can be `null`.
1199
+ # @param required [Boolean] (false)
1200
+ # Whether the param is required.
1201
+ # @return [void]
1202
+ #
1203
+ # @example Optional metadata
1204
+ # unknown? :metadata
1205
+ def unknown?(
1206
+ name,
1207
+ as: nil,
1208
+ default: nil,
1209
+ deprecated: false,
1210
+ description: nil,
1211
+ example: nil,
1212
+ nullable: false,
1213
+ required: false
1214
+ )
1215
+ param(
1216
+ name,
1217
+ as:,
1218
+ default:,
1219
+ deprecated:,
1220
+ description:,
1221
+ example:,
1222
+ nullable:,
1223
+ required:,
1224
+ optional: true,
1225
+ type: :unknown,
1226
+ )
1227
+ end
1228
+
1132
1229
  # @api public
1133
1230
  # Defines an object.
1134
1231
  #
@@ -1244,6 +1341,10 @@ module Apiwork
1244
1341
  # Whether deprecated. Metadata included in exports.
1245
1342
  # @param description [String, nil] (nil)
1246
1343
  # The description. Metadata included in exports.
1344
+ # @param max [Integer, nil] (nil)
1345
+ # The maximum number of elements.
1346
+ # @param min [Integer, nil] (nil)
1347
+ # The minimum number of elements.
1247
1348
  # @param nullable [Boolean] (false)
1248
1349
  # Whether the value can be `null`.
1249
1350
  # @param of [Symbol, Hash, nil] (nil)
@@ -1267,12 +1368,30 @@ module Apiwork
1267
1368
  # decimal :price
1268
1369
  # end
1269
1370
  # end
1371
+ #
1372
+ # @example Array of discriminated union
1373
+ # array :notifications do
1374
+ # union discriminator: :type do
1375
+ # variant tag: 'email' do
1376
+ # object do
1377
+ # string :address
1378
+ # end
1379
+ # end
1380
+ # variant tag: 'sms' do
1381
+ # object do
1382
+ # string :phone
1383
+ # end
1384
+ # end
1385
+ # end
1386
+ # end
1270
1387
  def array(
1271
1388
  name,
1272
1389
  as: nil,
1273
1390
  default: nil,
1274
1391
  deprecated: false,
1275
1392
  description: nil,
1393
+ max: nil,
1394
+ min: nil,
1276
1395
  nullable: false,
1277
1396
  of: nil,
1278
1397
  optional: false,
@@ -1285,6 +1404,8 @@ module Apiwork
1285
1404
  default:,
1286
1405
  deprecated:,
1287
1406
  description:,
1407
+ max:,
1408
+ min:,
1288
1409
  nullable:,
1289
1410
  of:,
1290
1411
  optional:,
@@ -1320,6 +1441,22 @@ module Apiwork
1320
1441
  # array? :labels do
1321
1442
  # string
1322
1443
  # end
1444
+ #
1445
+ # @example Optional array of discriminated union
1446
+ # array? :items do
1447
+ # union discriminator: :type do
1448
+ # variant tag: 'text' do
1449
+ # object do
1450
+ # string :content
1451
+ # end
1452
+ # end
1453
+ # variant tag: 'image' do
1454
+ # object do
1455
+ # string :url
1456
+ # end
1457
+ # end
1458
+ # end
1459
+ # end
1323
1460
  def array?(
1324
1461
  name,
1325
1462
  as: nil,
@@ -1346,6 +1483,113 @@ module Apiwork
1346
1483
  )
1347
1484
  end
1348
1485
 
1486
+ # @api public
1487
+ # Defines a record.
1488
+ #
1489
+ # @param name [Symbol]
1490
+ # The name.
1491
+ # @param as [Symbol, nil] (nil)
1492
+ # The target attribute name.
1493
+ # @param default [Object, nil] (nil)
1494
+ # The default value.
1495
+ # @param deprecated [Boolean] (false)
1496
+ # Whether deprecated. Metadata included in exports.
1497
+ # @param description [String, nil] (nil)
1498
+ # The description. Metadata included in exports.
1499
+ # @param nullable [Boolean] (false)
1500
+ # Whether the value can be `null`.
1501
+ # @param optional [Boolean] (false)
1502
+ # Whether the param is optional.
1503
+ # @param required [Boolean] (false)
1504
+ # Whether the param is required.
1505
+ # @yield block defining value type
1506
+ # @return [void]
1507
+ #
1508
+ # @example Record of integers
1509
+ # record :scores do
1510
+ # integer
1511
+ # end
1512
+ #
1513
+ # @example Record of objects
1514
+ # record :settings do
1515
+ # object do
1516
+ # string :value
1517
+ # boolean :enabled
1518
+ # end
1519
+ # end
1520
+ def record(
1521
+ name,
1522
+ as: nil,
1523
+ default: nil,
1524
+ deprecated: false,
1525
+ description: nil,
1526
+ nullable: false,
1527
+ optional: false,
1528
+ required: false,
1529
+ &block
1530
+ )
1531
+ param(
1532
+ name,
1533
+ as:,
1534
+ default:,
1535
+ deprecated:,
1536
+ description:,
1537
+ nullable:,
1538
+ optional:,
1539
+ required:,
1540
+ type: :record,
1541
+ &block
1542
+ )
1543
+ end
1544
+
1545
+ # @api public
1546
+ # Defines an optional record.
1547
+ #
1548
+ # @param name [Symbol]
1549
+ # The name.
1550
+ # @param as [Symbol, nil] (nil)
1551
+ # The target attribute name.
1552
+ # @param default [Object, nil] (nil)
1553
+ # The default value.
1554
+ # @param deprecated [Boolean] (false)
1555
+ # Whether deprecated. Metadata included in exports.
1556
+ # @param description [String, nil] (nil)
1557
+ # The description. Metadata included in exports.
1558
+ # @param nullable [Boolean] (false)
1559
+ # Whether the value can be `null`.
1560
+ # @param required [Boolean] (false)
1561
+ # Whether the param is required.
1562
+ # @yield block defining value type
1563
+ # @return [void]
1564
+ #
1565
+ # @example Optional record
1566
+ # record? :metadata do
1567
+ # string
1568
+ # end
1569
+ def record?(
1570
+ name,
1571
+ as: nil,
1572
+ default: nil,
1573
+ deprecated: false,
1574
+ description: nil,
1575
+ nullable: false,
1576
+ required: false,
1577
+ &block
1578
+ )
1579
+ param(
1580
+ name,
1581
+ as:,
1582
+ default:,
1583
+ deprecated:,
1584
+ description:,
1585
+ nullable:,
1586
+ required:,
1587
+ optional: true,
1588
+ type: :record,
1589
+ &block
1590
+ )
1591
+ end
1592
+
1349
1593
  # @api public
1350
1594
  # Defines a union.
1351
1595
  #
@@ -1552,6 +1796,8 @@ module Apiwork
1552
1796
  optional: false,
1553
1797
  required: false
1554
1798
  )
1799
+ reference_type = to || name
1800
+
1555
1801
  param(
1556
1802
  name,
1557
1803
  as:,
@@ -1561,7 +1807,8 @@ module Apiwork
1561
1807
  nullable:,
1562
1808
  optional:,
1563
1809
  required:,
1564
- type: to || name,
1810
+ custom_type: reference_type,
1811
+ type: reference_type,
1565
1812
  )
1566
1813
  end
1567
1814
 
@@ -1598,6 +1845,8 @@ module Apiwork
1598
1845
  nullable: false,
1599
1846
  required: false
1600
1847
  )
1848
+ reference_type = to || name
1849
+
1601
1850
  param(
1602
1851
  name,
1603
1852
  as:,
@@ -1606,8 +1855,9 @@ module Apiwork
1606
1855
  description:,
1607
1856
  nullable:,
1608
1857
  required:,
1858
+ custom_type: reference_type,
1609
1859
  optional: true,
1610
- type: to || name,
1860
+ type: reference_type,
1611
1861
  )
1612
1862
  end
1613
1863
 
@@ -117,7 +117,7 @@ module Apiwork
117
117
  element.validate!
118
118
  @element = element
119
119
  type = element.type
120
- @of = element.inner&.type if element.type == :array
120
+ @of = element.inner&.type if [:array, :record].include?(element.type)
121
121
  end
122
122
 
123
123
  if owner_representation_class.model_class.present?