diff --git a/lib/typelizer.rb b/lib/typelizer.rb index 46636cf..e91d3a7 100644 --- a/lib/typelizer.rb +++ b/lib/typelizer.rb @@ -18,6 +18,8 @@ require_relative "typelizer/railtie" if defined?(Rails) +require "logger" + module Typelizer class << self def enabled? diff --git a/lib/typelizer/config.rb b/lib/typelizer/config.rb index 4b5b3f7..b5c634e 100644 --- a/lib/typelizer/config.rb +++ b/lib/typelizer/config.rb @@ -29,7 +29,11 @@ class Config < Struct.new( class << self def instance @instance ||= new( - serializer_name_mapper: ->(serializer) { serializer.name.ends_with?("Serializer") ? serializer.name.delete_suffix("Serializer") : serializer.name.delete_suffix("Resource") }, + serializer_name_mapper: ->(serializer) do + return "" if serializer.name.nil? + + serializer.name.ends_with?("Serializer") ? serializer.name&.delete_suffix("Serializer") : serializer.name&.delete_suffix("Resource") + end, serializer_model_mapper: ->(serializer) do base_class = serializer_name_mapper.call(serializer) Object.const_get(base_class) if Object.const_defined?(base_class) diff --git a/lib/typelizer/generator.rb b/lib/typelizer/generator.rb index 921775e..2cbb6aa 100644 --- a/lib/typelizer/generator.rb +++ b/lib/typelizer/generator.rb @@ -33,8 +33,9 @@ def target_serializers raise ArgumentError, "Please ensure all your serializers include Typelizer::DSL." if base_classes.none? end - (base_classes + base_classes.flat_map(&:descendants)).uniq.sort_by(&:name) - .reject { |serializer| Typelizer.reject_class.call(serializer: serializer) } + (base_classes + base_classes.flat_map(&:descendants)).uniq + .reject { |serializer| Typelizer.reject_class.call(serializer: serializer) || serializer.name.nil? } + .sort_by(&:name) end def read_serializers(files = nil) diff --git a/lib/typelizer/inline_type.rb b/lib/typelizer/inline_type.rb new file mode 100644 index 0000000..5f5f3f0 --- /dev/null +++ b/lib/typelizer/inline_type.rb @@ -0,0 +1,22 @@ +module Typelizer + # InlineType is mainly used with Alba plugin to represent inline associations. + # `name `method` is the same interface as `Interface` class. + class InlineType + TEMPLATE = <<~ERB.strip + { + <%- properties.each do |property| %> + <%= property %>; + <% end %> + } + ERB + def initialize(serializer:, config:) + @serializer = serializer + @config = config + end + + def name + properties = SerializerPlugins::Alba.new(serializer: @serializer, config: @config).properties + ERB.new(TEMPLATE, trim_mode: "-").result_with_hash(properties: properties) + end + end +end diff --git a/lib/typelizer/interface.rb b/lib/typelizer/interface.rb index 11d62a3..c0f7625 100644 --- a/lib/typelizer/interface.rb +++ b/lib/typelizer/interface.rb @@ -54,6 +54,7 @@ def imports .filter_map { |interface| interface.name if interface.name != name } custom_type_imports = attribute_types + .reject { |type| type.is_a?(InlineType) } .flat_map { |type| extract_typescript_types(type.to_s) } .uniq .reject { |type| global_type?(type) } diff --git a/lib/typelizer/serializer_plugins/alba.rb b/lib/typelizer/serializer_plugins/alba.rb index dfb4830..b61dd98 100644 --- a/lib/typelizer/serializer_plugins/alba.rb +++ b/lib/typelizer/serializer_plugins/alba.rb @@ -1,4 +1,5 @@ require_relative "base" +require_relative "../inline_type" module Typelizer module SerializerPlugins @@ -74,9 +75,10 @@ def build_property(name, attr, **options) ) when ::Alba::Association resource = attr.instance_variable_get(:@resource) + type = (resource.is_a?(Class) && !resource.name.nil?) ? Interface.new(serializer: resource) : InlineType.new(serializer: resource, config: {}) Property.new( name: name, - type: Interface.new(serializer: resource), + type: type, optional: false, nullable: false, multi: false, # we override this in typelize_method_transform