Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support passing options to fetch #5

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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
3 changes: 2 additions & 1 deletion gleam.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand All @@ -14,6 +14,7 @@ links = [
[dependencies]
gleam_http = "~> 3.1"
gleam_javascript = "~> 0.3"
gleam_stdlib = "~> 0.34"

[dev-dependencies]
gleeunit = "~> 0.8"
Expand Down
9 changes: 5 additions & 4 deletions manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
16 changes: 16 additions & 0 deletions src/ffi.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 }
}
44 changes: 40 additions & 4 deletions src/gleam/fetch.gleam
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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)) {
Expand All @@ -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))
}
4 changes: 2 additions & 2 deletions test/gleam_fetch_test.gleam
Original file line number Diff line number Diff line change
@@ -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
Expand Down