types_from_serializers 0.1.0 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +28 -4
- data/lib/types_from_serializers/generator.rb +34 -19
- data/lib/types_from_serializers/version.rb +1 -1
- metadata +71 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ddc6e7ca4c4edfab72abe33e0a3688871bd8e746f00f0ea00c832e9b4bad700e
|
4
|
+
data.tar.gz: cf128002a889f0446b6ddc0c8a212dbbd5885af27c6aee1552a8de9b5f723a22
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4f05eada087b1d751525c2439ad60918e0179fce0766230d75502e3ad1d8970af74057e1262c89eaf04b372885574b01d645f852b284d56f73bb88eeab6e072e
|
7
|
+
data.tar.gz: 98c5830eb74ae87c10313ad07eb4f72f35f444f8f4c75db8e79640200017e67dff65e45464aa24afcfb68fad3261735ee7ae3b9e3a155a090e91160c8bb9fac3
|
data/CHANGELOG.md
CHANGED
@@ -1,15 +1,39 @@
|
|
1
|
-
|
1
|
+
## [0.1.3](https://github.com/ElMassimo/types_from_serializers/compare/types_from_serializers@0.1.2...types_from_serializers@0.1.3) (2022-07-12)
|
2
|
+
|
3
|
+
|
4
|
+
### Features
|
5
|
+
|
6
|
+
* apply the sql mapping fallback as the default ([64898c4](https://github.com/ElMassimo/types_from_serializers/commit/64898c4e3a3f83ea67294f2200f253cd2a64aea9))
|
7
|
+
|
8
|
+
|
9
|
+
|
10
|
+
## [0.1.2](https://github.com/ElMassimo/types_from_serializers/compare/types_from_serializers@0.1.1...types_from_serializers@0.1.2) (2022-07-12)
|
2
11
|
|
3
12
|
|
4
13
|
### Bug Fixes
|
5
14
|
|
6
|
-
*
|
15
|
+
* avoid having the full file path in the cache key ([556f8f6](https://github.com/ElMassimo/types_from_serializers/commit/556f8f667608fa950a3ad0647540055b1b5f1dc8))
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
## [0.1.1](https://github.com/ElMassimo/types_from_serializers/compare/types_from_serializers@0.1.0...types_from_serializers@0.1.1) (2022-07-12)
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
# 0.1.0 (2022-07-12)
|
7
24
|
|
8
25
|
|
9
26
|
### Features
|
10
27
|
|
11
|
-
|
12
|
-
|
28
|
+
- Start simple, no additional syntax required
|
29
|
+
- Infers types from a related `ActiveRecord` model, using the SQL schema
|
30
|
+
- Understands JS native types and how to map SQL columns: `string`, `boolean`, etc
|
31
|
+
- Automatically types [associations](https://github.com/ElMassimo/oj_serializers#associations-dsl-), importing the generated types for the referenced serializers
|
32
|
+
- Detects [conditional attributes](https://github.com/ElMassimo/oj_serializers#rendering-an-attribute-conditionally) and marks them as optional: `name?: string`
|
33
|
+
- Fallback to a custom interface using `type_from`
|
34
|
+
- Supports custom types and automatically adds the necessary imports
|
35
|
+
- handle non-ActiveRecord models and extract types from unions ([ea9b2a7](https://github.com/ElMassimo/types_from_serializers/commit/ea9b2a71cb85503ff691e5ef115ab73f89b005af))
|
36
|
+
- support specifying base serializers and additional dirs to scan ([164cfe1](https://github.com/ElMassimo/types_from_serializers/commit/164cfe17bb0527c59cf95441381aef7bf797a568))
|
13
37
|
|
14
38
|
|
15
39
|
|
@@ -15,7 +15,11 @@ module TypesFromSerializers
|
|
15
15
|
:native_types,
|
16
16
|
:sql_to_typescript_type_mapping,
|
17
17
|
keyword_init: true,
|
18
|
-
)
|
18
|
+
) do
|
19
|
+
def unknown_type
|
20
|
+
sql_to_typescript_type_mapping.default
|
21
|
+
end
|
22
|
+
end
|
19
23
|
|
20
24
|
# Internal: The type metadata for a serializer.
|
21
25
|
SerializerMetadata = Struct.new(
|
@@ -47,9 +51,10 @@ module TypesFromSerializers
|
|
47
51
|
to_s.classify.safe_constantize
|
48
52
|
end
|
49
53
|
|
50
|
-
|
54
|
+
def to_model
|
55
|
+
to_s.to_model
|
56
|
+
end
|
51
57
|
end
|
52
|
-
# rubocop:enable Rails/Delegate
|
53
58
|
|
54
59
|
refine Class do
|
55
60
|
# Internal: Name of the TypeScript interface.
|
@@ -133,7 +138,7 @@ module TypesFromSerializers
|
|
133
138
|
|
134
139
|
def typescript_fields(metadata)
|
135
140
|
(metadata.attributes + metadata.associations).map { |meta|
|
136
|
-
type = meta.type.is_a?(Class) ? meta.type.typescript_interface_name : meta.type ||
|
141
|
+
type = meta.type.is_a?(Class) ? meta.type.typescript_interface_name : meta.type || TypesFromSerializers.config.unknown_type
|
137
142
|
type = meta.many ? "#{type}[]" : type
|
138
143
|
" #{meta.typescript_name}#{"?" if meta.optional}: #{type}"
|
139
144
|
}
|
@@ -198,6 +203,7 @@ module TypesFromSerializers
|
|
198
203
|
# Public: Generates code for all serializers in the app.
|
199
204
|
def generate(force: ENV["SERIALIZER_TYPES_FORCE"])
|
200
205
|
@force_generation = force
|
206
|
+
config.output_dir.rmtree if force && config.output_dir.exist?
|
201
207
|
generate_index_file
|
202
208
|
|
203
209
|
loaded_serializers.each do |serializer|
|
@@ -221,26 +227,16 @@ module TypesFromSerializers
|
|
221
227
|
|
222
228
|
write_if_changed(filename: filename, cache_key: metadata.inspect) {
|
223
229
|
serializer.typescript_infer_types(metadata)
|
224
|
-
|
225
|
-
//
|
226
|
-
// DO NOT MODIFY: This file was automatically generated by TypesFromSerializers.
|
227
|
-
#{serializer.typescript_imports(metadata).join}
|
228
|
-
export default interface #{serializer.typescript_interface_name} {
|
229
|
-
#{serializer.typescript_fields(metadata).join("\n")}
|
230
|
-
}
|
231
|
-
TS
|
230
|
+
serializer_interface_content(serializer, metadata)
|
232
231
|
}
|
233
232
|
end
|
234
233
|
|
235
234
|
# Internal: Allows to import all serializer types from a single file.
|
236
235
|
def generate_index_file
|
237
|
-
|
236
|
+
cache_key = all_serializer_files.map { |file| file.delete_prefix(root.to_s) }.join
|
237
|
+
write_if_changed(filename: "index", cache_key: cache_key) {
|
238
238
|
load_serializers(all_serializer_files)
|
239
|
-
|
240
|
-
//
|
241
|
-
// DO NOT MODIFY: This file was automatically generated by TypesFromSerializers.
|
242
|
-
#{loaded_serializers.map { |s| "export type { default as #{s.typescript_interface_name} } from './#{s.typescript_interface_basename}'" }.join("\n")}
|
243
|
-
TS
|
239
|
+
serializers_index_content(loaded_serializers)
|
244
240
|
}
|
245
241
|
end
|
246
242
|
|
@@ -265,7 +261,7 @@ module TypesFromSerializers
|
|
265
261
|
end
|
266
262
|
|
267
263
|
def all_serializer_files
|
268
|
-
config.serializers_dirs.flat_map { |dir| Dir["#{dir}/**/*.rb"] }
|
264
|
+
config.serializers_dirs.flat_map { |dir| Dir["#{dir}/**/*.rb"] }.sort
|
269
265
|
end
|
270
266
|
|
271
267
|
def load_serializers(files)
|
@@ -335,6 +331,25 @@ module TypesFromSerializers
|
|
335
331
|
}
|
336
332
|
end
|
337
333
|
|
334
|
+
def serializers_index_content(serializers)
|
335
|
+
<<~TS
|
336
|
+
//
|
337
|
+
// DO NOT MODIFY: This file was automatically generated by TypesFromSerializers.
|
338
|
+
#{serializers.map { |s| "export type { default as #{s.typescript_interface_name} } from './#{s.typescript_interface_basename}'" }.join("\n")}
|
339
|
+
TS
|
340
|
+
end
|
341
|
+
|
342
|
+
def serializer_interface_content(serializer, metadata)
|
343
|
+
<<~TS
|
344
|
+
//
|
345
|
+
// DO NOT MODIFY: This file was automatically generated by TypesFromSerializers.
|
346
|
+
#{serializer.typescript_imports(metadata).join}
|
347
|
+
export default interface #{serializer.typescript_interface_name} {
|
348
|
+
#{serializer.typescript_fields(metadata).join("\n")}
|
349
|
+
}
|
350
|
+
TS
|
351
|
+
end
|
352
|
+
|
338
353
|
# Internal: Returns true if the cache key has changed since the last codegen.
|
339
354
|
def stale?(file, cache_key_comment)
|
340
355
|
@force_generation || file.gets != cache_key_comment
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: types_from_serializers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Máximo Mussini
|
@@ -114,6 +114,20 @@ dependencies:
|
|
114
114
|
- - "~>"
|
115
115
|
- !ruby/object:Gem::Version
|
116
116
|
version: '3.8'
|
117
|
+
- !ruby/object:Gem::Dependency
|
118
|
+
name: rspec-snapshot
|
119
|
+
requirement: !ruby/object:Gem::Requirement
|
120
|
+
requirements:
|
121
|
+
- - ">="
|
122
|
+
- !ruby/object:Gem::Version
|
123
|
+
version: '0'
|
124
|
+
type: :development
|
125
|
+
prerelease: false
|
126
|
+
version_requirements: !ruby/object:Gem::Requirement
|
127
|
+
requirements:
|
128
|
+
- - ">="
|
129
|
+
- !ruby/object:Gem::Version
|
130
|
+
version: '0'
|
117
131
|
- !ruby/object:Gem::Dependency
|
118
132
|
name: simplecov
|
119
133
|
requirement: !ruby/object:Gem::Requirement
|
@@ -142,6 +156,62 @@ dependencies:
|
|
142
156
|
- - "~>"
|
143
157
|
- !ruby/object:Gem::Version
|
144
158
|
version: '1.0'
|
159
|
+
- !ruby/object:Gem::Dependency
|
160
|
+
name: activerecord
|
161
|
+
requirement: !ruby/object:Gem::Requirement
|
162
|
+
requirements:
|
163
|
+
- - ">="
|
164
|
+
- !ruby/object:Gem::Version
|
165
|
+
version: '0'
|
166
|
+
type: :development
|
167
|
+
prerelease: false
|
168
|
+
version_requirements: !ruby/object:Gem::Requirement
|
169
|
+
requirements:
|
170
|
+
- - ">="
|
171
|
+
- !ruby/object:Gem::Version
|
172
|
+
version: '0'
|
173
|
+
- !ruby/object:Gem::Dependency
|
174
|
+
name: js_from_routes
|
175
|
+
requirement: !ruby/object:Gem::Requirement
|
176
|
+
requirements:
|
177
|
+
- - ">="
|
178
|
+
- !ruby/object:Gem::Version
|
179
|
+
version: '0'
|
180
|
+
type: :development
|
181
|
+
prerelease: false
|
182
|
+
version_requirements: !ruby/object:Gem::Requirement
|
183
|
+
requirements:
|
184
|
+
- - ">="
|
185
|
+
- !ruby/object:Gem::Version
|
186
|
+
version: '0'
|
187
|
+
- !ruby/object:Gem::Dependency
|
188
|
+
name: sqlite3
|
189
|
+
requirement: !ruby/object:Gem::Requirement
|
190
|
+
requirements:
|
191
|
+
- - ">="
|
192
|
+
- !ruby/object:Gem::Version
|
193
|
+
version: '0'
|
194
|
+
type: :development
|
195
|
+
prerelease: false
|
196
|
+
version_requirements: !ruby/object:Gem::Requirement
|
197
|
+
requirements:
|
198
|
+
- - ">="
|
199
|
+
- !ruby/object:Gem::Version
|
200
|
+
version: '0'
|
201
|
+
- !ruby/object:Gem::Dependency
|
202
|
+
name: rubocop
|
203
|
+
requirement: !ruby/object:Gem::Requirement
|
204
|
+
requirements:
|
205
|
+
- - ">="
|
206
|
+
- !ruby/object:Gem::Version
|
207
|
+
version: '0'
|
208
|
+
type: :development
|
209
|
+
prerelease: false
|
210
|
+
version_requirements: !ruby/object:Gem::Requirement
|
211
|
+
requirements:
|
212
|
+
- - ">="
|
213
|
+
- !ruby/object:Gem::Version
|
214
|
+
version: '0'
|
145
215
|
- !ruby/object:Gem::Dependency
|
146
216
|
name: rubocop-rails
|
147
217
|
requirement: !ruby/object:Gem::Requirement
|