forked from middyjs/middy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttpContentNegotiation.js
56 lines (47 loc) · 1.81 KB
/
httpContentNegotiation.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
const createError = require('http-errors')
module.exports = (opts) => {
const defaults = {
parseCharsets: true,
availableCharsets: undefined,
parseEncodings: true,
availableEncodings: undefined,
parseLanguages: true,
availableLanguages: undefined,
parseMediaTypes: true,
availableMediaTypes: undefined,
failOnMismatch: true
}
const options = Object.assign({}, defaults, opts)
const parseHeader = (headerName, type, availableValues, failOnMismatch, event) => {
const parseFn = require(`negotiator/lib/${type}`)
const singular = type.charAt(0).toUpperCase() + type.slice(1)
const plural = singular + 's'
const resultsName = `preferred${plural}`
const resultName = `preferred${singular}`
event[resultsName] = parseFn(event.headers[headerName], availableValues)
event[resultName] = event[resultsName][0]
if (typeof event[resultName] === 'undefined' && failOnMismatch) {
throw new createError.NotAcceptable(`Unsupported ${type}. Acceptable values: ${availableValues.join(', ')}`)
}
}
return ({
before: (handler, next) => {
const { event } = handler
if (event.headers) {
if (options.parseCharsets) {
parseHeader('Accept-Charset', 'charset', options.availableCharsets, options.failOnMismatch, event)
}
if (options.parseEncodings) {
parseHeader('Accept-Encoding', 'encoding', options.availableEncodings, options.failOnMismatch, event)
}
if (options.parseLanguages) {
parseHeader('Accept-Language', 'language', options.availableLanguages, options.failOnMismatch, event)
}
if (options.parseMediaTypes) {
parseHeader('Accept', 'mediaType', options.availableMediaTypes, options.failOnMismatch, event)
}
}
return next()
}
})
}