11import { Static , Type } from '@sinclair/typebox' ;
22import { FastifyLoggerInstance } from 'fastify' ;
3+ import pino from 'pino' ;
34
4- export interface PubSubConfig {
5+ export interface PubSubConfig < Data , Context > {
56 /**
67 * Handler
78 */
8- handler : PubSubHandler ;
9+ handler : PubSubHandler < Data , Context > ;
910 /**
1011 * OnError Handler
1112 *
1213 * When this is set, errors will not be
1314 * thrown.
1415 */
1516 onError ?: OnErrorHandler ;
17+
18+ parser ?: ( data : unknown ) => Data | Promise < Data > ;
1619 /**
1720 * This will run JSON.parse on request data
1821 *
1922 * **Tip**: `false` when sending strings
2023 * @default true
2124 */
2225 parseJson ?: boolean ; // Defaults to true
26+
2327 /**
2428 * Use this to set a different path
2529 * @default /
@@ -46,11 +50,20 @@ export class PubSubHandlerResponse {
4650 statusCode ?: number ;
4751}
4852
49- export type PubSubHandler < T = any > = ( args : {
53+ export type PubSubHandler < Data , Context > = ( args : {
5054 message : PubSubMessageType ;
51- data ?: T ;
52- context ?: unknown ;
55+ data : Data ;
56+ context ?: Context ;
5357 log : FastifyLoggerInstance ;
5458} ) => Promise < PubSubHandlerResponse | void > | PubSubHandlerResponse | void ;
5559
5660export type OnErrorHandler = ( error : unknown ) => void | Promise < void > ;
61+
62+ export interface HandlePubSubMessageArgs < Data , Context > {
63+ message : PubSubMessageType ;
64+ handler : PubSubHandler < Data , Context > ;
65+ parseJson ?: boolean ;
66+ parser ?: ( data : unknown ) => Data ;
67+ context ?: Context ;
68+ log ?: pino . Logger ;
69+ }
0 commit comments