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

feat(types): Add DefaultEnv for zero runtime factory #3819

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

miyaji255
Copy link
Contributor

Resolves #3793

This is BREAKING CHANGE.

I specified DefaultEnv for those that take Env as a type argument. By extending DefaultEnv with interface merging, you can change the default Env for various types.

This eliminates the need to use hono/factory in simple cases. However, if multiple Bindings are used, they must be explicitly specified.

interface DefaultEnv {}
interface DefaultBindings {}
interface DefaultVariables {}

export class Hono<E extends Env = DefaultEnv> {

Usage

By extending DefaultEnv, library users do not need to specify it explicitly.

// library
declare module "hono" {
  interface DefaultEnv {
    Bindings: DefaultBindings;
  }
  interface DefaultBindings {
    Foo: string;
  }
}

export function handle(app: Hono<any>): ...

You no longer need to explicitly specify Env.

import { Hono } from "hono";
import { handle } from "hono/aws-lambda/api-gateway";

const middleware: MiddlewareHandler = (c, next) => {
  c.env.event // APIGatewayProxyEvent
  c.env.lambdaContext // LambdaContext
  return next();
}

const app = new Hono()
  .get("/", c => {
    c.env.event // APIGatewayProxyEvent
    c.env.lambdaContext // LambdaContext
    return c.text(c.env.event.path);
  });
export default handle(app);

@yusukebe
Copy link
Member

Hi @miyaji255

Thank you for the PR! I also think it would be good if we could set the env with declare module 'hono'. I'll start to consider this feature.

@miyaji255
Copy link
Contributor Author

Thank you very much. I will also think about whether I can fix errors and design a better API.

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.

Zero Runtime Factory
2 participants