Skip to content

Commit 624977b

Browse files
committed
chore: Updated dependencies and linted code.
1 parent 6ca53c2 commit 624977b

File tree

7 files changed

+178
-188
lines changed

7 files changed

+178
-188
lines changed

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@
4141
"dependencies": {
4242
"ajv": "^8.6.2",
4343
"fastify-plugin": "^3.0.0",
44-
"http-errors-enhanced": "^1.0.0"
44+
"http-errors-enhanced": "^1.0.3"
4545
},
4646
"devDependencies": {
47-
"@cowtech/eslint-config": "^7.14.5",
48-
"@cowtech/esm-package-utils": "^0.9.1",
49-
"@types/node": "^16.6.1",
47+
"@cowtech/eslint-config": "^8.0.1",
48+
"@cowtech/esm-package-utils": "^0.9.3",
49+
"@types/node": "^17.0.2",
5050
"@types/tap": "^15.0.5",
5151
"ajv-formats": "^2.1.0",
5252
"c8": "^7.8.0",

src/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { FastifyError, FastifyInstance, FastifyPluginOptions, FastifyRequest } from 'fastify'
1+
import { FastifyError, FastifyInstance, FastifyPluginOptions } from 'fastify'
22
import fastifyPlugin from 'fastify-plugin'
33
import { handleErrors, handleNotFoundError } from './handlers'
44
import { Configuration, kHttpErrorsEnhancedConfiguration, kHttpErrorsEnhancedResponseValidations } from './interfaces'
@@ -23,8 +23,10 @@ export const plugin = fastifyPlugin(
2323
instance.decorate(kHttpErrorsEnhancedConfiguration, null)
2424
instance.decorateRequest(kHttpErrorsEnhancedConfiguration, null)
2525

26-
instance.addHook('onRequest', async (request: FastifyRequest) => {
26+
instance.addHook('onRequest', (request, _, done) => {
2727
request[kHttpErrorsEnhancedConfiguration] = configuration
28+
29+
done()
2830
})
2931

3032
instance.setErrorHandler(handleErrors)

src/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export function upperFirst(source: any): string {
77
}
88

99
export function get<T>(target: any, path: string): T {
10-
const tokens = path.split('.').map((t: string) => t.trim())
10+
const tokens = path.split('.').map(t => t.trim())
1111

1212
for (const token of tokens) {
1313
if (typeof target === 'undefined' || target === null) {

src/validation.ts

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export const validationMessagesFormatters: { [key: string]: ValidationFormatter
5151
hostname: () => 'must be a valid hostname',
5252
ipv4: () => 'must be a valid IPv4',
5353
ipv6: () => 'must be a valid IPv6',
54-
paramType: (type: string) => {
54+
paramType: type => {
5555
switch (type) {
5656
case 'integer':
5757
return 'must be a valid integer number'
@@ -68,8 +68,8 @@ export const validationMessagesFormatters: { [key: string]: ValidationFormatter
6868
}
6969
},
7070
presentString: () => 'must be a non empty string',
71-
minimum: (min: number) => `must be a number greater than or equal to ${min}`,
72-
maximum: (max: number) => `must be a number less than or equal to ${max}`,
71+
minimum: min => `must be a number greater than or equal to ${min}`,
72+
maximum: max => `must be a number less than or equal to ${max}`,
7373
minimumProperties(min: number): string {
7474
return min === 1 ? 'cannot be a empty object' : `must be a object with at least ${min} properties`
7575
},
@@ -82,16 +82,16 @@ export const validationMessagesFormatters: { [key: string]: ValidationFormatter
8282
maximumItems(max: number): string {
8383
return max === 0 ? 'must be a empty array' : `must be an array with at most ${max} items`
8484
},
85-
enum: (values: Array<string>) =>
85+
enum: values =>
8686
`must be one of the following values: ${niceJoin(
8787
values.map((f: string) => `"${f}"`),
8888
' or '
8989
)}`,
90-
pattern: (pattern: string) => `must match pattern "${pattern.replace(/\(\?:/g, '(')}"`,
91-
invalidResponseCode: (code: number) => `This endpoint cannot respond with HTTP status ${code}.`,
92-
invalidResponse: (code: number) =>
90+
pattern: pattern => `must match pattern "${pattern.replace(/\(\?:/g, '(')}"`,
91+
invalidResponseCode: code => `This endpoint cannot respond with HTTP status ${code}.`,
92+
invalidResponse: code =>
9393
`The response returned from the endpoint violates its specification for the HTTP status ${code}.`,
94-
invalidFormat: (format: string) => `must match format "${format}" (format)`
94+
invalidFormat: format => `must match format "${format}" (format)`
9595
}
9696

9797
export function convertValidationErrors(
@@ -227,41 +227,43 @@ export function addResponseValidation(this: FastifyInstance, route: RouteOptions
227227
])
228228

229229
// Note that this hook is not called for non JSON payloads therefore validation is not possible in such cases
230-
route.preSerialization = async function (
230+
route.preSerialization = function (
231231
this: FastifyInstance,
232232
request: FastifyRequest,
233233
reply: FastifyReply,
234-
payload: any
235-
): Promise<any> {
234+
payload: any,
235+
done: (err: Error | null, payload?: any) => void
236+
): void {
236237
const statusCode = reply.raw.statusCode
237238

238239
// Never validate error 500
239240
if (statusCode === INTERNAL_SERVER_ERROR) {
240-
return payload
241+
return done(null, payload)
241242
}
242243

243244
// No validator, it means the HTTP status is not allowed
244245
const validator = validators[statusCode]
245246

246247
if (!validator) {
247248
if (request[kHttpErrorsEnhancedConfiguration]!.allowUndeclaredResponses) {
248-
return payload
249+
return done(null, payload)
249250
}
250251

251-
throw new InternalServerError(validationMessagesFormatters.invalidResponseCode(statusCode))
252+
return done(new InternalServerError(validationMessagesFormatters.invalidResponseCode(statusCode)))
252253
}
253254

254255
// Now validate the payload
255256
const valid = validator(payload)
256257

257258
if (!valid) {
258-
console.log(validator.errors)
259-
throw new InternalServerError(validationMessagesFormatters.invalidResponse(statusCode), {
260-
failedValidations: convertValidationErrors('response', payload, validator.errors as Array<ValidationResult>)
261-
})
259+
return done(
260+
new InternalServerError(validationMessagesFormatters.invalidResponse(statusCode), {
261+
failedValidations: convertValidationErrors('response', payload, validator.errors as Array<ValidationResult>)
262+
})
263+
)
262264
}
263265

264-
return payload
266+
done(null, payload)
265267
}
266268
}
267269

0 commit comments

Comments
 (0)