@openapi-typescript-infra/service 4.6.3 → 4.7.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.
@@ -2,7 +2,8 @@
2
2
  "$schema": "tsschema://../src/config/schema#ConfigurationSchema",
3
3
  "logging": {
4
4
  "level": "info",
5
- "logRequestBody": "env_switch:LOG_HTTP_REQUESTS"
5
+ "logRequestBody": "env_switch:LOG_HTTP_REQUESTS",
6
+ "preLog": "env_switch:HTTP_PRE_LOG"
6
7
  },
7
8
  "routing": {
8
9
  "openapi": true,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openapi-typescript-infra/service",
3
- "version": "4.6.3",
3
+ "version": "4.7.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": {
@@ -91,31 +91,31 @@
91
91
  "lodash": "^4.17.21",
92
92
  "minimist": "^1.2.8",
93
93
  "opentelemetry-instrumentation-fetch-node": "^1.1.0",
94
- "pino": "^8.16.0",
94
+ "pino": "^8.16.1",
95
95
  "read-pkg-up": "^7.0.1"
96
96
  },
97
97
  "devDependencies": {
98
- "@commitlint/cli": "^18.0.0",
99
- "@commitlint/config-conventional": "^18.0.0",
98
+ "@commitlint/cli": "^18.2.0",
99
+ "@commitlint/config-conventional": "^18.1.0",
100
100
  "@openapi-typescript-infra/coconfig": "^4.2.2",
101
101
  "@semantic-release/changelog": "^6.0.3",
102
- "@semantic-release/commit-analyzer": "^11.0.0",
102
+ "@semantic-release/commit-analyzer": "^11.1.0",
103
103
  "@semantic-release/exec": "^6.0.3",
104
104
  "@semantic-release/git": "^10.0.1",
105
- "@semantic-release/release-notes-generator": "^12.0.0",
105
+ "@semantic-release/release-notes-generator": "^12.1.0",
106
106
  "@types/cookie-parser": "^1.4.5",
107
107
  "@types/express": "^4.17.20",
108
108
  "@types/glob": "^8.1.0",
109
109
  "@types/lodash": "^4.14.200",
110
110
  "@types/minimist": "^1.2.4",
111
- "@types/node": "^20.8.7",
111
+ "@types/node": "^20.8.10",
112
112
  "@types/supertest": "^2.0.15",
113
- "@typescript-eslint/eslint-plugin": "^6.8.0",
114
- "@typescript-eslint/parser": "^6.8.0",
113
+ "@typescript-eslint/eslint-plugin": "^6.10.0",
114
+ "@typescript-eslint/parser": "^6.10.0",
115
115
  "coconfig": "^1.0.0",
116
- "eslint": "^8.52.0",
116
+ "eslint": "^8.53.0",
117
117
  "eslint-config-prettier": "^9.0.0",
118
- "eslint-plugin-import": "^2.28.1",
118
+ "eslint-plugin-import": "^2.29.0",
119
119
  "pino-pretty": "^10.2.3",
120
120
  "pinst": "^3.0.0",
121
121
  "supertest": "^6.3.3",
@@ -11,6 +11,9 @@ export interface ConfigurationSchema extends BaseConfitSchema {
11
11
  level?: Level;
12
12
  logRequestBody?: boolean;
13
13
  logResponseBody?: boolean;
14
+ // Whether to log a "pre" message when request processing starts. Most useful in
15
+ // situations where there is some problem causing requests to hang
16
+ preLog?: boolean;
14
17
  };
15
18
  routing?: {
16
19
  openapi?: boolean;
@@ -124,7 +124,7 @@ export async function startApp<
124
124
  description: 'Duration of HTTP requests in seconds',
125
125
  });
126
126
 
127
- app.use(loggerMiddleware(app, histogram, logging?.logRequestBody, logging?.logResponseBody));
127
+ app.use(loggerMiddleware(app, histogram, logging));
128
128
 
129
129
  // Allow the service to add locals, etc. We put this before the body parsers
130
130
  // so that the req can decide whether to save the raw request body or not.
@@ -108,21 +108,20 @@ export function loggerMiddleware<
108
108
  >(
109
109
  app: ServiceExpress<SLocals>,
110
110
  histogram: Histogram,
111
- logRequests?: boolean,
112
- logResponses?: boolean,
111
+ config?: ConfigurationSchema['logging'],
113
112
  ): RequestHandler {
114
113
  const { logger, service } = app.locals;
115
114
  return function gblogger(req, res, next) {
116
115
  const prefs: LogPrefs = {
117
116
  start: process.hrtime(),
118
- logRequests,
119
- chunks: logResponses ? [] : undefined,
117
+ logRequests: config?.logRequestBody,
118
+ chunks: config?.logResponseBody ? [] : undefined,
120
119
  logged: false,
121
120
  };
122
121
 
123
122
  (res.locals as WithLogPrefs)[LOG_PREFS] = prefs;
124
123
 
125
- if (logResponses) {
124
+ if (config?.logResponseBody) {
126
125
  // res is a read-only stream, so the only way to intercept response
127
126
  // data is to monkey-patch.
128
127
  const oldWrite = res.write;
@@ -141,14 +140,16 @@ export function loggerMiddleware<
141
140
  }) as (typeof res)['end'];
142
141
  }
143
142
 
144
- const preLog: Record<string, string | string[] | number | undefined> = {
145
- ...getBasicInfo(req),
146
- ref: req.headers.referer || undefined,
147
- sid: (req as WithIdentifiedSession).session?.id,
148
- c: req.headers.correlationid || undefined,
149
- };
150
- service.getLogFields?.(req as RequestWithApp<SLocals>, preLog);
151
- logger.info(preLog, 'pre');
143
+ if (config?.preLog) {
144
+ const preLog: Record<string, string | string[] | number | undefined> = {
145
+ ...getBasicInfo(req),
146
+ ref: req.headers.referer || undefined,
147
+ sid: (req as WithIdentifiedSession).session?.id,
148
+ c: req.headers.correlationid || undefined,
149
+ };
150
+ service.getLogFields?.(req as RequestWithApp<SLocals>, preLog);
151
+ logger.info(preLog, 'pre');
152
+ }
152
153
 
153
154
  const logWriter = () => finishLog(app, undefined, req, res, histogram);
154
155
  res.on('finish', logWriter);