From 4f543a054ff3f4b9e65fb1aece28ee9b2c45cb43 Mon Sep 17 00:00:00 2001 From: Filip Figiel Date: Mon, 18 Sep 2023 22:49:48 +0200 Subject: [PATCH 1/4] feat: draft FetchRequestOptions feature --- manifest.toml | 6 +++--- src/ffi.mjs | 16 ++++++++++++++++ src/gleam/fetch.gleam | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 3 deletions(-) diff --git a/manifest.toml b/manifest.toml index eedb7a7..abf6296 100644 --- a/manifest.toml +++ b/manifest.toml @@ -3,9 +3,9 @@ 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_javascript", version = "0.6.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_javascript", source = "hex", outer_checksum = "BFEBB63ABE4A1694E07DEFD19B160C2980304B5D775A89D4B02E7DE7C9D8008B" }, + { name = "gleam_stdlib", version = "0.30.2", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "8D8BF3790AA31176B1E1C0B517DD74C86DA8235CF3389EA02043EE4FD82AE3DC" }, + { name = "gleeunit", version = "0.11.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "1397E5C4AC4108769EE979939AC39BF7870659C5AFB714630DEEEE16B8272AD5" }, ] [requirements] 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..e85960d 100644 --- a/src/gleam/fetch.gleam +++ b/src/gleam/fetch.gleam @@ -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)) +} From 8e5694d2bb6c0c7d79f0759aa467c5c29127de5a Mon Sep 17 00:00:00 2001 From: Filip Figiel Date: Fri, 26 Jan 2024 21:25:17 +0100 Subject: [PATCH 2/4] chore: use manifest.toml from current main --- manifest.toml | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/manifest.toml b/manifest.toml index abf6296..bada442 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.6.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_javascript", source = "hex", outer_checksum = "BFEBB63ABE4A1694E07DEFD19B160C2980304B5D775A89D4B02E7DE7C9D8008B" }, - { name = "gleam_stdlib", version = "0.30.2", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "8D8BF3790AA31176B1E1C0B517DD74C86DA8235CF3389EA02043EE4FD82AE3DC" }, - { name = "gleeunit", version = "0.11.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "1397E5C4AC4108769EE979939AC39BF7870659C5AFB714630DEEEE16B8272AD5" }, + { 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.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_javascript", source = "hex", outer_checksum = "B5E05F479C52217C02BA2E8FC650A716BFB62D4F8D20A90909C908598E12FBE0" }, + { name = "gleam_stdlib", version = "0.34.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "1FB8454D2991E9B4C0C804544D8A9AD0F6184725E20D63C3155F0AEB4230B016" }, + { name = "gleeunit", version = "1.0.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "D364C87AFEB26BDB4FB8A5ABDE67D635DC9FA52D6AB68416044C35B096C6882D" }, ] [requirements] gleam_http = { version = "~> 3.1" } -gleam_javascript = { version = "~> 0.3" } -gleeunit = { version = "~> 0.8" } +gleam_javascript = { version = "~> 0.3 or ~> 1.0" } +gleam_stdlib = { version = "~> 0.32 or ~> 1.0" } +gleeunit = { version = "~> 1.0" } From 05f9c4f6409aac93b3b02a375e6b243f5f0bf73d Mon Sep 17 00:00:00 2001 From: Filip Figiel Date: Fri, 26 Jan 2024 21:31:26 +0100 Subject: [PATCH 3/4] fix: solve build errors --- gleam.toml | 1 + manifest.toml | 10 +++++----- src/gleam/fetch.gleam | 8 ++++---- test/gleam_fetch_test.gleam | 4 ++-- 4 files changed, 12 insertions(+), 11 deletions(-) diff --git a/gleam.toml b/gleam.toml index ced87ed..fb75373 100644 --- a/gleam.toml +++ b/gleam.toml @@ -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 bada442..a30b661 100644 --- a/manifest.toml +++ b/manifest.toml @@ -3,13 +3,13 @@ packages = [ { 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.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_javascript", source = "hex", outer_checksum = "B5E05F479C52217C02BA2E8FC650A716BFB62D4F8D20A90909C908598E12FBE0" }, + { 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 = "1.0.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "D364C87AFEB26BDB4FB8A5ABDE67D635DC9FA52D6AB68416044C35B096C6882D" }, + { 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 or ~> 1.0" } -gleam_stdlib = { version = "~> 0.32 or ~> 1.0" } -gleeunit = { version = "~> 1.0" } +gleam_javascript = { version = "~> 0.3" } +gleam_stdlib = { version = "~> 0.34"} +gleeunit = { version = "~> 0.8" } diff --git a/src/gleam/fetch.gleam b/src/gleam/fetch.gleam index e85960d..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) 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 From 037f0670e9f08c1d66ed8be9a903104845e3fcde Mon Sep 17 00:00:00 2001 From: Filip Figiel Date: Fri, 26 Jan 2024 21:33:01 +0100 Subject: [PATCH 4/4] chore: update gleam.toml to use the oldest compatible version --- gleam.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gleam.toml b/gleam.toml index fb75373..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 = [