File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 44 catchUncaughtAction ,
55 catchUncaughtRoute ,
66 json ,
7+ parseJson ,
8+ parseText ,
79 redirect ,
810 response ,
911} from "./request-utils"
@@ -134,13 +136,23 @@ export function createFlytrapLogger<T>({
134136 // @ts -expect-error: `addContext` is incompatible due to `T`
135137 return catchUncaughtAction ( fn , addContext , flush , options )
136138 } ,
139+ // Request utils
137140 catchUncaughtRoute < T extends { params : Record < string , unknown > } > (
138141 fn : ( request : Request , context : T ) => Promise < Response > | Response ,
139142 options ?: Partial < z . infer < typeof baseLogSchema > >
140143 ) {
141144 // @ts -expect-error: `addContext` is incompatible due to `T`
142145 return catchUncaughtRoute ( fn , addContext , flush , options )
143146 } ,
147+ parseJson ( request : Request ) {
148+ // @ts -expect-error: `addContext` is incompatible due to `T`
149+ return parseJson ( request , addContext )
150+ } ,
151+ parseText ( request : Request ) {
152+ // @ts -expect-error: `addContext` is incompatible due to `T`
153+ return parseText ( request , addContext )
154+ } ,
155+ // Response utils
144156 response ( body : BodyInit , opts : ResponseInit = { } ) {
145157 // @ts -expect-error: `addContext` is incompatible due to `T`
146158 return response ( body , opts , addContext )
Original file line number Diff line number Diff line change @@ -40,6 +40,42 @@ export function redirect(
4040 return Response . redirect ( url , status )
4141}
4242
43+ export async function parseJson (
44+ request : Request ,
45+ addContext : AddContextFn < z . infer < typeof baseLogSchema > >
46+ ) {
47+ try {
48+ const requestBody = await request . json ( )
49+ addContext ( {
50+ req : requestBody ,
51+ } )
52+ return requestBody
53+ } catch ( error ) {
54+ addContext ( {
55+ error : serializeError ( error ) ,
56+ } )
57+ throw error
58+ }
59+ }
60+
61+ export async function parseText (
62+ request : Request ,
63+ addContext : AddContextFn < z . infer < typeof baseLogSchema > >
64+ ) {
65+ try {
66+ const requestBody = await request . text ( )
67+ addContext ( {
68+ req : requestBody ,
69+ } )
70+ return requestBody
71+ } catch ( error ) {
72+ addContext ( {
73+ error : serializeError ( error ) ,
74+ } )
75+ throw error
76+ }
77+ }
78+
4379export function catchUncaughtRoute <
4480 T extends { params : Record < string , unknown > } ,
4581> (
You can’t perform that action at this time.
0 commit comments