H
Hono3d ago
rykuno

Envs not extendable with `createFactory`

When utilizing the createFactory method, middleware is unable to modify or extend any of the types. This seems like a bug/issue as any auth middlware would want to ensure a session/user variable would be defined for type safety. factory.ts
export type HonoEnv = {
Variables: {
session: SessionDto | null;
};
};

export const factory = createFactory<HonoEnv>();
export type HonoEnv = {
Variables: {
session: SessionDto | null;
};
};

export const factory = createFactory<HonoEnv>();
middleware.ts
const authed: MiddlewareHandler<{
Variables: {
session: SessionDto;
};
}> = factory.createMiddleware(async (c, next) => {
if (!c.var.session) {
throw Unauthorized('You must be logged in to access this resource');
}
return next();
});
const authed: MiddlewareHandler<{
Variables: {
session: SessionDto;
};
}> = factory.createMiddleware(async (c, next) => {
if (!c.var.session) {
throw Unauthorized('You must be logged in to access this resource');
}
return next();
});
No description
No description
1 Reply
rykuno
rykuno3d ago
in the screenshot examples, I have one working example(without factory) and one broken(with factory).
Want results from more Discord servers?
Add your server