types_from_serializers 2.0.2 → 2.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +23 -0
- data/lib/types_from_serializers/generator.rb +42 -16
- data/lib/types_from_serializers/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0e3961bc80ddc72831c77e7cf9ddf0959809a49a845f0d778d0c6ac50148cb87
|
4
|
+
data.tar.gz: e676ddfe4c4c51081075bd5bbb3a66990d8ea469dc60305a550175abe8c43c99
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 01e286a9381c3d8f2e00c4085ba491ab4d4332ee3ac0ea2ebd109a7ff53ddb131b43d1c0839a09459884edc0536cd805d5303be2a80143a12998d6b5839dfc14
|
7
|
+
data.tar.gz: a3687e203f27c34a3243f1422588bd2c79362ef4b7f9c2432ec6ce53493c83c11782f784f8acb4a76d7a611fab6ca7e9422f63560c3053739e3b39c3258d91ed
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,26 @@
|
|
1
|
+
# [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)
|
2
|
+
|
3
|
+
|
4
|
+
### Bug Fixes
|
5
|
+
|
6
|
+
* be more accurate regarding decimal serialization ([cd63653](https://github.com/ElMassimo/types_from_serializers/commit/cd636530a5710112a14746cc7d0e3f15016cd5e1))
|
7
|
+
|
8
|
+
|
9
|
+
### Features
|
10
|
+
|
11
|
+
* infer type from enums ([#20](https://github.com/ElMassimo/types_from_serializers/issues/20)) ([49dc61d](https://github.com/ElMassimo/types_from_serializers/commit/49dc61da2718256e9b5f5743b5a65c4746d64c2f))
|
12
|
+
|
13
|
+
|
14
|
+
|
15
|
+
# [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)
|
16
|
+
|
17
|
+
|
18
|
+
### Features
|
19
|
+
|
20
|
+
* add `namespace` option to generate `.d.ts` files ([#9](https://github.com/ElMassimo/types_from_serializers/issues/9)) ([6f67b1a](https://github.com/ElMassimo/types_from_serializers/commit/6f67b1ad9283868e8e3325042645bceccc85b047))
|
21
|
+
|
22
|
+
|
23
|
+
|
1
24
|
## [2.0.2](https://github.com/ElMassimo/types_from_serializers/compare/types_from_serializers@2.0.1...types_from_serializers@2.0.2) (2023-04-05)
|
2
25
|
|
3
26
|
|
@@ -38,15 +38,12 @@ 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) || {}
|
45
|
-
end
|
46
|
-
|
47
41
|
# Internal: The TypeScript properties of the serialzeir interface.
|
48
42
|
def ts_properties
|
49
43
|
@ts_properties ||= begin
|
44
|
+
model_class = _serializer_model_name&.to_model
|
45
|
+
model_columns = model_class.try(:columns_hash) || {}
|
46
|
+
model_enums = model_class.try(:defined_enums) || {}
|
50
47
|
types_from = try(:_serializer_types_from)
|
51
48
|
|
52
49
|
prepare_attributes(
|
@@ -64,7 +61,7 @@ module TypesFromSerializers
|
|
64
61
|
multi: options[:association] == :many,
|
65
62
|
column_name: options.fetch(:value_from),
|
66
63
|
).tap do |property|
|
67
|
-
property.infer_type_from(model_columns, types_from)
|
64
|
+
property.infer_type_from(model_columns, model_enums, types_from)
|
68
65
|
end
|
69
66
|
end
|
70
67
|
}
|
@@ -94,6 +91,7 @@ module TypesFromSerializers
|
|
94
91
|
:sql_to_typescript_type_mapping,
|
95
92
|
:skip_serializer_if,
|
96
93
|
:transform_keys,
|
94
|
+
:namespace,
|
97
95
|
keyword_init: true,
|
98
96
|
) do
|
99
97
|
def relative_custom_types_dir
|
@@ -140,10 +138,11 @@ module TypesFromSerializers
|
|
140
138
|
end
|
141
139
|
|
142
140
|
def as_typescript
|
143
|
-
|
141
|
+
indent = TypesFromSerializers.config.namespace ? 3 : 1
|
142
|
+
<<~TS.gsub(/\n$/, "")
|
144
143
|
interface #{name} {
|
145
|
-
|
146
|
-
}
|
144
|
+
#{" " * indent}#{properties.index_by(&:name).values.map(&:as_typescript).join("\n#{" " * indent}")}
|
145
|
+
#{" " * (indent - 1)}}
|
147
146
|
TS
|
148
147
|
end
|
149
148
|
|
@@ -188,9 +187,11 @@ module TypesFromSerializers
|
|
188
187
|
|
189
188
|
# Internal: Infers the property's type by checking a corresponding SQL
|
190
189
|
# column, or falling back to a TypeScript interface if provided.
|
191
|
-
def infer_type_from(columns_hash, ts_interface)
|
190
|
+
def infer_type_from(columns_hash, defined_enums, ts_interface)
|
192
191
|
if type
|
193
192
|
type
|
193
|
+
elsif (enum = defined_enums[column_name.to_s])
|
194
|
+
self.type = enum.keys.map(&:inspect).join(" | ")
|
194
195
|
elsif (column = columns_hash[column_name.to_s])
|
195
196
|
self.multi = true if column.try(:array)
|
196
197
|
self.optional = true if column.null && !column.default
|
@@ -269,7 +270,12 @@ module TypesFromSerializers
|
|
269
270
|
def generate(force: ENV["SERIALIZER_TYPES_FORCE"])
|
270
271
|
@force_generation = force
|
271
272
|
config.output_dir.rmtree if force && config.output_dir.exist?
|
272
|
-
|
273
|
+
|
274
|
+
if config.namespace
|
275
|
+
load_serializers(all_serializer_files) if force
|
276
|
+
else
|
277
|
+
generate_index_file
|
278
|
+
end
|
273
279
|
|
274
280
|
loaded_serializers.each do |serializer|
|
275
281
|
generate_interface_for(serializer)
|
@@ -289,7 +295,7 @@ module TypesFromSerializers
|
|
289
295
|
def generate_interface_for(serializer)
|
290
296
|
interface = serializer.ts_interface
|
291
297
|
|
292
|
-
write_if_changed(filename: interface.filename, cache_key: interface.inspect) {
|
298
|
+
write_if_changed(filename: interface.filename, cache_key: interface.inspect, extension: config.namespace ? "d.ts" : "ts") {
|
293
299
|
serializer_interface_content(interface)
|
294
300
|
}
|
295
301
|
end
|
@@ -376,7 +382,7 @@ module TypesFromSerializers
|
|
376
382
|
boolean: :boolean,
|
377
383
|
date: "string | Date",
|
378
384
|
datetime: "string | Date",
|
379
|
-
decimal:
|
385
|
+
decimal: "string | number",
|
380
386
|
integer: :number,
|
381
387
|
string: :string,
|
382
388
|
text: :string,
|
@@ -387,6 +393,9 @@ module TypesFromSerializers
|
|
387
393
|
|
388
394
|
# Allows to transform keys, useful when converting objects client-side.
|
389
395
|
transform_keys: nil,
|
396
|
+
|
397
|
+
# Allows scoping typescript definitions to a namespace
|
398
|
+
namespace: nil,
|
390
399
|
)
|
391
400
|
end
|
392
401
|
|
@@ -394,8 +403,8 @@ module TypesFromSerializers
|
|
394
403
|
# The cache strategy consists of a comment on the first line of the file.
|
395
404
|
#
|
396
405
|
# Yields to receive the rendered file content when it needs to.
|
397
|
-
def write_if_changed(filename:, cache_key:)
|
398
|
-
filename = config.output_dir.join("#{filename}
|
406
|
+
def write_if_changed(filename:, cache_key:, extension: "ts")
|
407
|
+
filename = config.output_dir.join("#{filename}.#{extension}")
|
399
408
|
FileUtils.mkdir_p(filename.dirname)
|
400
409
|
cache_key_comment = "// TypesFromSerializers CacheKey #{Digest::MD5.hexdigest(cache_key)}\n"
|
401
410
|
File.open(filename, "a+") { |file|
|
@@ -418,6 +427,10 @@ module TypesFromSerializers
|
|
418
427
|
end
|
419
428
|
|
420
429
|
def serializer_interface_content(interface)
|
430
|
+
config.namespace ? declaration_interface_definition(interface) : standard_interface_definition(interface)
|
431
|
+
end
|
432
|
+
|
433
|
+
def standard_interface_definition(interface)
|
421
434
|
<<~TS
|
422
435
|
//
|
423
436
|
// DO NOT MODIFY: This file was automatically generated by TypesFromSerializers.
|
@@ -426,6 +439,19 @@ module TypesFromSerializers
|
|
426
439
|
TS
|
427
440
|
end
|
428
441
|
|
442
|
+
def declaration_interface_definition(interface)
|
443
|
+
<<~TS
|
444
|
+
//
|
445
|
+
// DO NOT MODIFY: This file was automatically generated by TypesFromSerializers.
|
446
|
+
#{interface.used_imports.empty? ? "export {}\n" : interface.used_imports.join}
|
447
|
+
declare global {
|
448
|
+
namespace #{config.namespace} {
|
449
|
+
#{interface.as_typescript}
|
450
|
+
}
|
451
|
+
}
|
452
|
+
TS
|
453
|
+
end
|
454
|
+
|
429
455
|
# Internal: Returns true if the cache key has changed since the last codegen.
|
430
456
|
def stale?(file, cache_key_comment)
|
431
457
|
@force_generation || file.gets != cache_key_comment
|
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.0
|
4
|
+
version: 2.2.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:
|
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.
|
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.
|