@@ -25,6 +25,15 @@ The intention behind this repository is to always maintain a `Http` package to a
2525
2626## Installation
2727
28+ > To use the high potential from this package you need to install first this other packages from SecJS,
29+ > it keeps as dev dependency because one day ` @secjs/core ` will install everything once.
30+
31+ ``` bash
32+ npm install @secjs/ioc @secjs/utils @secjs/exceptions
33+ ```
34+
35+ > Then you can install the package using:
36+
2837``` bash
2938npm install @secjs/http
3039```
@@ -40,7 +49,8 @@ import { Http, ContextContract } from '@secjs/http'
4049
4150const server = new Http ()
4251
43- server .use (ctx => ctx .data .param = ' param' )
52+ // Middleware
53+ server .use ((ctx : ContextContract ) => ctx .data .param = ' param' )
4454
4555server .get (' /' , ({ response }) => {
4656 response
@@ -53,6 +63,85 @@ server.listen(1335, () => console.log('Server running!'))
5363
5464---
5565
66+ ### Router
67+
68+ > Use Router class to map all the groups, resources and normal routes of the application
69+
70+ ``` ts
71+ import { TestController } from ' ./TestController'
72+ import { TestMiddleware } from ' ./TestMiddleware'
73+
74+ import { Http , Router , ContextContract } from ' @secjs/http'
75+
76+ // With router class you can map your routes inside groups and create resources
77+
78+ const http = new Http () // First you need to create the Http server
79+ const Route = new Router (http )
80+
81+ // Create a route group to set the API version as prefix
82+ Route .group (() => {
83+ Route .get (' posts' , (ctx : ContextContract ) => {
84+ ctx .response .status (200 ).send ({
85+ hello: ' world' ,
86+ userId: ctx .data .userId ,
87+ postId: ctx .data .postId
88+ })
89+ })
90+ .middleware ((ctx : ContextContract ) => {
91+ ctx .data .postId = 1
92+
93+ ctx .next ()
94+ })
95+
96+ // You can create a Resource route to create all the Http methods (index, store, show, update and delete)
97+ Route .resource (' tests' , new TestController ()).except ([' show' ]) // You can use except to create all minus show method
98+ })
99+ .prefix (' /api/v1' )
100+ // You can how many middlewares you want using builder pattern, .middleware, .middleware, .middleware ....
101+ .middleware ((ctx : ContextContract ) => {
102+ ctx .data .userId = 1
103+
104+ ctx .next ()
105+ })
106+
107+ // You can also use middlewares
108+
109+ // You need to call register method in the end to register all the routes in the Http server
110+ Route .register ()
111+ http .listen ()
112+ ```
113+
114+ > Registering routes like this could be a little difficult, so you can use the global Container from @secjs/ioc to register
115+ > controllers and middlewares in the container
116+
117+ ``` ts
118+ import ' @secjs/ioc/src/utils/global' // Will load the class Container in global runtime and in TS types
119+
120+ Container .singleton (TestController , ' TestController' )
121+ Container .singleton (
122+ // Named middlewares
123+ {
124+ // Is extremelly important that middleware implement MiddlewareContract from @secjs/http
125+ ' test.auth' : new TestMiddleware (),
126+ ' test.hello' : new TestMiddleware ()
127+ },
128+ ' Middlewares' ,
129+ )
130+
131+ // Now you can start using string names in routes
132+
133+ Route .group (() => {
134+ Route .resource (' posts' , ' TestController' ).only ([' index' , ' store' ]).middleware (' test.auth' )
135+ })
136+ .prefix (' /api/v2' )
137+ .middleware (' test.hello' )
138+
139+ Route .register ()
140+ http .listen ()
141+ ```
142+
143+ ---
144+
56145## License
57146
58147Made with 🖤 by [ jlenon7] ( https://github.com/jlenon7 ) :wave :
0 commit comments