Skip to content

Commit

Permalink
feat: add undici fetch interceptor (#687)
Browse files Browse the repository at this point in the history
Co-authored-by: Meier Lukas <meierschlumpf@gmail.com>
  • Loading branch information
manuel-rw and Meierschlumpf committed Jun 19, 2024
1 parent bf60734 commit 508369c
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 1 deletion.
3 changes: 2 additions & 1 deletion apps/tasks/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
"@homarr/analytics": "workspace:^0.1.0",
"dotenv": "^16.4.5",
"node-cron": "^3.0.3",
"superjson": "2.2.1"
"superjson": "2.2.1",
"undici": "6.18.2"
},
"devDependencies": {
"@homarr/eslint-config": "workspace:^0.2.0",
Expand Down
3 changes: 3 additions & 0 deletions apps/tasks/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// This import has to be the first import in the file so that the agent is overridden before any other modules are imported.
import "./undici-log-agent-override";

import { jobs } from "./jobs";
import { seedServerSettingsAsync } from "./seed-server-settings";

Expand Down
20 changes: 20 additions & 0 deletions apps/tasks/src/undici-log-agent-override.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import type { Dispatcher } from "undici";
import { Agent, setGlobalDispatcher } from "undici";

import { logger } from "@homarr/log";

class LoggingAgent extends Agent {
constructor(...props: ConstructorParameters<typeof Agent>) {
super(...props);
}

dispatch(options: Dispatcher.DispatchOptions, handler: Dispatcher.DispatchHandlers): boolean {
logger.info(
`Dispatching request ${options.method} ${options.origin as string}${options.path} (${Object.keys(options.headers as object).length} headers)`,
);
return super.dispatch(options, handler);
}
}

const agent = new LoggingAgent();
setGlobalDispatcher(agent);
9 changes: 9 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 508369c

Please sign in to comment.