{"id":20894961,"url":"https://github.com/islamaf/router-wrapper-js","last_synced_at":"2026-01-05T01:13:20.468Z","repository":{"id":209180940,"uuid":"723429672","full_name":"islamaf/router-wrapper-js","owner":"islamaf","description":"A router wrapper for Express \u0026 Fastify","archived":false,"fork":false,"pushed_at":"2024-05-18T17:08:49.000Z","size":46,"stargazers_count":1,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-10-17T07:05:58.867Z","etag":null,"topics":["express","express-router","expressjs","fastify","javascript","middleware","router","typescript"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/router-wrapper-js","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/islamaf.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","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}},"created_at":"2023-11-25T16:34:16.000Z","updated_at":"2024-05-18T17:08:53.000Z","dependencies_parsed_at":"2024-01-19T02:37:09.584Z","dependency_job_id":"96069477-c7a1-4499-814d-f89cbc0a007c","html_url":"https://github.com/islamaf/router-wrapper-js","commit_stats":null,"previous_names":["islamaf/router-wrapper-js","islamaf/express-router-wrapper"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/islamaf%2Frouter-wrapper-js","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/islamaf%2Frouter-wrapper-js/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/islamaf%2Frouter-wrapper-js/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/islamaf%2Frouter-wrapper-js/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/islamaf","download_url":"https://codeload.github.com/islamaf/router-wrapper-js/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244463901,"owners_count":20456962,"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","express-router","expressjs","fastify","javascript","middleware","router","typescript"],"created_at":"2024-11-18T10:24:08.332Z","updated_at":"2026-01-05T01:13:20.418Z","avatar_url":"https://github.com/islamaf.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# RouterWrapperJS\n\n\u003cp align=\"center\"\u003e\n  \u003cimg src=\"https://img.shields.io/github/package-json/v/islamaf/router-wrapper-js\"\u003e\n  \u003cimg src=\"https://img.shields.io/librariesio/release/npm/router-wrapper-js\"\u003e\n  \u003cimg src=\"https://img.shields.io/npm/dm/router-wrapper-js\"\u003e\n  \u003cimg src=\"https://img.shields.io/github/license/islamaf/router-wrapper-js\"\u003e\n  \u003cimg src=\"https://img.shields.io/github/contributors/islamaf/router-wrapper-js\"\u003e\n  \u003cimg src=\"https://img.shields.io/github/last-commit/islamaf/router-wrapper-js\"\u003e\n\u003c/p\u003e\n\n**RouterWrapperJS** is a wrapper for backend frameworks routes, making them more compact with better chaining and custom middleware sharing. Abstracting middlewares and other functions.\n\nCurrently supported frameworks:\n\n- Express\n- Fastify\n\n# Getting Started\n\n### Install RouterWrapperJS via npm:\n\n```shell\nnpm i router-wrapper-js\n```\n\n### Install RouterWrapperJS via yarn:\n\n```shell\nyarn add router-wrapper-js\n```\n\n### Usage\n\nThe basic structure for route params:\n\n- Express\n  ```ts\n  {\n      path: string;\n      handler: Function; // Controller function\n      middleware?: any[]; // Middlewares\n  }\n  ```\n- Fastify\n  ```ts\n  {\n      path: string;\n      handler: Function; // Controller function\n      schema?: {};\n      preHandler?: any[]; // Middlewares\n  }\n  ```\n\n### Usage with Express\n\nAn example of express router wrapper usage:\n\n```ts\nimport express, { Request } from \"express\";\nimport { ExpressRouterWrapper } from \"router-wrapper-js\";\n\nconst app = express();\n\nconst router = new ExpressRouterWrapper({ auth?: YOUR_AUTH_MIDDLEWARE })\n    .get({\n        path: \"/\",\n        handler: async () =\u003e await homeController(),\n        middleware: [MIDDLWARE_1, MIDDLWARE_2, MIDDLWARE_3]\n    })\n    .protectedGet({\n        path: \"/user\",\n        handler: async (req: Request) =\u003e await userController(req),\n        middleware: [MIDDLWARE_1, MIDDLWARE_2, MIDDLWARE_3]\n    })\n    .post({\n        path: \"/user\",\n        handler: async (req: Request) =\u003e await newUserController(req)\n    })\n    .protectedPost({\n        path: \"/book\",\n        handler: async (req: Request) =\u003e await newBookController(req)\n    })\n    .patch({\n        path: \"/user\",\n        handler: async (req: Request) =\u003e await editUserController(req)\n    })\n    .protectedPatch({\n        path: \"/book\",\n        handler: async (req: Request) =\u003e await editBookController(req)\n    })\n    .delete({\n        path: \"/user/:id\",\n        handler: async (req: Request) =\u003e await deleteUserController(req)\n    })\n    .protectedDelete({\n        path: \"/book/:id\",\n        handler: async (req: Request) =\u003e await deleteBookController(req)\n    })\n    .make();\n\napp.use(\"/\", router);\n\napp.listen(5000);\n```\n\n### Usage with Fastify\n\nAn example of fastify router wrapper usage:\n\n```ts\nimport Fastify, { FastifyReply, FastifyRequest } from \"fastify\";\nimport { FastifyRouterWrapper } from \"router-wrapper-js\";\n\nconst fastify = Fastify();\n\nconst route = new FastifyRouterWrapper(fastify, { auth?: YOUR_AUTH_MIDDLEWARE })\n    .get({\n        path: \"/\",\n        handler: async () =\u003e await homeController(),\n        preHandler: [MIDDLWARE_1, MIDDLWARE_2, MIDDLWARE_3]\n    })\n    .protectedGet({\n        path: \"/user\",\n        handler: async (req: FastifyRequest, reply: FastifyReply) =\u003e await userController(req),\n        preHandler: [MIDDLWARE_1, MIDDLWARE_2, MIDDLWARE_3]\n    })\n    .post({\n        path: \"/user\",\n        handler: async (req: FastifyRequest, reply: FastifyReply) =\u003e await newUserController(req),\n        schema: userSchema\n    })\n    .protectedPost({\n        path: \"/book\",\n        handler: async (req: FastifyRequest, reply: FastifyReply) =\u003e await newBookController(req),\n        schema: bookSchema,\n        preHandler: [MIDDLEWARE_1, MIDDLEWARE_2]\n    })\n    .patch({\n        path: \"/user\",\n        handler: async (req: FastifyRequest, reply: FastifyReply) =\u003e await editUserController(req)\n    })\n    .protectedPatch({\n        path: \"/book\",\n        handler: async (req: FastifyRequest, reply: FastifyReply) =\u003e await editBookController(req)\n    })\n    .delete({\n        path: \"/user/:id\",\n        handler: async (req: FastifyRequest, reply: FastifyReply) =\u003e await deleteUserController(req)\n    })\n    .protectedDelete({\n        path: \"/book/:id\",\n        handler: async (req: FastifyRequest, reply: FastifyReply) =\u003e await deleteBookController(req)\n    })\n    .make();\n\nfastify.register(route);\n\nfastify.listen({ port: 5000 });\n```\n\n# Sharing Middleware\n\nSharing middleware in both Express and Fastify router wrappers, allows middleware to be applied for some routes which are in the `shared middleware` array and ignoring routes not in the array. Usage example:\n\n### With Express\n\n```ts\nconst router = new ExpressRouterWrapper(\n    { auth: AUTH },\n    [SHARED_MIDDLEWARE_1, SHARED_MIDDLEWARE_2, ..., SHARED_MIDDLEWARE_N] // Shared middleware\n)\n    /**\n     * Routes which share middleware - This function has to always be before route methods\n     * It also applies to the multiple method routes, i.e, using the multiple()\n     * Here, only GET /user route will have shared middleware applied to it\n     * While PATCH /user and GET /user/data will not have the shared middleware\n     */\n    .shareTo([\"GET /user\"])\n    .get({\n        path: \"/user\",\n        handler: async (req: Request) =\u003e await userController(req)\n    })\n    .patch({\n        path: \"/user\",\n        handler: async (req: Request) =\u003e await editUserController(req),\n        middleware: [MIDDLEWARE]\n    })\n    .protectedGet({\n        path: \"/user/data\",\n        handler: async (req: Request) =\u003e await userDataController(req),\n        middleware: [MIDDLEWARE]\n    })\n    .make();\n```\n\n### With Fastify\n\n```ts\nconst routes = new FastifyRouterWrapper(fastify, { auth }, [\n    SHARED_MIDDLEWARE_1, SHARED_MIDDLEWARE_2, ..., SHARED_MIDDLEWARE_N\n])\n    .shareTo([\"GET /user\"])\n    .multiple(\n        \"/user\",\n        [\"get\", \"post\"],\n        [\n            {\n                handler: async (request: FastifyRequest, reply: FastifyReply) =\u003e await getUserController(req)\n                preHandler: [MIDDLEWARE_1]\n            },\n            {\n                handler: async (request: FastifyRequest, reply: FastifyReply) =\u003e await createUserController(req)\n            }\n        ]\n    )\n    .protectedGet({\n        path: \"/user\",\n        handler: async (request: FastifyRequest, reply: FastifyReply) =\u003e await userController(req),\n        preHandler: [PREHANDLERS]\n    })\n    .patch({\n        path: \"/book\",\n        handler: async (request: FastifyRequest, reply: FastifyReply) =\u003e await bookController(req),\n        preHandler: [PREHANDLERS]\n    })\n    .make();\n```\n\n# Multiple Methods per Route\nUsing `multiple()` accepts multiple methods with their handlers and middleware for a single route.  \nThe objects containing handlers and middlewares array are ordered relatively to the order of the the `methods` array.\n\n### With Express\n\n```ts\nconst router = new ExpressRouterWrapper(\n    { auth: AUTH },\n    [SHARED_MIDDLEWARE_1, SHARED_MIDDLEWARE_2, ..., SHARED_MIDDLEWARE_N] // Shared middleware\n)\n    .shareTo([\"GET /user\"])\n    /**\n     * Allowing multiple methods to a route\n     * Order of methods defines the order of route handlers\n     */\n    .multiple(\n        \"/user\",\n        [\"get\", \"post\"],\n        [\n            {\n                // Here the handler is for the GET method\n                handler: async (req: Request) =\u003e await getUserController(req)\n                middleware: [MIDDLEWARE_1]\n            },\n            {\n                // And here the handler is for the POST method\n                handler: async (req: Request) =\u003e await createUserController(req)\n            }\n        ]\n    )\n    .make();\n```\n\n### With Fastify\n\n```ts\nconst routes = new FastifyRouterWrapper(fastify, { auth }, [\n    SHARED_MIDDLEWARE_1, SHARED_MIDDLEWARE_2, ..., SHARED_MIDDLEWARE_N\n])\n    .shareTo([\"GET /user\"])\n    .multiple(\n        \"/user\",\n        [\"get\", \"post\"],\n        [\n            {\n                handler: async (request: FastifyRequest, reply: FastifyReply) =\u003e await getUserController(req)\n                middleware: [MIDDLEWARE_1]\n            },\n            {\n                handler: async (request: FastifyRequest, reply: FastifyReply) =\u003e await createUserController(req)\n            }\n        ]\n    )\n    .make();\n```\n\n\n# Controller Handler\n\nThe router wrapper has an internal controller handler wrapper for both the Express and Fastify. Basically wrapping the controller function passed to the route function. The format of the expected returned `data` object from the controller function in your `app` is supposed to look like this:\n\n```ts\n{\n    status: number, // Status code of the operation\n    success: boolean, // Success state of the operation\n    data: {} // Data returned from the operation\n}\n```\n\n### Example\n\nTaking the `newUserController()` as an example:\n\n```ts\nconst newUserController = async (req: Request) =\u003e {\n  const res = await addNewUserService(req);\n\n  return {\n    status: res.status,\n    success: res.success,\n    data: res.data,\n  };\n};\n```\n\n# Open to contributors\n\nTo add a middleware which might be useful in general use with the router wrapper, kindly, make a pull request with your extended implementation.  \nAlso, looking forward to extend the wrapping beyond ExpressJs and Fastify to include router wrapping for other JS backend frameworks!\n\nThank you:)\n\n# License\n\n[MIT](LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fislamaf%2Frouter-wrapper-js","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fislamaf%2Frouter-wrapper-js","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fislamaf%2Frouter-wrapper-js/lists"}