typespec_from_serializers 0.1.1 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a1f7f17073b80dd99f290b8f40ddc843abca595efe3609e937717238068820a0
4
- data.tar.gz: 57efeb77eb954a5a2dbcd6adf2752296673f3bee488bbbea47b26cbd6e4fadc4
3
+ metadata.gz: 86e41505961e13ef72df62124721ae6d1c929104726378b893c2c785dcec7da0
4
+ data.tar.gz: 985c5514cdc77c02ee25a4257c8c3fc7d3c8adb7d7ea7585d1d64c3f3af1f732
5
5
  SHA512:
6
- metadata.gz: 03df9644da15df1f324c5054363d8dd95423427f90ae33859e50bfaeaa69f45542f0fff5d3c99c93c7bb8058dd4ce7d2d0f55e42107428a4b7c6cc0927338538
7
- data.tar.gz: 61dc21244c335793da7228f310c530b2c65e81433ed62d20983b9ab720423ed91d6f72a4032221eb3431e29b73a1d8010acb91cb44e2706173595b7dc6c62215
6
+ metadata.gz: d51adab2df0d34f587eb375950cafd6231fd5e94b8e1359086d8b7fd5745c6c200900217c7e95edab0a855a07029aaf5147fde3f89639c9843770298d03ab031
7
+ data.tar.gz: 06fd0b0629657ae60d101bca4ed879cb5134801ce91097e87acc8e14825f7f2a290211dd282801bcd8154b4d7f1514ab2f24e83cfd5fd4f22be18db711bb6399
data/CHANGELOG.md CHANGED
@@ -1,4 +1,49 @@
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)
1
+ # TypeSpec From Serializers Changelog
2
+
3
+ ## [0.2.1] - 2025-12-19
4
+
5
+ ### Fixed
6
+ - Correct `@service` decorator syntax to use `#{}` for object literals
7
+ - Add newline after `@service` decorator for better readability
8
+
9
+ ## [0.2.0] - 2025-12-19
10
+
11
+ Initial release - fork of `types_from_serializers` adapted to generate TypeSpec instead of TypeScript.
12
+
13
+ ### Features
14
+
15
+ **Routes Generation**
16
+ - Generate TypeSpec HTTP interfaces from Rails routes automatically
17
+ - Type path parameters in `routes.rb`: `type: { id: Integer }`
18
+ - Type request body in controllers with `type` DSL
19
+ - Nested routes organized in namespaces
20
+ - Filter exported routes with `export_if` configuration
21
+
22
+ **Sorbet Integration**
23
+ - Automatic type inference from Sorbet signatures
24
+ - Support for `T.nilable`, `T::Array`, unions, and complex shapes
25
+ - Works with RBI files (Tapioca-generated)
26
+
27
+ **Type System**
28
+ - Simple `type:` syntax for explicit types
29
+ - Union types: `type: "string | int32"`
30
+ - Automatic inference from SQL schema and ActiveRecord enums
31
+ - Custom TypeSpec model references
32
+
33
+ **OpenAPI**
34
+ - Compile TypeSpec to OpenAPI 3.0: `rake typespec_from_serializers:compile_openapi`
35
+ - Setup dependencies: `rake typespec_from_serializers:setup`
36
+
37
+ **Developer Experience**
38
+ - Automatic regeneration in development mode
39
+ - Vite live reload support
40
+ - Incremental generation for modified files only
41
+
42
+ ---
43
+
44
+ # Previous versions (types_from_serializers)
45
+
46
+ ## [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
47
 
3
48
 
4
49
  ### Features
data/README.md CHANGED
@@ -1,20 +1,15 @@
1
1
  <h1 align="center">
2
2
  TypeSpec From Serializers
3
3
  <p align="center">
4
- <a href="https://travis-ci.org/dannote/typespec_from_serializers"><img alt="Build Status" src="https://travis-ci.org/dannote/typespec_from_serializers.svg"/></a>
5
- <a href="http://inch-ci.org/github/dannote/typespec_from_serializers"><img alt="Inline docs" src="http://inch-ci.org/github/dannote/typespec_from_serializers.svg"/></a>
4
+ <a href="https://github.com/dannote/typespec_from_serializers/actions"><img alt="Build Status" src="https://github.com/dannote/typespec_from_serializers/workflows/build/badge.svg"/></a>
6
5
  <a href="https://rubygems.org/gems/typespec_from_serializers"><img alt="Gem Version" src="https://img.shields.io/gem/v/typespec_from_serializers.svg?colorB=e9573f"/></a>
7
6
  <a href="https://github.com/dannote/typespec_from_serializers/blob/main/LICENSE.txt"><img alt="License" src="https://img.shields.io/badge/license-MIT-428F7E.svg"/></a>
8
7
  </p>
9
8
  </h1>
10
9
 
11
- [aliases]: https://vite-ruby.netlify.app/guide/development.html#import-aliases-%F0%9F%91%89
12
- [config options]: https://github.com/dannote/typespec_from_serializers/blob/main/lib/typespec_from_serializers/generator.rb#L82-L85
13
- [readme]: https://github.com/dannote/typespec_from_serializers
10
+ Automatically generate [TypeSpec](https://typespec.io) definitions from Ruby serializers and Rails routes.
14
11
 
15
- **TypeSpec From Serializers** is a Ruby gem that automatically generates [TypeSpec](https://typespec.io) definitions from Ruby serializers and Rails routes. It is a derivative work of [`types_from_serializers`][types_from_serializers] by ElMassimo, originally designed to generate TypeScript definitions. This fork adapts the core functionality to produce TypeSpec descriptions, enabling Rails developers to define APIs compatible with TypeSpec’s ecosystem, including OpenAPI generation and client/server code scaffolding.
16
-
17
- For more information, check the main [README].
12
+ For full documentation, see the [main README](https://github.com/dannote/typespec_from_serializers).
18
13
 
19
14
  ### Installation 💿
20
15
 
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/concern"
4
+
5
+ module TypeSpecFromSerializers
6
+ module DSL
7
+ # DSL for Rails controllers to declare parameter types
8
+ module Controller
9
+ extend ActiveSupport::Concern
10
+
11
+ module ClassMethods
12
+ # DSL for declaring method return types (same as serializer DSL)
13
+ # Usage:
14
+ # type id: Integer, title: String
15
+ # def video_params
16
+ # # ...
17
+ # end
18
+ def type(type_definition)
19
+ @_pending_return_type = type_definition
20
+ end
21
+
22
+ # Internal: Captures pending type declaration when method is defined
23
+ def method_added(method_name)
24
+ super
25
+ if (pending_type = @_pending_return_type)
26
+ return_type_registry[method_name] = pending_type
27
+ @_pending_return_type = nil
28
+ end
29
+ end
30
+
31
+ # Internal: Registry storing method return type declarations
32
+ def return_type_registry
33
+ @return_type_registry ||= {}
34
+ end
35
+
36
+ # Public: Retrieve declared return type for a method
37
+ def type_for_method(method_name)
38
+ return_type_registry[method_name.to_sym]
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,74 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/concern"
4
+
5
+ # Internal: A DSL to specify types for serializer attributes.
6
+ module TypeSpecFromSerializers
7
+ module DSL
8
+ module Serializer
9
+ extend ActiveSupport::Concern
10
+
11
+ module ClassMethods
12
+ # Override: Capture the name of the model related to the serializer.
13
+ #
14
+ # name - An alias for the internal object in the serializer.
15
+ # model - The name of an ActiveRecord model to infer types from the schema.
16
+ # typespec_from - The name of a TypeScript model to infer types from.
17
+ def object_as(name, model: nil, typespec_from: nil)
18
+ # NOTE: Avoid taking memory for type information that won't be used.
19
+ if Rails.env.development?
20
+ model ||= name.is_a?(Symbol) ? name : try(:_serializer_model_name) || name
21
+ define_singleton_method(:_serializer_model_name) { model }
22
+ define_singleton_method(:_serializer_typespec_from) { typespec_from } if typespec_from
23
+ end
24
+
25
+ super(name)
26
+ end
27
+
28
+ # Public: Declare type for an attribute or method return value.
29
+ #
30
+ # type - Symbol for attribute type, or Class/Sorbet type for method return
31
+ # options - Additional options passed to `attribute` when type is a Symbol
32
+ #
33
+ # Examples
34
+ #
35
+ # type :string # Attribute type
36
+ # type String # Method return type
37
+ # type T.nilable(String) # Sorbet type
38
+ #
39
+ def type(type, **options)
40
+ type.is_a?(Symbol) ? attribute(type: type, **options) : @_pending_return_type = type
41
+ end
42
+
43
+ # Internal: Captures pending type declaration when method is defined.
44
+ def method_added(method_name)
45
+ super
46
+ if (pending_type = @_pending_return_type)
47
+ return_type_registry[method_name] = pending_type
48
+ @_pending_return_type = nil
49
+ end
50
+ end
51
+
52
+ # Internal: Registry storing method return type declarations.
53
+ def return_type_registry
54
+ @return_type_registry ||= {}
55
+ end
56
+
57
+ # Public: Retrieve declared return type for a method.
58
+ def type_for_method(method_name)
59
+ return_type_registry[method_name.to_sym]
60
+ end
61
+
62
+ private
63
+
64
+ # Override: Remove unnecessary options in production, types are only
65
+ # used when generating code in development.
66
+ unless Rails.env.development?
67
+ def add_attribute(name, type: nil, optional: nil, **options)
68
+ super(name, **options)
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end
@@ -1,46 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "active_support/concern"
4
-
5
- # Internal: A DSL to specify types for serializer attributes.
6
- module TypeSpecFromSerializers
7
- module DSL
8
- extend ActiveSupport::Concern
9
-
10
- module ClassMethods
11
- # Override: Capture the name of the model related to the serializer.
12
- #
13
- # name - An alias for the internal object in the serializer.
14
- # model - The name of an ActiveRecord model to infer types from the schema.
15
- # typespec_from - The name of a TypeScript model to infer types from.
16
- def object_as(name, model: nil, typespec_from: nil)
17
- # NOTE: Avoid taking memory for type information that won't be used.
18
- if Rails.env.development?
19
- model ||= name.is_a?(Symbol) ? name : try(:_serializer_model_name) || name
20
- define_singleton_method(:_serializer_model_name) { model }
21
- define_singleton_method(:_serializer_typespec_from) { typespec_from } if typespec_from
22
- end
23
-
24
- super(name)
25
- end
26
-
27
- # Public: Shortcut for typing a serializer attribute.
28
- #
29
- # It specifies the type for a serializer method that will be defined
30
- # immediately after calling this method.
31
- def type(type, **options)
32
- attribute type: type, **options
33
- end
34
-
35
- private
36
-
37
- # Override: Remove unnecessary options in production, types are only
38
- # used when generating code in development.
39
- unless Rails.env.development?
40
- def add_attribute(name, type: nil, optional: nil, **options)
41
- super(name, **options)
42
- end
43
- end
44
- end
45
- end
46
- end
3
+ require_relative "dsl/serializer"
4
+ require_relative "dsl/controller"