feat: Refactor GraphQL context for user data and async creation
This commit is contained in:
@@ -1,22 +1,26 @@
|
|||||||
import type { Context as HonoContext } from "hono";
|
import type { Context as HonoContext } from "hono";
|
||||||
|
|
||||||
export interface GraphQLContext {
|
export interface GraphQLContext {
|
||||||
db: D1Database;
|
db: D1Database;
|
||||||
env: Env;
|
deviceId?: string;
|
||||||
deviceId?: string;
|
aniListToken?: string;
|
||||||
aniListToken?: string;
|
user: { id: number, name: string } | null;
|
||||||
honoContext: HonoContext;
|
honoContext: HonoContext;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function createGraphQLContext(c: HonoContext): GraphQLContext {
|
export async function createGraphQLContext(c: HonoContext<Env>): Promise<GraphQLContext> {
|
||||||
const deviceId = c.req.header("X-Device-ID");
|
const deviceId = c.req.header("X-Device-ID");
|
||||||
const aniListToken = c.req.header("X-AniList-Token");
|
const aniListToken = c.req.header("X-AniList-Token");
|
||||||
|
const env = c.env as Env;
|
||||||
|
|
||||||
return {
|
const stub = await env.ANILIST_DO.getByName("GLOBAL");
|
||||||
db: c.env.DB,
|
const user = await stub.getUser(aniListToken!);
|
||||||
env: c.env,
|
|
||||||
deviceId,
|
return {
|
||||||
aniListToken,
|
db: env.DB,
|
||||||
honoContext: c,
|
deviceId,
|
||||||
};
|
aniListToken,
|
||||||
|
user,
|
||||||
|
honoContext: c,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user