Skip to content

feat(serve): support export default class #28751

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open

Conversation

marvinhagemeister
Copy link
Contributor

@marvinhagemeister marvinhagemeister commented Apr 4, 2025

This PR adds support for passing a class instance for deno serve export default fetch.

class Foo implements Deno.ServeDefaultExport {
  fetch(_request: Request) {
    return new Response("Hello world!");
  }
}

export default new Foo();

Fixes #24062

Copy link
Member

@dsherret dsherret left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure it makes sense to support a class declaration here like:

export default class Foo implements Deno.ServeDefaultExport {
  fetch(_request: Request) {
    return new Response("Hello world!");
  }
}

...because generally classes are used to inject stuff in the constructor then pass around the instances. So practically someone would probably do something like:

const someInstance = new Foo(...some args here...);

// then
export default someInstance;

...which seems to be all that's necessary for #24062 ?

@marvinhagemeister
Copy link
Contributor Author

@dsherret fair point, dropped that from the PR so that only class instances are supported.

Comment on lines +14 to +18
class Foo implements Deno.ServeDefaultExport {
fetch(_request: Request) {
return new Response("Hello world!");
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe let's try updating this test to use something on the instance? For example, something like:

class Foo implements Deno.ServeDefaultExport {
  message = "Hello world!";
  fetch(_request: Request) {
    return new Response(this.message);
  }
}

I think right now it will not work because we need to bind the function to the instance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow classes with fetch method for deno serve.
2 participants