Skip to content

Commit dd367ed

Browse files
committed
fix: add correct types for catchUncaughtAction & catchUncaughtRoute
1 parent b9c04b3 commit dd367ed

File tree

2 files changed

+47
-21
lines changed

2 files changed

+47
-21
lines changed

packages/core/src/index.ts

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
response,
1111
} from "./request-utils"
1212
import { buildJsonLog, buildTextLog } from "./utils"
13+
import { AddContextFn } from "./types"
1314

1415
export type FlytrapLogsOptions = {
1516
/**
@@ -129,41 +130,66 @@ export function createFlytrapLogger<T>({
129130
getContext,
130131
addContext,
131132
flush,
132-
catchUncaughtAction<T extends (...args: unknown[]) => Promise<unknown>>(
133+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
134+
catchUncaughtAction<T extends (...args: any[]) => Promise<any>>(
133135
fn: T,
134136
options?: Partial<z.infer<typeof baseLogSchema>>
135137
) {
136-
// @ts-expect-error: `addContext` is incompatible due to `T`
137-
return catchUncaughtAction(fn, addContext, flush, options)
138+
return catchUncaughtAction(
139+
fn,
140+
addContext as AddContextFn<z.infer<typeof baseLogSchema>>,
141+
flush,
142+
options
143+
)
138144
},
139145
// Request utils
140-
catchUncaughtRoute<T extends { params: Record<string, unknown> }>(
141-
fn: (request: Request, context: T) => Promise<Response> | Response,
146+
catchUncaughtRoute<
147+
RequestType extends Request,
148+
T extends { params: Record<string, unknown> },
149+
>(
150+
fn: (request: RequestType, context: T) => Promise<Response> | Response,
142151
options?: Partial<z.infer<typeof baseLogSchema>>
143152
) {
144-
// @ts-expect-error: `addContext` is incompatible due to `T`
145-
return catchUncaughtRoute(fn, addContext, flush, options)
153+
return catchUncaughtRoute(
154+
fn,
155+
addContext as AddContextFn<z.infer<typeof baseLogSchema>>,
156+
flush,
157+
options
158+
)
146159
},
147160
parseJson(request: Request) {
148-
// @ts-expect-error: `addContext` is incompatible due to `T`
149-
return parseJson(request, addContext)
161+
return parseJson(
162+
request,
163+
addContext as AddContextFn<z.infer<typeof baseLogSchema>>
164+
)
150165
},
151166
parseText(request: Request) {
152-
// @ts-expect-error: `addContext` is incompatible due to `T`
153-
return parseText(request, addContext)
167+
return parseText(
168+
request,
169+
addContext as AddContextFn<z.infer<typeof baseLogSchema>>
170+
)
154171
},
155172
// Response utils
156173
response(body: BodyInit, opts: ResponseInit = {}) {
157-
// @ts-expect-error: `addContext` is incompatible due to `T`
158-
return response(body, opts, addContext)
174+
return response(
175+
body,
176+
opts,
177+
addContext as AddContextFn<z.infer<typeof baseLogSchema>>
178+
)
159179
},
160180
json(data: unknown, opts: ResponseInit = {}) {
161-
// @ts-expect-error: `addContext` is incompatible due to `T`
162-
return json(data, opts, addContext)
181+
return json(
182+
data,
183+
opts,
184+
addContext as AddContextFn<z.infer<typeof baseLogSchema>>
185+
)
163186
},
164187
redirect(url: string | URL, status?: number) {
165-
// @ts-expect-error: `addContext` is incompatible due to `T`
166-
return redirect(url, status, addContext)
188+
return redirect(
189+
url,
190+
status,
191+
addContext as AddContextFn<z.infer<typeof baseLogSchema>>
192+
)
167193
},
168194
}
169195
}

packages/core/src/request-utils.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,10 @@ export async function parseText(
7777
}
7878

7979
export function catchUncaughtRoute<
80+
RequestType extends Request,
8081
T extends { params: Record<string, unknown> },
8182
>(
82-
fn: (request: Request, context: T) => Promise<Response> | Response,
83+
fn: (request: RequestType, context: T) => Promise<Response> | Response,
8384
addContext: AddContextFn<z.infer<typeof baseLogSchema>>,
8485
flush: FlushFn,
8586
options?: Partial<z.infer<typeof baseLogSchema>>
@@ -125,9 +126,8 @@ export function catchUncaughtRoute<
125126
}
126127
}
127128

128-
export function catchUncaughtAction<
129-
T extends (...args: unknown[]) => Promise<unknown>,
130-
>(
129+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
130+
export function catchUncaughtAction<T extends (...args: any[]) => Promise<any>>(
131131
fn: T,
132132
addContext: AddContextFn<z.infer<typeof baseLogSchema>>,
133133
flush: FlushFn,

0 commit comments

Comments
 (0)