Skip to content

Commit

Permalink
feat: configurable empty relationship type
Browse files Browse the repository at this point in the history
  • Loading branch information
mattkhan committed Jan 25, 2025
1 parent fe08cc5 commit 8cab1ee
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 4 deletions.
4 changes: 3 additions & 1 deletion lib/anchor/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ class Config
:field_case,
:use_active_record_presence,
:use_active_record_comment,
:infer_nullable_relationships_as_optional
:infer_nullable_relationships_as_optional,
:empty_relationship_type

def initialize
@ar_column_to_type = nil
@field_case = nil
@use_active_record_presence = nil
@use_active_record_comment = nil
@infer_nullable_relationships_as_optional = nil
@empty_relationship_type = nil
end
end
end
7 changes: 6 additions & 1 deletion lib/anchor/type_script/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@ def express(context: {}, include_all_fields:, exclude_fields:)
included_fields = schema_fetchable_fields(context:, include_all_fields:)
included_fields -= exclude_fields if exclude_fields

relationships_property = anchor_relationships_property(included_fields:)
if relationships_property.nil? && Anchor.config.empty_relationship_type
relationships_property = Anchor::Types::Property.new(:relationships, Anchor.config.empty_relationship_type.call)
end

properties = [id_property, type_property] +
Array.wrap(anchor_attributes_properties(included_fields:)) +
Array.wrap(anchor_relationships_property(included_fields:)) +
Array.wrap(relationships_property) +
[anchor_meta_property].compact + [anchor_links_property].compact

expression = Anchor::TypeScript::Serializer.type_string(Anchor::Types::Object.new(properties))
Expand Down
1 change: 1 addition & 0 deletions lib/anchor/type_script/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def serialize_literal(value)
end

def serialize_object(type, depth)
return "{}" if type.properties.empty?
properties = type.properties.flat_map do |p|
[
p.description && "/** #{p.description} */",
Expand Down
6 changes: 4 additions & 2 deletions lib/anchor/types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ class Object
attr_reader :properties

def initialize(properties)
@properties = properties
@properties = properties || []
end

class << self
attr_reader :properties
def properties
@properties ||= []
end

def property(name, type, optional: nil, description: nil)
@properties ||= []
Expand Down
2 changes: 2 additions & 0 deletions spec/example/config/initializers/anchor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ module Anchor
return Types::Literal.new("never") if column.name == "loljk"
Types::Inference::ActiveRecord::SQL.default_ar_column_to_type(column)
}

c.empty_relationship_type = -> { Anchor::Types::Object }
end
end

Expand Down
2 changes: 2 additions & 0 deletions spec/example/test/files/all_fields_false_schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export type Comment = {
id: number;
type: "comments";
createdAt: string;
relationships: {};
};

export type User = {
Expand Down Expand Up @@ -87,6 +88,7 @@ export type Exhaustive = {
resourceOverridden: unknown;
/** This is a comment. */
withComment: Maybe<string>;
relationships: {};
meta: {
some_count: number;
extra_stuff: string;
Expand Down
2 changes: 2 additions & 0 deletions spec/example/test/files/excluded_fields_schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export type Comment = {
id: number;
type: "comments";
createdAt: string;
relationships: {};
};

export type User = {
Expand Down Expand Up @@ -85,6 +86,7 @@ export type Exhaustive = {
resourceOverridden: unknown;
/** This is a comment. */
withComment: Maybe<string>;
relationships: {};
meta: {
some_count: number;
extra_stuff: string;
Expand Down
1 change: 1 addition & 0 deletions spec/example/test/files/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ export type Exhaustive = {
resourceOverridden: unknown;
/** This is a comment. */
withComment: Maybe<string>;
relationships: {};
meta: {
some_count: number;
extra_stuff: string;
Expand Down
1 change: 1 addition & 0 deletions spec/example/test/files/test_schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ export type Exhaustive = {
resourceOverridden: unknown;
/** This is a comment. */
withComment: Maybe<string>;
relationships: {};
meta: {
some_count: number;
extra_stuff: string;
Expand Down

0 comments on commit 8cab1ee

Please sign in to comment.