Initial commit

This commit is contained in:
Jerome 2025-05-16 21:38:59 +00:00
commit b1d1790f62
No known key found for this signature in database

36
src/context.ts Normal file
View file

@ -0,0 +1,36 @@
import { AsyncLocalStorage } from "async_hooks";
import { FakeUpstreamInstallation } from "./types.js";
interface RequestContext {
mcpAccessToken: string;
fakeUpstreamInstallation: FakeUpstreamInstallation;
}
export const asyncLocalStorage = new AsyncLocalStorage<RequestContext>();
export function getFakeUpstreamInstallation(): FakeUpstreamInstallation {
const context = asyncLocalStorage.getStore();
if (!context) {
throw new Error(
"No request context found - are you calling this from within a request handler?",
);
}
return context.fakeUpstreamInstallation;
}
export function getMcpAccessToken(): string {
const context = asyncLocalStorage.getStore();
if (!context) {
throw new Error(
"No request context found - are you calling this from within a request handler?",
);
}
return context.mcpAccessToken;
}
export function withContext<T>(
context: RequestContext,
fn: () => T | Promise<T>,
): T | Promise<T> {
return asyncLocalStorage.run(context, fn);
}