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

Expose fetch's Response class to JavaScript land #417

Open
manekinekko opened this issue Aug 3, 2023 · 2 comments
Open

Expose fetch's Response class to JavaScript land #417

manekinekko opened this issue Aug 3, 2023 · 2 comments
Labels
📐 proposal A concept/proposition, no design work has been done yet

Comments

@manekinekko
Copy link

Describe the solution you'd like
As a JavaScript developer, I'd usually need to compose content (grab data from a provider) and return static HTML to clients (aka. Server Side Rendering). In order to accomplish this, JavaScript's engines expose a Response() class, part of fetch Web API.

It'd be great if the following example just works with Slight:

const html = `<!DOCTYPE html><h1>Hello World</h1>`;
const options = {
    headers: {
      "content-type": "text/html",
    },
  };
return new Response(html, options);

See specs: https://fetch.spec.whatwg.org/#response-class

Additional context
I'd also recommend considering exposing Request and Headers, also part of the fetch Web API.

cc @devigned @squillace

@manekinekko manekinekko added the 📐 proposal A concept/proposition, no design work has been done yet label Aug 3, 2023
@devigned
Copy link
Member

devigned commented Aug 3, 2023

Currently, the js HTTP server example looks like: https://github.com/danbugs/slightjs/blob/4fe13b0107c800aeeeaca423cd68d6612b61ae57/examples/http-server/index.js

function handle_get(req) {
    console.log("I just got a request @ uri: ", req.uri, ", w/ method: ", req.method);
    switch (req.uri) {
        case "/get/hello":
            console.log("hello!");
            break;
        default:
            break;
    }

    return JSON.stringify({ headers: null, body: "Hello, JS Wasm!", status: 200 });
}

The previous code snippet shows a route handler func which takes a request structure, logs some messages, and returns a string of JSON.

To clarify:

  1. the route handler should return a Response type matching the fetch spec
  2. consider exposing the fetch API

Is that accurate?

@manekinekko
Copy link
Author

That's correct.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
📐 proposal A concept/proposition, no design work has been done yet
Projects
None yet
Development

No branches or pull requests

2 participants