@@ -22,8 +22,7 @@ lightweight. You can use the projects you need in your web application.
2222- [ AWS Lambda implementation] ( https://github.com/rogelio-o/lambda-framework-aws )
2323- [ Google Cloud Functions implementation] ( https://github.com/rogelio-o/lambda-framework-gcloud )
2424- [ DustJS template engine implementation] ( https://github.com/rogelio-o/lambda-framework-dustjs )
25- - [ Website] ( https://github.com/rogelio-o/lambda-framework-website )
26- - [ Website Resources] ( https://github.com/rogelio-o/lambda-framework-website-resources )
25+ - [ Examples] ( https://github.com/rogelio-o/lambda-framework-examples )
2726
2827## Features
2928
@@ -53,8 +52,9 @@ A event handling is started passing that event to the App method `handle`.
5352app .handle (event , callback );
5453```
5554
56- You don't need to care about passing the event to the App handler, you can use the [ AWS Lambda implementation] ( https://github.com/rogelio-o/lambda-framework-aws ) or another provider
57- implementation. These will manage the creation of the raw event and passing it to the handler.
55+ You don't need to care about passing the event to the App handler, you can use the [ AWS Lambda implementation] ( https://github.com/rogelio-o/lambda-framework-aws ) , [ Google Cloud Functions implementation] ( https://github.com/rogelio-o/lambda-framework-gcloud ) or another provider
56+ implemented by yourself. These will manage the creation of the raw event and passing it to the handler.
57+
5858``` typescript
5959import { App , IApp } from " lambda-framework" ;
6060import { AWSHandler } from " lambda-framework-aws" ;
@@ -65,29 +65,253 @@ const handler: AWSHandler = new AWSHandler(app);
6565export const handle = handler .handle .bind (handler );
6666```
6767
68- ### Event handling
68+ ### Event handling
69+
70+ A handler can be related with an event type or with a event that fulfil a
71+ predicate. When the event happens, the handler is executed.
6972
70- TODO
73+ ``` typescript
74+ app .event (" eventType" , (req : IEventRequest , next : INext , error : Error ) => {
75+ ...
76+ });
77+
78+ const predicate: IEventRoutePredicate = (req : req : IEventRequest ) => {
79+ return req .event .original .param1 === " value1" ;
80+ };
81+ app .event (predicate , (req : IEventRequest , next : INext , error : Error ) => {
82+ ...
83+ });
84+ ```
7185
7286### HTTP Routing
7387
74- TODO
88+ #### use
89+
90+ Handlers can be executed when a request is received for a specified path.
91+
92+ ``` typescript
93+ app .use ((req : IHttpRequest , res : IHttpResponse , next : INext ) => {
94+ ... // Do something
95+ next ();
96+ }, " /blog/*" );
97+
98+ app .use ((req : IHttpRequest , res : IHttpResponse , next : INext , error : Error ) => {
99+ ... // Error handling
100+ });
101+ ```
102+
103+ #### route
104+
105+ Also, a final controller action can be executed when a request is received
106+ with a specified path and method (` POST ` , ` GET ` , ` PUT ` or ` DELETE ` ).
107+
108+ ``` typescript
109+ app .route (" /blog/:id" )
110+ .get ((req : IHttpRequest , res : IHttpResponse ) => {
111+ ... // Do something
112+ })
113+ .put ((req : IHttpRequest , res : IHttpResponse ) => {
114+ ... // Do something
115+ });
116+ ```
75117
76- ### HTTP body parsers
118+ #### mount
77119
78- TODO
120+ The app has a default router. The router can have subrouters with more routes.
121+ The subrouters can have a prefix in order to execute its routes only when
122+ the path starts with the prefix.
79123
80- ### Others HTTP features
124+ ``` typescript
125+ const subrouter: IRouter = new Router ();
81126
82- TODO
127+ subrouter .route (" /:id" )
128+ .get (... )
129+ .put (... );
83130
84- ### Templating
131+ app .mount (subrouter , " /blog" );
132+ ```
85133
86- TODO
134+ #### param
135+
136+ Handlers can be related also with the apparition of a param in a request.
137+ For example, each time the ` user ` param appears, a handler loading the user
138+ is executed.
139+
140+ ``` typescript
141+ app .param (" user" , (req : IHttpRequest , res : IHttpResponse , next : INext , placeholderValue : any ) => {
142+ ... // Load user
143+ next ();
144+ });
145+ ```
146+
147+ ### HTTP body parsers
148+
149+ There is a few body parsers that can be used. The body parsers have
150+ to be added as a handler to the path in which you want to use it.
151+
152+ #### JSON
153+
154+ ``` typescript
155+ const reviver = (key : string , value : string ): any => {
156+ if (key === " KEY" && value === " VALUE" ) {
157+ return 0 ;
158+ } else {
159+ return value ;
160+ }
161+ };
87162
88- ### More info, API and tutorials
163+ app .use (new JsonParser ().create (reviver ));
164+ ```
165+
166+ #### UrlEncoded
167+
168+ ``` typescript
169+ const options: {[name : string ]: any } = {};
170+ options .type = " application/x-www-form-urlencoded" ; // By default
171+ options .extended = false ; // By default
172+
173+ app .use (new UrlEncodedParser ().create (options ));
174+ ```
175+
176+ #### Multipart
177+
178+ ``` typescript
179+ const options: {[name : string ]: any } = {};
180+ options .type = " multipart/form-data" ; // By default
181+
182+ app .use (new MultipartParser ().create (options ));
183+ ```
184+
185+ ### Others HTTP features
186+
187+ #### Response body
188+
189+ To response with a plain body:
190+
191+ ``` typescript
192+ response .send (" RESPONSE" );
193+ ```
194+
195+ To response with a JSON body:
196+
197+ ``` typescript
198+ response .json ({key: " value" });
199+ ```
200+
201+ To response with a HTTP status and a body with a description:
202+
203+ ``` typescript
204+ response .sendStatus (400 );
205+ ```
89206
90- TODO
207+ To set the ` Content-Type ` of the response:
208+
209+ ``` typescript
210+ response .contentType (" text/html" );
211+ ```
212+
213+ #### Format
214+
215+ To do things according to the ` Accept ` header of the request:
216+
217+ ``` typescript
218+ response .format ({
219+ " text/html" : (req : IHttpRequest , res : IHttpResponse ) => {
220+ ... // Do things
221+ },
222+ " application/json" : (req : IHttpRequest , res : IHttpResponse ) => {
223+ ... // Do things
224+ }
225+ });
226+ ```
227+
228+ #### Response headers
229+
230+ ``` typescript
231+ response .putHeader (" key" , " value" );
232+
233+ response .putHeaders ({
234+ key1: " value1" ,
235+ key2: " value2"
236+ });
237+
238+ const previousPutHeader: string = response .header (" key" );
239+
240+ response .appendHeader (" key" , " value 2" );
241+
242+ response .removeHeader (" key2" );
243+ ```
244+
245+ #### Cookies
246+
247+ Cookies can be added creating a Cookie object and adding it to the response.
248+ The cookies can be configured with the expiration date and the path.
249+
250+ ``` typescript
251+ const cookie: ICookie = new Cookie (" cookie-name" , " value" , new Date (1 , 1 , 2020 ), " /" , false );
252+
253+ response .addCookie (cookie );
254+
255+ response .addCookies ([cookie ]);
256+ ```
257+
258+ Signed cookies can be added configuring the secret key in the app and
259+ configuring the cookie object on creation.
260+
261+ ``` typescript
262+ app .set (configuration .COOKIE_SECRET , " SIGNED KEY" );
263+
264+ const cookie: ICookie = new Cookie (" cookie-name" , " value" , new Date (1 , 1 , 2020 ), " /" , true );
265+ response .addCookie (cookie );
266+ ```
267+
268+ Added cookies can be get from the response.
269+
270+ ``` typescript
271+ const cookie = response .cookie (" cookie-name" );
272+ ```
273+
274+ #### Redirection
275+
276+ Redirections can be done in the response with ` location ` or ` redirect ` . The passed
277+ URL can be _ back_ , so the user will be redirected to _ Referrer_ or _ Referer_ headers
278+ (or to _ /_ if there is no referrer header). In the method ` redirect ` , the HTTP
279+ status code can be specified.
280+
281+ ``` typescript
282+ response .location (" back" );
283+
284+ response .redirect (" back" );
285+
286+ response .redirect (" /new-location" , 301 );
287+ ```
288+
289+ ### Templating
290+
291+ There is a render method for rendering HTML templates. The methods can have the
292+ following parameters:
293+
294+ - ** view:** the template name.
295+ - ** options:** the params that will be injected into the view.
296+ - ** callback:** optional. If no callback is given, the resulting HTML will be returned
297+ as response body with 200 HTTP status. If callback is given, the resulting HTML will
298+ be passed as callback param.
299+
300+ ``` typescript
301+ response .render (" template1" , {param: " value" });
302+
303+ response .render (" template1" , {param: " value" }, (html : string ) => {
304+ doSomethingWithTemplate (html );
305+ });
306+ ```
307+
308+ ** IMPORTANT:** in order to use the render method, a template engine has to be added
309+ to the app or to the subrouter in which it is used. Check the project to know
310+ more about the [ DustJS template engine] ( https://github.com/rogelio-o/lambda-framework-dustjs ) .
311+
312+ ``` typescript
313+ app .addTemplateEngine (templateEngine );
314+ ```
91315
92316## Contributions
93317
0 commit comments