This commit is contained in:
Morten Olsen
2024-01-12 12:43:51 +01:00
commit 6d8e5bf955
109 changed files with 9246 additions and 0 deletions

View File

@@ -0,0 +1,30 @@
import { initTRPC } from '@trpc/server';
import { CreateFastifyContextOptions } from '@trpc/server/adapters/fastify';
import superjson from 'superjson';
import { Runtime } from '../runtime/runtime.js';
type ContextOptions = {
runtime: Runtime;
};
const createContext = async ({ runtime }: ContextOptions) => {
return async ({ req }: CreateFastifyContextOptions) => {
const { authorization } = req.headers;
const { auth } = runtime;
if (!authorization) {
throw new Error('No authorization header');
}
await auth.validateToken(authorization);
return {
runtime,
};
};
};
type Context = Awaited<ReturnType<typeof createContext>>;
const { router, procedure: publicProcedure } = initTRPC.context<Context>().create({
transformer: superjson,
});
export { createContext, router, publicProcedure };