types_from_serializers 2.0.2 → 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: b2d8cc0fd6d5da180f0420fbabea0c24d428de7bec297658740d1ce9fbdbb551
4
- data.tar.gz: 350a9692de1a188757db7b0efbd0be88fa8da038959070e8ff966a27f2d816bd
3
+ metadata.gz: 61ca195394efd6b1c37c31b81ff85f7ccbcba562e9cf7b76d7810dfc613e1aa6
4
+ data.tar.gz: 20ef589a30a448e668d1da4106ab565e768227e74f950c5d56bfbd2a83aa9a48
5
5
  SHA512:
6
- metadata.gz: a3222cd4804c0cc431b8b300f21b3b0758035cbdd66872b259571e754ad5cba32716a4bb30662809e244a6e5684047172b26712b28bc58c1f192a7e7d6ac5544
7
- data.tar.gz: 1de827c5f9b4981644b2954e5579eeebe2bea02a8a1463c9e57614555e034d4afa998bf2a14518080fced93d3faa2de943dbf9c1b12d9b7977f986086fe45097
6
+ metadata.gz: fec219f23757f68e03678195ac3f66f13d3d48897e30c3f4b141c12257eb394577c997ecc639110ac3fee8be57e90f915c94989e84116a9be76d9d1e5df47f52
7
+ data.tar.gz: 1956d02db12c7ea5eab5edff52dc48356f163f71a17ab08fc93e6dcae810d29df26ea7501ddc0e8ac609087981d2456e4b927bcdb01acfeff455adf4671b58b0
data/CHANGELOG.md CHANGED
@@ -1,3 +1,12 @@
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
+
1
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)
2
11
 
3
12
 
@@ -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
@@ -387,6 +394,9 @@ module TypesFromSerializers
387
394
 
388
395
  # Allows to transform keys, useful when converting objects client-side.
389
396
  transform_keys: nil,
397
+
398
+ # Allows scoping typescript definitions to a namespace
399
+ namespace: nil,
390
400
  )
391
401
  end
392
402
 
@@ -394,8 +404,8 @@ module TypesFromSerializers
394
404
  # The cache strategy consists of a comment on the first line of the file.
395
405
  #
396
406
  # 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}.ts")
407
+ def write_if_changed(filename:, cache_key:, extension: "ts")
408
+ filename = config.output_dir.join("#{filename}.#{extension}")
399
409
  FileUtils.mkdir_p(filename.dirname)
400
410
  cache_key_comment = "// TypesFromSerializers CacheKey #{Digest::MD5.hexdigest(cache_key)}\n"
401
411
  File.open(filename, "a+") { |file|
@@ -418,6 +428,10 @@ module TypesFromSerializers
418
428
  end
419
429
 
420
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)
421
435
  <<~TS
422
436
  //
423
437
  // DO NOT MODIFY: This file was automatically generated by TypesFromSerializers.
@@ -426,6 +440,19 @@ module TypesFromSerializers
426
440
  TS
427
441
  end
428
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
+
429
456
  # Internal: Returns true if the cache key has changed since the last codegen.
430
457
  def stale?(file, cache_key_comment)
431
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.2"
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.2
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-05 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
@@ -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.3.7
280
+ rubygems_version: 3.2.32
281
281
  signing_key:
282
282
  specification_version: 4
283
283
  summary: Generate TypeScript interfaces from your JSON serializers.