{"id":14969154,"url":"https://github.com/mcollina/fastify-api","last_synced_at":"2025-08-30T19:31:31.452Z","repository":{"id":46774602,"uuid":"348131727","full_name":"mcollina/fastify-api","owner":"mcollina","description":"A radically simple API routing and method injection plugin for Fastify.","archived":false,"fork":false,"pushed_at":"2024-06-16T21:11:15.000Z","size":473,"stargazers_count":18,"open_issues_count":0,"forks_count":2,"subscribers_count":20,"default_branch":"main","last_synced_at":"2024-12-21T09:39:55.109Z","etag":null,"topics":["fastify","fastify-plugin"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mcollina.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"open_collective":"fastify"}},"created_at":"2021-03-15T21:46:22.000Z","updated_at":"2024-09-12T06:48:45.000Z","dependencies_parsed_at":"2023-02-06T11:10:13.024Z","dependency_job_id":"d7fa3ee8-bbcb-4951-9cda-fa8745e45db5","html_url":"https://github.com/mcollina/fastify-api","commit_stats":{"total_commits":55,"total_committers":3,"mean_commits":"18.333333333333332","dds":"0.23636363636363633","last_synced_commit":"c5c3c4b4bbf406fb0af09607b1fd76a9cfdba27c"},"previous_names":["galvez/fastify-api"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mcollina%2Ffastify-api","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mcollina%2Ffastify-api/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mcollina%2Ffastify-api/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mcollina%2Ffastify-api/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mcollina","download_url":"https://codeload.github.com/mcollina/fastify-api/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":231521940,"owners_count":18389472,"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","fastify-plugin"],"created_at":"2024-09-24T13:41:14.387Z","updated_at":"2024-12-27T17:49:11.725Z","avatar_url":"https://github.com/mcollina.png","language":"JavaScript","readme":"# fastify-api\n\nA **radically simple** API **routing and method injection plugin** for [Fastify](https://fastify.dev).\n\nUses [`fastify.inject`](https://github.com/fastify/light-my-request) under the hood, with _developer ergonomics_ in mind.\n\nInjects `fastify.api.client` with automatically mapped methods from route definitions. Also injects `fastify.api.meta`, which you can serve and use [`manifetch`](https://github.com/galvez/manifetch) to automatically build an API client for the browser.\n\n## Usage\n\n1. **Original fastify.{method}() with `exposeAs` option, without params:**\n\n```js\nfastify.get('/1/method', { exposeAs: 'method' }, (_, reply) =\u003e {\n  reply.send('Hello from /1/method')\n})\nfastify.get('/invoke/1/method', async (_, reply) =\u003e {\n  const result = await fastify.api.client.method()\n  reply.send(result)\n})\n```\n\n2. **Original fastify.{method}() with `exposeAs` option, with params:**\n\n```js\nfastify.get('/2/method/:id', { exposeAs: 'methodWithParams' }, ({ id }, _, reply) =\u003e {\n  reply.send(`Hello from /2/method/ with id ${id}`)\n})\nfastify.get('/invoke/2/method', async (req, reply) =\u003e {\n  const result = await fastify.api.client.methodWithParams({ id: 123 })\n  reply.send(result)\n})\n```\n\n3. **Will automatically create a nested structure too, if needed:**\n\n```js\nfastify.get('/3/nested/method/:id', { exposeAs: 'nested.method' }, ({ id }, _, reply) =\u003e {\n  reply.send(`Hello from /3/nested/method/ with id ${id}`)\n})\nfastify.get('/invoke/3/nested/method', async (req, reply) =\u003e {\n  const result = await fastify.api.client.nested.method({ id: 123 })\n  reply.send(result)\n})\n```\n\n4. **Modified fastify.api.{method}() setter if the handler is a named function:**\n\n```js\nfastify.api.get('/4/method', function methodFromNamedFunction ({ id }, _, reply) {\n  reply.send(`Hello from /4/method with id ${id}`)\n})\nfastify.get('/invoke/4/method', async (req, reply) =\u003e {\n  const result = await fastify.api.client.methodFromNamedFunction({ id: 123 })\n  reply.send(result)\n})\n```\n\n5. **Modified fastify.api(setter) helper to quickly define multiple methods:**\n\n_Makes more sense if the setter function is coming from another file._\n\n```js\nfastify.api(({ get }) =\u003e ({\n  topLevelMethod: get('/5/top-level-method/:id', function ({ id }, _, reply) {\n    reply.send({ id })\n  }),\n  nestedMethods: {\n    method: get('/5/nested-methods/method/:id', ({ id }, _, reply) =\u003e {\n      reply.send({ id })\n    }),\n    otherMethod: get('/5/nested-methods/other-method/:id', ({ id }, _, reply) =\u003e {\n      reply.send({ id })\n    }),\n    deeplyNestedMethods: {\n      method: get('/5/nested-methods/deeply-nested-methods/method/:id', ({ id }, _, reply) =\u003e {\n        reply.send({ id })\n      }),\n      otherMethod: get('/5/nested-methods/deeply-nested-methods/other-method/:id', ({ id }, _, reply) =\u003e {\n        reply.send({ id })\n      })\n    }\n  }\n}))\n\nfastify.get('/invoke/5/top-level-method', async (req, reply) =\u003e {\n  const result = await fastify.api.client.topLevelMethod({ id: 123 })\n  reply.send(result)\n})\nfastify.get('/invoke/5/nested-methods/method', async (_, reply) =\u003e {\n  const result = await fastify.api.client.nestedMethods.method({ id: 123 })\n  reply.send(result)\n})\nfastify.get('/invoke/5/nested-methods/other-method', async (_, reply) =\u003e {\n  const result = await fastify.api.client.nestedMethods.otherMethod({ id: 123 })\n  reply.send(result)\n})\nfastify.get('/invoke/5/nested-methods/deeply-nested-methods/method', async (_, reply) =\u003e {\n  const result = await fastify.api.client.nestedMethods.deeplyNestedMethods.method({ id: 123 })\n  reply.send(result)\n})\nfastify.get('/invoke/5/nested-methods/deeply-nested-methods/other-method', async (_, reply) =\u003e {\n  const result = await fastify.api.client.nestedMethods.deeplyNestedMethods.otherMethod({ id: 123 })\n  reply.send(result)\n})\n```\n\n6. **Any API method exposed in fastify.api.client can take options:**\n\n```js\nfastify.get('/6/method', { exposeAs: 'methodWithOptions' }, (req, reply) =\u003e {\n  reply.send(`Hello from /6/method/ with query.arg ${\n    req.query.arg\n  } and the x-foobar header ${\n    req.headers['x-foobar']\n  }`)\n})\nfastify.get('/invoke/6/method', async (_, reply) =\u003e {\n  const result = await fastify.api.client.methodWithOptions({\n    query: {\n      arg: 1\n    },\n    headers: {\n      'x-foobar': 1\n    }\n  })\n  reply.send(result)\n})\n```\n\n## API responses\n\nIf you call a route via HTTP, it'll operate normally as if weren't using the plugin. If you use `fastify.api.client.xyz()` to invoke it from another handler, you'll get an object containing `{ json, body, status, headers }` as response. If it's unable to parse a JSON document out of `body`, `json` is undefined.\n\n","funding_links":["https://opencollective.com/fastify"],"categories":["JavaScript"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmcollina%2Ffastify-api","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmcollina%2Ffastify-api","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmcollina%2Ffastify-api/lists"}