@resapi/express 0.2.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/package.json +3 -3
- package/src/EndpointHandler.res +1 -1
- package/src/Router.res +18 -2
- package/src/Router.resi +3 -1
package/README.md
CHANGED
|
@@ -8,4 +8,4 @@ npm install @resapi/core @resapi/express express
|
|
|
8
8
|
|
|
9
9
|
Add `"@resapi/core"` and `"@resapi/express"` to `dependencies` in `rescript.json`. Modules are under the `ResApiExpress` namespace. Requires Express 5 (tested with 5.2).
|
|
10
10
|
|
|
11
|
-
See the [README](https://github.com/Seaside-Logic/resapi#readme) and [API reference](https://github.com/Seaside-Logic/resapi/blob/main/
|
|
11
|
+
See the [README](https://github.com/Seaside-Logic/resapi#readme) and [API reference](https://github.com/Seaside-Logic/resapi/blob/main/site/content/api.md).
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@resapi/express",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "Express server adapter for resAPI contracts.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"homepage": "https://resapi.dev",
|
|
@@ -32,9 +32,9 @@
|
|
|
32
32
|
"rescript": "^12.3.1"
|
|
33
33
|
},
|
|
34
34
|
"dependencies": {
|
|
35
|
-
"@resapi/core": "^0.
|
|
35
|
+
"@resapi/core": "^0.4.0"
|
|
36
36
|
},
|
|
37
37
|
"devDependencies": {
|
|
38
|
-
"express": "5.
|
|
38
|
+
"express": "5.3.0"
|
|
39
39
|
}
|
|
40
40
|
}
|
package/src/EndpointHandler.res
CHANGED
|
@@ -15,7 +15,7 @@ type route = {
|
|
|
15
15
|
run: (Req.t, Res.t) => promise<unit>,
|
|
16
16
|
}
|
|
17
17
|
|
|
18
|
-
// Router.register mounts POST routes behind `express.text`, so the body is the raw string. A body
|
|
18
|
+
// Router.register mounts POST and PUT routes behind `express.text`, so the body is the raw string. A body
|
|
19
19
|
// already parsed by an upstream `express.json()` is used as it is. No body at all parses as "".
|
|
20
20
|
let rawBody = req =>
|
|
21
21
|
switch req->Req.body->Nullable.toOption {
|
package/src/Router.res
CHANGED
|
@@ -16,16 +16,20 @@ type server
|
|
|
16
16
|
@module("express") external text: {"type": string => bool} => middleware = "text"
|
|
17
17
|
@send external expressGet: (t, string, handler) => unit = "get"
|
|
18
18
|
@send external expressPost: (t, string, handler) => unit = "post"
|
|
19
|
+
@send external expressPut: (t, string, handler) => unit = "put"
|
|
20
|
+
@send external expressDelete: (t, string, handler) => unit = "delete"
|
|
19
21
|
@send external expressAll: (t, string, handler) => unit = "all"
|
|
20
22
|
@send external expressPostWith: (t, string, middleware, handler) => unit = "post"
|
|
23
|
+
@send external expressPutWith: (t, string, middleware, handler) => unit = "put"
|
|
21
24
|
@send external expressUse: (t, middleware) => unit = "use"
|
|
22
25
|
@send external expressUseAt: (t, string, middleware) => unit = "use"
|
|
23
26
|
|
|
24
27
|
@send external listen: (t, int, unit => unit) => server = "listen"
|
|
25
28
|
@send external close: server => unit = "close"
|
|
26
29
|
|
|
27
|
-
// Express's error
|
|
28
|
-
// "[object Object]". Rethrowing it as an Error keeps
|
|
30
|
+
// Express's error page reads `err.stack`, which a raw ReScript exception lacks: outside production it
|
|
31
|
+
// shows "[object Object]", and Express before 5.3 logs that too. Rethrowing it as an Error keeps both
|
|
32
|
+
// readable. JS errors pass untouched.
|
|
29
33
|
let rethrowAsError = exn =>
|
|
30
34
|
switch exn {
|
|
31
35
|
| JsExn(e) => JsExn.throw(e)
|
|
@@ -51,6 +55,16 @@ let post = (router, path, handler) => {
|
|
|
51
55
|
router
|
|
52
56
|
}
|
|
53
57
|
|
|
58
|
+
let put = (router, path, handler) => {
|
|
59
|
+
router->expressPut(path, catchExceptions(handler))
|
|
60
|
+
router
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
let delete = (router, path, handler) => {
|
|
64
|
+
router->expressDelete(path, catchExceptions(handler))
|
|
65
|
+
router
|
|
66
|
+
}
|
|
67
|
+
|
|
54
68
|
let all = (router, path, handler) => {
|
|
55
69
|
router->expressAll(path, catchExceptions(handler))
|
|
56
70
|
router
|
|
@@ -83,6 +97,8 @@ let register = (router, route: EndpointHandler.route) => {
|
|
|
83
97
|
switch route.method {
|
|
84
98
|
| Get => router->expressGet(route.path, route.run)
|
|
85
99
|
| Post => router->expressPostWith(route.path, rawText, route.run)
|
|
100
|
+
| Put => router->expressPutWith(route.path, rawText, route.run)
|
|
101
|
+
| Delete => router->expressDelete(route.path, route.run)
|
|
86
102
|
}
|
|
87
103
|
router
|
|
88
104
|
}
|
package/src/Router.resi
CHANGED
|
@@ -23,12 +23,14 @@ type server
|
|
|
23
23
|
/** A rejected handler goes to Express's error handler. */
|
|
24
24
|
let get: (t, string, handler) => t
|
|
25
25
|
let post: (t, string, handler) => t
|
|
26
|
+
let put: (t, string, handler) => t
|
|
27
|
+
let delete: (t, string, handler) => t
|
|
26
28
|
|
|
27
29
|
/** Runs for every request whose path starts with `~path` (default all), including unmatched routes.
|
|
28
30
|
The first `use` is the outermost layer. */
|
|
29
31
|
let use: (t, ~path: string=?, middleware) => t
|
|
30
32
|
|
|
31
|
-
/** POST endpoints get a raw-text body parser (100kb limit). A body already parsed upstream is used as is. */
|
|
33
|
+
/** POST and PUT endpoints get a raw-text body parser (100kb limit). A body already parsed upstream is used as is. */
|
|
32
34
|
let register: (t, EndpointHandler.route) => t
|
|
33
35
|
|
|
34
36
|
/** A plain route for every HTTP method. For unmatched requests, add a last `use` (Express has no
|