{"id":28711722,"url":"https://github.com/curveball/controller","last_synced_at":"2025-06-14T22:10:25.868Z","repository":{"id":34448654,"uuid":"156139404","full_name":"curveball/controller","owner":"curveball","description":"A simple controller design pattern for curveballjs","archived":false,"fork":false,"pushed_at":"2024-06-18T21:54:09.000Z","size":983,"stargazers_count":5,"open_issues_count":4,"forks_count":0,"subscribers_count":9,"default_branch":"main","last_synced_at":"2025-05-06T19:56:15.968Z","etag":null,"topics":["controller","curveball","framework","http","http2","middleware","typescript"],"latest_commit_sha":null,"homepage":null,"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/curveball.png","metadata":{"files":{"readme":"README.md","changelog":"changelog.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":".github/CODE_OF_CONDUCT.md","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}},"created_at":"2018-11-05T00:34:44.000Z","updated_at":"2024-01-15T16:22:18.000Z","dependencies_parsed_at":"2024-01-15T05:45:49.328Z","dependency_job_id":"0ca80b24-7bfc-49f8-89e8-ec8d7e894e3b","html_url":"https://github.com/curveball/controller","commit_stats":{"total_commits":86,"total_committers":6,"mean_commits":"14.333333333333334","dds":"0.19767441860465118","last_synced_commit":"a3718c4431271480945fe8eed8fcf71210f9835b"},"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/curveball%2Fcontroller","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/curveball%2Fcontroller/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/curveball%2Fcontroller/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/curveball%2Fcontroller/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/curveball","download_url":"https://codeload.github.com/curveball/controller/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/curveball%2Fcontroller/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":258505087,"owners_count":22711976,"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":["controller","curveball","framework","http","http2","middleware","typescript"],"created_at":"2025-06-14T22:10:24.322Z","updated_at":"2025-06-14T22:10:25.858Z","avatar_url":"https://github.com/curveball.png","language":"TypeScript","readme":"Curveball Controller\n=====================\n\nThis package provides a simple controller pattern for curveballjs\napplications.\n\nIt's usage in the Curveball framework is entrirely optional, but it makes\ndesigning resource-oriented API's easier.\n\nBasic features\n\n* Handles all methods for a single route in an application.\n* Implements the `OPTIONS` method and returns all supported HTTP methods in\n  an `Allow` header.\n* Automatically throws a `405 Method Not Allowed` for unsupported methods.\n* Support for negotiating the `Accept` header.\n\n\nInstallation\n------------\n\n    npm install @curveball/controller\n\n\nGetting started\n---------------\n\nTo create a controller, subclass the main controller. HTTP methods are\nrepresented by methods on the class.\n\n```typescript\nimport Controller from '@curveball/controller';\nimport { Context } from '@curveball/core';\n\nclass MyController extends Controller {\n\n  get(ctx: Context) {\n\n    ctx.response.body = 'Hello world';\n\n  }\n\n}\n```\n\nTo use your controller, you probably want to use the [@curveball/router][2]\npackage:\n\n```typescript\nimport { Application } from '@curveball/core';\nimport { router } from '@curveball/router';\n\nconst app = new Application();\n\napp.use(\n  router('/hello-world', new MyController())\n);\n```\n\nDifferences from common frameworks\n----------------------------------\n\nEvery controller is responsible for exactly 1 route in your application.\nA controller is a ES6 class, and it's methods match HTTP methods.\n\nThis makes it slightly different from common controllers from many popular\nframeworks, where a single controller typically handles a 'group' of\nfunctionality with `index`, `create`, `update`, `read` and `delete` functions.\n\nTo model the same index, create, update, read, delete functions with this\ncontroller, you just need two controllers instead:\n\n```typescript\nclass Collection extends Controller {\n\n  get(ctx: Context) {\n    // index\n  }\n\n  post(ctx: Context) {\n    // create\n  }\n\n}\n\nclass Item extends Controller {\n\n  get(ctx: Context) {\n    // read\n  }\n\n  put(ctx: Context) {\n    // update\n  }\n\n  delete(ctx: Context) {\n    // delete\n  }\n}\n```\n\nAnd then to use them:\n\n```typescript\nimport { Application } from '@curveball/core';\nimport { router } from '@curveball/router';\n\nconst app = new Application();\n\napp.use(\n  router('/articles', new Collection())\n);\napp.use(\n  router('/articles/:id', new Item())\n);\n```\n\nNegotiating the Accept header\n-----------------------------\n\nIf you API supports multiple formats, for example `json` and `html`, you can\nuse the `@accept` and `@method` annotations to automatically handle these.\n\n```typescript\nimport { Controller, method, accept } from '@curveball/controller';\nimport { Context } from '@curveball/core';\n\nclass MyFancyController extends Controller {\n\n  @method('GET')\n  @accept('application/json')\n  getJson(ctx: Context) {\n\n    ctx.response.type = 'application/json';\n    ctx.response.body = { 'hello': 'world' };\n\n  }\n\n  @method('GET')\n  @accept('html')\n  getHtml(ctx: Context) {\n\n    ctx.response.type = 'text/html';\n    ctx.response.body = '\u003ch1\u003eHello world\u003c/h1\u003e';\n\n  }\n\n}\n```\n\nThis controller uses the `@method` annotation to automatically route\na HTTP method to a controller function.\n\nIf there was no match for the `@accept` annotation, the server will\nautomatically throw `406 Not Acceptable`.\n\nIt's possible to specify multiple `@accept` annotations. The `@accept`\nannotation contains a mimetype, but it's possible to only specify a part of\nthe mimetype. For example, the following values for the `@accept` annotation\nwill all match `application/hal+json`:\n\n* `json`\n* `application/*`\n* `*/json`\n* `application/json`\n* `application/hal+json`\n* `hal+json`\n* `application/hal+json; version=2`\n\nTo make a specific function match any accept header, you can add an `@accept('*')`\nannotation\n\n\nWebSocket Support\n-----------------\n\nThe Controller has built-in WebSocket support. Sample usage:\n\n\n```typescript\nimport { Controller } from '@curveball/controller';\nimport { Application, WsContext } from '@curveball/core';\n\nclass MyController extends Controller {\n\n  webSocket(ctx: WsContext) {\n\n    ctx.webSocket.send('Hello');\n\n  }\n\n}\n\nconst app = new Application();\napp.use(new MyController());\n\n// Listen on port 5000 for Websocket\napp.listenWs(5000);\n```\n\n\n[1]: https://github.com/curveball/\n[2]: https://github.com/curveball/router\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcurveball%2Fcontroller","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcurveball%2Fcontroller","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcurveball%2Fcontroller/lists"}