11/* eslint-disable @typescript-eslint/no-explicit-any */
22import { cryptoHash , decrypt } from '@supaglue/core/lib' ;
33import prisma from '@supaglue/db' ;
4+ import { createEnv } from '@t3-oss/env-core' ;
45import { TRPCError , initTRPC } from '@trpc/server' ;
56import type { OpenApiMeta } from '@usevenice/trpc-openapi' ;
67import { z } from 'zod' ;
@@ -12,20 +13,33 @@ extendZodWithOpenApi(z);
1213
1314export { z } ;
1415
15- export function createContext ( opts : {
16- headers : { 'x-api-key' : string ; 'x-customer-id' : string ; 'x-provider-name' : string } ;
17- } ) {
18- return { ...opts , prisma } ;
16+ export const env = createEnv ( {
17+ server : {
18+ /** Reqruired for prisma */
19+ SUPAGLUE_DATABASE_URL : z . string ( ) . url ( ) ,
20+ /* Required for encryption & decryption */
21+ SUPAGLUE_API_ENCRYPTION_SECRET : z . string ( ) . min ( 1 ) ,
22+ } ,
23+ runtimeEnv : process . env ,
24+ } ) ;
25+
26+ export function createContext ( opts : { headers : unknown } ) {
27+ const headers = z
28+ . object ( {
29+ 'x-api-key' : z . string ( ) . optional ( ) ,
30+ 'x-customer-id' : z . string ( ) . optional ( ) ,
31+ 'x-provider-name' : z . string ( ) . optional ( ) ,
32+ } )
33+ . parse ( opts . headers ) ;
34+
35+ return { headers, env, prisma } ;
1936}
2037
2138/**
2239 * Initialization of tRPC backend
2340 * Should be done only once per backend!
2441 */
25- export const t = initTRPC
26- . context < { headers : { 'x-api-key' : string ; 'x-customer-id' : string ; 'x-provider-name' : string } } > ( )
27- . meta < OpenApiMeta > ( )
28- . create ( ) ;
42+ export const t = initTRPC . context < ReturnType < typeof createContext > > ( ) . meta < OpenApiMeta > ( ) . create ( ) ;
2943
3044export const authedProcedure = t . procedure . use ( async ( { next, ctx } ) => {
3145 if ( ! ctx . headers [ 'x-api-key' ] ) {
@@ -76,6 +90,3 @@ export const remoteProcedure = authedProcedure.use(async ({ next, ctx }) => {
7690
7791 return next ( { ctx : { ...ctx , provider, providerName, customerId } } ) ;
7892} ) ;
79- export type RemoteProcedureContext = ReturnType < ( typeof remoteProcedure ) [ 'query' ] > [ '_def' ] [ '_ctx_out' ] ;
80-
81- export type MaybePromise < T > = T | Promise < T > ;
0 commit comments