types_from_serializers 2.1.0 → 2.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 61ca195394efd6b1c37c31b81ff85f7ccbcba562e9cf7b76d7810dfc613e1aa6
4
- data.tar.gz: 20ef589a30a448e668d1da4106ab565e768227e74f950c5d56bfbd2a83aa9a48
3
+ metadata.gz: e9253a2af8e018bad4fc2015153df9e0f2c5acd8fa6dc1980ee6c5942fd63b0b
4
+ data.tar.gz: 3ddc115f8667dcba81f51ae867323c9852939c019b18e49a29bf5a73d8c0c790
5
5
  SHA512:
6
- metadata.gz: fec219f23757f68e03678195ac3f66f13d3d48897e30c3f4b141c12257eb394577c997ecc639110ac3fee8be57e90f915c94989e84116a9be76d9d1e5df47f52
7
- data.tar.gz: 1956d02db12c7ea5eab5edff52dc48356f163f71a17ab08fc93e6dcae810d29df26ea7501ddc0e8ac609087981d2456e4b927bcdb01acfeff455adf4671b58b0
6
+ metadata.gz: 8dc521ddba354a4443a907597ba1b5c4d6093c156b6685e0ac683bcc922f2414e3380f00ba8e2ae5e96587dd519efd738304ec98d93643d6859d1dc6047be1ca
7
+ data.tar.gz: f69cf927cc528364a62c2a96ffbdcf54dff910700d80a89e8109bdb1d4c208bf9cfce6221c43c04bc914a5cd4c284a3854e2504122bef256365c6f4c654c5f0c
data/CHANGELOG.md CHANGED
@@ -1,3 +1,26 @@
1
+ # [2.3.0](https://github.com/ElMassimo/types_from_serializers/compare/types_from_serializers@2.2.0...types_from_serializers@2.3.0) (2024-08-23)
2
+
3
+
4
+ ### Features
5
+
6
+ * generate types for inline serializers, exclude them from the index ([1c3657c](https://github.com/ElMassimo/types_from_serializers/commit/1c3657c61a1bc891f3219f6eaf8557cd3cd6344a)), closes [#19](https://github.com/ElMassimo/types_from_serializers/issues/19)
7
+
8
+
9
+
10
+ # [2.2.0](https://github.com/ElMassimo/types_from_serializers/compare/types_from_serializers@2.1.0...types_from_serializers@2.2.0) (2024-08-23)
11
+
12
+
13
+ ### Bug Fixes
14
+
15
+ * be more accurate regarding decimal serialization ([cd63653](https://github.com/ElMassimo/types_from_serializers/commit/cd636530a5710112a14746cc7d0e3f15016cd5e1))
16
+
17
+
18
+ ### Features
19
+
20
+ * infer type from enums ([#20](https://github.com/ElMassimo/types_from_serializers/issues/20)) ([49dc61d](https://github.com/ElMassimo/types_from_serializers/commit/49dc61da2718256e9b5f5743b5a65c4746d64c2f))
21
+
22
+
23
+
1
24
  # [2.1.0](https://github.com/ElMassimo/types_from_serializers/compare/types_from_serializers@2.0.2...types_from_serializers@2.1.0) (2023-07-19)
2
25
 
3
26
 
@@ -38,15 +38,17 @@ module TypesFromSerializers
38
38
  TypesFromSerializers.config.name_from_serializer.call(name).gsub("::", "/")
39
39
  end
40
40
 
41
- # Internal: The columns corresponding to the serializer model, if it's a
42
- # record.
43
- def model_columns
44
- @model_columns ||= _serializer_model_name&.to_model.try(:columns_hash) || {}
41
+ # Internal: If the serializer was defined inside a file.
42
+ def inline_serializer?
43
+ name.include?("Serializer::")
45
44
  end
46
45
 
47
46
  # Internal: The TypeScript properties of the serialzeir interface.
48
47
  def ts_properties
49
48
  @ts_properties ||= begin
49
+ model_class = _serializer_model_name&.to_model
50
+ model_columns = model_class.try(:columns_hash) || {}
51
+ model_enums = model_class.try(:defined_enums) || {}
50
52
  types_from = try(:_serializer_types_from)
51
53
 
52
54
  prepare_attributes(
@@ -64,7 +66,7 @@ module TypesFromSerializers
64
66
  multi: options[:association] == :many,
65
67
  column_name: options.fetch(:value_from),
66
68
  ).tap do |property|
67
- property.infer_type_from(model_columns, types_from)
69
+ property.infer_type_from(model_columns, model_enums, types_from)
68
70
  end
69
71
  end
70
72
  }
@@ -190,9 +192,11 @@ module TypesFromSerializers
190
192
 
191
193
  # Internal: Infers the property's type by checking a corresponding SQL
192
194
  # column, or falling back to a TypeScript interface if provided.
193
- def infer_type_from(columns_hash, ts_interface)
195
+ def infer_type_from(columns_hash, defined_enums, ts_interface)
194
196
  if type
195
197
  type
198
+ elsif (enum = defined_enums[column_name.to_s])
199
+ self.type = enum.keys.map(&:inspect).join(" | ")
196
200
  elsif (column = columns_hash[column_name.to_s])
197
201
  self.multi = true if column.try(:array)
198
202
  self.optional = true if column.null && !column.default
@@ -313,9 +317,7 @@ module TypesFromSerializers
313
317
  # Internal: Checks if it should avoid generating an interface.
314
318
  def skip_serializer?(serializer)
315
319
  serializer.name.in?(config.base_serializers) ||
316
- config.skip_serializer_if.call(serializer) ||
317
- # NOTE: Ignore inline serializers.
318
- serializer.ts_name.include?("Serializer")
320
+ config.skip_serializer_if.call(serializer)
319
321
  end
320
322
 
321
323
  # Internal: Returns an object compatible with FileUpdateChecker.
@@ -363,7 +365,9 @@ module TypesFromSerializers
363
365
  output_dir: root.join(defined?(ViteRuby) ? ViteRuby.config.source_code_dir : "app/frontend").join("types/serializers"),
364
366
 
365
367
  # Remove the serializer suffix from the class name.
366
- name_from_serializer: ->(name) { name.delete_suffix("Serializer") },
368
+ name_from_serializer: ->(name) {
369
+ name.split("::").map { |n| n.delete_suffix("Serializer") }.join("::")
370
+ },
367
371
 
368
372
  # Types that don't need to be imported in TypeScript.
369
373
  global_types: [
@@ -383,7 +387,7 @@ module TypesFromSerializers
383
387
  boolean: :boolean,
384
388
  date: "string | Date",
385
389
  datetime: "string | Date",
386
- decimal: :number,
390
+ decimal: "string | number",
387
391
  integer: :number,
388
392
  string: :string,
389
393
  text: :string,
@@ -421,7 +425,7 @@ module TypesFromSerializers
421
425
  <<~TS
422
426
  //
423
427
  // DO NOT MODIFY: This file was automatically generated by TypesFromSerializers.
424
- #{serializers.map { |s|
428
+ #{serializers.reject(&:inline_serializer?).map { |s|
425
429
  "export type { default as #{s.ts_name} } from './#{s.ts_filename}'"
426
430
  }.join("\n")}
427
431
  TS
@@ -2,5 +2,5 @@
2
2
 
3
3
  module TypesFromSerializers
4
4
  # Public: This library adheres to semantic versioning.
5
- VERSION = "2.1.0"
5
+ VERSION = "2.3.0"
6
6
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: types_from_serializers
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.0
4
+ version: 2.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Máximo Mussini
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-07-19 00:00:00.000000000 Z
11
+ date: 2024-08-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: railties
@@ -277,7 +277,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
277
277
  - !ruby/object:Gem::Version
278
278
  version: '0'
279
279
  requirements: []
280
- rubygems_version: 3.2.32
280
+ rubygems_version: 3.4.10
281
281
  signing_key:
282
282
  specification_version: 4
283
283
  summary: Generate TypeScript interfaces from your JSON serializers.