Skip to content

Commit b41d3d8

Browse files
committed
feat: add custom links property
1 parent 119be6a commit b41d3d8

File tree

10 files changed

+47
-1
lines changed

10 files changed

+47
-1
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module Anchor
2+
module CustomLinkable
3+
extend ActiveSupport::Concern
4+
5+
included do
6+
class << self
7+
# @param type [Anchor::Types]
8+
def anchor_links_schema(type = nil)
9+
@anchor_links_schema ||= type
10+
end
11+
end
12+
end
13+
end
14+
end

lib/anchor/concerns/schema_serializable.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ module SchemaSerializable
44
include Anchor::TypeInferable
55
include Anchor::StaticContext
66
include Anchor::Annotatable
7+
include Anchor::CustomLinkable
78
end
89
end

lib/anchor/resource.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ def initialize(resource_klass)
1313
@anchor_attributes_descriptions = resource_klass.try(:anchor_attributes_descriptions) || {}
1414
@anchor_relationships_descriptions = resource_klass.try(:anchor_relationships_descriptions) || {}
1515
@anchor_method_added_count = resource_klass.anchor_method_added_count || Hash.new(0)
16+
@anchor_links_schema = resource_klass.try(:anchor_links_schema) || nil
1617
end
1718

1819
def express(...)
@@ -108,6 +109,12 @@ def anchor_relationships_property(included_fields:)
108109
end
109110
end
110111

112+
def anchor_links_property
113+
if @anchor_links_schema
114+
Anchor::Types::Property.new("links", @anchor_links_schema, false)
115+
end
116+
end
117+
111118
# @param included_fields [Array<Symbol>]
112119
# @return [Array<Anchor::Types::Property>]
113120
def anchor_relationships_properties(included_fields:)

lib/anchor/type_script/resource.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ def express(context: {}, include_all_fields:, exclude_fields:)
66

77
properties = [id_property, type_property] +
88
Array.wrap(anchor_attributes_properties(included_fields:)) +
9-
Array.wrap(anchor_relationships_property(included_fields:))
9+
Array.wrap(anchor_relationships_property(included_fields:)) + [anchor_links_property].compact
1010

1111
expression = Anchor::TypeScript::Serializer.type_string(Anchor::Types::Object.new(properties))
1212
"export type #{anchor_schema_name} = " + expression + ";"

lib/jsonapi-resources-anchor.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
require "anchor/resource"
55
require "anchor/schema_generator"
66
require "anchor/concerns/annotatable"
7+
require "anchor/concerns/custom_linkable"
78
require "anchor/concerns/static_context"
89
require "anchor/concerns/type_inferable"
910
require "anchor/concerns/schema_serializable"

spec/example/app/resources/exhaustive_resource.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ class AssertedObject < Types::Object
4343
attribute :resource_overridden
4444
attribute :with_comment
4545

46+
class LinkSchema < Anchor::Types::Object
47+
property :self, Anchor::Types::String
48+
property :some_url, Anchor::Types::String
49+
end
50+
51+
anchor_links_schema LinkSchema
52+
4653
def asserted_string = "asserted_string"
4754

4855
def asserted_number = 1

spec/example/test/files/all_fields_false_schema.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,8 @@ export type Exhaustive = {
8787
resourceOverridden: unknown;
8888
/** This is a comment. */
8989
withComment: Maybe<string>;
90+
links: {
91+
self: string;
92+
some_url: string;
93+
};
9094
};

spec/example/test/files/excluded_fields_schema.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,8 @@ export type Exhaustive = {
8585
resourceOverridden: unknown;
8686
/** This is a comment. */
8787
withComment: Maybe<string>;
88+
links: {
89+
self: string;
90+
some_url: string;
91+
};
8892
};

spec/example/test/files/schema.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,8 @@ export type Exhaustive = {
9595
resourceOverridden: unknown;
9696
/** This is a comment. */
9797
withComment: Maybe<string>;
98+
links: {
99+
self: string;
100+
some_url: string;
101+
};
98102
};

spec/example/test/files/test_schema.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,8 @@ export type Exhaustive = {
9292
resourceOverridden: unknown;
9393
/** This is a comment. */
9494
withComment: Maybe<string>;
95+
links: {
96+
self: string;
97+
some_url: string;
98+
};
9599
};

0 commit comments

Comments
 (0)