{"id":15013045,"url":"https://github.com/thomasthiebaud/fastify-schema-to-typescript","last_synced_at":"2025-04-12T04:21:20.136Z","repository":{"id":38172731,"uuid":"271607126","full_name":"thomasthiebaud/fastify-schema-to-typescript","owner":"thomasthiebaud","description":"Convert JSON schema to typescript so your types always match Fastify validation","archived":false,"fork":false,"pushed_at":"2024-12-08T13:42:00.000Z","size":1447,"stargazers_count":12,"open_issues_count":2,"forks_count":3,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-25T23:51:19.584Z","etag":null,"topics":["fastify","jsonschema","openapi","swagger","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","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/thomasthiebaud.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-06-11T17:28:30.000Z","updated_at":"2024-12-08T13:41:03.000Z","dependencies_parsed_at":"2023-02-08T07:30:50.420Z","dependency_job_id":null,"html_url":"https://github.com/thomasthiebaud/fastify-schema-to-typescript","commit_stats":null,"previous_names":[],"tags_count":36,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thomasthiebaud%2Ffastify-schema-to-typescript","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thomasthiebaud%2Ffastify-schema-to-typescript/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thomasthiebaud%2Ffastify-schema-to-typescript/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thomasthiebaud%2Ffastify-schema-to-typescript/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/thomasthiebaud","download_url":"https://codeload.github.com/thomasthiebaud/fastify-schema-to-typescript/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248103872,"owners_count":21048245,"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":["fastify","jsonschema","openapi","swagger","typescript"],"created_at":"2024-09-24T19:43:39.413Z","updated_at":"2025-04-12T04:21:20.117Z","avatar_url":"https://github.com/thomasthiebaud.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# fastify-schema-to-typescript\n\n## How to install?\n\n```sh\nnpm i -D fastify-schema-to-typescript\n```\n\n## How to use?\n\nRunning `npx fastify-schema-to-typescript` will convert all `schema.json` files in `src` to `schema.ts`.\nYou can then import `schema.ts` directly\n\n```ts\nimport { RouteGeneric, schema } from \"./schema\";\n\nconst options = {\n  schema: {\n    params: schema.params,\n    body: schema.body,\n    response: schema.response,\n  },\n};\n\napp.get\u003cRouteGeneric\u003e(\"/healthcheck\", options, async () =\u003e {\n  return { ok: true };\n});\n```\n\nMore options are available\n\n```sh\nnpx fastify-schema-to-typescript -h\n\nUsage: fastify-schema-to-typescript [options]\n\nOptions:\n  -g, --glob \u003cvalue\u003e    glob matching JSON schema to convert (default: \"src/**/schema.{json,yaml,yml}\")\n  -p, --prefix \u003cvalue\u003e  prefix to use before interfaces' name (default: \"\")\n  -e, --ext \u003cvalue\u003e     file extension to use for generated files (default: \".ts\")\n  -m, --module \u003cvalue\u003e  module to import the RouteHandler type from (default: \"fastify\")\n  -h, --help            display help for command\n```\n\n## Project example\n\nHere is a project structure that I'm usually using with that module.\nEach endpoint has a folder with a `schema.yaml` and `index.ts`\n\n```\npackage.json\nsrc/\n  app.ts\n  api/\n    healthcheck/\n      index.ts\n      schema.yaml\n    anotherEndpoint/\n      index.ts\n      schema.yaml\n    ...\n```\n\nRunning `npx fastify-schema-to-typescript` will convert `schema.yaml` to `schema.ts` that you can then import in your apps like that\n\napp.ts\n\n```ts\nimport fastify from \"fastify\";\nimport { healthcheck } from \"./api\";\n\nexport async function run() {\n  const app = fastify();\n  await app.register(healthcheck);\n\n  await app.listen(3000);\n}\n\nrun();\n```\n\napi/index.ts\n\n```ts\nexport * from \"./healthcheck\";\n```\n\napi/healthcheck/schema.yaml\n\n```yml\nheaders:\n  type: object\n  properties: ...\n\nbody:\n  type: object\n  properties: ...\n\nquery:\n  type: object\n  properties: ...\n\nparams:\n  type: object\n  properties: ...\n```\n\napi/healthcheck/index.ts\n\n```ts\nimport { FastifyPluginAsync } from \"fastify\";\nimport { RouteGeneric, schema } from \"./schema\";\n\nexport const getHealthcheck: FastifyPluginAsync = async (app) =\u003e {\n  app.get\u003cRouteGeneric\u003e(\"/healthcheck\", { schema }, async () =\u003e {\n    return { ok: true };\n  });\n};\n```\n\nI usually add the following in my package.json so I'm sure the code is in sync with the schemas\n\npackage.json\n\n```\n...\n\"scripts\": {\n  ...\n  \"build\": \"npm run clean \u0026\u0026 npm run generate \u0026\u0026 npm run compile\",\n  \"compile\": \"tsc\",\n  \"clean\": \"rimraf dist\",\n  \"generate\": \"npx fastify-schema-to-typescript\",\n  ...\n}\n```\n\nand I also update `.gitignore` to not include the generated `schema.ts`\n\n.gitignore\n\n```\n...\nschema.ts\n...\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthomasthiebaud%2Ffastify-schema-to-typescript","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthomasthiebaud%2Ffastify-schema-to-typescript","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthomasthiebaud%2Ffastify-schema-to-typescript/lists"}