types_from_serializers 2.0.1 → 2.1.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: e3ceb7a2dea10b4bc18d98eb9d0425f6a77caf1f3f9be3ca104063651bb64461
4
- data.tar.gz: 18d9ba2eccf240725e53ee2ffd56398a1baa0df1642379c5ff5a9efefbf9fac9
3
+ metadata.gz: 61ca195394efd6b1c37c31b81ff85f7ccbcba562e9cf7b76d7810dfc613e1aa6
4
+ data.tar.gz: 20ef589a30a448e668d1da4106ab565e768227e74f950c5d56bfbd2a83aa9a48
5
5
  SHA512:
6
- metadata.gz: 7b5f49158e7e4b81ee3f2645177200e727868827e8a0a81500c1445729dabc693e19a45a593b9fd264341a66abc53ac1f76b138664958eda64a39d0950e61728
7
- data.tar.gz: 9d0d6711c70947f4a11f66b59523048982f5ab05914b35269be018ad415f789c959cff9977c6bb8fe5f43f4e53e126b7934881544b059db1c6c583952db28b5e
6
+ metadata.gz: fec219f23757f68e03678195ac3f66f13d3d48897e30c3f4b141c12257eb394577c997ecc639110ac3fee8be57e90f915c94989e84116a9be76d9d1e5df47f52
7
+ data.tar.gz: 1956d02db12c7ea5eab5edff52dc48356f163f71a17ab08fc93e6dcae810d29df26ea7501ddc0e8ac609087981d2456e4b927bcdb01acfeff455adf4671b58b0
data/CHANGELOG.md CHANGED
@@ -1,3 +1,21 @@
1
+ # [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
+
3
+
4
+ ### Features
5
+
6
+ * 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))
7
+
8
+
9
+
10
+ ## [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)
11
+
12
+
13
+ ### Features
14
+
15
+ * map citext from PostgreSQL to string ([#7](https://github.com/ElMassimo/types_from_serializers/issues/7)) ([d8c6848](https://github.com/ElMassimo/types_from_serializers/commit/d8c6848b99b0f4ba3770871f491755c229a2c4b0))
16
+
17
+
18
+
1
19
  ## [2.0.1](https://github.com/ElMassimo/types_from_serializers/compare/types_from_serializers@2.0.0...types_from_serializers@2.0.1) (2023-04-03)
2
20
 
3
21
 
@@ -94,6 +94,7 @@ module TypesFromSerializers
94
94
  :sql_to_typescript_type_mapping,
95
95
  :skip_serializer_if,
96
96
  :transform_keys,
97
+ :namespace,
97
98
  keyword_init: true,
98
99
  ) do
99
100
  def relative_custom_types_dir
@@ -140,10 +141,11 @@ module TypesFromSerializers
140
141
  end
141
142
 
142
143
  def as_typescript
143
- <<~TS
144
+ indent = TypesFromSerializers.config.namespace ? 3 : 1
145
+ <<~TS.gsub(/\n$/, "")
144
146
  interface #{name} {
145
- #{properties.index_by(&:name).values.map(&:as_typescript).join("\n ")}
146
- }
147
+ #{" " * indent}#{properties.index_by(&:name).values.map(&:as_typescript).join("\n#{" " * indent}")}
148
+ #{" " * (indent - 1)}}
147
149
  TS
148
150
  end
149
151
 
@@ -269,7 +271,12 @@ module TypesFromSerializers
269
271
  def generate(force: ENV["SERIALIZER_TYPES_FORCE"])
270
272
  @force_generation = force
271
273
  config.output_dir.rmtree if force && config.output_dir.exist?
272
- generate_index_file
274
+
275
+ if config.namespace
276
+ load_serializers(all_serializer_files) if force
277
+ else
278
+ generate_index_file
279
+ end
273
280
 
274
281
  loaded_serializers.each do |serializer|
275
282
  generate_interface_for(serializer)
@@ -289,7 +296,7 @@ module TypesFromSerializers
289
296
  def generate_interface_for(serializer)
290
297
  interface = serializer.ts_interface
291
298
 
292
- write_if_changed(filename: interface.filename, cache_key: interface.inspect) {
299
+ write_if_changed(filename: interface.filename, cache_key: interface.inspect, extension: config.namespace ? "d.ts" : "ts") {
293
300
  serializer_interface_content(interface)
294
301
  }
295
302
  end
@@ -380,12 +387,16 @@ module TypesFromSerializers
380
387
  integer: :number,
381
388
  string: :string,
382
389
  text: :string,
390
+ citext: :string,
383
391
  }.tap do |types|
384
392
  types.default = :unknown
385
393
  end,
386
394
 
387
395
  # Allows to transform keys, useful when converting objects client-side.
388
396
  transform_keys: nil,
397
+
398
+ # Allows scoping typescript definitions to a namespace
399
+ namespace: nil,
389
400
  )
390
401
  end
391
402
 
@@ -393,8 +404,8 @@ module TypesFromSerializers
393
404
  # The cache strategy consists of a comment on the first line of the file.
394
405
  #
395
406
  # Yields to receive the rendered file content when it needs to.
396
- def write_if_changed(filename:, cache_key:)
397
- filename = config.output_dir.join("#{filename}.ts")
407
+ def write_if_changed(filename:, cache_key:, extension: "ts")
408
+ filename = config.output_dir.join("#{filename}.#{extension}")
398
409
  FileUtils.mkdir_p(filename.dirname)
399
410
  cache_key_comment = "// TypesFromSerializers CacheKey #{Digest::MD5.hexdigest(cache_key)}\n"
400
411
  File.open(filename, "a+") { |file|
@@ -417,6 +428,10 @@ module TypesFromSerializers
417
428
  end
418
429
 
419
430
  def serializer_interface_content(interface)
431
+ config.namespace ? declaration_interface_definition(interface) : standard_interface_definition(interface)
432
+ end
433
+
434
+ def standard_interface_definition(interface)
420
435
  <<~TS
421
436
  //
422
437
  // DO NOT MODIFY: This file was automatically generated by TypesFromSerializers.
@@ -425,6 +440,19 @@ module TypesFromSerializers
425
440
  TS
426
441
  end
427
442
 
443
+ def declaration_interface_definition(interface)
444
+ <<~TS
445
+ //
446
+ // DO NOT MODIFY: This file was automatically generated by TypesFromSerializers.
447
+ #{interface.used_imports.empty? ? "export {}\n" : interface.used_imports.join}
448
+ declare global {
449
+ namespace #{config.namespace} {
450
+ #{interface.as_typescript}
451
+ }
452
+ }
453
+ TS
454
+ end
455
+
428
456
  # Internal: Returns true if the cache key has changed since the last codegen.
429
457
  def stale?(file, cache_key_comment)
430
458
  @force_generation || file.gets != cache_key_comment
@@ -2,5 +2,5 @@
2
2
 
3
3
  module TypesFromSerializers
4
4
  # Public: This library adheres to semantic versioning.
5
- VERSION = "2.0.1"
5
+ VERSION = "2.1.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.0.1
4
+ version: 2.1.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-04-03 00:00:00.000000000 Z
11
+ date: 2023-07-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: railties
@@ -72,20 +72,6 @@ dependencies:
72
72
  - - "~>"
73
73
  - !ruby/object:Gem::Version
74
74
  version: '2'
75
- - !ruby/object:Gem::Dependency
76
- name: pry-byebug
77
- requirement: !ruby/object:Gem::Requirement
78
- requirements:
79
- - - "~>"
80
- - !ruby/object:Gem::Version
81
- version: '3.9'
82
- type: :development
83
- prerelease: false
84
- version_requirements: !ruby/object:Gem::Requirement
85
- requirements:
86
- - - "~>"
87
- - !ruby/object:Gem::Version
88
- version: '3.9'
89
75
  - !ruby/object:Gem::Dependency
90
76
  name: rake
91
77
  requirement: !ruby/object:Gem::Requirement
@@ -291,7 +277,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
291
277
  - !ruby/object:Gem::Version
292
278
  version: '0'
293
279
  requirements: []
294
- rubygems_version: 3.3.7
280
+ rubygems_version: 3.2.32
295
281
  signing_key:
296
282
  specification_version: 4
297
283
  summary: Generate TypeScript interfaces from your JSON serializers.