{"id":20170145,"url":"https://github.com/fabrix-app/spool-router","last_synced_at":"2025-04-10T02:26:10.703Z","repository":{"id":32598950,"uuid":"137797715","full_name":"fabrix-app/spool-router","owner":"fabrix-app","description":"Spool: Router for Fabrix","archived":false,"fork":false,"pushed_at":"2022-12-30T18:53:26.000Z","size":657,"stargazers_count":4,"open_issues_count":14,"forks_count":0,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-03-17T22:55:09.029Z","etag":null,"topics":["fabrix","nodejs","router","spools","typescript","validation"],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/fabrix-app.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":".github/CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-06-18T19:34:54.000Z","updated_at":"2019-10-26T16:25:44.000Z","dependencies_parsed_at":"2023-01-14T21:41:40.096Z","dependency_job_id":null,"html_url":"https://github.com/fabrix-app/spool-router","commit_stats":null,"previous_names":[],"tags_count":14,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fabrix-app%2Fspool-router","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fabrix-app%2Fspool-router/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fabrix-app%2Fspool-router/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fabrix-app%2Fspool-router/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fabrix-app","download_url":"https://codeload.github.com/fabrix-app/spool-router/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248143685,"owners_count":21054821,"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":["fabrix","nodejs","router","spools","typescript","validation"],"created_at":"2024-11-14T01:17:25.918Z","updated_at":"2025-04-10T02:26:10.673Z","avatar_url":"https://github.com/fabrix-app.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# spool-router\n\n\n[![Gitter][gitter-image]][gitter-url]\n[![NPM version][npm-image]][npm-url]\n[![Build Status][ci-image]][ci-url]\n[![Test Coverage][coverage-image]][coverage-url]\n[![Dependency Status][daviddm-image]][daviddm-url]\n[![Follow @FabrixApp on Twitter][twitter-image]][twitter-url]\n\nSpool Router. Aggregates all routes from `config.routes` to create a Fabrix Route which is easily translated to [hapi.js route objects](http://hapijs.com/api#route-configuration), Express.js routes, or your own!\n\n## Install\n```sh\n$ npm install @fabrix/spool-router --save\n```\n\n## Usage\nLoad from your spool config. (This pack is included by default).\n\n```js\n// config/main.ts\nimport { RouterSpool } from '@fabrix/spool-router'\nexport const main = {\n  // ...\n  spools: [\n    RouterSpool\n  ]\n}\n```\n\n## Configure\n\n#### `config.router`\nThe Router takes a few Configuration values\n```js\n// config/router.ts\nexport const router = {\n  sortOrder: 'asc', // (asc | desc)\n  default: '', // the default or home route\n  catchAllRoute: '*', // the catch all handler route\n  prefix: '/api/v1',\n  debug: false // if the router needs to log all debugs\n}\n```\n##### router.sortOrder\nThis will sort the routes based on the key (path) either ascending or descending. This is used in spools like Express where the order of routes matters.\n\n##### router.prefix\nThis config is optional and can be left as `''` or `null`.  This will prefix each route with the specified prefix.\n\n##### router.default\nThis config is optional and can be left as `''` or `null`.  This is '/' in express, but '' in Hapi\n\n##### router.catchAllRoute\nThis config is optional and can be left as `''` or `null`.  This is '*' in express and Hapi\n\n\n#### `config.routes`\nThe list of route objects to be compiled for use by the webserver.\n\n```js\n// config/routes.ts\nconst routes = {\n  '/example/test': {\n    'GET': 'ExampleController.test'\n  }\n}\n```\n\nDuring initialization, for the above example, a route object will be compiled\nthat takes the following form:\n\n```js\n{\n  // ...\n  '/example/test': {\n    'GET': {\n      handler: 'ExampleController.test',\n      config: {\n        pre: [ ]\n      }\n    }\n  }\n  // ...\n}\n```\n\nYou can also refine this by explicitly defining the handler and config:\n\n```js\n{\n  // ...\n  '/example/test': {\n    'GET': {\n      handler: 'ExampleController.get',\n      config: {\n        pre: [ 'ExamplePolicy.get' ]\n      }\n    },\n    'POST': {\n      handler: 'ExampleController.post',\n      config: {\n        pre: [ 'ExamplePolicy.post' ]\n      }\n    }\n  }\n  // ...\n}\n```\nWhich is useful for refining controller over different http methods on a route.\n\n##### Prefixes\n```js\n{\n  // ...\n  '/example/test': {\n    'GET': 'ExampleController.test',\n    config: {\n      prefix: '/api/v2'\n    }\n  }\n  // ...\n}\n```\nThe Configuration above, will give this route a prefix of `/api/v2` instead of using the `config.prefix` that was specified \n\nOptionally:\n\n```js\n{\n  // ...\n  '/example/test': {\n    'GET': 'ExampleController.test',\n    config: {\n      prefix: false\n    }\n  }\n  // ...\n}\n```\nThe Configuration above, will ignore any prefix given to it. \n\nOptionally:\n\n```js\n{\n  // ...\n  '/example/test': {\n    'GET': 'ExampleController.test',\n    config: {\n      prefix: 'customPrefixer.prefix'\n    }\n  }\n  // ...\n}\n```\nThe configuration above will take the configuration of another config attribute, in this case: `app.config.customPrefixer.prefix` is set to `/custom/endpoint` so the resulting route prefix will be `/custom/endpoint/example/test`\n\nAdditionally, you can also provide 2 different prefixes for the same route with different methods.\n\n```js\n{\n  // ...\n  '/example/test': {\n    'GET': {\n      handler: 'ExampleController.get',\n      config: {\n        prefix: '/api/v1'\n        pre: [ 'ExamplePolicy.get' ]\n      }\n    },\n    'POST': {\n      handler: 'ExampleController.post',\n      config: {\n        prefix: '/api/v2'\n        pre: [ 'ExamplePolicy.post' ]\n      }\n    }\n  }\n  // ...\n}\n```\n\nThe configuration above will produce 2 routes, one for `GET /api/v1/example/test` and one for `POST /api/v2/example/test` respecting their prefixes. This is useful for when one method may still be on an older API than the other or they need to be handled differently.\n\n\nFinally, you can version your route with prefixes\n__under development__\n```js\n{\n  // ...\n  '/example/test': {\n    'GET': {\n       versions: { \n           'ExampleController.get': {\n              config: {\n                 prefix: 'prefix.one',\n                 pre: [ 'ExamplePolicy.get' ]\n              }\n          },\n          'ExampleController.getTwo': {\n                config: {\n                 prefix: 'prefix.two',\n                 pre: [ 'ExamplePolicy.get' ]\n              }\n           }\n        }\n    },\n    'POST': {\n      handler: 'ExampleController.post',\n      config: {\n        prefix: '/api/v2',\n        pre: [ 'ExamplePolicy.post' ]\n      }\n    }\n  }\n  // ...\n}\n```\n\n## Tapestries and Policies\n\nSupport for tapestries and Policies is provided by [spool-tapestries](https://github.com/fabrix-app/spool-tapestries).\n\n## Compatible Spools\n- [spool-express](https://github.com/fabrix-app/spool-express)\n- [spool-hapi](https://github.com/fabrix-app/spool-hapi)\n- [spool-koa](https://github.com/fabrix-app/spool-koa) (TODO)\n\n## Contributing\nWe love contributions! Please see our [Contribution Guide](https://github.com/fabrix-app/fabrix/blob/master/CONTRIBUTING.md)\nfor more information.\n\n## License\n[MIT](https://github.com/fabrix-app/spool-router/blob/master/LICENSE)\n\n[npm-image]: https://img.shields.io/npm/v/@fabrix/spool-router.svg?style=flat-square\n[npm-url]: https://npmjs.org/package/@fabrix/spool-router\n[ci-image]: https://img.shields.io/circleci/project/github/fabrix-app/spool-router/master.svg\n[ci-url]: https://circleci.com/gh/fabrix-app/spool-router/tree/master\n[daviddm-image]: http://img.shields.io/david/fabrix-app/spool-router.svg?style=flat-square\n[daviddm-url]: https://david-dm.org/fabrix-app/spool-router\n[gitter-image]: http://img.shields.io/badge/+%20GITTER-JOIN%20CHAT%20%E2%86%92-1DCE73.svg?style=flat-square\n[gitter-url]: https://gitter.im/fabrix-app/fabrix\n[twitter-image]: https://img.shields.io/twitter/follow/FabrixApp.svg?style=social\n[twitter-url]: https://twitter.com/FabrixApp\n[coverage-image]: https://img.shields.io/codeclimate/coverage/github/fabrix-app/spool-router.svg?style=flat-square\n[coverage-url]: https://codeclimate.com/github/fabrix-app/spool-router/coverage\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffabrix-app%2Fspool-router","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffabrix-app%2Fspool-router","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffabrix-app%2Fspool-router/lists"}