Skip to content

Commit 86bd371

Browse files
mransanvramana
authored andcommitted
Add many more method to the Request module (#10)
* Add more express API for the Request class * Add accepts method * Improve method_ and protocol interface, add accepts and acceptsCharsets * Add more method to Request module * Use local module for Raw (ie private) functions
1 parent 200a9af commit 86bd371

File tree

6 files changed

+594
-108
lines changed

6 files changed

+594
-108
lines changed

example/Index.re

Lines changed: 154 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
open Express;
22

3-
/* The tests below relies upon the ability to store in the Request
4-
objects abritrary JSON properties.
3+
/* The tests below relies upon the ability to store in the Request
4+
objects abritrary JSON properties.
55
6-
Each middleware will both check that previous middleware
7-
have been called by making properties exists in the Request object and
8-
upon success will themselves adds anothe property to the Request.
6+
Each middleware will both check that previous middleware
7+
have been called by making properties exists in the Request object and
8+
upon success will themselves adds anothe property to the Request.
99
1010
*/
1111

12-
/* [checkProperty req next property k] makes sure [property] is
13-
present in [req]. If success then [k()] is invoked, otherwise
12+
/* [checkProperty req next property k] makes sure [property] is
13+
present in [req]. If success then [k()] is invoked, otherwise
1414
[Next.route] is called with next */
1515
let checkProperty req next property k => {
16-
let reqData = Request.asJsonObject req;
16+
let reqData = Request.asJsonObject req;
1717
switch (Js.Dict.get reqData property) {
1818
| None => next Next.route
1919
| Some x => {
2020
switch (Js.Json.decodeBoolean x) {
21-
| Some b when (b == Js.true_) => k ()
22-
| _ => next Next.route
21+
| Some b when (b == Js.true_) => k ();
22+
| _ => next Next.route;
2323
};
2424
};
2525
};
@@ -30,20 +30,20 @@ let checkProperties req next properties k => {
3030
let rec aux properties => {
3131
switch properties {
3232
| [] => k ();
33-
| [p, ...tl] => checkProperty req next p (fun () => aux tl);
34-
};
33+
| [p, ...tl] => checkProperty req next p (fun () => aux tl);
34+
};
3535
};
3636
aux properties;
3737
};
3838

39-
/* [setProperty req property] sets the [property] in the [req] Request
39+
/* [setProperty req property] sets the [property] in the [req] Request
4040
value */
41-
let setProperty req property => {
42-
let reqData = Request.asJsonObject req;
41+
let setProperty req property => {
42+
let reqData = Request.asJsonObject req;
4343
Js.Dict.set reqData property (Js.Json.boolean Js.true_);
4444
};
4545

46-
/* return the string value for [key], None if the key is not in [dict]
46+
/* return the string value for [key], None if the key is not in [dict]
4747
TODO once BOption.map is released */
4848
let getDictString dict key => {
4949
switch (Js.Dict.get dict key) {
@@ -54,8 +54,8 @@ let getDictString dict key => {
5454

5555
/* make a common JSON object representing success */
5656
let makeSuccessJson () => {
57-
let json = Js.Dict.empty ();
58-
Js.Dict.set json "success" (Js.Json.boolean Js.true_);
57+
let json = Js.Dict.empty ();
58+
Js.Dict.set json "success" (Js.Json.boolean Js.true_);
5959
Js.Json.object_ json;
6060
};
6161

@@ -72,27 +72,27 @@ App.useWithMany app [|
7272
checkProperty req next "middleware0" (fun () => {
7373
setProperty req "middleware1";
7474
next Next.middleware;
75-
});
75+
});
7676
}),
7777

7878
Middleware.from (fun req _ next => {
7979
checkProperties req next ["middleware0", "middleware1"] (fun () => {
8080
setProperty req "middleware2";
8181
next Next.middleware;
82-
});
82+
});
8383
})
84-
|];
84+
|];
8585

86-
App.get app path::"/" @@ Middleware.from (fun req res next => {
86+
App.get app path::"/" @@ Middleware.from (fun req res next => {
8787
let previousMiddlewares = [ "middleware0", "middleware1", "middleware2"];
8888
checkProperties req next previousMiddlewares (fun () => {
8989
Response.sendJson res (makeSuccessJson ());
9090
});
9191
});
9292

9393
App.useOnPath app path::"/static" {
94-
let options = Static.defaultOptions ();
95-
Static.make "static" options |> Static.asMiddleware
94+
let options = Static.defaultOptions ();
95+
Static.make "static" options |> Static.asMiddleware
9696
};
9797

9898
App.postWithMany app path::"/:id/id" [|
@@ -107,6 +107,136 @@ App.postWithMany app path::"/:id/id" [|
107107
})
108108
|];
109109

110+
App.get app path::"/baseUrl" @@ Middleware.from (fun req res next => {
111+
switch (Request.baseUrl req) {
112+
| "" => Response.sendJson res (makeSuccessJson ());
113+
| _ => next Next.route;
114+
};
115+
});
116+
117+
App.get app path::"/hostname" @@ Middleware.from (fun req res next => {
118+
switch (Request.hostname req) {
119+
| "localhost" => Response.sendJson res (makeSuccessJson ());
120+
| _ => next Next.route;
121+
};
122+
});
123+
124+
App.get app path::"/ip" @@ Middleware.from (fun req res next => {
125+
switch (Request.ip req) {
126+
| "127.0.0.1" => Response.sendJson res (makeSuccessJson ());
127+
| s => {Js.log s; next Next.route;}
128+
/* TODO why is it printing ::1 */
129+
};
130+
});
131+
132+
App.get app path::"/method" @@ Middleware.from (fun req res next => {
133+
switch (Request.method_ req) {
134+
| Request.Get => Response.sendJson res (makeSuccessJson ());
135+
| s => {Js.log s; next Next.route;}
136+
};
137+
});
138+
139+
App.get app path::"/originalUrl" @@ Middleware.from (fun req res next => {
140+
switch (Request.originalUrl req) {
141+
| "/originalUrl" => Response.sendJson res (makeSuccessJson ());
142+
| s => {Js.log s; next Next.route;}
143+
};
144+
});
145+
146+
App.get app path::"/path" @@ Middleware.from (fun req res next => {
147+
switch (Request.path req) {
148+
| "/path" => Response.sendJson res (makeSuccessJson ());
149+
| s => {Js.log s; next Next.route;}
150+
};
151+
});
152+
153+
App.get app path::"/protocol" @@ Middleware.from (fun req res next => {
154+
switch (Request.protocol req) {
155+
| Request.Http => Response.sendJson res (makeSuccessJson ());
156+
| s => {Js.log s; next Next.route;}
157+
};
158+
});
159+
160+
App.get app path::"/query" @@ Middleware.from (fun req res next => {
161+
switch (getDictString (Request.query req) "key") {
162+
| Some "value" => Response.sendJson res (makeSuccessJson ());
163+
| _ => next Next.route;
164+
};
165+
});
166+
167+
App.getWithMany app path::"/accepts" [|
168+
Middleware.from (fun req _ next => {
169+
switch (Request.accepts req [|"audio/whatever" , "audio/basic" |]) {
170+
| Some "audio/basic" => next Next.middleware;
171+
| _ => next Next.route;
172+
};
173+
}),
174+
Middleware.from (fun req res next => {
175+
switch (Request.accepts req [| "text/css" |]) {
176+
| None => Response.sendJson res (makeSuccessJson ());
177+
| _ => next Next.route;
178+
};
179+
})
180+
|];
181+
182+
App.getWithMany app path::"/accepts-charsets" [|
183+
Middleware.from (fun req _ next => {
184+
switch (Request.acceptsCharsets req [|"UTF-8" , "UTF-16" |]) {
185+
| Some "UTF-8" => next Next.middleware;
186+
| _ => next Next.route;
187+
};
188+
}),
189+
Middleware.from (fun req res next => {
190+
switch (Request.acceptsCharsets req [| "UTF-16" |]) {
191+
| None => Response.sendJson res (makeSuccessJson ());
192+
| _ => next Next.route;
193+
};
194+
})
195+
|];
196+
197+
App.get app path::"/get" @@ Middleware.from (fun req res next => {
198+
switch (Request.get req "key") {
199+
| Some "value" => Response.sendJson res (makeSuccessJson ());
200+
| _ => next Next.route;
201+
};
202+
});
203+
204+
App.get app path::"/fresh" @@ Middleware.from (fun req res next => {
205+
if(not @@ Request.fresh req) {
206+
Response.sendJson res (makeSuccessJson ());
207+
}
208+
else {
209+
next Next.route;
210+
};
211+
});
212+
213+
App.get app path::"/stale" @@ Middleware.from (fun req res next => {
214+
if(Request.stale req) {
215+
Response.sendJson res (makeSuccessJson ());
216+
}
217+
else {
218+
next Next.route;
219+
};
220+
});
221+
222+
App.get app path::"/secure" @@ Middleware.from (fun req res next => {
223+
if(not @@ Request.secure req) {
224+
Response.sendJson res (makeSuccessJson ());
225+
}
226+
else {
227+
next Next.route;
228+
};
229+
});
230+
231+
App.get app path::"/xhr" @@ Middleware.from (fun req res next => {
232+
if(not @@ Request.xhr req) {
233+
Response.sendJson res (makeSuccessJson ());
234+
}
235+
else {
236+
next Next.route;
237+
};
238+
});
239+
110240
App.listen app port::3000 ;
111241

112242
/* -- Test the server --

0 commit comments

Comments
 (0)