Skip to content

Commit

Permalink
create example of logging result
Browse files Browse the repository at this point in the history
  • Loading branch information
mdtanrikulu committed Dec 18, 2023
1 parent b1e3624 commit 25a9b9c
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,22 @@ const routeHandler = (env: ENV, trackEvent?: Function) => {
return app;
};

const logResult = async (request, result: Response) => {
if (!result.body) {
return result;
}
const [streamForLog, streamForResult] = result.body.tee();
const logResult = await new Response(streamForLog).json();

await tracker.trackEvent(
request,
'result',
{ props: { result: logResult.data.substring(0, 200) } },
true
);
return new Response(streamForResult, result);
};

module.exports = {
fetch: async function(
request: CFWRequest,
Expand All @@ -36,6 +52,6 @@ module.exports = {
await tracker.trackEvent(request, 'request', {}, true);
await tracker.trackPageview(request, {}, true);
const router = routeHandler(env, tracker.trackEvent.bind(tracker, request));
return await router.handle(request);
return router.handle(request).then(logResult.bind(this, request));
},
};

0 comments on commit 25a9b9c

Please sign in to comment.