@middy/http-content-negotiation 3.0.0-alpha.3 → 3.0.0-alpha.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +32 -22
  2. package/index.js +2 -97
  3. package/package.json +4 -4
package/README.md CHANGED
@@ -1,26 +1,36 @@
1
- # Middy http-content-negotiation middleware
2
-
3
- <div align="center">
4
- <img alt="Middy logo" src="https://raw.githubusercontent.com/middyjs/middy/main/docs/img/middy-logo.png"/>
5
- </div>
6
-
7
1
  <div align="center">
2
+ <h1>Middy http-content-negotiation middleware</h1>
3
+ <img alt="Middy logo" src="https://raw.githubusercontent.com/middyjs/middy/main/docs/img/middy-logo.svg"/>
8
4
  <p><strong>HTTP content negotiation middleware for the middy framework, the stylish Node.js middleware engine for AWS Lambda</strong></p>
9
- </div>
10
-
11
- <div align="center">
12
5
  <p>
13
- <a href="http://badge.fury.io/js/%40middy%2Fhttp-content-negotiation">
6
+ <a href="https://www.npmjs.com/package/@middy/http-content-negotiation?activeTab=versions">
14
7
  <img src="https://badge.fury.io/js/%40middy%2Fhttp-content-negotiation.svg" alt="npm version" style="max-width:100%;">
15
8
  </a>
9
+ <a href="https://packagephobia.com/result?p=@middy/http-content-negotiation">
10
+ <img src="https://packagephobia.com/badge?p=@middy/http-content-negotiation" alt="npm install size" style="max-width:100%;">
11
+ </a>
12
+ <a href="https://github.com/middyjs/middy/actions">
13
+ <img src="https://github.com/middyjs/middy/workflows/Tests/badge.svg" alt="GitHub Actions test status badge" style="max-width:100%;">
14
+ </a>
15
+ <br/>
16
+ <a href="https://standardjs.com/">
17
+ <img src="https://img.shields.io/badge/code_style-standard-brightgreen.svg" alt="Standard Code Style" style="max-width:100%;">
18
+ </a>
16
19
  <a href="https://snyk.io/test/github/middyjs/middy">
17
20
  <img src="https://snyk.io/test/github/middyjs/middy/badge.svg" alt="Known Vulnerabilities" data-canonical-src="https://snyk.io/test/github/middyjs/middy" style="max-width:100%;">
18
21
  </a>
19
- <a href="https://standardjs.com/">
20
- <img src="https://img.shields.io/badge/code_style-standard-brightgreen.svg" alt="Standard Code Style" style="max-width:100%;">
22
+ <a href="https://lgtm.com/projects/g/middyjs/middy/context:javascript">
23
+ <img src="https://img.shields.io/lgtm/grade/javascript/g/middyjs/middy.svg?logo=lgtm&logoWidth=18" alt="Language grade: JavaScript" style="max-width:100%;">
24
+ </a>
25
+ <a href="https://bestpractices.coreinfrastructure.org/projects/5280">
26
+ <img src="https://bestpractices.coreinfrastructure.org/projects/5280/badge" alt="Core Infrastructure Initiative (CII) Best Practices" style="max-width:100%;">
21
27
  </a>
28
+ <br/>
22
29
  <a href="https://gitter.im/middyjs/Lobby">
23
- <img src="https://badges.gitter.im/gitterHQ/gitter.svg" alt="Chat on Gitter" style="max-width:100%;">
30
+ <img src="https://badges.gitter.im/gitterHQ/gitter.svg" alt="Chat on Gitter" style="max-width:100%;">
31
+ </a>
32
+ <a href="https://stackoverflow.com/questions/tagged/middy?sort=Newest&uqlId=35052">
33
+ <img src="https://img.shields.io/badge/StackOverflow-[middy]-yellow" alt="Ask questions on StackOverflow" style="max-width:100%;">
24
34
  </a>
25
35
  </p>
26
36
  </div>
@@ -54,15 +64,15 @@ npm install --save @middy/http-content-negotiation
54
64
 
55
65
  ## Options
56
66
 
57
- - `parseCharsets` (defaults to `true`) - Allows enabling/disabling the charsets parsing
58
- - `availableCharsets` (defaults to `undefined`) - Allows defining the list of charsets supported by the Lambda function
59
- - `parseEncodings` (defaults to `true`) - Allows enabling/disabling the encodings parsing
60
- - `availableEncodings` (defaults to `undefined`) - Allows defining the list of encodings supported by the Lambda function
61
- - `parseLanguages` (defaults to `true`) - Allows enabling/disabling the languages parsing
62
- - `availableLanguages` (defaults to `undefined`) - Allows defining the list of languages supported by the Lambda function
63
- - `parseMediaTypes` (defaults to `true`) - Allows enabling/disabling the media types parsing
64
- - `availableMediaTypes` (defaults to `undefined`) - Allows defining the list of media types supported by the Lambda function
65
- - `failOnMismatch` (defaults to `true`) - If set to true it will throw an HTTP `NotAcceptable` (406) exception when the negotiation fails for one of the headers (e.g. none of the languages requested are supported by the app)
67
+ - `parseCharsets` (boolean) (defaults to `true`) - Allows enabling/disabling the charsets parsing
68
+ - `availableCharsets` (string) (defaults to `undefined`) - Allows defining the list of charsets supported by the Lambda function
69
+ - `parseEncodings` (boolean) (defaults to `true`) - Allows enabling/disabling the encodings parsing
70
+ - `availableEncodings` (string) (defaults to `undefined`) - Allows defining the list of encodings supported by the Lambda function
71
+ - `parseLanguages` (boolean) (defaults to `true`) - Allows enabling/disabling the languages parsing
72
+ - `availableLanguages` (string) (defaults to `undefined`) - Allows defining the list of languages supported by the Lambda function
73
+ - `parseMediaTypes` (boolean) (defaults to `true`) - Allows enabling/disabling the media types parsing
74
+ - `availableMediaTypes` (string) (defaults to `undefined`) - Allows defining the list of media types supported by the Lambda function
75
+ - `failOnMismatch` (boolean) (defaults to `true`) - If set to true it will throw an HTTP `NotAcceptable` (406) exception when the negotiation fails for one of the headers (e.g. none of the languages requested are supported by the app)
66
76
 
67
77
 
68
78
  ## Sample usage
package/index.js CHANGED
@@ -1,98 +1,3 @@
1
- import charset from 'negotiator/lib/charset.js'
2
- import encoding from 'negotiator/lib/encoding.js'
3
- import language from 'negotiator/lib/language.js'
4
- import mediaType from 'negotiator/lib/mediaType.js'
5
- import { createError } from '@middy/util'
1
+ import charset from'negotiator/lib/charset.js';import encoding from'negotiator/lib/encoding.js';import language from'negotiator/lib/language.js';import mediaType from'negotiator/lib/mediaType.js';import{createError}from'@middy/util';const parseFn={Charset:charset,Encoding:encoding,Language:language,MediaType:mediaType};const defaults={parseCharsets:true,availableCharsets:undefined,parseEncodings:true,availableEncodings:undefined,parseLanguages:true,availableLanguages:undefined,parseMediaTypes:true,availableMediaTypes:undefined,failOnMismatch:true};const httpContentNegotiationMiddleware=(opts={})=>{const options={...defaults,...opts};const httpContentNegotiationMiddlewareBefore=async request=>{const{event}=request;if(!event.headers)return;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{before:httpContentNegotiationMiddlewareBefore}};const parseHeader=(headerName,type,availableValues,failOnMismatch,event)=>{const resultsName=`preferred${type}s`;const resultName=`preferred${type}`;const headerValue=event.headers[headerName]??event.headers[headerName.toLowerCase()];event[resultsName]=parseFn[type](headerValue,availableValues);event[resultName]=event[resultsName][0];if(failOnMismatch&&event[resultName]===undefined){throw createError(406,`Unsupported ${type}. Acceptable values: ${availableValues.join(', ')}`)}};export default httpContentNegotiationMiddleware
6
2
 
7
- const parseFn = {
8
- Charset: charset,
9
- Encoding: encoding,
10
- Language: language,
11
- MediaType: mediaType
12
- }
13
-
14
- const defaults = {
15
- parseCharsets: true,
16
- availableCharsets: undefined,
17
- parseEncodings: true,
18
- availableEncodings: undefined,
19
- parseLanguages: true,
20
- availableLanguages: undefined,
21
- parseMediaTypes: true,
22
- availableMediaTypes: undefined,
23
- failOnMismatch: true
24
- }
25
-
26
- const httpContentNegotiationMiddleware = (opts = {}) => {
27
- const options = { ...defaults, ...opts }
28
-
29
- const httpContentNegotiationMiddlewareBefore = async (request) => {
30
- const { event } = request
31
- if (!event.headers) return
32
- if (options.parseCharsets) {
33
- parseHeader(
34
- 'Accept-Charset',
35
- 'Charset',
36
- options.availableCharsets,
37
- options.failOnMismatch,
38
- event
39
- )
40
- }
41
-
42
- if (options.parseEncodings) {
43
- parseHeader(
44
- 'Accept-Encoding',
45
- 'Encoding',
46
- options.availableEncodings,
47
- options.failOnMismatch,
48
- event
49
- )
50
- }
51
-
52
- if (options.parseLanguages) {
53
- parseHeader(
54
- 'Accept-Language',
55
- 'Language',
56
- options.availableLanguages,
57
- options.failOnMismatch,
58
- event
59
- )
60
- }
61
-
62
- if (options.parseMediaTypes) {
63
- parseHeader(
64
- 'Accept',
65
- 'MediaType',
66
- options.availableMediaTypes,
67
- options.failOnMismatch,
68
- event
69
- )
70
- }
71
- }
72
-
73
- return {
74
- before: httpContentNegotiationMiddlewareBefore
75
- }
76
- }
77
-
78
- const parseHeader = (
79
- headerName,
80
- type,
81
- availableValues,
82
- failOnMismatch,
83
- event
84
- ) => {
85
- const resultsName = `preferred${type}s`
86
- const resultName = `preferred${type}`
87
- const headerValue =
88
- event.headers[headerName] ?? event.headers[headerName.toLowerCase()]
89
- event[resultsName] = parseFn[type](headerValue, availableValues)
90
- event[resultName] = event[resultsName][0]
91
-
92
- if (failOnMismatch && event[resultName] === undefined) {
93
- // NotAcceptable
94
- throw createError(406, `Unsupported ${type}. Acceptable values: ${availableValues.join(', ')}`)
95
- }
96
- }
97
-
98
- export default httpContentNegotiationMiddleware
3
+ //# sourceMappingURL=index.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@middy/http-content-negotiation",
3
- "version": "3.0.0-alpha.3",
3
+ "version": "3.0.0-alpha.7",
4
4
  "description": "Http content negotiation middleware for the middy framework",
5
5
  "type": "module",
6
6
  "engines": {
@@ -48,11 +48,11 @@
48
48
  },
49
49
  "homepage": "https://github.com/middyjs/middy#readme",
50
50
  "dependencies": {
51
- "@middy/util": "^3.0.0-alpha.3",
51
+ "@middy/util": "^3.0.0-alpha.7",
52
52
  "negotiator": "0.6.2"
53
53
  },
54
54
  "devDependencies": {
55
- "@middy/core": "^3.0.0-alpha.3"
55
+ "@middy/core": "^3.0.0-alpha.7"
56
56
  },
57
- "gitHead": "1441158711580313765e6d156046ef0fade0d156"
57
+ "gitHead": "5cef39ebe49c201f97d71bb0680004de4b82cb91"
58
58
  }