@openapi-typescript-infra/service 4.13.1 → 4.15.0

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/build/types.d.ts CHANGED
@@ -49,7 +49,7 @@ export interface Service<SLocals extends AnyServiceLocals = ServiceLocals<Config
49
49
  healthy?: (app: ServiceExpress<SLocals>) => boolean | Promise<boolean>;
50
50
  onRequest?(req: RequestWithApp<SLocals>, res: Response<unknown, RLocals>): void | Promise<void>;
51
51
  authorize?(req: RequestWithApp<SLocals>, res: Response<unknown, RLocals>): boolean | Promise<boolean>;
52
- getLogFields?(req: RequestWithApp<SLocals>, values: Record<string, string | string[] | number | undefined>): void;
52
+ getLogFields?(req: RequestWithApp<SLocals>, values: Record<string, string | string[] | number | undefined>): string | undefined;
53
53
  attachRepl?(app: ServiceExpress<SLocals>, repl: REPLServer): void;
54
54
  }
55
55
  export type ServiceFactory<SLocals extends AnyServiceLocals = ServiceLocals<ConfigurationSchema>, RLocals extends RequestLocals = RequestLocals> = () => Service<SLocals, RLocals>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openapi-typescript-infra/service",
3
- "version": "4.13.1",
3
+ "version": "4.15.0",
4
4
  "description": "An opinionated framework for building configuration driven services - web, api, or ob. Uses OpenAPI, pino logging, express, confit, Typescript and vitest.",
5
5
  "main": "build/index.js",
6
6
  "scripts": {
@@ -122,7 +122,7 @@
122
122
  "ts-node": "^10.9.2",
123
123
  "tsconfig-paths": "^4.2.0",
124
124
  "typescript": "^5.3.3",
125
- "vitest": "^1.2.1"
125
+ "vitest": "^1.2.2"
126
126
  },
127
127
  "resolutions": {
128
128
  "qs": "^6.11.0"
@@ -31,11 +31,10 @@ interface ErrorWithStatus extends Error {
31
31
  status?: number;
32
32
  }
33
33
 
34
- function getBasicInfo(req: Request) {
34
+ function getBasicInfo(req: Request): [string, Record<string, string | number>] {
35
35
  const url = req.originalUrl || req.url;
36
36
 
37
37
  const preInfo: Record<string, string> = {
38
- url,
39
38
  ip: requestip.getClientIp(req) || '',
40
39
  m: req.method,
41
40
  };
@@ -45,7 +44,7 @@ function getBasicInfo(req: Request) {
45
44
  preInfo.sid = sessionReq.session.id;
46
45
  }
47
46
 
48
- return preInfo;
47
+ return [url, preInfo];
49
48
  }
50
49
 
51
50
  function finishLog<SLocals extends AnyServiceLocals = ServiceLocals<ConfigurationSchema>>(
@@ -65,8 +64,10 @@ function finishLog<SLocals extends AnyServiceLocals = ServiceLocals<Configuratio
65
64
  const hrdur = process.hrtime(prefs.start);
66
65
 
67
66
  const dur = hrdur[0] + hrdur[1] / 1000000000;
67
+ const [url, preInfo] = getBasicInfo(req);
68
68
  const endLog: Record<string, string | string[] | number | undefined> = {
69
- ...getBasicInfo(req),
69
+ ...preInfo,
70
+ t: 'req',
70
71
  s: (error as ErrorWithStatus)?.status || res.statusCode || 0,
71
72
  dur,
72
73
  };
@@ -107,8 +108,8 @@ function finishLog<SLocals extends AnyServiceLocals = ServiceLocals<Configuratio
107
108
  }
108
109
  }
109
110
 
110
- service.getLogFields?.(req as RequestWithApp<SLocals>, endLog);
111
- logger.info(endLog, 'req');
111
+ const msg = service.getLogFields?.(req as RequestWithApp<SLocals>, endLog) || url;
112
+ logger.info(endLog, msg);
112
113
  }
113
114
 
114
115
  export function loggerMiddleware<
@@ -153,14 +154,16 @@ export function loggerMiddleware<
153
154
  }
154
155
 
155
156
  if (config?.preLog) {
157
+ const [url, preInfo] = getBasicInfo(req);
156
158
  const preLog: Record<string, string | string[] | number | undefined> = {
157
- ...getBasicInfo(req),
159
+ ...preInfo,
160
+ t: 'pre',
158
161
  ref: req.headers.referer || undefined,
159
162
  sid: (req as WithIdentifiedSession).session?.id,
160
163
  c: req.headers.correlationid || undefined,
161
164
  };
162
- service.getLogFields?.(req as RequestWithApp<SLocals>, preLog);
163
- logger.info(preLog, 'pre');
165
+ const msg = service.getLogFields?.(req as RequestWithApp<SLocals>, preLog) || url;
166
+ logger.info(preLog, msg);
164
167
  }
165
168
 
166
169
  const logWriter = () => finishLog(app, undefined, req, res, histogram);
package/src/types.ts CHANGED
@@ -95,11 +95,13 @@ export interface Service<
95
95
  ): boolean | Promise<boolean>;
96
96
 
97
97
  // Add or redact any fields for logging. Note this will be called twice per request,
98
- // once at the start and once at the end. Modify the values directly.
98
+ // once at the start and once at the end. Modify the values directly. Return a
99
+ // string to control the "msg" field of the logs, or return undefined to leave it
100
+ // as the default, which is the request URL.
99
101
  getLogFields?(
100
102
  req: RequestWithApp<SLocals>,
101
103
  values: Record<string, string | string[] | number | undefined>,
102
- ): void;
104
+ ): string | undefined;
103
105
 
104
106
  // The repl is a useful tool for diagnosing issues in non-dev environments.
105
107
  // The attachRepl method provides a way to add custom functionality
@@ -195,11 +197,8 @@ export interface ServiceTypes<
195
197
  ServiceLocals: SLocals;
196
198
  }
197
199
 
198
- export type UnwrapServiceConfig<SLocals extends ServiceLocals> = SLocals extends ServiceLocals<
199
- infer C
200
- >
201
- ? C
202
- : never;
200
+ export type UnwrapServiceConfig<SLocals extends ServiceLocals> =
201
+ SLocals extends ServiceLocals<infer C> ? C : never;
203
202
 
204
203
  // TODO this allows us to clean up the generics by having a loose parameter
205
204
  // but using the UnwrapServiceConfig to get the specific type back