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 +4 -4
- data/CHANGELOG.md +40 -1
- data/README.md +3 -10
- data/lib/typespec_from_serializers/dsl/controller.rb +43 -0
- data/lib/typespec_from_serializers/dsl/serializer.rb +74 -0
- data/lib/typespec_from_serializers/dsl.rb +2 -44
- data/lib/typespec_from_serializers/generator.rb +836 -120
- data/lib/typespec_from_serializers/io.rb +30 -0
- data/lib/typespec_from_serializers/openapi_compiler.rb +62 -0
- data/lib/typespec_from_serializers/railtie.rb +91 -3
- data/lib/typespec_from_serializers/rbi.rb +186 -0
- data/lib/typespec_from_serializers/runner.rb +54 -0
- data/lib/typespec_from_serializers/sorbet.rb +461 -0
- data/lib/typespec_from_serializers/version.rb +1 -1
- data/lib/typespec_from_serializers.rb +2 -0
- metadata +51 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 85c22cdd4d7f9bb9402cceab726de827f7ebcc9692e1c1a19f7575fb429ca48e
|
|
4
|
+
data.tar.gz: 8a7ebd811666bec1c591802c126794c7761c8ab7f6abbb54f56c5ddef5f54030
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 6240113cea4819efe395d5b7c259430cfc949822a2f8b11ebd0e6dfdef2e349596170da79bcdce8bc82dde3ba8290258d5d7061a7056cdacd7bd25e9f90fe5d3
|
|
7
|
+
data.tar.gz: e81ff91f9b8de9e81b61b796b1f201e37c2ae59dffc7300ce7c927a7c07c017001bd6d81ee8fc34be849bb937bc0ae1895895a00be180899272a9f7ad5ce23c6
|
data/CHANGELOG.md
CHANGED
|
@@ -1,4 +1,43 @@
|
|
|
1
|
-
#
|
|
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://
|
|
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
|
-
[
|
|
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
|
-
|
|
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
|
-
|
|
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"
|