JavaDad
JavaDad
Explore posts from servers
HHono
Created by JavaDad on 4/10/2024 in #help
Typing Middleware JWT and others
Yeah I think this would make sense. And if you don’t pass the generic then it just stays as any, that shouldn’t be breaking?
23 replies
HHono
Created by JavaDad on 4/10/2024 in #help
Typing Middleware JWT and others
The docs here seem to indicate there is a way to do this though - https://hono.dev/api/context#contextvariablemap
23 replies
HHono
Created by JavaDad on 4/10/2024 in #help
Typing Middleware JWT and others
Its only an issue when you are trying to type variables in existing middleware
23 replies
HHono
Created by JavaDad on 4/10/2024 in #help
Typing Middleware JWT and others
23 replies
HHono
Created by JavaDad on 4/10/2024 in #help
Typing Middleware JWT and others
not jwt
23 replies
HHono
Created by JavaDad on 4/10/2024 in #help
Typing Middleware JWT and others
But the middelware JWT plugin sets a variable called jwtPayload
23 replies
HHono
Created by JavaDad on 4/10/2024 in #help
Typing Middleware JWT and others
Yes this works
23 replies
HHono
Created by JavaDad on 4/10/2024 in #help
Typing Middleware JWT and others
it also requires anywhere in the code that c.get() is used for the developer to know what type they should be expecting as there will be no inference
23 replies
HHono
Created by JavaDad on 4/10/2024 in #help
Typing Middleware JWT and others
as CustomType will work because I am casting the result but this isn't exactly type safe because if the type changes or is unexpexted this won't catch it or give any errors.
23 replies
HHono
Created by JavaDad on 4/10/2024 in #help
Typing Middleware JWT and others
The types are not throwing errors this way, they are just not being inferred. If I check the type of id from the jwtPayload its type is any
23 replies
HHono
Created by JavaDad on 4/10/2024 in #help
Typing Middleware JWT and others
@Nico here is the thread, thanks!
23 replies
HHono
Created by JavaDad on 4/10/2024 in #help
Typing Middleware JWT and others
// ====================================

// types.ts

export type Bindings = {
DATABASE_URL: string;
DATABASE_AUTH_TOKEN: string;
GOOGLE_AUTH_CLIENT_ID: string;
GOOGLE_AUTH_CLIENT_SECRET: string;
SERVER_URL: string;
CLIENT_URL: string;
};

export type Variables = {
token: {
token: string;
expires_in: number;
};
"granted-scopes": string[];
"user-google": GoogleUserInfo;
};

export type JWTPayload = {
id: string;
email: string;
name: string;
profilePicture: string;
role: Role;
};
// ====================================

// types.ts

export type Bindings = {
DATABASE_URL: string;
DATABASE_AUTH_TOKEN: string;
GOOGLE_AUTH_CLIENT_ID: string;
GOOGLE_AUTH_CLIENT_SECRET: string;
SERVER_URL: string;
CLIENT_URL: string;
};

export type Variables = {
token: {
token: string;
expires_in: number;
};
"granted-scopes": string[];
"user-google": GoogleUserInfo;
};

export type JWTPayload = {
id: string;
email: string;
name: string;
profilePicture: string;
role: Role;
};
23 replies