@@ -23,13 +23,24 @@ export interface JobMeta
2323 readonly nextRepetition ?: Date ;
2424}
2525
26+ export type QuirrelLogger < Payload = unknown > = {
27+ receivedJob : ( route : string , data : Payload ) => void ;
28+ processingError : ( route : string , data : Payload , error : unknown ) => void ;
29+ } ;
30+
31+ const defaultLogger : QuirrelLogger = {
32+ receivedJob : ( route , data ) => console . log ( `Received job to ${ route } ` , data ) ,
33+ processingError : ( route , data , error ) =>
34+ console . error ( `Error in job at ${ route } ` , data , error ) ,
35+ } ;
36+
2637export type QuirrelJobHandler < T > = ( job : T , meta : JobMeta ) => Promise < void > ;
2738export type DefaultJobOptions = Pick < EnqueueJobOptions , "exclusive" | "retry" > ;
2839
2940interface CreateQuirrelClientArgs < T > {
3041 route : string ;
3142 handler : QuirrelJobHandler < T > ;
32- defaultJobOptions ?: DefaultJobOptions ;
43+ options ?: QuirrelOptions < T > ;
3344 config ?: {
3445 /**
3546 * Recommended way to set this: process.env.QUIRREL_BASE_URL
@@ -137,10 +148,6 @@ const EnqueueJobOptionsSchema = z.object({
137148
138149type EnqueueJobOptionsSchema = z . TypeOf < typeof EnqueueJobOptionsSchema > ;
139150
140- type EnqueueJobOptionssSchemaMatchesDocs = AssertTrue <
141- IsExact < EnqueueJobOptions , EnqueueJobOptionsSchema >
142- > ;
143-
144151/**
145152 * @deprecated renamed to EnqueueJobOptions
146153 */
@@ -250,6 +257,10 @@ function getAuthHeaders(
250257 return { Authorization : `Bearer ${ token } ` } ;
251258}
252259
260+ export interface QuirrelOptions < T = unknown > extends DefaultJobOptions {
261+ logger ?: QuirrelLogger < T > ;
262+ }
263+
253264export class QuirrelClient < T > {
254265 private handler ;
255266 private route ;
@@ -265,17 +276,19 @@ export class QuirrelClient<T> {
265276 private fetch ;
266277 private catchDecryptionErrors ;
267278 private signaturePublicKey ;
279+ private logger : QuirrelLogger < T > ;
268280
269281 constructor ( args : CreateQuirrelClientArgs < T > ) {
270282 this . handler = args . handler ;
271- this . defaultJobOptions = args . defaultJobOptions ;
283+ this . defaultJobOptions = args . options ;
272284
273285 const token = args . config ?. token ?? config . getQuirrelToken ( ) ;
274286 this . defaultHeaders = {
275287 ...getAuthHeaders ( token ) ,
276288 "X-QuirrelClient-Version" : pack . version ,
277289 } ;
278290
291+ this . logger = args . options ?. logger ?? defaultLogger ;
279292 const quirrelBaseUrl =
280293 args . config ?. quirrelBaseUrl ?? config . getQuirrelBaseUrl ( ) ;
281294 this . applicationBaseUrl = config . withoutTrailingSlash (
@@ -691,7 +704,7 @@ export class QuirrelClient<T> {
691704 ( headers [ "x-quirrel-meta" ] as string ) ?? "{}"
692705 ) ;
693706
694- console . log ( `Received job to ${ this . route } : ` , payload ) ;
707+ this . logger . receivedJob ( this . route , payload ) ;
695708
696709 try {
697710 await this . handler ( payload , {
@@ -708,7 +721,7 @@ export class QuirrelClient<T> {
708721 body : "OK" ,
709722 } ;
710723 } catch ( error ) {
711- console . error ( error ) ;
724+ this . logger . processingError ( this . route , payload , error ) ;
712725 return {
713726 status : 500 ,
714727 headers : { } ,
0 commit comments