typespec_from_serializers 0.1.0 → 0.2.0

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: 8e14e0d2601ec8ee7b5efa388163e3997830d825b6f98f9df7e40ecd0248a8f6
4
- data.tar.gz: d15e88b3aa25f587697c2614b867ccaa31b3438c08d11ca12bd17ddf752313b8
3
+ metadata.gz: 85c22cdd4d7f9bb9402cceab726de827f7ebcc9692e1c1a19f7575fb429ca48e
4
+ data.tar.gz: 8a7ebd811666bec1c591802c126794c7761c8ab7f6abbb54f56c5ddef5f54030
5
5
  SHA512:
6
- metadata.gz: a2a246f2e90ce1fb583ce3bd1abc4c621c36f69fca705c853360e371beb22f668aa51ba043e4b7c3fdec9e2055577dd5df9b92c645984a4af81543aec546826b
7
- data.tar.gz: 47249bf5f3c9f9af0c801790f65d0d8aa829071770f7590c15897c73fe286af61922bf92d9985bea9acb201c7d43b8c9fb5d84f2a5e71bb9720dd2a2c1d90fce
6
+ metadata.gz: 6240113cea4819efe395d5b7c259430cfc949822a2f8b11ebd0e6dfdef2e349596170da79bcdce8bc82dde3ba8290258d5d7061a7056cdacd7bd25e9f90fe5d3
7
+ data.tar.gz: e81ff91f9b8de9e81b61b796b1f201e37c2ae59dffc7300ce7c927a7c07c017001bd6d81ee8fc34be849bb937bc0ae1895895a00be180899272a9f7ad5ce23c6
data/CHANGELOG.md CHANGED
@@ -1,4 +1,43 @@
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.0] - 2025-12-19
4
+
5
+ Initial release - fork of `types_from_serializers` adapted to generate TypeSpec instead of TypeScript.
6
+
7
+ ### Features
8
+
9
+ **Routes Generation**
10
+ - Generate TypeSpec HTTP interfaces from Rails routes automatically
11
+ - Type path parameters in `routes.rb`: `type: { id: Integer }`
12
+ - Type request body in controllers with `type` DSL
13
+ - Nested routes organized in namespaces
14
+ - Filter exported routes with `export_if` configuration
15
+
16
+ **Sorbet Integration**
17
+ - Automatic type inference from Sorbet signatures
18
+ - Support for `T.nilable`, `T::Array`, unions, and complex shapes
19
+ - Works with RBI files (Tapioca-generated)
20
+
21
+ **Type System**
22
+ - Simple `type:` syntax for explicit types
23
+ - Union types: `type: "string | int32"`
24
+ - Automatic inference from SQL schema and ActiveRecord enums
25
+ - Custom TypeSpec model references
26
+
27
+ **OpenAPI**
28
+ - Compile TypeSpec to OpenAPI 3.0: `rake typespec_from_serializers:compile_openapi`
29
+ - Setup dependencies: `rake typespec_from_serializers:setup`
30
+
31
+ **Developer Experience**
32
+ - Automatic regeneration in development mode
33
+ - Vite live reload support
34
+ - Incremental generation for modified files only
35
+
36
+ ---
37
+
38
+ # Previous versions (types_from_serializers)
39
+
40
+ ## [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
41
 
3
42
 
4
43
  ### Features
data/README.md CHANGED
@@ -1,22 +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>
6
- <a href="https://codeclimate.com/github/dannote/typespec_from_serializers"><img alt="Maintainability" src="https://codeclimate.com/github/dannote/typespec_from_serializers/badges/gpa.svg"/></a>
7
- <a href="https://codeclimate.com/github/dannote/typespec_from_serializers"><img alt="Test Coverage" src="https://codeclimate.com/github/dannote/typespec_from_serializers/badges/coverage.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>
8
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>
9
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>
10
7
  </p>
11
8
  </h1>
12
9
 
13
- [aliases]: https://vite-ruby.netlify.app/guide/development.html#import-aliases-%F0%9F%91%89
14
- [config options]: https://github.com/dannote/typespec_from_serializers/blob/main/lib/typespec_from_serializers/generator.rb#L82-L85
15
- [readme]: https://github.com/dannote/typespec_from_serializers
10
+ Automatically generate [TypeSpec](https://typespec.io) definitions from Ruby serializers and Rails routes.
16
11
 
17
- **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.
18
-
19
- For more information, check the main [README].
12
+ For full documentation, see the [main README](https://github.com/dannote/typespec_from_serializers).
20
13
 
21
14
  ### Installation 💿
22
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"