@q32/core 0.1.20 → 0.1.22

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/hono.ts ADDED
@@ -0,0 +1,54 @@
1
+ import type { AuthSystem, McpAuthService, McpAuthorizationParams } from "./auth.js";
2
+ import { errorResponse, HttpError } from "./http.js";
3
+ import type { OAuthClientInformation } from "./oauth.js";
4
+
5
+ export type HonoContextLike<TEnv = unknown> = {
6
+ req: {
7
+ raw: Request;
8
+ url: string;
9
+ };
10
+ env?: TEnv;
11
+ set?: (key: string, value: unknown) => void;
12
+ get?: (key: string) => unknown;
13
+ json: (value: unknown, status?: number) => Response;
14
+ redirect: (location: string, status?: number) => Response;
15
+ res?: Response;
16
+ };
17
+
18
+ export type HonoNext = () => Promise<void>;
19
+
20
+ export function honoAuthMiddleware<TPrincipal, TSession>(
21
+ auth: AuthSystem<TPrincipal, TSession>,
22
+ options: { contextKey?: string; requirePrincipal?: boolean } = {},
23
+ ): (context: HonoContextLike, next: HonoNext) => Promise<Response | void> {
24
+ return async (context, next) => {
25
+ try {
26
+ const authContext = await auth.contextFromRequest(context.req.raw);
27
+ if (options.requirePrincipal && !authContext.principal) {
28
+ throw new HttpError(401, "Authentication required.", "authentication_required");
29
+ }
30
+ context.set?.(options.contextKey ?? "auth", authContext);
31
+ await next();
32
+ } catch (error) {
33
+ return errorResponse(error);
34
+ }
35
+ };
36
+ }
37
+
38
+ export async function honoRequirePrincipal<TPrincipal, TSession>(
39
+ context: HonoContextLike,
40
+ auth: AuthSystem<TPrincipal, TSession>,
41
+ ): Promise<TPrincipal> {
42
+ return auth.requirePrincipal(context.req.raw);
43
+ }
44
+
45
+ export async function honoMcpAuthorize<TPrincipal, TSubject extends Record<string, string>>(
46
+ context: HonoContextLike,
47
+ service: McpAuthService<TPrincipal, TSubject>,
48
+ client: OAuthClientInformation,
49
+ params: McpAuthorizationParams,
50
+ ): Promise<Response> {
51
+ const result = await service.authorize(client, params, context.req.raw);
52
+ if (result.kind === "redirect") return context.redirect(result.location, result.status ?? 302);
53
+ return context.redirect(result.redirect, 302);
54
+ }
package/src/index.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  export * from "./api.js";
2
2
  export * from "./ai.js";
3
+ export * from "./auth.js";
3
4
  export * from "./billing.js";
4
5
  export * from "./cloudflare.js";
5
6
  export * from "./crypto.js";
@@ -7,6 +8,7 @@ export * from "./d1.js";
7
8
  export * from "./encoding.js";
8
9
  export * from "./email.js";
9
10
  export * from "./env.js";
11
+ export * from "./hono.js";
10
12
  export * from "./http.js";
11
13
  export * from "./ids.js";
12
14
  export * from "./jobs.js";
@@ -17,6 +19,7 @@ export * from "./pg.js";
17
19
  export * from "./pg-kysely.js";
18
20
  export * from "./r2-json.js";
19
21
  export * from "./rate-limit.js";
22
+ export * from "./react-router.js";
20
23
  export * from "./seo.js";
21
24
  export * from "./session.js";
22
25
  export * from "./testing.js";