@orpc/experimental-pino 0.0.0 → 0.0.1

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.
package/dist/index.d.mts CHANGED
@@ -20,17 +20,18 @@ interface LoggingHandlerPluginOptions<T extends Context> {
20
20
  *
21
21
  * @default () => crypto.randomUUID()
22
22
  */
23
- generateId?: (logger: Logger, options: StandardHandlerInterceptorOptions<T>) => string;
23
+ generateId?: (options: StandardHandlerInterceptorOptions<T>) => string;
24
24
  /**
25
- * Enables info-level lifecycle logging for each request.
25
+ * Enables logging for request/response events.
26
26
  *
27
27
  * @example
28
28
  * - request received
29
29
  * - request handled
30
+ * - no matching procedure found
30
31
  *
31
32
  * @default undefined (disabled)
32
33
  */
33
- logRequestLifecycle?: boolean;
34
+ logRequestResponse?: boolean;
34
35
  /**
35
36
  * Enables logging when requests are aborted.
36
37
  *
@@ -41,13 +42,13 @@ interface LoggingHandlerPluginOptions<T extends Context> {
41
42
  *
42
43
  * @default undefined (disabled)
43
44
  */
44
- logAbort?: boolean;
45
+ logRequestAbort?: boolean;
45
46
  }
46
47
  declare class LoggingHandlerPlugin<T extends Context> implements StandardHandlerPlugin<T> {
47
48
  private readonly logger;
48
49
  private readonly generateId;
49
- private readonly logRequestLifecycle;
50
- private readonly logAbort;
50
+ private readonly logRequestResponse;
51
+ private readonly logRequestAbort;
51
52
  constructor(options?: LoggingHandlerPluginOptions<T>);
52
53
  init(options: StandardHandlerOptions<T>): void;
53
54
  }
package/dist/index.d.ts CHANGED
@@ -20,17 +20,18 @@ interface LoggingHandlerPluginOptions<T extends Context> {
20
20
  *
21
21
  * @default () => crypto.randomUUID()
22
22
  */
23
- generateId?: (logger: Logger, options: StandardHandlerInterceptorOptions<T>) => string;
23
+ generateId?: (options: StandardHandlerInterceptorOptions<T>) => string;
24
24
  /**
25
- * Enables info-level lifecycle logging for each request.
25
+ * Enables logging for request/response events.
26
26
  *
27
27
  * @example
28
28
  * - request received
29
29
  * - request handled
30
+ * - no matching procedure found
30
31
  *
31
32
  * @default undefined (disabled)
32
33
  */
33
- logRequestLifecycle?: boolean;
34
+ logRequestResponse?: boolean;
34
35
  /**
35
36
  * Enables logging when requests are aborted.
36
37
  *
@@ -41,13 +42,13 @@ interface LoggingHandlerPluginOptions<T extends Context> {
41
42
  *
42
43
  * @default undefined (disabled)
43
44
  */
44
- logAbort?: boolean;
45
+ logRequestAbort?: boolean;
45
46
  }
46
47
  declare class LoggingHandlerPlugin<T extends Context> implements StandardHandlerPlugin<T> {
47
48
  private readonly logger;
48
49
  private readonly generateId;
49
- private readonly logRequestLifecycle;
50
- private readonly logAbort;
50
+ private readonly logRequestResponse;
51
+ private readonly logRequestAbort;
51
52
  constructor(options?: LoggingHandlerPluginOptions<T>);
52
53
  init(options: StandardHandlerOptions<T>): void;
53
54
  }
package/dist/index.mjs CHANGED
@@ -10,58 +10,79 @@ function getLogger(context) {
10
10
  class LoggingHandlerPlugin {
11
11
  logger;
12
12
  generateId;
13
- logRequestLifecycle;
14
- logAbort;
13
+ logRequestResponse;
14
+ logRequestAbort;
15
15
  constructor(options = {}) {
16
16
  this.logger = options.logger ?? pino();
17
17
  this.generateId = options.generateId ?? (() => crypto.randomUUID());
18
- this.logRequestLifecycle = options.logRequestLifecycle;
19
- this.logAbort = options.logAbort;
18
+ this.logRequestResponse = options.logRequestResponse;
19
+ this.logRequestAbort = options.logRequestAbort;
20
20
  }
21
21
  init(options) {
22
22
  options.rootInterceptors ??= [];
23
23
  options.interceptors ??= [];
24
24
  options.clientInterceptors ??= [];
25
25
  options.rootInterceptors.unshift(async (interceptorOptions) => {
26
- const logger = (interceptorOptions.context[CONTEXT_LOGGER_SYMBOL] ?? this.logger).child({});
27
- const currentBindings = logger.bindings();
28
- const extraBindings = {
29
- orpc: { ...currentBindings.orpc, id: this.generateId(logger, interceptorOptions) }
30
- };
31
- if (!currentBindings.req) {
32
- extraBindings.req = { url: interceptorOptions.request.url, method: interceptorOptions.request.method };
26
+ const logger = (interceptorOptions.context[CONTEXT_LOGGER_SYMBOL] ?? this.logger).child({
27
+ orpc: { id: this.generateId(interceptorOptions) }
28
+ });
29
+ if (!logger.bindings().req) {
30
+ logger.setBindings({
31
+ req: {
32
+ url: interceptorOptions.request.url,
33
+ method: interceptorOptions.request.method,
34
+ headers: {
35
+ "content-type": interceptorOptions.request.headers["content-type"],
36
+ "content-length": interceptorOptions.request.headers["content-length"],
37
+ "content-disposition": interceptorOptions.request.headers["content-disposition"]
38
+ }
39
+ }
40
+ });
41
+ }
42
+ if (this.logRequestAbort) {
43
+ const signal = interceptorOptions.request.signal;
44
+ if (signal?.aborted) {
45
+ logger?.info(`request was aborted before handling (${String(signal.reason)})`);
46
+ } else {
47
+ signal?.addEventListener("abort", () => {
48
+ logger?.info(`request is aborted (${String(signal.reason)})`);
49
+ }, { once: true });
50
+ }
33
51
  }
34
- logger.setBindings(extraBindings);
35
52
  try {
36
- return await interceptorOptions.next({
53
+ if (this.logRequestResponse) {
54
+ logger?.info("request received");
55
+ }
56
+ const result = await interceptorOptions.next({
37
57
  ...interceptorOptions,
38
58
  context: {
39
59
  ...interceptorOptions.context,
40
60
  [CONTEXT_LOGGER_SYMBOL]: logger
41
61
  }
42
62
  });
63
+ if (this.logRequestResponse) {
64
+ if (result.matched) {
65
+ logger?.info({
66
+ msg: "request handled",
67
+ res: {
68
+ status: result.response.status
69
+ }
70
+ });
71
+ } else {
72
+ logger?.info("no matching procedure found");
73
+ }
74
+ }
75
+ return result;
43
76
  } catch (error) {
44
77
  this.logger.error(error);
45
78
  throw error;
46
79
  }
47
80
  });
48
81
  options.interceptors.unshift(async ({ next, context, request }) => {
49
- const logger = getLogger(context);
50
- if (this.logAbort) {
51
- request.signal?.addEventListener("abort", () => {
52
- logger?.info(`request is aborted (${String(request.signal?.reason)})`);
53
- }, { once: true });
54
- }
55
82
  try {
56
- if (this.logRequestLifecycle) {
57
- logger?.info("request received");
58
- }
59
- const result = await next();
60
- if (this.logRequestLifecycle) {
61
- logger?.info(result.matched ? "request handled" : "no matching procedure found");
62
- }
63
- return result;
83
+ return await next();
64
84
  } catch (error) {
85
+ const logger = getLogger(context);
65
86
  if (request.signal?.aborted && request.signal.reason === error) {
66
87
  logger?.info(error);
67
88
  } else {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@orpc/experimental-pino",
3
3
  "type": "module",
4
- "version": "0.0.0",
4
+ "version": "0.0.1",
5
5
  "license": "MIT",
6
6
  "homepage": "https://orpc.unnoq.com",
7
7
  "repository": {
@@ -27,9 +27,9 @@
27
27
  "pino": ">=10.1.0"
28
28
  },
29
29
  "dependencies": {
30
- "@orpc/client": "1.10.4",
30
+ "@orpc/server": "1.10.4",
31
31
  "@orpc/shared": "1.10.4",
32
- "@orpc/server": "1.10.4"
32
+ "@orpc/client": "1.10.4"
33
33
  },
34
34
  "devDependencies": {
35
35
  "pino": "^10.1.0"