openapi-request-coercer
Advanced tools
Comparing version 2.0.1 to 2.1.0
@@ -7,2 +7,6 @@ # openapi-request-coercer Changelog | ||
## 2.1.0 - 2018-10-20 | ||
### Added | ||
- `args.enableObjectCoercion` to enable coercion for `type: 'object'` parameters. | ||
## 2.0.1 - 2018-10-03 | ||
@@ -9,0 +13,0 @@ ### Fixed |
@@ -7,2 +7,3 @@ import { OpenAPI } from 'openapi-types'; | ||
loggingKey?: string; | ||
enableObjectCoercion?: boolean; | ||
extensionBase?: string; | ||
@@ -16,4 +17,5 @@ parameters: OpenAPI.Parameters; | ||
private coerceFormData; | ||
private enableObjectCoercion; | ||
constructor(args: OpenAPIRequestCoercerArgs); | ||
coerce(request: any): void; | ||
} |
@@ -8,6 +8,2 @@ "use strict"; | ||
''; | ||
var extensionBase = args && args.extensionBase ? | ||
args.extensionBase : | ||
'x-openapi-coercion'; | ||
var strictExtension = extensionBase + "-strict"; | ||
if (!args) { | ||
@@ -19,6 +15,39 @@ throw new Error(loggingKey + "missing args argument"); | ||
} | ||
this.coerceHeaders = buildCoercer(args.parameters, 'header', true, loggingKey, strictExtension); | ||
this.coerceParams = buildCoercer(args.parameters, 'path', false, loggingKey, strictExtension); | ||
this.coerceQuery = buildCoercer(args.parameters, 'query', false, loggingKey, strictExtension); | ||
this.coerceFormData = buildCoercer(args.parameters, 'formData', false, loggingKey, strictExtension); | ||
var extensionBase = args && args.extensionBase ? | ||
args.extensionBase : | ||
'x-openapi-coercion'; | ||
var strictExtensionName = extensionBase + "-strict"; | ||
var enableObjectCoercion = !!args.enableObjectCoercion; | ||
this.coerceHeaders = buildCoercer({ | ||
params: args.parameters, | ||
property: 'header', | ||
isHeaders: true, | ||
loggingKey: loggingKey, | ||
strictExtensionName: strictExtensionName, | ||
enableObjectCoercion: enableObjectCoercion | ||
}); | ||
this.coerceParams = buildCoercer({ | ||
params: args.parameters, | ||
property: 'path', | ||
isHeaders: false, | ||
loggingKey: loggingKey, | ||
strictExtensionName: strictExtensionName, | ||
enableObjectCoercion: enableObjectCoercion | ||
}); | ||
this.coerceQuery = buildCoercer({ | ||
params: args.parameters, | ||
property: 'query', | ||
isHeaders: false, | ||
loggingKey: loggingKey, | ||
strictExtensionName: strictExtensionName, | ||
enableObjectCoercion: enableObjectCoercion | ||
}); | ||
this.coerceFormData = buildCoercer({ | ||
params: args.parameters, | ||
property: 'formData', | ||
isHeaders: false, | ||
loggingKey: loggingKey, | ||
strictExtensionName: strictExtensionName, | ||
enableObjectCoercion: enableObjectCoercion | ||
}); | ||
} | ||
@@ -42,2 +71,6 @@ OpenAPIRequestCoercer.prototype.coerce = function (request) { | ||
exports["default"] = OpenAPIRequestCoercer; | ||
var OBJECT_FORMAT_COERCER = { | ||
"default": function (input) { return JSON.parse(input); } | ||
//other formats | ||
}; | ||
var COERCION_STRATEGIES = { | ||
@@ -54,2 +87,5 @@ array: function (itemCoercer, collectionFormat, input) { | ||
}, | ||
object: function (format, input) { | ||
return (OBJECT_FORMAT_COERCER[format] || OBJECT_FORMAT_COERCER['default'])(input); | ||
}, | ||
boolean: function (input) { | ||
@@ -86,4 +122,4 @@ if (typeof input === 'boolean') { | ||
}; | ||
function buildCoercer(params, property, isHeaders, loggingKey, strictExtensionName) { | ||
var l = isHeaders ? | ||
function buildCoercer(args) { | ||
var l = args.isHeaders ? | ||
function (name) { | ||
@@ -96,20 +132,36 @@ return name.toLowerCase(); | ||
var coercion; | ||
if (params.length) { | ||
if (args.params.length) { | ||
var coercers_1 = {}; | ||
params.filter(byLocation(property)).forEach(function (param) { | ||
args.params.filter(byLocation(args.property)).forEach(function (param) { | ||
var name = param.name; | ||
var type = param.type; | ||
var strict = !!param[strictExtensionName]; | ||
var strict = !!param[args.strictExtensionName]; | ||
var coercer; | ||
var itemCoercer; | ||
if (type === 'array') { | ||
var disableCoercer = void 0; | ||
if (!param.items) { | ||
throw new Error(loggingKey + 'items is a required property with type array'); | ||
throw new Error(args.loggingKey + "items is a required property with type array"); | ||
} | ||
if (param.items.type === 'array') { | ||
throw new Error(loggingKey + 'nested arrays are not allowed (items was of type array)'); | ||
throw new Error(args.loggingKey + "nested arrays are not allowed (items was of type array)"); | ||
} | ||
itemCoercer = getCoercer(param.items.type, strict); | ||
coercer = COERCION_STRATEGIES.array.bind(null, itemCoercer, param.collectionFormat); | ||
if (param.items.type === 'object') { | ||
if (!args.enableObjectCoercion) { | ||
disableCoercer = true; | ||
} | ||
else { | ||
itemCoercer = itemCoercer.bind(null, param.items.format); | ||
} | ||
} | ||
if (!disableCoercer) { | ||
coercer = COERCION_STRATEGIES.array.bind(null, itemCoercer, param.collectionFormat); | ||
} | ||
} | ||
else if (type === 'object') { | ||
if (args.enableObjectCoercion) { | ||
coercer = getCoercer(param.type, strict).bind(null, param.format); | ||
} | ||
} | ||
else { | ||
@@ -116,0 +168,0 @@ coercer = getCoercer(param.type, strict); |
{ | ||
"name": "openapi-request-coercer", | ||
"version": "2.0.1", | ||
"version": "2.1.0", | ||
"description": "Coerce request properties according to an openapi parameter list.", | ||
@@ -5,0 +5,0 @@ "main": "./dist/index.js", |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
19193
221
0