forked from fastify/fastify-sensible
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
84 lines (73 loc) · 2.45 KB
/
index.js
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
'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')
function fastifySensible (fastify, opts, next) {
fastify.decorate('httpErrors', httpErrors)
fastify.decorate('assert', assert)
fastify.decorate('to', to)
fastify.decorateRequest('forwarded', function () {
return forwarded(this.raw)
})
fastify.decorateRequest('is', function (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)
for (let i = 0; i < httpErrorsKeys.length; ++i) {
const httpError = httpErrorsKeys[i]
switch (httpError) {
case 'HttpError':
// skip abstract class constructor
break
case 'getHttpError':
fastify.decorateReply('getHttpError', function (errorCode, message) {
this.send(httpErrors.getHttpError(errorCode, message))
return this
})
break
default:
fastify.decorateReply(httpError, function (message) {
this.send(httpErrors[httpError](message))
return this
})
}
}
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' }
}
})
}
function to (promise) {
return promise.then(data => [null, data], err => [err, undefined])
}
next()
}
module.exports = fp(fastifySensible, {
name: '@fastify/sensible',
fastify: '4.x'
})
module.exports.default = fastifySensible
module.exports.fastifySensible = fastifySensible
module.exports.httpErrors = httpErrors