@@ -16,10 +16,43 @@ module Exn = {
1616@inline
1717let panic = message => Exn .raiseError (Exn .makeError (` [rescript-rest] ${message}` ))
1818
19- type options <'input , 'req , 'res > = {
19+ type req = private {
20+ cookies : Js .Dict .t <string >,
21+ method : Rest .method ,
22+ url : string ,
23+ port : int ,
24+ body : Js .Json .t ,
25+ query : Js .Json .t ,
26+ headers : Js .Dict .t <string >,
27+ rawHeaders : array <string >,
28+ rawTrailers : array <string >,
29+ aborted : bool ,
30+ complete : bool ,
31+ statusCode : Rest .Response .numiricStatus ,
32+ statusMessage : string ,
33+ trailers : Js .Dict .t <string >,
34+ }
35+ // @send
36+ // external destroy: (req, ~error: option<Js.Exn.t>=?) => bool = "destroy"
37+
38+ type reply
39+ type rec res = private {
40+ statusCode : Rest .Response .numiricStatus ,
41+ statusMessage : string ,
42+ getHeader : string => option <string >,
43+ setHeader : (string , string ) => unit ,
44+ status : int => res ,
45+ end : unit => reply ,
46+ json : Js .Json .t => reply ,
47+ // The type is not 100% correct.
48+ // It asccepts a string, object or a Buffer
49+ send : Js .Json .t => reply ,
50+ }
51+
52+ type options <'input > = {
2053 input : 'input ,
21- req : ' req ,
22- res : ' res ,
54+ req : req ,
55+ res : res ,
2356}
2457
2558let handler = (route , implementation ) => {
@@ -41,40 +74,38 @@ let handler = (route, implementation) => {
4174 )
4275 }
4376
44- async (genericReq , genericRes ) => {
45- let req = genericReq -> Obj .magic
46- let res = genericRes -> Obj .magic
47-
48- if req ["method" ] !== definition .method {
49- res ["status" ](404 )["end" ]()
50- }
51- switch req -> S .parseOrThrow (inputSchema ) {
52- | input =>
53- try {
54- let implementationResult = await implementation ({
55- req : genericReq ,
56- res : genericRes ,
57- input ,
58- })
59- let data : {.. } = implementationResult -> S .reverseConvertOrThrow (responseSchema )-> Obj .magic
60- let headers : option <dict <string >> = data ["headers" ]
61- switch headers {
62- | Some (headers ) =>
63- headers
64- -> Js .Dict .keys
65- -> Js .Array2 .forEach (key => {
66- res ["setHeader" ](key , headers -> Js .Dict .unsafeGet (key ))
77+ async (req , res ) => {
78+ if req .method !== definition .method {
79+ res .status (404 ).end ()
80+ } else {
81+ switch req -> S .parseOrThrow (inputSchema ) {
82+ | input =>
83+ try {
84+ let implementationResult = await implementation ({
85+ req ,
86+ res ,
87+ input ,
6788 })
68- | None => ()
89+ let data : {.. } = implementationResult -> S .reverseConvertOrThrow (responseSchema )-> Obj .magic
90+ let headers : option <dict <string >> = data ["headers" ]
91+ switch headers {
92+ | Some (headers ) =>
93+ headers
94+ -> Js .Dict .keys
95+ -> Js .Array2 .forEach (key => {
96+ res .setHeader (key , headers -> Js .Dict .unsafeGet (key ))
97+ })
98+ | None => ()
99+ }
100+ res .status (%raw (` data .status || 200 ` ) ).json (data ["data" ])
101+ } catch {
102+ | S .Raised (error ) =>
103+ Js .Exn .raiseError (
104+ ` Unexpected error in the ${definition.path} route: ${error-> S.Error.message}` ,
105+ )
69106 }
70- res ["status" ](%raw (` data .status || 200 ` ) )["json" ](data ["data" ])
71- } catch {
72- | S .Raised (error ) =>
73- Js .Exn .raiseError (
74- ` Unexpected error in the ${definition.path} route: ${error-> S.Error.message}` ,
75- )
107+ | exception S .Raised (error ) => res .status (400 ).send (error -> S .Error .message -> Js .Json .string )
76108 }
77- | exception S .Raised (error ) => res ["status" ](400 )["send" ](error -> S .Error .message )
78109 }
79110 }
80111}
0 commit comments