generated from stabledata/surface
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsurface.app.ctx.ts
40 lines (31 loc) · 1.18 KB
/
surface.app.ctx.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import { hc } from "hono/client";
import { createFactory } from "hono/factory";
import { decode, sign, verify } from "hono/jwt";
import { cookies } from './cookies/cookies.js';
import { logger } from './logger/logger.js';
import { Context } from "hono";
const jwt = { decode, sign, verify };
export type Dependencies = {
// utils
logger: typeof logger;
cookies: ReturnType<typeof cookies>;
jwt: typeof jwt;
// specifically for testing, allows overwriting the rpc client
rpcClientMock?: typeof hc;
};
type Env = {
Variables: Dependencies;
};
export type SurfaceContext = Context<Env>;
export const { createMiddleware, createHandlers } = createFactory<Env>();
// type safe dependency injection via middleware.
// more verbose than the previous pattern but no type acrobatics!
// handlers docs here: https://hono.dev/guides/best-practices#factory-createhandlers-in-hono-factory
export const applyContext = (injections: Partial<Dependencies>) =>
createMiddleware(async (c, next) => {
c.set("logger", injections.logger ?? logger);
c.set("cookies", injections.cookies ?? cookies(c));
c.set("jwt", jwt);
c.set("rpcClientMock", injections.rpcClientMock);
await next();
});