|
1 | 1 | import fastifyPlugin from 'fastify-plugin'; |
2 | 2 | import { handleErrors, handleNotFoundError } from "./handlers.mjs"; |
3 | | -import { kHttpErrorsEnhancedProperties, kHttpErrorsEnhancedResponseValidations } from "./interfaces.mjs"; |
| 3 | +import { kHttpErrorsEnhancedConfiguration, kHttpErrorsEnhancedResponseValidations } from "./interfaces.mjs"; |
4 | 4 | import { addResponseValidation, compileResponseValidationSchema } from "./validation.mjs"; |
5 | 5 | export * from "./handlers.mjs"; |
6 | 6 | export * from "./interfaces.mjs"; |
7 | 7 | export { convertValidationErrors, niceJoin, validationMessagesFormatters } from "./validation.mjs"; |
8 | 8 | export const plugin = fastifyPlugin(function (instance, options, done) { |
9 | 9 | var _a, _b, _c, _d; |
10 | 10 | const isProduction = process.env.NODE_ENV === 'production'; |
11 | | - const hideUnhandledErrors = (_a = options.hideUnhandledErrors) !== null && _a !== void 0 ? _a : isProduction; |
12 | | - const convertValidationErrors = (_b = options.convertValidationErrors) !== null && _b !== void 0 ? _b : true; |
13 | | - const convertResponsesValidationErrors = (_c = options.convertResponsesValidationErrors) !== null && _c !== void 0 ? _c : !isProduction; |
14 | | - const allowUndeclaredResponses = (_d = options.allowUndeclaredResponses) !== null && _d !== void 0 ? _d : false; |
15 | | - instance.decorateRequest(kHttpErrorsEnhancedProperties, null); |
| 11 | + const convertResponsesValidationErrors = (_a = options.convertResponsesValidationErrors) !== null && _a !== void 0 ? _a : !isProduction; |
| 12 | + const configuration = { |
| 13 | + hideUnhandledErrors: (_b = options.hideUnhandledErrors) !== null && _b !== void 0 ? _b : isProduction, |
| 14 | + convertValidationErrors: (_c = options.convertValidationErrors) !== null && _c !== void 0 ? _c : true, |
| 15 | + responseValidatorCustomizer: options.responseValidatorCustomizer, |
| 16 | + allowUndeclaredResponses: (_d = options.allowUndeclaredResponses) !== null && _d !== void 0 ? _d : false |
| 17 | + }; |
| 18 | + instance.decorate(kHttpErrorsEnhancedConfiguration, null); |
| 19 | + instance.decorateRequest(kHttpErrorsEnhancedConfiguration, null); |
16 | 20 | instance.addHook('onRequest', async (request) => { |
17 | | - request[kHttpErrorsEnhancedProperties] = { |
18 | | - hideUnhandledErrors, |
19 | | - convertValidationErrors, |
20 | | - allowUndeclaredResponses |
21 | | - }; |
| 21 | + request[kHttpErrorsEnhancedConfiguration] = configuration; |
22 | 22 | }); |
23 | 23 | instance.setErrorHandler(handleErrors); |
24 | 24 | instance.setNotFoundHandler(handleNotFoundError); |
25 | 25 | if (convertResponsesValidationErrors) { |
26 | 26 | instance.decorate(kHttpErrorsEnhancedResponseValidations, []); |
27 | 27 | instance.addHook('onRoute', addResponseValidation); |
28 | | - instance.addHook('onReady', compileResponseValidationSchema); |
| 28 | + instance.addHook('onReady', compileResponseValidationSchema.bind(instance, configuration)); |
29 | 29 | } |
30 | 30 | done(); |
31 | 31 | }, { name: 'fastify-http-errors-enhanced' }); |
|
0 commit comments