{"_id":"@achtjs/router","_rev":"4-6ab3dd8e1e6bd1820fa54900e9be6500","name":"@achtjs/router","dist-tags":{"latest":"0.1.3"},"versions":{"0.0.0":{"name":"@achtjs/router","version":"0.0.0","description":"achtJS Router Module - Simple NodeJS routing library","main":"lib/index.js","author":{"name":"Pedro Harbs","email":"harbspj@gmail.com"},"license":"MIT","keywords":["nodejs","rest","api","http"],"repository":{"type":"git","url":"git+https://github.com/achtjs/router.git"},"bugs":{"url":"https://github.com/achtjs/router/issues"},"homepage":"https://github.com/achtjs/router#readme","scripts":{"test":"echo \"Error: no test specified\" && exit 1"},"devDependencies":{"@achtjs/ts-config":"^0.1.0","@types/node":"^13.11.0","@types/ws":"^8.5.3","jest":"^28.0.2","reflect-metadata":"^0.1.13","ts-node":"^8.8.2","typescript":"^4.1.2"},"dependencies":{"ws":"^8.6.0"},"gitHead":"89031520a162583dda2c9ba63236e12e75a3915f","_id":"@achtjs/router@0.0.0","_nodeVersion":"12.22.12","_npmVersion":"6.14.16","dist":{"integrity":"sha512-Jw63HKA0kg+vBxgzujb2+soUV8bE3h6Yh1ffJjslaARMvv2HdrlRZb2NM7H5N6eRd1XfZPUDWFm7FpJelXGwCg==","shasum":"13cd86c883e3d76c93d037f2cf7b05d1ea45e01d","tarball":"https://registry.npmjs.org/@achtjs/router/-/router-0.0.0.tgz","fileCount":26,"unpackedSize":55848,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIF+iMxOifqRP7bLuU6ZzuVCZv4mKBQNpjtUOeZjjyTsHAiA0LwouAehfAnnjSS5c7wilJwVTGhLQ5qm14ftOwbz8lQ=="}]},"_npmUser":{"name":"jharbs","email":"harbspj@gmail.com"},"directories":{},"maintainers":[{"name":"jharbs","email":"harbspj@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/router_0.0.0_1692545561183_0.03721427292709767"},"_hasShrinkwrap":false},"0.1.0":{"name":"@achtjs/router","version":"0.1.0","description":"achtJS Router Module - Simple NodeJS routing library","main":"lib/index.js","author":{"name":"Pedro Harbs","email":"harbspj@gmail.com"},"license":"MIT","keywords":["nodejs","rest","api","http"],"repository":{"type":"git","url":"git+https://github.com/achtjs/router.git"},"bugs":{"url":"https://github.com/achtjs/router/issues"},"homepage":"https://github.com/achtjs/router#readme","scripts":{"test":"echo \"Error: no test specified\" && exit 1"},"devDependencies":{"@achtjs/ts-config":"^0.1.0","@types/node":"^13.11.0","@types/ws":"^8.5.3","jest":"^28.0.2","reflect-metadata":"^0.1.13","ts-node":"^8.8.2","typescript":"^4.1.2"},"dependencies":{"ws":"^8.6.0"},"gitHead":"dba525bf0b3e34e4b63315f65c8ef6ecf67e3849","_id":"@achtjs/router@0.1.0","_nodeVersion":"12.22.12","_npmVersion":"6.14.16","dist":{"integrity":"sha512-fgrGB6Q9mzJQPksL39BGALaaHvM65+mZyV1XcFN/0yk604/nWDu1mldz9FCNFxrnuHGfSm2OjploiriToWuuRw==","shasum":"c1a51579e59c845a216c2e41d8a10ab6db9e80a7","tarball":"https://registry.npmjs.org/@achtjs/router/-/router-0.1.0.tgz","fileCount":26,"unpackedSize":55848,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQCSH/yhMSsprp7kXZ5HnvdFhdvf+oNmsWdFt6rP5/IZ4QIhALoJfDixNuE90cv7cY3QIyCvU5nU2liLrnISOSYa/Dxu"}]},"_npmUser":{"name":"jharbs","email":"harbspj@gmail.com"},"directories":{},"maintainers":[{"name":"jharbs","email":"harbspj@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/router_0.1.0_1692545665925_0.011975978472481552"},"_hasShrinkwrap":false},"0.1.1":{"name":"@achtjs/router","version":"0.1.1","description":"achtJS Router Module - Simple NodeJS routing library","main":"lib/index.js","types":"lib/index.d.ts","author":{"name":"Pedro Harbs","email":"harbspj@gmail.com"},"license":"MIT","keywords":["nodejs","rest","api","http"],"repository":{"type":"git","url":"git+https://github.com/achtjs/router.git"},"bugs":{"url":"https://github.com/achtjs/router/issues"},"homepage":"https://github.com/achtjs/router#readme","scripts":{"build":"tsc"},"devDependencies":{"@achtjs/ts-config":"^0.1.0","@types/node":"^13.11.0","@types/ws":"^8.5.4","jest":"^28.0.2","reflect-metadata":"^0.1.13","ts-node":"^8.8.2","typescript":"^4.1.2"},"dependencies":{"ws":"^8.6.0"},"gitHead":"a42593b84496802e6912e140752bc74af780c843","_id":"@achtjs/router@0.1.1","_nodeVersion":"12.22.12","_npmVersion":"6.14.16","dist":{"integrity":"sha512-7bTaRVzsr5W/Os0K231+7R8bx1iqcH2ZTIpGeTo/duymgPHK6Wf8B+OmrNKQt4AMow2E1k58elE9D7Plvohj9Q==","shasum":"a1d1cc3d16c7f28dd315750e5f1e9a4cab00bd64","tarball":"https://registry.npmjs.org/@achtjs/router/-/router-0.1.1.tgz","fileCount":68,"unpackedSize":104777,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQDyLufhZue5TZNT917kqI+OEFwRtCAbyvYnBcy+xQ+q9gIgU24oot5ATFso2IE7FxfOxUQHaIOLyQBtGhsjHJkGbfw="}]},"_npmUser":{"name":"jharbs","email":"harbspj@gmail.com"},"directories":{},"maintainers":[{"name":"jharbs","email":"harbspj@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/router_0.1.1_1692547250247_0.8805245614860058"},"_hasShrinkwrap":false},"0.1.2":{"name":"@achtjs/router","version":"0.1.2","description":"achtJS Router Module - Simple NodeJS routing library","main":"lib/index.js","types":"lib/index.d.ts","author":{"name":"Pedro Harbs","email":"harbspj@gmail.com"},"license":"MIT","keywords":["nodejs","rest","api","http"],"repository":{"type":"git","url":"git+https://github.com/achtjs/router.git"},"bugs":{"url":"https://github.com/achtjs/router/issues"},"homepage":"https://github.com/achtjs/router#readme","scripts":{"build":"tsc"},"devDependencies":{"@achtjs/ts-config":"^0.1.0","@types/node":"^13.11.0","@types/ws":"^8.5.4","jest":"^28.0.2","reflect-metadata":"^0.1.13","ts-node":"^8.8.2","typescript":"^4.1.2"},"dependencies":{"ws":"^8.6.0"},"gitHead":"e9c5dd9ba09c3f67309b5da3f505f2f6e7442b45","_id":"@achtjs/router@0.1.2","_nodeVersion":"12.22.12","_npmVersion":"6.14.16","dist":{"integrity":"sha512-JJGeoZ42C51Vv1RqjAcxFg6z6DiE671EHtZTjH+S+HbJzsuGMd3D2uhpBu/R7Fq3uc6+zica0OoeT0GhcO3YUQ==","shasum":"5644232c2bf332be23eab3170e5f5b3f3cda3d23","tarball":"https://registry.npmjs.org/@achtjs/router/-/router-0.1.2.tgz","fileCount":68,"unpackedSize":116009,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIGGyaRd5rEPSJEnzI86zogHUkVAJxoPp99nRQU+KPb7BAiEApipD9U7IWXQ6VorsbmHkOwsSns6gcapcpoUJvM49wWA="}]},"_npmUser":{"name":"jharbs","email":"harbspj@gmail.com"},"directories":{},"maintainers":[{"name":"jharbs","email":"harbspj@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/router_0.1.2_1692557280621_0.9816348623642923"},"_hasShrinkwrap":false},"0.1.3":{"name":"@achtjs/router","version":"0.1.3","description":"achtJS Router Module - Simple NodeJS routing library","main":"lib/index.js","types":"lib/index.d.ts","author":{"name":"Pedro Harbs","email":"harbspj@gmail.com"},"license":"MIT","keywords":["nodejs","rest","api","http"],"repository":{"type":"git","url":"git+https://github.com/achtjs/router.git"},"bugs":{"url":"https://github.com/achtjs/router/issues"},"homepage":"https://github.com/achtjs/router#readme","scripts":{"build":"tsc"},"devDependencies":{"@achtjs/ts-config":"^0.1.3","@types/node":"^13.11.0","@types/ws":"^8.5.4","jest":"^28.0.2","reflect-metadata":"^0.1.13","ts-node":"^8.8.2","typescript":"^4.1.2"},"dependencies":{"ws":"^8.6.0"},"gitHead":"3b34c0fbb350de4857a08c3357df474a9b883dc6","_id":"@achtjs/router@0.1.3","_nodeVersion":"12.22.12","_npmVersion":"6.14.16","dist":{"integrity":"sha512-Iv6pCfHGwErXoVvIVyMRriYvj/yjJSKQau35I6h8sB0NBLBLo8Nae5ltKP12+7ZENQ/wW1o/4JjXuZpJzj7yGQ==","shasum":"56a20257bc2df0a48f1b9264f3baa0ad03a00809","tarball":"https://registry.npmjs.org/@achtjs/router/-/router-0.1.3.tgz","fileCount":46,"unpackedSize":88298,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIDV9UIYh5K3+aMOjGtRRzhfbMKpylyr4YhzGHvn2NDvJAiEAi25OKy98beFifRTFrTjQCl+6bQ47/dYpT5EMXp5nyzY="}]},"_npmUser":{"name":"jharbs","email":"harbspj@gmail.com"},"directories":{},"maintainers":[{"name":"jharbs","email":"harbspj@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/router_0.1.3_1692565002541_0.4634080605107487"},"_hasShrinkwrap":false}},"time":{"created":"2023-08-20T15:32:41.045Z","0.0.0":"2023-08-20T15:32:41.465Z","modified":"2023-08-20T20:56:42.977Z","0.1.0":"2023-08-20T15:34:26.099Z","0.1.1":"2023-08-20T16:00:50.442Z","0.1.2":"2023-08-20T18:48:00.824Z","0.1.3":"2023-08-20T20:56:42.741Z"},"maintainers":[{"name":"jharbs","email":"harbspj@gmail.com"}],"description":"achtJS Router Module - Simple NodeJS routing library","homepage":"https://github.com/achtjs/router#readme","keywords":["nodejs","rest","api","http"],"repository":{"type":"git","url":"git+https://github.com/achtjs/router.git"},"author":{"name":"Pedro Harbs","email":"harbspj@gmail.com"},"bugs":{"url":"https://github.com/achtjs/router/issues"},"license":"MIT","readme":"# @achtjs/router\n\n<h1 align=\"center\">AchtJS Router Module</h1>\n\n<h2 align=\"center\">Lightweight, simple and fast NodeJS Router developed using Typescript.</h2>\n\n## Suggestions\n\nFor suggestions and features you want to see in the future you can visit our [issues section.](https://github.com/achtjs/router/issues)\n\n## Getting started\n\nBefore installing this package make sure to download and install Node.js 10 or higher.\n\n### Setup Node\n\nTo check your node version\n\n```shell\nnpm -v\n```\n\nFor new project kindly initialize npm using the command below or see documentation [here](https://docs.npmjs.com/creating-a-package-json-file).\n\n```shell\nnpm init\n```\n\n### Setup Typescript\n\nInstall typescript and typescript watcher\n\n```shell\nnpm i typescript ts-node-dev -D\n```\n\nInitialize typescript\n\n```shell\ntsc --init\n```\n\n### Setup Router\n\nInstall achtjs router\n\n```shell\nnpm i @achtjs/router\n```\n\nAdd `start` script on your `package.json`\n\n```js\n\"scripts\": {\n  \"start\": \"tsnd src\"\n}\n```\n\n## Features\n\n-   Simple and easy\n-   Lightweight library\n-   Built with TypeScript\n-   No third party library\n-   Modules for shareable code\n-   Dependency Injection and IOC\n-   Fast execution with route caching\n-   Supports beginners to advanced developers\n-   `Function` and `Class` based coding style\n-   Supports third party middlewares like `ExpressJS`\n\n## Quick Start\n\nThe easiest way to get started is to create a simple route. Create a `src` folder inside your project folder. Inside the `src` folder create an `index.ts` file. Copy the code below into `index.ts` file.\n\n```ts\nimport achtjs from \"@achtjs/router\";\nimport http from \"http\";\n\nconst PORT = 3000;\nconst app = achtjs();\n\napp.add([\n    {\n        path: \"/\",\n        GET: () => \"Hello, World\",\n    },\n]);\n\nhttp.createServer(app).listen(PORT, () =>\n    console.log(`Server listening on port ${PORT}.`)\n);\n```\n\n## RUN\n\nTo run your achtjs project open a terminal inside your current project and run the command below.\n\n```shell\nnpm start\n```\n\n## ROUTE\n\nDefining routes in AchtJS is like creating a JSON object.\n\n```ts\nconst user = {\n    path: \"user\",\n    GET: () => {\n        //  ...\n    },\n    POST: () => {\n        //  ...\n    },\n    PUT: () => {\n        //  ...\n    },\n    PATCH: () => {\n        //  ...\n    },\n    DELETE: () => {\n        //  ...\n    },\n    OPTIONS: () => {\n        //  ...\n    },\n};\n```\n\nAs seen above each method name has a `callback` function. AchtJS will set the correct `content-type` based on what the method callback will return.\n\n```ts\nconst route = {\n    path: \"user\",\n    GET: () => {\n        // This will return `undefined`\n        // This will have a content-type of \"text/plain\"\n    },\n    POST: () => {\n        return \"Hello\"; // This will have a content-type of \"text/plain\"\n    },\n    PUT: () => {\n        return { message: \"Hello\" }; // This will have a content-type of \"application/json\"\n    },\n    PATCH: () => {\n        return \"<h1>Hello</h1>\"; // This will have a content-type of \"text/html\"\n    },\n};\n```\n\nIncase you need to use a middleware for an specific route method you need to create a route object instead.\n\n```ts\nconst route = {\n    path: \"user\",\n    GET: {\n        middlewares: [middleware],\n        callback: () => {\n            // ...\n        },\n    },\n};\n```\n\n## PARAMS\n\nAchtJS passes a context object on each route method. You can acces the route params by destructuring the context object on the callback method.\n\n```ts\nconst route = {\n    path: \"user/:id\",\n    GET: ({ params }) => {\n        // `params` object contains the value of `id` define on the path above e.g. `user/hello`\n        return params.id; // This has a value of \"hello\"\n    },\n};\n```\n\n## QUERY STRING\n\nYou can also access the query string using the context object provided by AchtJS on your route method callback function.\n\n```ts\nconst route = {\n    path: \"user\",\n    GET: ({ query }) => {\n        // `query` object contains all the value on your query string e.g. `user?id=1`\n        return query.id; // This has a value of 1\n    },\n};\n```\n\n## BODY\n\nRequest body is also accessible in the context but you need a middleware before you can actually uses. There is a popular middleware called [body-parser](https://www.npmjs.com/package/body-parser) that we can use to achieve this.\n\n```ts\nimport achtjs from \"@achtjs/router\";\n// Third party middleware you need to install\nimport bodyParser from \"body-parser\";\n\nconst app = achtjs();\n\n// Initialize body parser for achtjs to use it internally\napp.use(bodyParser.json());\napp.use(bodyParser.urlencoded({ extended: false }));\n\nconst route = {\n    path: \"user\",\n    POST: ({ body }) => {\n        // `body` object contains all the value from the request body\n        return body; // This has a value of req.body\n    },\n};\n\napp.add([route]);\n```\n\n## MIDDLEWARES\n\nAs seen above how we used third party middlewares to parse the request body. We can also create our own middlewares. Although AchtJS supports all [ExpressJs Middlewares](https://expressjs.com/en/resources/middleware.html), we also can create a `AchtJsMiddleware` of our own. The context is where you can access `params, query and body` objects.\n\n```ts\nrouter.use((context, next, error) => {\n    // This `error` variable comes from the previous middleware\n    console.log(error);\n\n    // The `next` function will trigger the end of this callback function\n    // and execute the next middleware in the list\n    next(\n        \"Error message or any value that you want to pass to the next middleware\"\n    );\n});\n```\n\n## CHILD ROUTES\n\nA child route is a route that appended to its parent route. It can be added using the `children` property from the route object.\n\n```ts\napp.add([\n    {\n        path: \"parent\", // This is the `parent` route\n        children: [\n            {\n                path: \"child\", // You can call this child route by `parent/child`\n                GET: () => \"This is from a child route\",\n            },\n        ],\n    },\n]);\n```\n\n```\nNOTE: A `child` route can also have its own `children`.\n```\n\n```ts\napp.add([\n    {\n        path: \"parent\", // This is the `parent` route\n        GET: () => \"This is a parent route\",\n        children: [\n            {\n                path: \"child\", // You can call this child route by `users/child`\n                GET: () => \"This is a child route\",\n                children: [\n                    {\n                        path: \"grandchild\", // You can call this child route by `parent/child/grandchild`\n                        GET: () => \"This is a grandchild route\",\n                    },\n                ],\n            },\n        ],\n    },\n]);\n```\n\n## CONTROLLER\n\nA controller is a class that act as a replacement for the route method objects. It can be used to make your code more modular and manageable when your code base get bigger.\n\n```ts\nclass UsersController extends Controller {\n    GET() {\n        return \"Hello form UsersController\";\n    }\n\n    // You can also add additional route method like this\n    POST() {\n        // ...\n    }\n\n    // Other routes available are PUT, PATCH, DELETE and OPTIONS\n}\n\napp.add([\n    {\n        path: \"user\",\n        controller: UsersController,\n    },\n]);\n```\n\nIf you want to add middleware a middleware to a specific method you can add the `middleware` property in a controller.\n\n```ts\nclass UsersController extends Controller {\n    middleware = {\n        GET: [middleware], // This middleware will be added to the `GET` method below\n    };\n\n    GET() {\n        //  ...\n    }\n}\n```\n\n```\nNOTE: A controller has no `children` property\n```\n\n## SERVICES\n\nA service is an injectable class that can be used inside of a controller or another service.\n\n```ts\nexport class UsersServices extends Services {\n    // Every function inside the service can be accessed by the class its injected\n    // All public properties are also accessible\n    // A service is instatiated once and making it a singleton\n\n    add() {\n        // ...\n    }\n}\n\nclass UsersController extends Controller {\n    constructor(userServices: UsersServices) {\n        super();\n    }\n\n    addUser() {\n        // This `add` function coming from the `UsersService`.\n        // It is accessible on this controller via dependency injection.\n        // AchtJS automatically instantiated it and created it singleton instance.\n        this.userServices.add();\n    }\n}\n```\n\n## MODULE\n\nA module is a like a container of routes, controller and services that act on its own. This will allow your code to modular and shareable accross all routes.\n\n```ts\nclass UsersModule extends AchtJsModule {\n    // List of `module` or `services` that this module will use for its controllers\n    imports = [];\n    // List of controllers that this module will use\n    declarations = [];\n    // List of services that can be use inside this module\n    providers = [];\n    // List of services that this module will exports that can be used by other modules\n    exports = [];\n    // List of dependencies that will be injected to this module\n    dependencies = [];\n}\n```\n\nAs you can see when we create a module we dont have any way to add routes on it. Below is an example of implementation of how to add a `RouterModule` for this module to add routes.\n\n```ts\nimport { UsersController, UsersIdController } from \"./controller\";\nimport { RouterModule } from \"@achtjs/router\";\nimport { UsersServices } from \"./services\";\n\n// Define the routes\nconst routes = [\n    {\n        path: \"/\",\n        controller: UsersController,\n    },\n    {\n        path: \":id\",\n        controller: UsersIdController,\n    },\n];\n\nexport class UsersModule extends AchtJsModule {\n    // User `RouterModule.forRoot` to inject the routes\n    imports = [RouterModule.forRoot(routes)];\n    // Declare the controllers that will be used inside the routes\n    declarations = [UsersIdController, UsersController];\n}\n```\n\n## CUSTOM MODULE\n\nUnlike the common achtjs module, custom modules provide other functionality aside from managing your controllers. One example of this the `RouterModule` that is reponsible for adding routes to your module. A key concept of a custom module is it has an `invoke` and `forRoot` function that provides additional functionalities.\n\n```ts\nexport class UsersModule extends CustomModule {\n    invoke() {\n        // This function will run after an instance of this module is created\n        // But the providers will stay singleton on each instance\n    }\n\n    static forRoot(options: any) {\n        return {\n            module: UsersModule, // Returns the current module reference\n            providers: [], // List of providers that this module will use on its controllers\n        };\n    }\n}\n```\n\nNow we know how to create a module but how we can call it on our routes. We are now introducing to `loadChildren` property of our route object. AchtJS will automatically resolve the module asynchronously when the path is added on the route list.\n\n```ts\nimport achtjs from \"@achtjs/router\";\nimport http from \"http\";\n\nconst app = achtjs();\n\napp.add([\n    {\n        path: \"users\",\n        // We are using dynamic importing so we don't need it to manually import the module in the file\n        // AchtJS will resolve the module asynchronously\n        loadChildren: () => import(\"./users.module\").then((m) => m.UsersModule),\n    },\n]);\n```\n\n## License\n\n_MIT_\n","readmeFilename":"README.md"}