@digitraffic/common 2026.10.26-1 → 2026.10.27-2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,4 @@
1
+ import type { Hooks } from "ky";
1
2
  import type { Dispatcher } from "undici";
2
3
  /**
3
4
  * Creates request loggers that share integration-level context.
@@ -16,7 +17,7 @@ export declare class LoggerFactory {
16
17
  urlTemplate?: string;
17
18
  });
18
19
  /** Creates a timer and logger for one outbound HTTP request. */
19
- logger(url: string): HttpLogger;
20
+ logger(url: string, urlTemplate?: string): HttpLogger;
20
21
  }
21
22
  /**
22
23
  * Logs the outcome and duration of one outbound HTTP request.
@@ -43,7 +44,9 @@ export declare class HttpLogger {
43
44
  });
44
45
  /** Records a received HTTP response and writes the request log entry. */
45
46
  end(statusCode: number, contentLength?: number): void;
46
- /** Hook logging into response events */
47
+ /** Create hooks for Ky HTTP client using the provided logger */
48
+ createKyHooks(): Hooks;
49
+ /** Hook logging into undici response events */
47
50
  log(response: Dispatcher.ResponseData<unknown>): void;
48
51
  /**
49
52
  * Records a failed request and writes the request log entry.
@@ -18,12 +18,12 @@ export class LoggerFactory {
18
18
  this._urlTemplate = parameters.urlTemplate;
19
19
  }
20
20
  /** Creates a timer and logger for one outbound HTTP request. */
21
- logger(url) {
21
+ logger(url, urlTemplate) {
22
22
  return new HttpLogger({
23
23
  context: this._context,
24
24
  url,
25
25
  traceId: this._traceId,
26
- urlTemplate: this._urlTemplate,
26
+ urlTemplate: urlTemplate ?? this._urlTemplate,
27
27
  });
28
28
  }
29
29
  }
@@ -57,7 +57,34 @@ export class HttpLogger {
57
57
  this._contentLength = contentLength;
58
58
  this.logStatus();
59
59
  }
60
- /** Hook logging into response events */
60
+ /** Create hooks for Ky HTTP client using the provided logger */
61
+ createKyHooks() {
62
+ return {
63
+ afterResponse: [
64
+ async (state) => {
65
+ const contentLengthHeader = state.response.headers.get("content-length");
66
+ // use header when available to determine content length
67
+ if (contentLengthHeader) {
68
+ const contentLength = parseInt(contentLengthHeader, 10);
69
+ this.end(state.response.status, contentLength);
70
+ return state.response;
71
+ }
72
+ // create clone from the response, so we can read its body without affecting the original response
73
+ const resp = state.response.clone();
74
+ const contentLength = (await resp.bytes()).length;
75
+ this.end(resp.status, contentLength);
76
+ return state.response;
77
+ },
78
+ ],
79
+ beforeError: [
80
+ (state) => {
81
+ this.error(state.error);
82
+ return state.error;
83
+ },
84
+ ],
85
+ };
86
+ }
87
+ /** Hook logging into undici response events */
61
88
  log(response) {
62
89
  this._statusCode = response.statusCode;
63
90
  const contentLengthHeader = response.headers["content-length"];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@digitraffic/common",
3
- "version": "2026.10.26-1",
3
+ "version": "2026.10.27-2",
4
4
  "private": false,
5
5
  "description": "",
6
6
  "repository": {