gleam-lang / fetch Goto Github PK
View Code? Open in Web Editor NEW๐ก Make requests to HTTP servers with fetch
License: Apache License 2.0
๐ก Make requests to HTTP servers with fetch
License: Apache License 2.0
Some time ago we chatted how we could make it possible to pass request options to fetch.send()
. I actually forgot about the issue until I hopped back on my toy project this weekend and got stuck ๐ Opening a GH issue this time to keep it visible.
Here's the related discord discussion. We didn't reach any conclusion but it should help jog our memory.
The simplest solution would be to introduce a FetchOptions
type that we would pass to a new dedicated function like fetch.send_with_options(request, options)
.
Another path would be having a new FetchRequest
type that wraps the classic gleam/http/request.{Request}
with additional fetch-specific fields, such as mode
, credentials
, etc. We could then provide a builder pipeline to take the request, add options, and send it.
Hello, I am learning gleam and wanted to figure out how to make http calls in JS targets but the README example produces an error and I am not sure what to do.
Here are my steps:
$ gleam new fetch
$ cd fetch
$ gleam add gleam_fetch
$ gleam run -t javascript
The result:
Compiled in 0.02s
Running fetch.main
file:///Users/george/dev/gleam/fetch/build/dev/javascript/gleam_fetch/ffi.mjs:40
let headers = new globalThis.Headers();
^
TypeError: globalThis.Headers is not a constructor
at make_headers (file:///Users/george/dev/gleam/fetch/build/dev/javascript/gleam_fetch/ffi.mjs:40:17)
at to_fetch_request (file:///Users/george/dev/gleam/fetch/build/dev/javascript/gleam_fetch/ffi.mjs:32:14)
at Module.send (file:///Users/george/dev/gleam/fetch/build/dev/javascript/gleam_fetch/gleam/fetch.mjs:37:17)
at main (file:///Users/george/dev/gleam/fetch/build/dev/javascript/fetch/fetch.mjs:21:12)
at file:///Users/george/dev/gleam/fetch/build/dev/javascript/fetch/gleam.main.mjs:2:1
at ModuleJob.run (node:internal/modules/esm/module_job:175:25)
at async Loader.import (node:internal/modules/esm/loader:178:24)
at async Object.loadESM (node:internal/process/esm_loader:68:5)
3rd party provides me with large gzip'ed json.
Add
@external(javascript, "../ffi.mjs", "read_body")
pub fn read_body(
a: Response(FetchBody),
) -> Promise(Result(Response(BitArray), FetchError))
which the user must gunzip afterwards?
If so, I have a PR.
Specifying a timeout when making a web request seems like a core feature, otherwise your code is at the whim of whatever you're communicating with.
Usually I'd pass a AbortSignal.timeout(time)
in the fetch options.
Another solution would be to race the promise with setTimeout. Doesn't look like the promises package exposed race functionality though.
Possibly linked with #4.. depending on the approach
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.