honojs / hono

Web framework built on Web Standards
https://hono.dev
MIT License
19.63k stars 559 forks source link

(bug): Usng hono/cache breaks types for c.env in Hono 3.6.0 #1445

Closed AdiRishi closed 1 year ago

AdiRishi commented 1 year ago

What version of Hono are you using?

3.6.0

What runtime/platform is your app running on?

Cloudflare Workers

What steps can reproduce the bug?

  1. In CF workers, add a few types to the Env interface for workers. E.g
    export type Env = {
    R2_STORE: R2Bucket;
    };
  2. Create a new app instance of Hono with proper env bindings. E.g
    export const app = new Hono<{ Bindings: Env; }>();
  3. Setup a route using one of those bindings. E.g
    app.get('/bucket-get', async (c) => {
    // the type of R2_BUCKET is R2Bucket (correct)
    const value = await c.env.R2_BUCKET.get('example.jpg');
    return c.json({ value });
    });
  4. Add the cache middleware to the route. E.g
    app.get(
    '/bucket-get',
    cache({
    cacheName: 'r2-artifacts',
    wait: false,
    cacheControl: 'max-age=86400, stale-while-revalidate=3600',
    }),
    async (c) => {
    // the type of R2_BUCKET is now any
    const value = await c.env.R2_BUCKET.get('example.jpg');
    return c.json({ value });
    });

What is the expected behavior?

The type of c.env.R2_BUCKET should be R2Bucket

What do you see instead?

The type of c.env.R2_BUCKET is any because the type of c.env is now any

Additional information

If a reproduction repository helps, you can check the renovate/hono-3.x branch on https://github.com/AdiRishi/turborepo-remote-cache-cloudflare

To setup the repository, just clone it and run yarn install, the specific line which can showcase this error is at https://github.com/AdiRishi/turborepo-remote-cache-cloudflare/blob/renovate/hono-3.x/src/routes/v8/artifacts.ts#L62

AdiRishi commented 1 year ago

Hey @yusukebe

I just tried the fix you pushed in 3.6.1 and it does fix the reported problem šŸŽ‰ .

Unfortunately I found a few new type errors that still exist in 3.6.1, I'll list them below, but let me know if you want me to open a new issue to deal with them

Code example:

import { middleware } from 'hono/factory'; // import for the second code block (attempted re-write)
export const internalRouter = new Hono<{ Bindings: Env }>();

// Old code before 3.6.x
// This used to work, but the middleware(c,next) line throws an error as it doesn't like the type of c anymore
// internalRouter.use('*', async (c, next) => {
//   const middleware = bearerAuth({ token: c.env.TURBO_TOKEN });
//   await middleware(c, next);
// });

// I tried to re-write this using the new middleware helper you made, but it also has a type error
internalRouter.use(
  '*',
  middleware(async (c, next) => {
    const authMiddleware = bearerAuth({ token: c.env?.TURBO_TOKEN });
    await authMiddleware(c, next);
  })
);

In the re-write I tied with the new middleware helper, the type of c.env doesn't seem to properly contain the Env type that I'm passing in? E.g c.env.TURBO_TOKEN is of type unknown.

yusukebe commented 1 year ago

@AdiRishi

Hi! That issue might be fixed in v3.6.2 released now. Try it!

AdiRishi commented 1 year ago

Just updated to 3.6.3 and everything is working great, than you šŸš€

yusukebe commented 1 year ago

Nice!