{"id":14955266,"url":"https://github.com/mattkhan/jsonapi-resources-anchor","last_synced_at":"2025-10-24T07:31:03.112Z","repository":{"id":248564381,"uuid":"825592796","full_name":"mattkhan/jsonapi-resources-anchor","owner":"mattkhan","description":"Easily generate TypeScript schemas, JSON Schemas, or any schema of your choice from cerebris/jsonapi-resources JSONAPI::Resource classes.","archived":false,"fork":false,"pushed_at":"2025-02-07T02:55:42.000Z","size":199,"stargazers_count":4,"open_issues_count":5,"forks_count":0,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-02-07T03:28:05.520Z","etag":null,"topics":["codegen","jsonapi-resources","rails","ruby","typescript"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mattkhan.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-07-08T06:29:29.000Z","updated_at":"2025-02-07T02:54:42.000Z","dependencies_parsed_at":"2025-02-07T03:35:59.896Z","dependency_job_id":null,"html_url":"https://github.com/mattkhan/jsonapi-resources-anchor","commit_stats":{"total_commits":64,"total_committers":2,"mean_commits":32.0,"dds":0.15625,"last_synced_commit":"293c011a821cc39318607d94f77dcd643a588c72"},"previous_names":["mattkhan/jsonapi-resources-typescript-gen"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mattkhan%2Fjsonapi-resources-anchor","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mattkhan%2Fjsonapi-resources-anchor/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mattkhan%2Fjsonapi-resources-anchor/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mattkhan%2Fjsonapi-resources-anchor/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mattkhan","download_url":"https://codeload.github.com/mattkhan/jsonapi-resources-anchor/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":237932070,"owners_count":19389560,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["codegen","jsonapi-resources","rails","ruby","typescript"],"created_at":"2024-09-24T13:10:47.303Z","updated_at":"2025-10-24T07:31:03.107Z","avatar_url":"https://github.com/mattkhan.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# JSON:API Resource Schema Generation: Anchor\n\n[Documentation](https://jsonapi-resources-anchor.up.railway.app/)\n\nEasily generate TypeScript schemas, JSON Schemas, or any schema of your choice\nfrom [cerebris/jsonapi-resources](https://github.com/cerebris/jsonapi-resources)\n`JSONAPI::Resource` classes.\n\nIdeally, API schemas have the types of each payload fully specified.\n\n`jsonapi-resources-anchor` provides:\n\n- [Type inference](https://jsonapi-resources-anchor.up.railway.app/docs/Features/type_inference)\n  via the underlying ActiveRecord model of a resource\n- [Type annotation](https://jsonapi-resources-anchor.up.railway.app/docs/Features/type_annotation),\n  e.g. `attribute :name_length, Anchor::Types::Integer`\n- [Configuration](https://jsonapi-resources-anchor.up.railway.app/docs/API/configuration),\n  e.g. setting the case (camel, snake, etc.) of properties and deriving\n  TypeScript comments from database comments\n- TypeScript and JSON Schema generators via\n  `Anchor::TypeScript::SchemaGenerator` and\n  `Anchor::JSONSchema::SchemaGenerator`\n\nSee the [example](./spec/example) Rails app for a fully functional app using\n`Anchor`.\n\n## Installation\n\n```ruby\ngem 'jsonapi-resources-anchor'\n```\n\n```sh\nbundle install\n```\n\n## TypeScript Demo\n\nGiven:\n\nActiveRecord Schema:\n\n```rb\n  create_table \"comments\", force: :cascade do |t|\n    t.string \"text\", null: false\n    t.string \"commentable_type\"\n    t.bigint \"commentable_id\"\n    t.bigint \"user_id\", null: false\n    t.bigint \"deleted_by_id\"\n    t.datetime \"created_at\", null: false\n    t.datetime \"updated_at\", null: false\n    t.index [\"commentable_type\", \"commentable_id\"], name: \"index_comments_on_commentable\"\n    t.index [\"deleted_by_id\"], name: \"index_comments_on_deleted_by_id\"\n    t.index [\"user_id\"], name: \"index_comments_on_user_id\"\n  end\n\n  create_table \"posts\", force: :cascade do |t|\n    t.string \"description\", null: false\n    t.bigint \"user_id\", null: false\n    t.datetime \"created_at\", null: false\n    t.datetime \"updated_at\", null: false\n    t.index [\"user_id\"], name: \"index_posts_on_user_id\"\n  end\n\n  create_table \"users\", force: :cascade do |t|\n    t.string \"name\", null: false\n    t.integer \"integer\"\n    t.decimal \"decimal\"\n    t.datetime \"created_at\", null: false\n    t.datetime \"updated_at\", null: false\n    t.string \"role\"\n  end\n```\n\n`JSONAPI::Resource` classes:\n\n```rb\nclass ApplicationResource \u003c JSONAPI::Resource\n  abstract\n  include Anchor::SchemaSerializable\nend\n\nclass CommentResource \u003c ApplicationResource\n  attribute :text\n  attribute :created_at\n  attribute :updated_at\n  attribute :inferred_unknown\n  attribute :type_given, Anchor::Types::String\n\n  relationship :user, to: :one\n  relationship :commentable, Anchor::Types::Relationship.new(resources: [UserResource, PostResource], null: true), polymorphic: true, to: :one\nend\n\nclass UserResource \u003c ApplicationResource\n  attribute :name\n  attribute :role, UserRoleEnum\n\n  relationship :comments, to: :many\n  relationship :posts, to: :many\nend\n\nclass UserRoleEnum \u003c Anchor::Types::Enum\n  anchor_schema_name \"UserRole\"\n\n  value :admin, \"admin\"\n  value :content_creator, \"content_creator\"\n  value :external, \"external\"\n  value :guest, \"guest\"\n  value :system, \"system\"\nend\n\nclass PostResource \u003c ApplicationResource\n  attribute :description\n\n  relationship :user, to: :one\n  relationship :comment, to: :many\nend\n\nclass Schema \u003c Anchor::Schema\n  resource CommentResource\n  resource UserResource\n  resource PostResource\n\n  enum UserRoleEnum\nend\n```\n\n`Anchor::TypeScript::SchemaGenerator.call(register: Schema.register)` will\nreturn the schema below in a `String`:\n\n```ts\ntype Maybe\u003cT\u003e = T | null;\n\nexport type Comment = {\n  id: number;\n  type: \"comments\";\n  text: string;\n  created_at: string;\n  updated_at: string;\n  inferred_unknown: unknown;\n  type_given: string;\n  relationships: {\n    user: User;\n    commentable: Maybe\u003cUser | Post\u003e;\n  };\n};\n\nexport type User = {\n  id: number;\n  type: \"users\";\n  name: string;\n  role: UserRole;\n  relationships: {\n    comments: Array\u003cComment\u003e;\n    posts: Array\u003cPost\u003e;\n  };\n};\n\nexport type Post = {\n  id: number;\n  type: \"posts\";\n  description: string;\n  relationships: {\n    user: User;\n    comment: Array\u003cComment\u003e;\n  };\n};\n\nexport enum UserRole {\n  Admin = \"admin\",\n  ContentCreator = \"content_creator\",\n  External = \"external\",\n  Guest = \"guest\",\n  System = \"system\",\n}\n```\n\n#### References\n\n- [@nopelluhh](https://github.com/nopelluhh)\n- [ElMassimo/types_from_serializers](https://github.com/ElMassimo/types_from_serializers)\n- [rmosolgo/graphql-ruby](https://github.com/rmosolgo/graphql-ruby)\n\n#### Security\n\n[Security Policy](/SECURITY.md)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmattkhan%2Fjsonapi-resources-anchor","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmattkhan%2Fjsonapi-resources-anchor","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmattkhan%2Fjsonapi-resources-anchor/lists"}