@resapi/express 0.1.0 → 0.3.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 +2 -2
- package/src/EndpointHandler.res +1 -1
- package/src/Reject.res +12 -0
- package/src/Reject.resi +8 -0
- package/src/Req.res +7 -0
- package/src/Req.resi +24 -0
- package/src/Router.res +38 -6
- package/src/Router.resi +9 -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.3.0",
|
|
4
4
|
"description": "Express server adapter for resAPI contracts.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"homepage": "https://resapi.dev",
|
|
@@ -32,7 +32,7 @@
|
|
|
32
32
|
"rescript": "^12.3.1"
|
|
33
33
|
},
|
|
34
34
|
"dependencies": {
|
|
35
|
-
"@resapi/core": "^0.
|
|
35
|
+
"@resapi/core": "^0.3.0"
|
|
36
36
|
},
|
|
37
37
|
"devDependencies": {
|
|
38
38
|
"express": "5.2.1"
|
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/Reject.res
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// Auth rejections in resAPI's error shape, for middleware. See Reject.resi.
|
|
2
|
+
let send = (res, ~status, ~kind, ~reason) => {
|
|
3
|
+
let {status, body} = ResApi.Dispatch.errorResponse(
|
|
4
|
+
~status,
|
|
5
|
+
~kind,
|
|
6
|
+
~message=reason->Option.getOr(""),
|
|
7
|
+
)
|
|
8
|
+
res->Res.status(status)->Res.json(body)
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
let unauthenticated = (res, ~reason=?) => res->send(~status=401, ~kind="unauthenticated", ~reason)
|
|
12
|
+
let forbidden = (res, ~reason=?) => res->send(~status=403, ~kind="forbidden", ~reason)
|
package/src/Reject.resi
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
// Auth rejections for middleware, in resAPI's error shape, so the typed client sees them as
|
|
2
|
+
// `Unauthorized({status, reason})`. Call one instead of `next()`: `Reject.unauthenticated(res)`.
|
|
3
|
+
|
|
4
|
+
/** Sends 401 `{"error": {"kind": "unauthenticated", "message": <reason or "">, "path": []}}`. */
|
|
5
|
+
let unauthenticated: (Res.t, ~reason: string=?) => unit
|
|
6
|
+
|
|
7
|
+
/** Sends 403 `{"error": {"kind": "forbidden", "message": <reason or "">, "path": []}}`. */
|
|
8
|
+
let forbidden: (Res.t, ~reason: string=?) => unit
|
package/src/Req.res
CHANGED
|
@@ -10,3 +10,10 @@ type t
|
|
|
10
10
|
@send @return(nullable) external header: (t, string) => option<string> = "get"
|
|
11
11
|
@get external method: t => string = "method"
|
|
12
12
|
@get external path: t => string = "path"
|
|
13
|
+
|
|
14
|
+
@get_index external field: (t, string) => Nullable.t<'a> = ""
|
|
15
|
+
@set_index external setField: (t, string, 'a) => unit = ""
|
|
16
|
+
|
|
17
|
+
let set = (req, key, value) => req->setField(ResApi.Key.name(key), value)
|
|
18
|
+
let get = (req, key) => req->field(ResApi.Key.name(key))->Nullable.toOption
|
|
19
|
+
let getOrThrow = (req, key) => req->get(key)->ResApi.Key.orThrow(key)
|
package/src/Req.resi
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// Express's request object.
|
|
2
|
+
type t
|
|
3
|
+
|
|
4
|
+
/** Path parameters, already percent-decoded by Express. */
|
|
5
|
+
@get external params: t => dict<string> = "params"
|
|
6
|
+
|
|
7
|
+
/** Set by body-parsing middleware: a string from `express.text`, parsed JSON from `express.json`. */
|
|
8
|
+
@get external body: t => Nullable.t<JSON.t> = "body"
|
|
9
|
+
|
|
10
|
+
@send @return(nullable) external header: (t, string) => option<string> = "get"
|
|
11
|
+
@get external method: t => string = "method"
|
|
12
|
+
@get external path: t => string = "path"
|
|
13
|
+
|
|
14
|
+
/** Stores a value on the request under `key` (as `req[name]`), usually in middleware. The name shares
|
|
15
|
+
`req` with Express's own fields: a getter such as `query` or `path` throws, and a field Express writes,
|
|
16
|
+
such as `params` or `body`, gets overwritten. Pick a name Express doesn't use. */
|
|
17
|
+
let set: (t, ResApi.Key.t<'a>, 'a) => unit
|
|
18
|
+
|
|
19
|
+
/** The value under `key` (`req[name]`), or `None` when it's unset, `undefined` or `null`. */
|
|
20
|
+
let get: (t, ResApi.Key.t<'a>) => option<'a>
|
|
21
|
+
|
|
22
|
+
/** For routes behind the middleware that sets `key`. Throws when the value is missing: a logged 500
|
|
23
|
+
that reveals nothing, inside a typed endpoint. */
|
|
24
|
+
let getOrThrow: (t, ResApi.Key.t<'a>) => 'a
|
package/src/Router.res
CHANGED
|
@@ -16,7 +16,10 @@ 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 expressAll: (t, string, handler) => unit = "all"
|
|
19
21
|
@send external expressPostWith: (t, string, middleware, handler) => unit = "post"
|
|
22
|
+
@send external expressPutWith: (t, string, middleware, handler) => unit = "put"
|
|
20
23
|
@send external expressUse: (t, middleware) => unit = "use"
|
|
21
24
|
@send external expressUseAt: (t, string, middleware) => unit = "use"
|
|
22
25
|
|
|
@@ -34,30 +37,40 @@ let rethrowAsError = exn =>
|
|
|
34
37
|
)
|
|
35
38
|
}
|
|
36
39
|
|
|
37
|
-
let
|
|
40
|
+
let catchExceptions = (handler: handler): handler =>
|
|
38
41
|
async (req, res) =>
|
|
39
42
|
try await handler(req, res) catch {
|
|
40
43
|
| exn => rethrowAsError(exn)
|
|
41
44
|
}
|
|
42
45
|
|
|
43
46
|
let get = (router, path, handler) => {
|
|
44
|
-
router->expressGet(path,
|
|
47
|
+
router->expressGet(path, catchExceptions(handler))
|
|
45
48
|
router
|
|
46
49
|
}
|
|
47
50
|
|
|
48
51
|
let post = (router, path, handler) => {
|
|
49
|
-
router->expressPost(path,
|
|
52
|
+
router->expressPost(path, catchExceptions(handler))
|
|
53
|
+
router
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
let put = (router, path, handler) => {
|
|
57
|
+
router->expressPut(path, catchExceptions(handler))
|
|
58
|
+
router
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
let all = (router, path, handler) => {
|
|
62
|
+
router->expressAll(path, catchExceptions(handler))
|
|
50
63
|
router
|
|
51
64
|
}
|
|
52
65
|
|
|
53
66
|
let use = (router, ~path=?, middleware: middleware) => {
|
|
54
|
-
let
|
|
67
|
+
let wrapped = (req, res, next) =>
|
|
55
68
|
try middleware(req, res, next) catch {
|
|
56
69
|
| exn => rethrowAsError(exn)
|
|
57
70
|
}
|
|
58
71
|
switch path {
|
|
59
|
-
| None => router->expressUse(
|
|
60
|
-
| Some(path) => router->expressUseAt(path,
|
|
72
|
+
| None => router->expressUse(wrapped)
|
|
73
|
+
| Some(path) => router->expressUseAt(path, wrapped)
|
|
61
74
|
}
|
|
62
75
|
router
|
|
63
76
|
}
|
|
@@ -66,10 +79,29 @@ let use = (router, ~path=?, middleware: middleware) => {
|
|
|
66
79
|
// upstream parser already read.
|
|
67
80
|
let rawText = text({"type": _ => true})
|
|
68
81
|
|
|
82
|
+
// Express has no public route list, so `register` records what it mounted, for assertRegistered.
|
|
83
|
+
let registered: WeakMap.t<t, array<string>> = WeakMap.make()
|
|
84
|
+
|
|
85
|
+
let routeName = (method, path) => `${ResApi.Endpoint.methodName(method)} ${path}`
|
|
86
|
+
|
|
69
87
|
let register = (router, route: EndpointHandler.route) => {
|
|
88
|
+
let names = registered->WeakMap.get(router)->Option.getOr([])
|
|
89
|
+
registered->WeakMap.set(router, [...names, routeName(route.method, route.path)])->ignore
|
|
70
90
|
switch route.method {
|
|
71
91
|
| Get => router->expressGet(route.path, route.run)
|
|
72
92
|
| Post => router->expressPostWith(route.path, rawText, route.run)
|
|
93
|
+
| Put => router->expressPutWith(route.path, rawText, route.run)
|
|
73
94
|
}
|
|
74
95
|
router
|
|
75
96
|
}
|
|
97
|
+
|
|
98
|
+
let assertRegistered = (router, endpoints: array<ResApi.Endpoint.any>) => {
|
|
99
|
+
let names = registered->WeakMap.get(router)->Option.getOr([])
|
|
100
|
+
let missing =
|
|
101
|
+
endpoints
|
|
102
|
+
->Array.map(endpoint => routeName(endpoint.method, endpoint.path))
|
|
103
|
+
->Array.filter(name => !(names->Array.includes(name)))
|
|
104
|
+
if missing != [] {
|
|
105
|
+
JsError.throwWithMessage(`Endpoints without a route: ${missing->Array.join(", ")}`)
|
|
106
|
+
}
|
|
107
|
+
}
|
package/src/Router.resi
CHANGED
|
@@ -23,10 +23,18 @@ 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
|
|
26
27
|
|
|
27
28
|
/** Runs for every request whose path starts with `~path` (default all), including unmatched routes.
|
|
28
29
|
The first `use` is the outermost layer. */
|
|
29
30
|
let use: (t, ~path: string=?, middleware) => t
|
|
30
31
|
|
|
31
|
-
/** POST endpoints get a raw-text body parser (100kb limit). A body already parsed upstream is used as is. */
|
|
32
|
+
/** POST and PUT endpoints get a raw-text body parser (100kb limit). A body already parsed upstream is used as is. */
|
|
32
33
|
let register: (t, EndpointHandler.route) => t
|
|
34
|
+
|
|
35
|
+
/** A plain route for every HTTP method. For unmatched requests, add a last `use` (Express has no
|
|
36
|
+
separate not-found handler). */
|
|
37
|
+
let all: (t, string, handler) => t
|
|
38
|
+
|
|
39
|
+
/** For tests: throws, naming each endpoint in the list that wasn't mounted with `register`. */
|
|
40
|
+
let assertRegistered: (t, array<ResApi.Endpoint.any>) => unit
|