@trojs/openapi-server 1.10.0 → 1.11.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/package.json +1 -1
- package/src/express-callback.js +11 -4
package/package.json
CHANGED
package/src/express-callback.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { hrtime } from 'node:process'
|
|
1
2
|
import getStatusByError from './error-status.js'
|
|
2
3
|
import { parseParams } from './params.js'
|
|
3
4
|
|
|
@@ -17,11 +18,12 @@ import { parseParams } from './params.js'
|
|
|
17
18
|
* @param {Logger=} params.logger
|
|
18
19
|
* @param {object=} params.meta
|
|
19
20
|
* @param {boolean=} params.mock
|
|
20
|
-
* @param {Function=} params.
|
|
21
|
+
* @param {Function=} params.preLog
|
|
22
|
+
* @param {Function=} params.postLog
|
|
21
23
|
* @returns {Function}
|
|
22
24
|
*/
|
|
23
25
|
export const makeExpressCallback
|
|
24
|
-
= ({ controller, specification, errorDetails, logger, meta, mock,
|
|
26
|
+
= ({ controller, specification, errorDetails, logger, meta, mock, preLog, postLog }) =>
|
|
25
27
|
/**
|
|
26
28
|
* Handle controller
|
|
27
29
|
* @async
|
|
@@ -31,6 +33,7 @@ export const makeExpressCallback
|
|
|
31
33
|
* @returns {Promise<any>}
|
|
32
34
|
*/
|
|
33
35
|
async (context, request, response) => {
|
|
36
|
+
const startTime = hrtime()
|
|
34
37
|
try {
|
|
35
38
|
const allParameters = {
|
|
36
39
|
...(context.request?.params || {}),
|
|
@@ -53,10 +56,14 @@ export const makeExpressCallback
|
|
|
53
56
|
logger,
|
|
54
57
|
meta
|
|
55
58
|
}
|
|
56
|
-
if (
|
|
57
|
-
|
|
59
|
+
if (preLog) {
|
|
60
|
+
preLog(feedback)
|
|
58
61
|
}
|
|
59
62
|
const responseBody = await controller(feedback)
|
|
63
|
+
const responseTime = hrtime(startTime)[1] / 1000000 // convert to milliseconds
|
|
64
|
+
if (postLog) {
|
|
65
|
+
postLog({ ...feedback, responseBody, responseTime })
|
|
66
|
+
}
|
|
60
67
|
logger.debug({
|
|
61
68
|
url,
|
|
62
69
|
parameters,
|