Skip to content

Commit

Permalink
feat: export getTrpc for workign around multiple error formatters error
Browse files Browse the repository at this point in the history
  • Loading branch information
DawidWraga committed Apr 1, 2024
1 parent 6e9eb1a commit 8a7c531
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions packages/service/src/trpc-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { Resolver, Service } from './service';
import { initTRPC, TRPCError } from '@trpc/server';
import SuperJSON from 'superjson';

const getTrpc = <TContext extends object>() => {
export const getTrpc = <TContext extends object>() => {
return initTRPC.context<TContext>().create({
transformer: SuperJSON,
errorFormatter({ shape, error }) {
Expand Down Expand Up @@ -76,7 +76,7 @@ const getTrpc = <TContext extends object>() => {

export function createTrpcProcedureFromService<
TService extends Service<any, any, any, any, any>,
>(service: TService) {
>(service: TService, customTrpc?: ReturnType<typeof getTrpc>) {
if (!service.resolver) {
throw new Error('Resolver not defined');
}
Expand All @@ -96,6 +96,7 @@ export function createTrpcProcedureFromService<

const inputSchema = service.inputSchema ?? z.void();
const t =
customTrpc ??
getTrpc<
TService['middleware'] extends object ? TService['middleware'] : object
>();
Expand All @@ -118,7 +119,7 @@ export function createTrpcProcedureFromService<

export function createManyTrpcProceduresFromServices<
TServiceMap extends Record<string, Service<any, any, any, any, any>>,
>(serviceMap: TServiceMap) {
>(serviceMap: TServiceMap, customTrpc?: ReturnType<typeof getTrpc>) {
type ServiceKeys = keyof TServiceMap;
type Procedures = {
[K in ServiceKeys]: ReturnType<
Expand All @@ -128,7 +129,8 @@ export function createManyTrpcProceduresFromServices<
const procedures = Object.entries(serviceMap).reduce(
(acc, [key, service]) => {
acc[key as ServiceKeys] = createTrpcProcedureFromService(
service
service,
customTrpc
) as Procedures[ServiceKeys];
return acc;
},
Expand All @@ -140,9 +142,10 @@ export function createManyTrpcProceduresFromServices<

export function createTrpcRouterFromServices<
TServiceMap extends Record<string, Service<any, any, any, any, any>>,
>(serviceMap: TServiceMap) {
const procedures = createManyTrpcProceduresFromServices(serviceMap);
return getTrpc().router(procedures);
>(serviceMap: TServiceMap, customTrpc?: ReturnType<typeof getTrpc>) {
const t = customTrpc ?? getTrpc();
const procedures = createManyTrpcProceduresFromServices(serviceMap, t);
return t.router(procedures);
}

// export function createServicesRouter<
Expand Down

0 comments on commit 8a7c531

Please sign in to comment.