-
-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathindex.js
More file actions
98 lines (87 loc) · 3.16 KB
/
index.js
File metadata and controls
98 lines (87 loc) · 3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
'use strict'
const fp = require('fastify-plugin')
// External utilities
const forwarded = require('forwarded')
const typeis = require('type-is')
// Internals Utilities
const httpErrors = require('./lib/httpErrors')
const assert = require('./lib/assert')
const vary = require('./lib/vary')
const cache = require('./lib/cache-control')
/** @type {typeof import('./types/index').fastifySensible} */
function fastifySensible (fastify, opts, next) {
fastify.decorate('httpErrors', httpErrors)
fastify.decorate('assert', assert)
fastify.decorate('to', to)
fastify.decorateRequest('forwarded', function requestForwarded () {
return forwarded(this.raw)
})
fastify.decorateRequest('is', function requestIs (types) {
return typeis(this.raw, Array.isArray(types) ? types : [types])
})
fastify.decorateReply('vary', vary)
fastify.decorateReply('cacheControl', cache.cacheControl)
fastify.decorateReply('preventCache', cache.preventCache)
fastify.decorateReply('revalidate', cache.revalidate)
fastify.decorateReply('staticCache', cache.staticCache)
fastify.decorateReply('stale', cache.stale)
fastify.decorateReply('maxAge', cache.maxAge)
const httpErrorsKeys = Object.keys(httpErrors)
const httpErrorsKeysLength = httpErrorsKeys.length
for (let i = 0; i < httpErrorsKeysLength; ++i) {
const httpError = httpErrorsKeys[i]
switch (httpError) {
case 'HttpError':
// skip abstract class constructor
break
case 'getHttpError':
fastify.decorateReply('getHttpError', function replyGetHttpError (errorCode, message) {
this.send(httpErrors.getHttpError(errorCode, message))
return this
})
break
default: {
const capitalizedMethodName = httpError.replace(/(?:^|\s)\S/gu, a => a.toUpperCase())
const replyMethodName = 'sensible' + capitalizedMethodName
fastify.decorateReply(httpError, {
[replyMethodName]: function (message) {
this.send(httpErrors[httpError](message))
return this
}
}[replyMethodName])
}
}
}
if (opts?.sharedSchemaId) {
// The schema must be the same as:
// https://github.com/fastify/fastify/blob/c08b67e0bfedc9935b51c787ae4cd6b250ad303c/build/build-error-serializer.js#L8-L16
fastify.addSchema({
$id: opts.sharedSchemaId,
type: 'object',
properties: {
statusCode: { type: 'number' },
code: { type: 'string' },
error: { type: 'string' },
message: { type: 'string' }
}
})
}
/**
* Wraps a promise for easier error handling without try/catch.
* @template T
* @param {Promise<T>} promise - The promise to wrap.
* @returns {Promise<[Error, undefined] | [null, T]>} A promise that resolves to a tuple containing either an error or the resolved data.
*/
function to (promise) {
return promise.then(data => [null, data], err => [err, undefined])
}
next()
}
module.exports = fp(fastifySensible, {
name: '@fastify/sensible',
fastify: '5.x'
})
module.exports.default = fastifySensible
module.exports.fastifySensible = fastifySensible
module.exports.httpErrors = httpErrors
module.exports.HttpError = httpErrors.HttpError