Skip to content

Commit

Permalink
feat: add createContext support & associated test
Browse files Browse the repository at this point in the history
  • Loading branch information
edorgeville committed May 24, 2024
1 parent 07baf65 commit 76ab8c8
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 8 deletions.
12 changes: 7 additions & 5 deletions src/adapter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@ export type CreateMQTTHandlerOptions<TRouter extends AnyRouter> = {
router: TRouter;
onError?: OnErrorFunction<TRouter, ConsumeMessage>;
verbose?: boolean;
createContext?: () => Promise<inferRouterContext<TRouter>>;
};

export const createMQTTHandler = <TRouter extends AnyRouter>(
opts: CreateMQTTHandlerOptions<TRouter>
) => {
const { client, requestTopic: requestTopic, router, onError, verbose } = opts;
const { client, requestTopic: requestTopic, router, onError, verbose, createContext } = opts;

const protocolVersion = client.options.protocolVersion ?? 4;
client.subscribe(requestTopic);
Expand All @@ -43,7 +44,7 @@ export const createMQTTHandler = <TRouter extends AnyRouter>(
const correlationId = packet.properties?.correlationData?.toString();
const responseTopic = packet.properties?.responseTopic?.toString();
if (!correlationId || !responseTopic) return;
const res = await handleMessage(router, msg, onError);
const res = await handleMessage(router, msg, onError, createContext);
if (!res) return;
client.publish(responseTopic, Buffer.from(JSON.stringify({ trpc: res })), {
properties: {
Expand All @@ -62,7 +63,7 @@ export const createMQTTHandler = <TRouter extends AnyRouter>(
return;
}
if (!correlationId || !responseTopic) return;
const res = await handleMessage(router, msg, onError);
const res = await handleMessage(router, msg, onError, createContext);
if (!res) return;
client.publish(responseTopic, Buffer.from(JSON.stringify({ trpc: res, correlationId })));
}
Expand All @@ -72,7 +73,8 @@ export const createMQTTHandler = <TRouter extends AnyRouter>(
async function handleMessage<TRouter extends AnyRouter>(
router: TRouter,
msg: ConsumeMessage,
onError?: OnErrorFunction<TRouter, ConsumeMessage>
onError?: OnErrorFunction<TRouter, ConsumeMessage>,
createContext?: () => Promise<inferRouterContext<TRouter>>
) {
const { transformer } = router._def._config;

Expand All @@ -85,7 +87,7 @@ async function handleMessage<TRouter extends AnyRouter>(

const { id, params } = trpc;
const type = MQTT_METHOD_PROCEDURE_TYPE_MAP[trpc.method] ?? ('query' as const);
const ctx: inferRouterContext<TRouter> | undefined = undefined;
const ctx: inferRouterContext<TRouter> | undefined = await createContext?.();

try {
const path = params.path;
Expand Down
11 changes: 10 additions & 1 deletion test/appRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ import { initTRPC } from '@trpc/server';

export type AppRouter = typeof appRouter;

const t = initTRPC.create();
export async function createContext() {
return { hello: 'world' };
}

export type Context = Awaited<ReturnType<typeof createContext>>;

const t = initTRPC.context<Context>().create();

const publicProcedure = t.procedure;
const router = t.router;
Expand All @@ -28,5 +34,8 @@ export const appRouter = router({
slow: publicProcedure.query(async () => {
await new Promise(resolve => setTimeout(resolve, 10 * 1000));
return 'done';
}),
getContext: publicProcedure.query(({ ctx }) => {
return ctx;
})
});
5 changes: 3 additions & 2 deletions test/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { createServer } from 'net';

import { createMQTTHandler } from '../src/adapter';
import { mqttLink } from '../src/link';
import { AppRouter, appRouter } from './appRouter';
import { type AppRouter, appRouter, createContext } from './appRouter';

export function factory() {
const requestTopic = 'rpc/request';
Expand All @@ -20,7 +20,8 @@ export function factory() {
createMQTTHandler({
client: mqttClient,
requestTopic,
router: appRouter
router: appRouter,
createContext
});

const client = createTRPCProxyClient<AppRouter>({
Expand Down
9 changes: 9 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,12 @@ describe('procedures', () => {
});
});
});

describe('context', () => {
test('getContext query', async () => {
await withFactory(async ({ client }) => {
const ctx = await client.getContext.query();
expect(ctx).toEqual({ hello: 'world' });
});
});
});

0 comments on commit 76ab8c8

Please sign in to comment.