mirror of
https://github.com/morten-olsen/mini-loader.git
synced 2026-02-08 01:36:26 +01:00
init
This commit is contained in:
30
packages/server/src/router/router.utils.ts
Normal file
30
packages/server/src/router/router.utils.ts
Normal 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 };
|
||||
Reference in New Issue
Block a user