@trojs/openapi-server 1.10.0 → 1.11.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/package.json +1 -1
- package/src/api.js +8 -4
- package/src/express-callback.js +11 -4
- package/src/router.js +6 -3
package/package.json
CHANGED
package/src/api.js
CHANGED
|
@@ -22,7 +22,8 @@ import { setupRouter } from './router.js'
|
|
|
22
22
|
* @property {boolean=} strictSpecification
|
|
23
23
|
* @property {boolean=} errorDetails
|
|
24
24
|
* @property {Logger=} logger
|
|
25
|
-
* @property {Function=}
|
|
25
|
+
* @property {Function=} preLog
|
|
26
|
+
* @property {Function=} postLog
|
|
26
27
|
* @property {object=} meta
|
|
27
28
|
* @property {SecurityHandler[]=} securityHandlers
|
|
28
29
|
* @property {Handler=} unauthorizedHandler
|
|
@@ -52,7 +53,8 @@ export class Api {
|
|
|
52
53
|
strictSpecification,
|
|
53
54
|
errorDetails,
|
|
54
55
|
logger,
|
|
55
|
-
|
|
56
|
+
preLog,
|
|
57
|
+
postLog,
|
|
56
58
|
meta,
|
|
57
59
|
securityHandlers,
|
|
58
60
|
unauthorizedHandler,
|
|
@@ -69,7 +71,8 @@ export class Api {
|
|
|
69
71
|
this.strictSpecification = strictSpecification
|
|
70
72
|
this.errorDetails = errorDetails || false
|
|
71
73
|
this.logger = logger || console
|
|
72
|
-
this.
|
|
74
|
+
this.preLog = preLog || undefined
|
|
75
|
+
this.postLog = postLog || undefined
|
|
73
76
|
this.meta = meta || {}
|
|
74
77
|
this.securityHandlers = securityHandlers || []
|
|
75
78
|
this.unauthorizedHandler = unauthorizedHandler || undefined
|
|
@@ -103,7 +106,8 @@ export class Api {
|
|
|
103
106
|
strictSpecification: this.strictSpecification,
|
|
104
107
|
errorDetails: this.errorDetails,
|
|
105
108
|
logger: this.logger,
|
|
106
|
-
|
|
109
|
+
preLog: this.preLog,
|
|
110
|
+
postLog: this.postLog,
|
|
107
111
|
meta: this.meta,
|
|
108
112
|
securityHandlers: this.securityHandlers,
|
|
109
113
|
unauthorizedHandler: this.unauthorizedHandler,
|
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,
|
package/src/router.js
CHANGED
|
@@ -24,7 +24,8 @@ import { unauthorized } from './handlers/unauthorized.js'
|
|
|
24
24
|
* @param {boolean=} params.strictSpecification
|
|
25
25
|
* @param {boolean=} params.errorDetails
|
|
26
26
|
* @param {Logger=} params.logger
|
|
27
|
-
* @param {Function=} params.
|
|
27
|
+
* @param {Function=} params.preLog
|
|
28
|
+
* @param {Function=} params.postLog
|
|
28
29
|
* @param {object=} params.meta
|
|
29
30
|
* @param {SecurityHandler[]=} params.securityHandlers
|
|
30
31
|
* @param {Handler=} params.unauthorizedHandler
|
|
@@ -40,7 +41,8 @@ export const setupRouter = ({
|
|
|
40
41
|
strictSpecification,
|
|
41
42
|
errorDetails,
|
|
42
43
|
logger,
|
|
43
|
-
|
|
44
|
+
preLog,
|
|
45
|
+
postLog,
|
|
44
46
|
meta,
|
|
45
47
|
securityHandlers = [],
|
|
46
48
|
unauthorizedHandler,
|
|
@@ -81,7 +83,8 @@ export const setupRouter = ({
|
|
|
81
83
|
logger,
|
|
82
84
|
meta,
|
|
83
85
|
mock,
|
|
84
|
-
|
|
86
|
+
preLog,
|
|
87
|
+
postLog
|
|
85
88
|
})
|
|
86
89
|
)
|
|
87
90
|
}
|