@openapi-typescript-infra/service 4.6.3 → 4.8.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/.trunk/trunk.yaml +5 -5
- package/CHANGELOG.md +14 -0
- package/build/config/index.d.ts +3 -3
- package/build/config/index.js +2 -4
- package/build/config/index.js.map +1 -1
- package/build/config/schema.d.ts +1 -0
- package/build/express-app/app.js +5 -3
- package/build/express-app/app.js.map +1 -1
- package/build/telemetry/requestLogger.d.ts +1 -1
- package/build/telemetry/requestLogger.js +14 -12
- package/build/telemetry/requestLogger.js.map +1 -1
- package/build/tsconfig.build.tsbuildinfo +1 -1
- package/build/types.d.ts +2 -0
- package/config/config.json +2 -1
- package/package.json +11 -11
- package/src/config/index.ts +10 -9
- package/src/config/schema.ts +3 -0
- package/src/express-app/app.ts +5 -3
- package/src/telemetry/requestLogger.ts +14 -13
- package/src/types.ts +3 -0
package/build/types.d.ts
CHANGED
|
@@ -8,6 +8,7 @@ import type { Request, Response } from 'express';
|
|
|
8
8
|
import type { Application } from 'express-serve-static-core';
|
|
9
9
|
import type { middleware } from 'express-openapi-validator';
|
|
10
10
|
import type { Meter } from '@opentelemetry/api';
|
|
11
|
+
import { ShortstopHandler } from '@sesamecare-oss/confit';
|
|
11
12
|
import { ConfigurationSchema } from './config/schema';
|
|
12
13
|
export interface InternalLocals<SLocals extends AnyServiceLocals = ServiceLocals<ConfigurationSchema>> extends Record<string, unknown> {
|
|
13
14
|
server?: Server;
|
|
@@ -66,6 +67,7 @@ export interface DelayLoadServiceStartOptions extends Omit<ServiceStartOptions,
|
|
|
66
67
|
export interface ServiceOptions {
|
|
67
68
|
configurationDirectories: string[];
|
|
68
69
|
openApiOptions?: Parameters<typeof middleware>[0];
|
|
70
|
+
shortstopHandlers: Record<string, ShortstopHandler<string, unknown>>;
|
|
69
71
|
}
|
|
70
72
|
export interface ServiceLike<SLocals extends AnyServiceLocals = ServiceLocals<ConfigurationSchema>> {
|
|
71
73
|
locals: SLocals;
|
package/config/config.json
CHANGED
|
@@ -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.
|
|
3
|
+
"version": "4.8.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.
|
|
94
|
+
"pino": "^8.16.1",
|
|
95
95
|
"read-pkg-up": "^7.0.1"
|
|
96
96
|
},
|
|
97
97
|
"devDependencies": {
|
|
98
|
-
"@commitlint/cli": "^18.
|
|
99
|
-
"@commitlint/config-conventional": "^18.
|
|
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.
|
|
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.
|
|
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.
|
|
111
|
+
"@types/node": "^20.8.10",
|
|
112
112
|
"@types/supertest": "^2.0.15",
|
|
113
|
-
"@typescript-eslint/eslint-plugin": "^6.
|
|
114
|
-
"@typescript-eslint/parser": "^6.
|
|
113
|
+
"@typescript-eslint/eslint-plugin": "^6.10.0",
|
|
114
|
+
"@typescript-eslint/parser": "^6.10.0",
|
|
115
115
|
"coconfig": "^1.0.0",
|
|
116
|
-
"eslint": "^8.
|
|
116
|
+
"eslint": "^8.53.0",
|
|
117
117
|
"eslint-config-prettier": "^9.0.0",
|
|
118
|
-
"eslint-plugin-import": "^2.
|
|
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",
|
package/src/config/index.ts
CHANGED
|
@@ -1,11 +1,16 @@
|
|
|
1
1
|
import fs from 'fs';
|
|
2
2
|
import path from 'path';
|
|
3
3
|
|
|
4
|
-
import {
|
|
4
|
+
import {
|
|
5
|
+
BaseConfitSchema,
|
|
6
|
+
Confit,
|
|
7
|
+
Factory,
|
|
8
|
+
ShortstopHandler,
|
|
9
|
+
confit,
|
|
10
|
+
} from '@sesamecare-oss/confit';
|
|
5
11
|
|
|
6
12
|
import { findPort } from '../development/port-finder';
|
|
7
13
|
|
|
8
|
-
import { shortstops } from './shortstops';
|
|
9
14
|
import type { ConfigurationSchema } from './schema';
|
|
10
15
|
|
|
11
16
|
// Order matters here.
|
|
@@ -51,27 +56,23 @@ async function addDefaultConfiguration<Config extends ConfigurationSchema = Conf
|
|
|
51
56
|
}
|
|
52
57
|
|
|
53
58
|
export interface ServiceConfigurationSpec {
|
|
54
|
-
// Used for "sourcerequire" and other source-relative paths and for the package name
|
|
55
|
-
sourceDirectory: string;
|
|
56
59
|
// The LAST configuration is the most "specific" - if a configuration value
|
|
57
60
|
// exists in all directories, the last one wins
|
|
58
61
|
configurationDirectories: string[];
|
|
59
|
-
|
|
62
|
+
shortstopHandlers: Record<string, ShortstopHandler<string, unknown>>;
|
|
60
63
|
}
|
|
61
64
|
|
|
62
65
|
export async function loadConfiguration<Config extends ConfigurationSchema>({
|
|
63
|
-
name,
|
|
64
66
|
configurationDirectories: dirs,
|
|
65
|
-
|
|
67
|
+
shortstopHandlers,
|
|
66
68
|
}: ServiceConfigurationSpec): Promise<Config> {
|
|
67
|
-
const defaultProtocols = shortstops({ name }, sourceDirectory);
|
|
68
69
|
const specificConfig = dirs[dirs.length - 1];
|
|
69
70
|
|
|
70
71
|
// This confit version just gets us environment info
|
|
71
72
|
const envConfit = await confit({ basedir: specificConfig }).create();
|
|
72
73
|
const configFactory = confit<Config>({
|
|
73
74
|
basedir: specificConfig,
|
|
74
|
-
protocols:
|
|
75
|
+
protocols: shortstopHandlers,
|
|
75
76
|
});
|
|
76
77
|
|
|
77
78
|
/**
|
package/src/config/schema.ts
CHANGED
|
@@ -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;
|
package/src/express-app/app.ts
CHANGED
|
@@ -28,6 +28,7 @@ import type {
|
|
|
28
28
|
ServiceStartOptions,
|
|
29
29
|
} from '../types';
|
|
30
30
|
import { ConfigurationSchema } from '../config/schema';
|
|
31
|
+
import { shortstops } from '../config/shortstops';
|
|
31
32
|
import { getNodeEnv, isDev } from '../env';
|
|
32
33
|
import { getGlobalPrometheusExporter } from '../telemetry/index';
|
|
33
34
|
|
|
@@ -76,15 +77,16 @@ export async function startApp<
|
|
|
76
77
|
const serviceImpl = service();
|
|
77
78
|
assert(serviceImpl?.start, 'Service function did not return a conforming object');
|
|
78
79
|
|
|
80
|
+
const sourceDirectory = path.join(rootDirectory, codepath);
|
|
79
81
|
const baseOptions: ServiceOptions = {
|
|
80
82
|
configurationDirectories: [path.resolve(rootDirectory, './config')],
|
|
83
|
+
shortstopHandlers: shortstops({ name }, sourceDirectory),
|
|
81
84
|
};
|
|
82
85
|
const options = serviceImpl.configure?.(startOptions, baseOptions) || baseOptions;
|
|
83
86
|
|
|
84
87
|
const config = await loadConfiguration({
|
|
85
|
-
name,
|
|
86
88
|
configurationDirectories: options.configurationDirectories,
|
|
87
|
-
|
|
89
|
+
shortstopHandlers: options.shortstopHandlers,
|
|
88
90
|
});
|
|
89
91
|
|
|
90
92
|
const logging = config.logging;
|
|
@@ -124,7 +126,7 @@ export async function startApp<
|
|
|
124
126
|
description: 'Duration of HTTP requests in seconds',
|
|
125
127
|
});
|
|
126
128
|
|
|
127
|
-
app.use(loggerMiddleware(app, histogram, logging
|
|
129
|
+
app.use(loggerMiddleware(app, histogram, logging));
|
|
128
130
|
|
|
129
131
|
// Allow the service to add locals, etc. We put this before the body parsers
|
|
130
132
|
// 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
|
-
|
|
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:
|
|
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 (
|
|
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
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
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);
|
package/src/types.ts
CHANGED
|
@@ -6,6 +6,7 @@ import type { Request, Response } from 'express';
|
|
|
6
6
|
import type { Application } from 'express-serve-static-core';
|
|
7
7
|
import type { middleware } from 'express-openapi-validator';
|
|
8
8
|
import type { Meter } from '@opentelemetry/api';
|
|
9
|
+
import { ShortstopHandler } from '@sesamecare-oss/confit';
|
|
9
10
|
|
|
10
11
|
import { ConfigurationSchema } from './config/schema';
|
|
11
12
|
|
|
@@ -143,6 +144,8 @@ export interface ServiceOptions {
|
|
|
143
144
|
|
|
144
145
|
// Add or control OpenAPI options such as security handlers
|
|
145
146
|
openApiOptions?: Parameters<typeof middleware>[0];
|
|
147
|
+
|
|
148
|
+
shortstopHandlers: Record<string, ShortstopHandler<string, unknown>>;
|
|
146
149
|
}
|
|
147
150
|
|
|
148
151
|
export interface ServiceLike<
|