Skip to content

Commit

Permalink
feat: generate types for inline serializers, exclude them from the index
Browse files Browse the repository at this point in the history
closes #19
  • Loading branch information
ElMassimo committed Aug 23, 2024
1 parent 3b7502a commit 1c3657c
Show file tree
Hide file tree
Showing 12 changed files with 60 additions and 21 deletions.
2 changes: 1 addition & 1 deletion playground/vanilla/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: ../..
specs:
types_from_serializers (2.1.0)
types_from_serializers (2.2.0)
listen (~> 3.2)
oj_serializers (~> 2.0, >= 2.0.2)
railties (>= 5.1)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// TypesFromSerializers CacheKey 1e3a5dea8847b2dcfe76fec134589cbb
// TypesFromSerializers CacheKey 9111c6d881174b0013582fa6366868db
//
// DO NOT MODIFY: This file was automatically generated by TypesFromSerializers.
import type Model from './Model'
import type ComposerWithSongsSong from './ComposerWithSongs/Song'

export default interface ComposerWithSongs {
id: number
firstName?: string
lastName?: string
name: string
songs: Model[]
songs: ComposerWithSongsSong[]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// TypesFromSerializers CacheKey a69c0dbcc904740e5de89181ef4b2837
//
// DO NOT MODIFY: This file was automatically generated by TypesFromSerializers.

export default interface ComposerWithSongsSong {
id: number
title?: string
}
4 changes: 2 additions & 2 deletions playground/vanilla/app/frontend/types/serializers/Song.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// TypesFromSerializers CacheKey 460b4a83a2e81c9d693ac5490f9e0b76
// TypesFromSerializers CacheKey f4f9e398dc092747d13607e2bdcb0846
//
// DO NOT MODIFY: This file was automatically generated by TypesFromSerializers.
import type Composer from './Composer'

export default interface Song {
id: number
composer: Composer
genre: "disco" | "rock" | "classical"
genre: "fingerstyle" | "rock" | "classical"
tempo: "slow" | "medium" | "fast"
title?: string
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// TypesFromSerializers CacheKey 3aa811bd4673913bbd09f2967979b304
// TypesFromSerializers CacheKey c2d167325e7342eba01d0a78482d6d76
//
// DO NOT MODIFY: This file was automatically generated by TypesFromSerializers.
import type Composer from './Composer'
Expand All @@ -7,7 +7,7 @@ import type Video from './Video'
export default interface SongWithVideos {
id: number
composer: Composer
genre: "disco" | "rock" | "classical"
genre: "fingerstyle" | "rock" | "classical"
tempo: "slow" | "medium" | "fast"
title?: string
videos: Video[]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
class ComposerWithSongsSerializer < ComposerSerializer
has_many :songs, serializer: ModelSerializer
class SongSerializer < BaseSerializer
attributes(:id, :title)
end

has_many :songs, serializer: SongSerializer
end
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// TypesFromSerializers CacheKey 1e3a5dea8847b2dcfe76fec134589cbb
// TypesFromSerializers CacheKey 9111c6d881174b0013582fa6366868db
//
// DO NOT MODIFY: This file was automatically generated by TypesFromSerializers.
import type Model from './Model'
import type ComposerWithSongsSong from './ComposerWithSongs/Song'

export default interface ComposerWithSongs {
id: number
firstName?: string
lastName?: string
name: string
songs: Model[]
songs: ComposerWithSongsSong[]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// TypesFromSerializers CacheKey a69c0dbcc904740e5de89181ef4b2837
//
// DO NOT MODIFY: This file was automatically generated by TypesFromSerializers.

export default interface ComposerWithSongsSong {
id: number
title?: string
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// TypesFromSerializers CacheKey 1e3a5dea8847b2dcfe76fec134589cbb
// TypesFromSerializers CacheKey 9111c6d881174b0013582fa6366868db
//
// DO NOT MODIFY: This file was automatically generated by TypesFromSerializers.
import type Model from './Model'
import type ComposerWithSongsSong from './ComposerWithSongs/Song'

declare global {
namespace Schema {
Expand All @@ -10,7 +10,7 @@ declare global {
firstName?: string
lastName?: string
name: string
songs: Model[]
songs: ComposerWithSongsSong[]
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// TypesFromSerializers CacheKey a69c0dbcc904740e5de89181ef4b2837
//
// DO NOT MODIFY: This file was automatically generated by TypesFromSerializers.
export {}

declare global {
namespace Schema {
interface ComposerWithSongsSong {
id: number
title?: string
}
}
}
3 changes: 2 additions & 1 deletion spec/types_from_serializers/generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@
SongWithVideosSerializer
ModelSerializer
ComposerWithSongsSerializer
ComposerWithSongsSerializer::SongSerializer
ComposerSerializer
SnakeComposerSerializer
]
}

def file_for(dir, name, ext)
dir.join("#{name.chomp("Serializer").gsub("::", "/")}.#{ext}")
dir.join("#{TypesFromSerializers.config.name_from_serializer.call(name).gsub("::", "/")}.#{ext}")
end

def app_file_for(name, ext = "ts")
Expand Down
15 changes: 10 additions & 5 deletions types_from_serializers/lib/types_from_serializers/generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ def ts_filename
TypesFromSerializers.config.name_from_serializer.call(name).gsub("::", "/")
end

# Internal: If the serializer was defined inside a file.
def inline_serializer?
name.include?("Serializer::")
end

# Internal: The TypeScript properties of the serialzeir interface.
def ts_properties
@ts_properties ||= begin
Expand Down Expand Up @@ -312,9 +317,7 @@ def generate_index_file
# Internal: Checks if it should avoid generating an interface.
def skip_serializer?(serializer)
serializer.name.in?(config.base_serializers) ||
config.skip_serializer_if.call(serializer) ||
# NOTE: Ignore inline serializers.
serializer.ts_name.include?("Serializer")
config.skip_serializer_if.call(serializer)
end

# Internal: Returns an object compatible with FileUpdateChecker.
Expand Down Expand Up @@ -362,7 +365,9 @@ def default_config(root)
output_dir: root.join(defined?(ViteRuby) ? ViteRuby.config.source_code_dir : "app/frontend").join("types/serializers"),

# Remove the serializer suffix from the class name.
name_from_serializer: ->(name) { name.delete_suffix("Serializer") },
name_from_serializer: ->(name) {
name.split("::").map { |n| n.delete_suffix("Serializer") }.join("::")
},

# Types that don't need to be imported in TypeScript.
global_types: [
Expand Down Expand Up @@ -420,7 +425,7 @@ def serializers_index_content(serializers)
<<~TS
//
// DO NOT MODIFY: This file was automatically generated by TypesFromSerializers.
#{serializers.map { |s|
#{serializers.reject(&:inline_serializer?).map { |s|
"export type { default as #{s.ts_name} } from './#{s.ts_filename}'"
}.join("\n")}
TS
Expand Down

0 comments on commit 1c3657c

Please sign in to comment.