Files
zeekay d3e091aa97 Serve and call every route under /v1
The resource API is /v1/rest and the operation API is /v1/rpc: named
for what they are, so neither shadows the other, and the v2-beta alias
is gone. Hanzo IAM returns to /auth/callback, the callback path IAM
already derives for this host, so sign-in needs no IAM change.

Claude-Session: https://claude.ai/code/session_01TUxvfuta1Z44KVNYuAz71F
2026-09-24 14:52:07 -07:00

68 lines
1.6 KiB
TypeScript

import type { Context, Next } from 'hono';
import { extractSessionCookieFromHeaders } from '@hanzo/esign-auth/server/lib/session/session-cookies';
import {
type RequestMetadata,
extractRequestMetadata,
} from '@hanzo/esign-lib/universal/extract-request-metadata';
export type AppContext = {
requestMetadata: RequestMetadata;
};
/**
* Apply a context which can be accessed throughout the app.
*
* Keep this as lean as possible in terms of awaiting, because anything
* here will increase each page load time.
*/
export const appContext = async (c: Context, next: Next) => {
const request = c.req.raw;
const url = new URL(request.url);
const noSessionCookie = extractSessionCookieFromHeaders(request.headers) === null;
setAppContext(c, {
requestMetadata: extractRequestMetadata(request),
});
// These are non page paths like API.
if (!isPageRequest(request) || noSessionCookie || blacklistedPathsRegex.test(url.pathname)) {
return next();
}
// Add context to any pages you want here.
return next();
};
const setAppContext = (c: Context, context: AppContext) => {
c.set('context', context);
};
const isPageRequest = (request: Request) => {
const url = new URL(request.url);
if (request.method !== 'GET') {
return false;
}
// If it ends with .data it's the loader which we need to pass context for.
if (url.pathname.endsWith('.data')) {
return true;
}
if (request.headers.get('Accept')?.includes('text/html')) {
return true;
}
return false;
};
/**
* List of paths to reject
* - Urls that start with /v1
* - Urls that start with _
*/
const blacklistedPathsRegex = new RegExp('^/v1/|^/__');