feat: Refactor GraphQL context for user data and async creation

This commit is contained in:
2025-12-06 08:26:16 -05:00
parent cc4f518de7
commit 311d575c09

View File

@@ -1,22 +1,26 @@
import type { Context as HonoContext } from "hono";
export interface GraphQLContext {
db: D1Database;
env: Env;
deviceId?: string;
aniListToken?: string;
honoContext: HonoContext;
db: D1Database;
deviceId?: string;
aniListToken?: string;
user: { id: number, name: string } | null;
honoContext: HonoContext;
}
export function createGraphQLContext(c: HonoContext): GraphQLContext {
const deviceId = c.req.header("X-Device-ID");
const aniListToken = c.req.header("X-AniList-Token");
export async function createGraphQLContext(c: HonoContext<Env>): Promise<GraphQLContext> {
const deviceId = c.req.header("X-Device-ID");
const aniListToken = c.req.header("X-AniList-Token");
const env = c.env as Env;
return {
db: c.env.DB,
env: c.env,
deviceId,
aniListToken,
honoContext: c,
};
const stub = await env.ANILIST_DO.getByName("GLOBAL");
const user = await stub.getUser(aniListToken!);
return {
db: env.DB,
deviceId,
aniListToken,
user,
honoContext: c,
};
}