Skip to content

Commit

Permalink
Add Response.json binding (#5)
Browse files Browse the repository at this point in the history
* feat: add Response.json binding

* chore: update changelog
  • Loading branch information
DCKT committed Feb 19, 2024
1 parent 027d069 commit 1c9770e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# main

- Add bindings for `Response.json()`. https://github.com/zth/rescript-bun/pull/5
- Return JSON data with `Response.makeWithJson`
- Return any data type with `Response.makeWithJsonUnsafe`

# 0.4.1

- Fix `package-lock.json` issue.
Expand Down
9 changes: 7 additions & 2 deletions playground/examples/HTTP.E1.SimpleServer.res
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@

let server = Bun.serve({
port: 3000,
fetch: async (_request, _server) => {
Response.make("Welcome to Bun!")
fetch: async (request, _server) => {
let url = request->Globals.Request.url->URL.make

switch url->Globals.URL.pathname {
| "/" => Response.makeWithJsonUnsafe({"test": 1})
| _ => Response.make("404!")
}
},
})

Expand Down
5 changes: 5 additions & 0 deletions src/Globals.res
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,11 @@ module Response = {
external makeFromReadableStream: (ReadableStream.t<'t>, ~options: responseInit=?) => t =
"Response"

/** HTTP response with JSON */
external makeWithJson: (JSON.t, ~options: responseInitWithHeaders=?) => t = "Response.json"
external makeWithJsonUnsafe: ('jsonCompatiblePayload, ~options: responseInitWithHeaders=?) => t =
"Response.json"

/** Create a new Response that redirects to url */
external makeRedirect: (string, ~status: int=?) => t = "Response.redirect"

Expand Down

0 comments on commit 1c9770e

Please sign in to comment.