Has someone implemented fetch? #1999
Replies: 4 comments 1 reply
-
I haven't, but there's https://github.com/AngleSharp/AngleSharp.Js , which doesn't seem to have such support, but might be a logical place to extend and consume such features. |
Beta Was this translation helpful? Give feedback.
-
Since I am ignorant about it I found some docs about it https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API. Maybe some of these "well established" APIs could be opted-in like in options or with Even something like file system apis could be useful (assuming Jint could also provide different kinds of filesystems). |
Beta Was this translation helpful? Give feedback.
-
Edit: Fixed it with I'm trying to implement a Specifically, this test fails: [Fact]
public void Can_Handle_Async_Json_Parse_As_JS_From_Object()
{
var engine = new Engine();
// We don't need the full functionality here because there is a failure in the async/await we are testing
var fetcher = new Fetcher();
engine.SetValue("fetch", fetcher.FetchAsync);
var messages = new List<string>();
engine.SetValue(
"log",
new Action<object>(msg =>
{
messages.Add(msg?.ToString() ?? string.Empty);
})
);
var script = """
(async () => {
const response = await fetch();
log(response);
const json = await response.json(); // 👈 This await of the Task<JObject> seems to be ignored?
log(json); // 👈 This is returns Task<JObject> and not JObject
return await json.text;
})()
""";
var result = engine.Evaluate(script).UnwrapIfPromise();
Assert.NotNull(result);
Assert.Equal("Hello, World!", result);
}
class Fetcher
{
public async Task<Response> FetchAsync()
{
return await Task.FromResult(new Response());
}
}
class Response
{
// 👇 This is the cause of the failure
public async Task<JObject> json()
{
return await Task.FromResult(
JObject.Parse("""{ "text": "Hello, World!" }""")
);
}
} Specifically, the line Any thoughts on what I'm doing wrong here? I've looked through the examples of the tests for async and can't quite get this one working. |
Beta Was this translation helpful? Give feedback.
-
I ended up implementing a full MCP server for executing LLM generated JavaScript using Jint and gave it a |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi,
I am just curious if someone has implemented ´fetch` already. I have a few http helper functions for my scripts but fetch would give more flexibility.
Beta Was this translation helpful? Give feedback.
All reactions