Skip to content

Commit 56c35be

Browse files
committed
feat: meta property
1 parent b41d3d8 commit 56c35be

File tree

10 files changed

+47
-2
lines changed

10 files changed

+47
-2
lines changed

lib/anchor/concerns/custom_meta.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module Anchor
2+
module CustomMeta
3+
extend ActiveSupport::Concern
4+
5+
included do
6+
class << self
7+
# @param type [Anchor::Types]
8+
def anchor_meta_schema(type = nil)
9+
@anchor_meta_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
@@ -5,5 +5,6 @@ module SchemaSerializable
55
include Anchor::StaticContext
66
include Anchor::Annotatable
77
include Anchor::CustomLinkable
8+
include Anchor::CustomMeta
89
end
910
end

lib/anchor/resource.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ def initialize(resource_klass)
1414
@anchor_relationships_descriptions = resource_klass.try(:anchor_relationships_descriptions) || {}
1515
@anchor_method_added_count = resource_klass.anchor_method_added_count || Hash.new(0)
1616
@anchor_links_schema = resource_klass.try(:anchor_links_schema) || nil
17+
@anchor_meta_schema = resource_klass.try(:anchor_meta_schema) || nil
1718
end
1819

1920
def express(...)
@@ -115,6 +116,12 @@ def anchor_links_property
115116
end
116117
end
117118

119+
def anchor_meta_property
120+
if @anchor_meta_schema
121+
Anchor::Types::Property.new("meta", @anchor_meta_schema, false)
122+
end
123+
end
124+
118125
# @param included_fields [Array<Symbol>]
119126
# @return [Array<Anchor::Types::Property>]
120127
def anchor_relationships_properties(included_fields:)

lib/anchor/type_script/resource.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ 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:)) + [anchor_links_property].compact
9+
Array.wrap(anchor_relationships_property(included_fields:)) +
10+
[anchor_meta_property].compact + [anchor_links_property].compact
1011

1112
expression = Anchor::TypeScript::Serializer.type_string(Anchor::Types::Object.new(properties))
1213
"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
@@ -5,6 +5,7 @@
55
require "anchor/schema_generator"
66
require "anchor/concerns/annotatable"
77
require "anchor/concerns/custom_linkable"
8+
require "anchor/concerns/custom_meta"
89
require "anchor/concerns/static_context"
910
require "anchor/concerns/type_inferable"
1011
require "anchor/concerns/schema_serializable"

spec/example/app/resources/exhaustive_resource.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,14 @@ class LinkSchema < Anchor::Types::Object
4747
property :self, Anchor::Types::String
4848
property :some_url, Anchor::Types::String
4949
end
50-
5150
anchor_links_schema LinkSchema
5251

52+
class MetaSchema < Anchor::Types::Object
53+
property :some_count, Anchor::Types::Integer
54+
property :extra_stuff, Anchor::Types::String
55+
end
56+
anchor_meta_schema MetaSchema
57+
5358
def asserted_string = "asserted_string"
5459

5560
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,6 +87,10 @@ export type Exhaustive = {
8787
resourceOverridden: unknown;
8888
/** This is a comment. */
8989
withComment: Maybe<string>;
90+
meta: {
91+
some_count: number;
92+
extra_stuff: string;
93+
};
9094
links: {
9195
self: string;
9296
some_url: string;

spec/example/test/files/excluded_fields_schema.ts

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

spec/example/test/files/schema.ts

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

spec/example/test/files/test_schema.ts

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

0 commit comments

Comments
 (0)