Skip to content

Commit

Permalink
Update for Gleam v0.30
Browse files Browse the repository at this point in the history
  • Loading branch information
lpil committed Aug 3, 2023
1 parent d41413a commit 6529336
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 44 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## v0.2.0 - 2023-08-03

- Updated for Gleam v0.30.0.

## v0.1.0 - 2022-12-29

- Initial release
14 changes: 7 additions & 7 deletions manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
# You typically do not need to edit this file

packages = [
{ name = "gleam_http", version = "3.1.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_http", source = "hex", outer_checksum = "B66B7A1539CCB577119E4DC80DD3484C1A652CB032967954498EEDBAE3355763" },
{ name = "gleam_javascript", version = "0.3.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_javascript", source = "hex", outer_checksum = "4A3EEBF82C0D201E4CA0E6A5145246FBA01D35EECB2813B5F8EE9290070265F5" },
{ name = "gleam_stdlib", version = "0.25.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "AD0F89928E0B919C8F8EDF640484633B28DBF88630A9E6AE504617A3E3E5B9A2" },
{ name = "gleeunit", version = "0.8.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "A1170754BF54F5DD6E9EF392FB1DC612528B007CCBE41B52F0C5453254708490" },
{ name = "gleam_http", version = "3.4.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_http", source = "hex", outer_checksum = "B6EB76D304E0E66267485983E6B7BC28F3BFA6795BB2BF90FC411F6903AF6A1A" },
{ name = "gleam_javascript", version = "0.5.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_javascript", source = "hex", outer_checksum = "9457F4C5F1B1F5CFA82A7006AC42E3AA2F7CB3670C7A50092A23E5812DEC4A58" },
{ name = "gleam_stdlib", version = "0.30.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "03710B3DA047A3683117591707FCA19D32B980229DD8CE8B0603EB5B5144F6C3" },
{ name = "gleeunit", version = "0.10.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "ECEA2DE4BE6528D36AFE74F42A21CDF99966EC36D7F25DEB34D47DD0F7977BAF" },
]

[requirements]
gleam_http = "~> 3.1"
gleam_javascript = "~> 0.3"
gleeunit = "~> 0.8"
gleam_http = { version = "~> 3.1" }
gleam_javascript = { version = "~> 0.3" }
gleeunit = { version = "~> 0.8" }
36 changes: 17 additions & 19 deletions src/gleam/fetch.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,14 @@ pub type FetchError {
InvalidJsonBody
}

pub external type FetchBody
pub type FetchBody

pub external type FetchRequest
pub type FetchRequest

pub external type FetchResponse
pub type FetchResponse

pub external fn raw_send(
FetchRequest,
) -> Promise(Result(FetchResponse, FetchError)) =
"../ffi.mjs" "raw_send"
@external(javascript, "../ffi.mjs", "raw_send")
pub fn raw_send(a: FetchRequest) -> Promise(Result(FetchResponse, FetchError))

pub fn send(
request: Request(String),
Expand All @@ -31,18 +29,18 @@ pub fn send(
})
}

pub external fn to_fetch_request(Request(String)) -> FetchRequest =
"../ffi.mjs" "to_fetch_request"
@external(javascript, "../ffi.mjs", "to_fetch_request")
pub fn to_fetch_request(a: Request(String)) -> FetchRequest

pub external fn from_fetch_response(FetchResponse) -> Response(FetchBody) =
"../ffi.mjs" "from_fetch_response"
@external(javascript, "../ffi.mjs", "from_fetch_response")
pub fn from_fetch_response(a: FetchResponse) -> Response(FetchBody)

pub external fn read_text_body(
Response(FetchBody),
) -> Promise(Result(Response(String), FetchError)) =
"../ffi.mjs" "read_text_body"
@external(javascript, "../ffi.mjs", "read_text_body")
pub fn read_text_body(
a: Response(FetchBody),
) -> Promise(Result(Response(String), FetchError))

pub external fn read_json_body(
Response(FetchBody),
) -> Promise(Result(Response(Dynamic), FetchError)) =
"../ffi.mjs" "read_json_body"
@external(javascript, "../ffi.mjs", "read_json_body")
pub fn read_json_body(
a: Response(FetchBody),
) -> Promise(Result(Response(Dynamic), FetchError))
39 changes: 21 additions & 18 deletions test/gleam_fetch_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ pub fn request_test() {
fetch.send(req)
|> promise.try_await(fetch.read_text_body)
|> promise.await(fn(resp: Result(Response(String), FetchError)) {
assert Ok(resp) = resp
assert 200 = resp.status
assert Ok("application/json") = response.get_header(resp, "content-type")
assert "{\"message\":\"Hello World\"}" = resp.body
let assert Ok(resp) = resp
let assert 200 = resp.status
let assert Ok("application/json") =
response.get_header(resp, "content-type")
let assert "{\"message\":\"Hello World\"}" = resp.body
promise.resolve(Ok(Nil))
})
}
Expand All @@ -39,9 +40,10 @@ pub fn json_request_test() {
fetch.send(req)
|> promise.try_await(fetch.read_json_body)
|> promise.await(fn(resp) {
assert Ok(resp) = resp
assert 200 = resp.status
assert Ok("application/json") = response.get_header(resp, "content-type")
let assert Ok(resp) = resp
let assert 200 = resp.status
let assert Ok("application/json") =
response.get_header(resp, "content-type")
// // TODO: make assertions about body
promise.resolve(Ok(Nil))
})
Expand All @@ -59,10 +61,11 @@ pub fn get_request_discards_body_test() {
fetch.send(req)
|> promise.try_await(fetch.read_text_body)
|> promise.await(fn(resp: Result(Response(String), FetchError)) {
assert Ok(resp) = resp
assert 200 = resp.status
assert Ok("application/json") = response.get_header(resp, "content-type")
assert "{\"message\":\"Hello World\"}" = resp.body
let assert Ok(resp) = resp
let assert 200 = resp.status
let assert Ok("application/json") =
response.get_header(resp, "content-type")
let assert "{\"message\":\"Hello World\"}" = resp.body
promise.resolve(Ok(Nil))
})
}
Expand All @@ -77,11 +80,11 @@ pub fn head_request_discards_body_test() {

use response <- promise.try_await(fetch.send(request))
use response <- promise.await(fetch.read_text_body(response))
assert Ok(resp) = response
assert 200 = resp.status
assert Ok("application/json; charset=utf-8") =
let assert Ok(resp) = response
let assert 200 = resp.status
let assert Ok("application/json; charset=utf-8") =
response.get_header(resp, "content-type")
assert "" = resp.body
let assert "" = resp.body
promise.resolve(Ok(Nil))
}

Expand All @@ -96,10 +99,10 @@ pub fn options_request_discards_body_test() {
fetch.send(req)
|> promise.try_await(fetch.read_text_body)
|> promise.await(fn(resp) {
assert Ok(Response(status: 200, ..) as resp) = resp
assert Ok("text/html; charset=utf-8") =
let assert Ok(Response(status: 200, ..) as resp) = resp
let assert Ok("text/html; charset=utf-8") =
response.get_header(resp, "content-type")
assert "GET,HEAD,PUT,POST,DELETE,PATCH" = resp.body
let assert "GET,HEAD,PUT,POST,DELETE,PATCH" = resp.body
promise.resolve(Ok(Nil))
})
}

0 comments on commit 6529336

Please sign in to comment.