Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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