JSONAPI Resources Anchor

MultifileSchemaGenerator

Anchor::TypeScript::MultifileSchemaGenerator

Generate a file for each resource type definition.

Maybe and enums are defined in shared.ts.

Example

class Schema < Anchor::Schema
  resource CommentResource
  resource PostResource
  resource UserResource
 
  enum UserRole
end
 
results = Anchor::TypeScript::MultifileSchemaGenerator.call(
  register: Schema.register,
  context: {},
  include_all_fields: true,
  exclude_fields: nil,
  manually_editable: true,
)
 
results.each do |result|
  puts result.name # String, name of the file
  result.type # String, either "resource" for resource files or "util" for shared.ts
  result.text # String, serialized TypeScript
end
 
# shared.ts
# User.ts
# Post.ts

.call Parameters

register, context, include_all_fields, and exclude_fields all have the same behavior as the parameters defined in Anchor::TypeScript::SchemaGenerator.

manually_editable

  • Type: Boolean
  • Default: true

If true, the generated type and imports are wrapped by // START AUTOGEN and // END AUTOGEN.

This is useful if you want to make manual edits to a resource's type and ensure it flows to all the other resources that reference it.

Example Output

User.ts
// START AUTOGEN
 
import type { UserRole } from "./shared";
import type { Comment } from "./Comment";
import type { Post } from "./Post";
 
type Model = {
  id: number;
  type: "users";
  name: string;
  role: UserRole;
  relationships: {
    comments: Array<Comment>;
    posts: Array<Post>;
  };
};
 
// END AUTOGEN
 
type User = Model;
 
export { type User };

On this page