@middy/http-content-negotiation 3.0.0-alpha.6 → 3.0.0
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.
- package/README.md +33 -22
- package/index.cjs +3 -0
- package/index.js +2 -68
- package/package.json +13 -6
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="
|
|
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://
|
|
20
|
-
<img src="https://img.shields.io/
|
|
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"
|
|
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,16 @@ 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
|
-
- `
|
|
64
|
-
- `
|
|
65
|
-
- `
|
|
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
|
+
- `defaultToFirstLanguage` (boolean) (deafults to `false`) - Will set `preferredCharset` to the first value of `availableLanguages` if unset.
|
|
74
|
+
- `parseMediaTypes` (boolean) (defaults to `true`) - Allows enabling/disabling the media types parsing
|
|
75
|
+
- `availableMediaTypes` (string) (defaults to `undefined`) - Allows defining the list of media types supported by the Lambda function
|
|
76
|
+
- `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
77
|
|
|
67
78
|
|
|
68
79
|
## Sample usage
|
package/index.cjs
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.default=void 0;var _charsetJs=_interopRequireDefault(require("negotiator/lib/charset.js"));var _encodingJs=_interopRequireDefault(require("negotiator/lib/encoding.js"));var _languageJs=_interopRequireDefault(require("negotiator/lib/language.js"));var _mediaTypeJs=_interopRequireDefault(require("negotiator/lib/mediaType.js"));var _util=require("@middy/util");function _interopRequireDefault(obj){return obj&&obj.__esModule?obj:{default:obj}}const parseFn={Charset:_charsetJs.default,Encoding:_encodingJs.default,Language:_languageJs.default,MediaType:_mediaTypeJs.default};const defaults={parseCharsets:true,availableCharsets:undefined,parseEncodings:true,availableEncodings:undefined,parseLanguages:true,availableLanguages:undefined,defaultToFirstLanguage:false,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.defaultToFirstCharset,options.failOnMismatch,event)}if(options.parseEncodings){parseHeader("Accept-Encoding","Encoding",options.availableEncodings,options.defaultToFirstEncoding,options.failOnMismatch,event)}if(options.parseLanguages){parseHeader("Accept-Language","Language",options.availableLanguages,options.defaultToFirstLanguage,options.failOnMismatch,event)}if(options.parseMediaTypes){parseHeader("Accept","MediaType",options.availableMediaTypes,options.defaultToFirstMediaType,options.failOnMismatch,event)}};return{before:httpContentNegotiationMiddlewareBefore}};const parseHeader=(headerName,type,availableValues,defaultToFirstValue,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(defaultToFirstValue&&event[resultName]===undefined){event[resultName]=availableValues[0]}if(failOnMismatch&&event[resultName]===undefined){throw(0,_util).createError(406,`Unsupported ${type}. Acceptable values: ${availableValues.join(", ")}`)}};var _default=httpContentNegotiationMiddleware;exports.default=_default
|
|
2
|
+
|
|
3
|
+
//# sourceMappingURL=index.cjs.map
|
package/index.js
CHANGED
|
@@ -1,69 +1,3 @@
|
|
|
1
|
-
import charset from
|
|
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';
|
|
6
|
-
const parseFn = {
|
|
7
|
-
Charset: charset,
|
|
8
|
-
Encoding: encoding,
|
|
9
|
-
Language: language,
|
|
10
|
-
MediaType: mediaType
|
|
11
|
-
};
|
|
12
|
-
const defaults = {
|
|
13
|
-
parseCharsets: true,
|
|
14
|
-
availableCharsets: undefined,
|
|
15
|
-
parseEncodings: true,
|
|
16
|
-
availableEncodings: undefined,
|
|
17
|
-
parseLanguages: true,
|
|
18
|
-
availableLanguages: undefined,
|
|
19
|
-
parseMediaTypes: true,
|
|
20
|
-
availableMediaTypes: undefined,
|
|
21
|
-
failOnMismatch: true
|
|
22
|
-
};
|
|
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,defaultToFirstLanguage:false,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.defaultToFirstCharset,options.failOnMismatch,event)}if(options.parseEncodings){parseHeader("Accept-Encoding","Encoding",options.availableEncodings,options.defaultToFirstEncoding,options.failOnMismatch,event)}if(options.parseLanguages){parseHeader("Accept-Language","Language",options.availableLanguages,options.defaultToFirstLanguage,options.failOnMismatch,event)}if(options.parseMediaTypes){parseHeader("Accept","MediaType",options.availableMediaTypes,options.defaultToFirstMediaType,options.failOnMismatch,event)}};return{before:httpContentNegotiationMiddlewareBefore}};const parseHeader=(headerName,type,availableValues,defaultToFirstValue,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(defaultToFirstValue&&event[resultName]===undefined){event[resultName]=availableValues[0]}if(failOnMismatch&&event[resultName]===undefined){throw createError(406,`Unsupported ${type}. Acceptable values: ${availableValues.join(", ")}`)}};export default httpContentNegotiationMiddleware
|
|
23
2
|
|
|
24
|
-
|
|
25
|
-
const options = { ...defaults,
|
|
26
|
-
...opts
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
const httpContentNegotiationMiddlewareBefore = async request => {
|
|
30
|
-
const {
|
|
31
|
-
event
|
|
32
|
-
} = request;
|
|
33
|
-
if (!event.headers) return;
|
|
34
|
-
|
|
35
|
-
if (options.parseCharsets) {
|
|
36
|
-
parseHeader('Accept-Charset', 'Charset', options.availableCharsets, options.failOnMismatch, event);
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
if (options.parseEncodings) {
|
|
40
|
-
parseHeader('Accept-Encoding', 'Encoding', options.availableEncodings, options.failOnMismatch, event);
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
if (options.parseLanguages) {
|
|
44
|
-
parseHeader('Accept-Language', 'Language', options.availableLanguages, options.failOnMismatch, event);
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
if (options.parseMediaTypes) {
|
|
48
|
-
parseHeader('Accept', 'MediaType', options.availableMediaTypes, options.failOnMismatch, event);
|
|
49
|
-
}
|
|
50
|
-
};
|
|
51
|
-
|
|
52
|
-
return {
|
|
53
|
-
before: httpContentNegotiationMiddlewareBefore
|
|
54
|
-
};
|
|
55
|
-
};
|
|
56
|
-
|
|
57
|
-
const parseHeader = (headerName, type, availableValues, failOnMismatch, event) => {
|
|
58
|
-
const resultsName = `preferred${type}s`;
|
|
59
|
-
const resultName = `preferred${type}`;
|
|
60
|
-
const headerValue = event.headers[headerName] ?? event.headers[headerName.toLowerCase()];
|
|
61
|
-
event[resultsName] = parseFn[type](headerValue, availableValues);
|
|
62
|
-
event[resultName] = event[resultsName][0];
|
|
63
|
-
|
|
64
|
-
if (failOnMismatch && event[resultName] === undefined) {
|
|
65
|
-
throw createError(406, `Unsupported ${type}. Acceptable values: ${availableValues.join(', ')}`);
|
|
66
|
-
}
|
|
67
|
-
};
|
|
68
|
-
|
|
69
|
-
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
|
|
3
|
+
"version": "3.0.0",
|
|
4
4
|
"description": "Http content negotiation middleware for the middy framework",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -10,10 +10,17 @@
|
|
|
10
10
|
"publishConfig": {
|
|
11
11
|
"access": "public"
|
|
12
12
|
},
|
|
13
|
-
"exports":
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"import": "./index.js",
|
|
16
|
+
"require": "./index.cjs",
|
|
17
|
+
"types": "./index.d.ts"
|
|
18
|
+
}
|
|
19
|
+
},
|
|
14
20
|
"types": "index.d.ts",
|
|
15
21
|
"files": [
|
|
16
22
|
"index.js",
|
|
23
|
+
"index.cjs",
|
|
17
24
|
"index.d.ts"
|
|
18
25
|
],
|
|
19
26
|
"scripts": {
|
|
@@ -46,13 +53,13 @@
|
|
|
46
53
|
"bugs": {
|
|
47
54
|
"url": "https://github.com/middyjs/middy/issues"
|
|
48
55
|
},
|
|
49
|
-
"homepage": "https://
|
|
56
|
+
"homepage": "https://middy.js.org",
|
|
50
57
|
"dependencies": {
|
|
51
|
-
"@middy/util": "^3.0.0
|
|
58
|
+
"@middy/util": "^3.0.0",
|
|
52
59
|
"negotiator": "0.6.2"
|
|
53
60
|
},
|
|
54
61
|
"devDependencies": {
|
|
55
|
-
"@middy/core": "^3.0.0
|
|
62
|
+
"@middy/core": "^3.0.0"
|
|
56
63
|
},
|
|
57
|
-
"gitHead": "
|
|
64
|
+
"gitHead": "01520fa8628a36c2f89e126cad656a16547ea0d6"
|
|
58
65
|
}
|