{"id":19780004,"url":"https://github.com/jondot/hypercontroller","last_synced_at":"2025-04-30T21:32:50.953Z","repository":{"id":46938825,"uuid":"111196791","full_name":"jondot/hypercontroller","owner":"jondot","description":"A more civilized controller abstraction for TypeScript and Node.js. Hosts libraries for controllers built using hypergen","archived":false,"fork":false,"pushed_at":"2023-01-07T04:03:31.000Z","size":2794,"stargazers_count":8,"open_issues_count":29,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-06T04:51:12.085Z","etag":null,"topics":["express","fastify","nodejs","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/jondot.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":null,"support":null}},"created_at":"2017-11-18T10:36:10.000Z","updated_at":"2021-04-02T16:19:31.000Z","dependencies_parsed_at":"2023-02-06T11:16:25.379Z","dependency_job_id":null,"html_url":"https://github.com/jondot/hypercontroller","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jondot%2Fhypercontroller","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jondot%2Fhypercontroller/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jondot%2Fhypercontroller/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jondot%2Fhypercontroller/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jondot","download_url":"https://codeload.github.com/jondot/hypercontroller/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251785602,"owners_count":21643508,"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":["express","fastify","nodejs","typescript"],"created_at":"2024-11-12T05:38:24.759Z","updated_at":"2025-04-30T21:32:50.637Z","avatar_url":"https://github.com/jondot.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"![](media/cover.png)\n\n# Hypercontroller\n\n✅ Structured and declarative project layout with controllers, actions, and middleware  \n✅ Bring your own framework: use with [Fastify](https://fastify.io) or [Express](https://github.com/expressjs/express)  \n✅ Automatic routing and mounting  \n✅ Helpers and best-practices for strong params and async promise-based flows  \n✅ TypeScript-first  \n✅ Generators driven (quickly add controllers, actions)  \n\n\n```ts\nimport { Controller, get, postWithRoute, permitParams } from 'hypercontroller'\nconst accountParams = permitParams('account', ['name'])\n\n@Controller()\nclass AccountController {\n  @get()\n  index(_req, res) {\n    res.json({ name: 'Homer Simpson' })\n  }\n\n  @postWithRoute('/')\n  async update(_req, res) {\n    const params = accountParams(req.body)\n    const record = await save(params)\n    res.json(record)\n  }\n}\n```\n\n## Quick Start\n\nInstall:\n\n```\n$ yarn add hypercontroller\n```\n\nSet up a `server.ts` file using a framework of your choice:\n\n```ts\nimport express from 'express'\nimport { Server, ExpressAdapter } from 'hypercontroller'\nimport AccountController from './controllers/account'\n\nconst server = new Server(new ExpressAdapter(express))\nserver.mount([\n  new AccountController()\n])\n\n\nconst port = process.env.PORT || 5160\nconst createServer = () =\u003e\n  server\n    .start()\n    .then(({ opts }) =\u003e console.log(`Listening on ${opts.port}`))\n    .then(() =\u003e server)\ncreateServer()\n```\n\nAnd run (here using `ts-node`):\n\n```\n$ ts-node server.ts\n```\n\n## Controllers, Actions and Middleware\n\nHypercontroller tries to follow the same concepts as [Rails ActionController](https://edgeguides.rubyonrails.org/action_controller_overview.html).\n\nA controller is created with the `@Controller` decorator and a plain class:\n\n```ts\n@Controller()\nclass AccountController {\n    ...\n```\n\nYou add actions (request handlers) to controllers by marking it with an HTTP verb decorator:\n\n```ts\nimport { Controller, get } from 'hypercontroller'\n\n@Controller()\nclass AccountController {\n  @get()\n  index(_req, res) {\n    res.json({ name: 'Homer Simpson' })\n  }\n  ...\n```\n\nIn terms of routing, hypercontroller will _infer_ the route name from the decorated subject. When `index` or `Index` is used, it will use the index route `/` instead of the literal word `index`.\n\nIn any case, hypercontroller lets you specify a route explicitly with the `WithRoute` variant of the `Controller` decorator and each of the HTTP verb decorators.\n\n```ts\n@Controller()\nclass AccountController {\n  @get()\n  index(_req, res) {\n    res.json({ name: 'Homer Simpson' })\n  }\n\n  @postWithRoute('/')\n  async update(_req, res) {\n    const params = accountParams(req.body)\n    const record = await save(params)\n    res.json(record)\n  }\n}\n```\n\nThis controller will form the following routes once mounted:\n\n```\n GET  /accounts\nPOST  /accounts\n```\n\nAny decorator you use accepts middleware as an array of middleware or a single instance:\n\n```ts\n@Controller(jwtAuth)\nclass AccountController {\n  @get([cacheWithEtag, compress])\n  index(_req, res) {\n    res.json({ name: 'Homer Simpson' })\n  }\n  ...\n```\n\nAll middleware are the same ones you would originally use with Express or Fastify.\n\n## Server and Mounting\n\nHypercontroller's `Server` is an entrypoint that takes controllers and understands their structure, and mount actions and middleware cleanly using your chosen web framework.\n\n```ts\nconst server = new Server(new ExpressAdapter(express))\nserver.mount([\n  new AccountController()\n])\n```\n\nYou can use either `ExpressAdapter` or `FastifyAdapter`, and give each an instance of `express` or `fastify` to work with.\n\nHypercontroller always lets you work with your web framework directly, and exposes the current app via `server.app`. This way you can use existing legacy code, framework-specific modules, practices, testing harnesses and more -- you take it from here.\n\n\n## Strong Params\n\nHypercontroller makes [hyperparams](https://github.com/jondot/hyperparams) accessible for you to use in your actions if you want to implement [strong parameter](https://edgeguides.rubyonrails.org/action_controller_overview.html#strong-parameters) (which you should).\n\nYou create a requirement statically:\n\n```ts\nconst accountParams = permitParams('account', ['name'])\n```\n\nAnd use it in your actions, or anywhere else you want:\n\n\n```ts\n   @get()\n   index(req, res){\n       const params = accoutnParams(req.body)\n       ...\n   }\n```\n\nHyperparams is extremely performant, and modular compared to full-fledged validation libraries.\n\n\n# Contributing\n\nFork, implement, add tests, pull request, get my everlasting thanks and a respectable place here :).\n\n### Thanks:\n\nTo all [Contributors](https://github.com/jondot/hypercontroller/graphs/contributors) - you make this happen, thanks!\n\n# Copyright\n\nCopyright (c) 2019 [@jondot](http://twitter.com/jondot). See [LICENSE](LICENSE.txt) for further details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjondot%2Fhypercontroller","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjondot%2Fhypercontroller","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjondot%2Fhypercontroller/lists"}