diff --git a/gleam.toml b/gleam.toml index ced87ed..e479be0 100644 --- a/gleam.toml +++ b/gleam.toml @@ -3,7 +3,7 @@ version = "0.2.0" licences = ["Apache-2.0"] description = "Make HTTP requests in Gleam JavaScript with Fetch" target = "javascript" -gleam = ">= 0.30.0" +gleam = ">= 0.32.0" repository = { type = "github", user = "gleam-lang", repo = "fetch" } links = [ @@ -14,6 +14,7 @@ links = [ [dependencies] gleam_http = "~> 3.1" gleam_javascript = "~> 0.3" +gleam_stdlib = "~> 0.34" [dev-dependencies] gleeunit = "~> 0.8" diff --git a/manifest.toml b/manifest.toml index eedb7a7..a30b661 100644 --- a/manifest.toml +++ b/manifest.toml @@ -2,13 +2,14 @@ # You typically do not need to edit this file packages = [ - { name = "gleam_http", version = "3.5.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_http", source = "hex", outer_checksum = "FAE9AE3EB1CA90C2194615D20FFFD1E28B630E84DACA670B28D959B37BCBB02C" }, - { name = "gleam_javascript", version = "0.5.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_javascript", source = "hex", outer_checksum = "9FBC0BA8E15355B1CAA2775109E1E05EA0950D0FF451A8AFD41BDD972C3652A1" }, - { 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" }, + { name = "gleam_http", version = "3.5.3", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_http", source = "hex", outer_checksum = "C2FC3322203B16F897C1818D9810F5DEFCE347F0751F3B44421E1261277A7373" }, + { name = "gleam_javascript", version = "0.7.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_javascript", source = "hex", outer_checksum = "EEA30D1ABF62B06FC378764D598DF041303CFA33A6586BFF4C4BFEFFA83DBDBE" }, + { name = "gleam_stdlib", version = "0.34.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "1FB8454D2991E9B4C0C804544D8A9AD0F6184725E20D63C3155F0AEB4230B016" }, + { name = "gleeunit", version = "0.11.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "1397E5C4AC4108769EE979939AC39BF7870659C5AFB714630DEEEE16B8272AD5" }, ] [requirements] gleam_http = { version = "~> 3.1" } gleam_javascript = { version = "~> 0.3" } +gleam_stdlib = { version = "~> 0.34"} gleeunit = { version = "~> 0.8" } diff --git a/src/ffi.mjs b/src/ffi.mjs index f0f4619..89177e6 100644 --- a/src/ffi.mjs +++ b/src/ffi.mjs @@ -17,6 +17,14 @@ export async function raw_send(request) { } } +export async function raw_send_with_options(request, options) { + try { + return new Ok(await fetch(request, options)); + } catch (error) { + return new Error(new NetworkError(error.toString())); + } +} + export function from_fetch_response(response) { return new Response( response.status, @@ -60,3 +68,11 @@ export async function read_json_body(response) { return new Error(new InvalidJsonBody()); } } + +export function make_options() { + return {} +} + +export function update_options(options, key, value) { + return { ...options, [key]: value } +} diff --git a/src/gleam/fetch.gleam b/src/gleam/fetch.gleam index 090428b..3593e16 100644 --- a/src/gleam/fetch.gleam +++ b/src/gleam/fetch.gleam @@ -1,7 +1,7 @@ -import gleam/http/request.{Request} -import gleam/http/response.{Response} -import gleam/dynamic.{Dynamic} -import gleam/javascript/promise.{Promise} +import gleam/http/request.{type Request} +import gleam/http/response.{type Response} +import gleam/dynamic.{type Dynamic} +import gleam/javascript/promise.{type Promise} pub type FetchError { NetworkError(String) @@ -13,11 +13,19 @@ pub type FetchBody pub type FetchRequest +pub type FetchRequestOptions + pub type FetchResponse @external(javascript, "../ffi.mjs", "raw_send") pub fn raw_send(a: FetchRequest) -> Promise(Result(FetchResponse, FetchError)) +@external(javascript, "../ffi.mjs", "raw_send_with_options") +pub fn raw_send_with_options( + a: FetchRequest, + b: FetchRequestOptions, +) -> Promise(Result(FetchResponse, FetchError)) + pub fn send( request: Request(String), ) -> Promise(Result(Response(FetchBody), FetchError)) { @@ -44,3 +52,31 @@ pub fn read_text_body( pub fn read_json_body( a: Response(FetchBody), ) -> Promise(Result(Response(Dynamic), FetchError)) + +@external(javascript, "../ffi.mjs", "make_options") +pub fn make_options() -> FetchRequestOptions + +@external(javascript, "../ffi.mjs", "update_options") +fn update_options( + a: FetchRequestOptions, + key: String, + value: Dynamic, +) -> FetchRequestOptions + +pub type CredentialsOption { + Include + SameOrigin + Omit +} + +pub fn with_credentials( + o: FetchRequestOptions, + v: CredentialsOption, +) -> FetchRequestOptions { + let encoded_value = case v { + Include -> "include" + SameOrigin -> "same-origin" + Omit -> "omit" + } + update_options(o, "credentials", dynamic.from(encoded_value)) +} diff --git a/test/gleam_fetch_test.gleam b/test/gleam_fetch_test.gleam index c612c76..f3324bd 100644 --- a/test/gleam_fetch_test.gleam +++ b/test/gleam_fetch_test.gleam @@ -1,6 +1,6 @@ -import gleam/fetch.{FetchError} +import gleam/fetch.{type FetchError} import gleam/http.{Get, Head, Options} -import gleam/http/response.{Response} +import gleam/http/response.{type Response, Response} import gleam/http/request import gleam/javascript/promise import gleeunit