@resapi/core 0.2.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 +1 -1
- package/src/Client.res +2 -2
- package/src/Dispatch.res +1 -1
- package/src/Dispatch.resi +1 -1
- package/src/Endpoint.res +15 -1
- package/src/Endpoint.resi +11 -3
package/README.md
CHANGED
|
@@ -8,4 +8,4 @@ npm install @resapi/core
|
|
|
8
8
|
|
|
9
9
|
Add `"@resapi/core"` to `dependencies` in `rescript.json`. Modules are under the `ResApi` namespace.
|
|
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
package/src/Client.res
CHANGED
|
@@ -144,8 +144,8 @@ let requestInit = (
|
|
|
144
144
|
headers: Fetch.HeadersInit.fromDict(mergeHeaders([client.headers, headers])),
|
|
145
145
|
?signal,
|
|
146
146
|
}
|
|
147
|
-
| Post => {
|
|
148
|
-
method:
|
|
147
|
+
| Post | Put => {
|
|
148
|
+
method: Endpoint.methodName(contract.method),
|
|
149
149
|
headers: Fetch.HeadersInit.fromDict(
|
|
150
150
|
mergeHeaders([client.headers, dict{"content-type": "application/json"}, headers]),
|
|
151
151
|
),
|
package/src/Dispatch.res
CHANGED
|
@@ -37,7 +37,7 @@ let applicationErrorResponse = (contract: Endpoint.t<_, _, _, _>, error) => {
|
|
|
37
37
|
let readJsonBody = async (contract: Endpoint.t<_, _, _, _>, readBody) =>
|
|
38
38
|
switch contract.method {
|
|
39
39
|
| Get => Ok(JSON.Null)
|
|
40
|
-
| Post =>
|
|
40
|
+
| Post | Put =>
|
|
41
41
|
switch readBody() {
|
|
42
42
|
| Parsed(json) => Ok(json)
|
|
43
43
|
| Text(text) =>
|
package/src/Dispatch.resi
CHANGED
|
@@ -17,7 +17,7 @@ let errorResponse: (
|
|
|
17
17
|
~path: array<string>=?,
|
|
18
18
|
) => response
|
|
19
19
|
|
|
20
|
-
/** `readBody` is called only for POST, after params decode. `handle` must return one of the contract's
|
|
20
|
+
/** `readBody` is called only for POST and PUT, after params decode. `handle` must return one of the contract's
|
|
21
21
|
declared errors. Any exception from a codec, `handle` or reading the body is logged and answered
|
|
22
22
|
with a 500 that reveals nothing. */
|
|
23
23
|
let run: (
|
package/src/Endpoint.res
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// it references no server or runtime code. Server adapters turn contracts into routes.
|
|
3
3
|
// Constructors check the route's params against the params codec and throw on a mismatch (RouteCheck).
|
|
4
4
|
|
|
5
|
-
type method = Get | Post
|
|
5
|
+
type method = Get | Post | Put
|
|
6
6
|
|
|
7
7
|
type noError
|
|
8
8
|
|
|
@@ -48,6 +48,19 @@ let post = (path, ~params, ~body, ~response) => {
|
|
|
48
48
|
}
|
|
49
49
|
}
|
|
50
50
|
|
|
51
|
+
let put = (path, ~params, ~body, ~response) => {
|
|
52
|
+
RouteCheck.assertValid(path, params)
|
|
53
|
+
{
|
|
54
|
+
method: Put,
|
|
55
|
+
path,
|
|
56
|
+
paramsCodec: params,
|
|
57
|
+
bodyCodec: body,
|
|
58
|
+
responseCodec: response,
|
|
59
|
+
errorCodec: noErrorCodec,
|
|
60
|
+
errorStatus: _ => 500,
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
51
64
|
let withErrors = (contract: t<'p, 'b, 'r, noError>, errorCodec: Codec.t<'e>, ~status) => {
|
|
52
65
|
...contract,
|
|
53
66
|
errorCodec,
|
|
@@ -62,4 +75,5 @@ let methodName = method =>
|
|
|
62
75
|
switch method {
|
|
63
76
|
| Get => "GET"
|
|
64
77
|
| Post => "POST"
|
|
78
|
+
| Put => "PUT"
|
|
65
79
|
}
|
package/src/Endpoint.resi
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
// A contract describes an endpoint's wire format and is safe to share with clients: it references no
|
|
2
2
|
// server or runtime code. Server adapters turn contracts into routes.
|
|
3
3
|
|
|
4
|
-
type method = Get | Post
|
|
4
|
+
type method = Get | Post | Put
|
|
5
5
|
|
|
6
6
|
/** The error type of an endpoint that declares no application errors. It has no values, so a handler
|
|
7
7
|
for such an endpoint cannot return `Error(_)`. */
|
|
8
8
|
type noError
|
|
9
9
|
|
|
10
|
-
/** Read-only outside this module: contracts are built with `get`/`post`, which check the route. */
|
|
10
|
+
/** Read-only outside this module: contracts are built with `get`/`post`/`put`, which check the route. */
|
|
11
11
|
type t<'params, 'body, 'response, 'error> = private {
|
|
12
12
|
method: method,
|
|
13
13
|
path: string,
|
|
@@ -30,6 +30,14 @@ let post: (
|
|
|
30
30
|
~response: Codec.t<'r>,
|
|
31
31
|
) => t<'p, 'b, 'r, noError>
|
|
32
32
|
|
|
33
|
+
/** Throws if the route's `:params` don't match the params codec (see `RouteCheck`). */
|
|
34
|
+
let put: (
|
|
35
|
+
string,
|
|
36
|
+
~params: Codec.t<'p>,
|
|
37
|
+
~body: Codec.t<'b>,
|
|
38
|
+
~response: Codec.t<'r>,
|
|
39
|
+
) => t<'p, 'b, 'r, noError>
|
|
40
|
+
|
|
33
41
|
/** Declares the endpoint's application errors and the HTTP status (4xx or 5xx) each one is sent with. */
|
|
34
42
|
let withErrors: (t<'p, 'b, 'r, noError>, Codec.t<'e>, ~status: 'e => int) => t<'p, 'b, 'r, 'e>
|
|
35
43
|
|
|
@@ -39,5 +47,5 @@ type any = private {method: method, path: string}
|
|
|
39
47
|
|
|
40
48
|
let any: t<'p, 'b, 'r, 'e> => any
|
|
41
49
|
|
|
42
|
-
/** `"GET"` or `"
|
|
50
|
+
/** `"GET"`, `"POST"`, or `"PUT"`. */
|
|
43
51
|
let methodName: method => string
|