httpexceptions
Advanced tools
Comparing version 1.0.2 to 1.0.3
26
index.js
@@ -45,2 +45,28 @@ /// <reference path="typings/node/node.d.ts" /> | ||
* | ||
* @returns {function(Express.Request, Express.Response, Function): *} | ||
*/ | ||
function paramsRequired() { | ||
return function (req, res, next) { | ||
req.paramsRequired = function (fields) { | ||
var a = []; | ||
for (var field in fields) { | ||
if (fields[field]) { | ||
for (var i = 0; i < fields[field].length; i++) { | ||
var key = fields[field][i]; | ||
if (this[field][key] === undefined) { | ||
a.push(key); | ||
} | ||
} | ||
} | ||
} | ||
if (a.length) { | ||
throw new HTTPException_1.BadRequest(a); | ||
} | ||
}; | ||
return next(); | ||
}; | ||
} | ||
HTTPException_1.paramsRequired = paramsRequired; | ||
/** | ||
* | ||
* @returns {function(any, Express.Request, Express.Response): undefined} | ||
@@ -47,0 +73,0 @@ */ |
41
index.ts
@@ -7,2 +7,3 @@ /// <reference path="typings/node/node.d.ts" /> | ||
accepts?: Function; | ||
paramsRequired(fields:Object); | ||
} | ||
@@ -15,2 +16,8 @@ export interface Response{ | ||
interface ParamsRequired{ | ||
body?:Array<String>; | ||
params?:Array<String>; | ||
query?:Array<String>; | ||
} | ||
module HTTPException{ | ||
@@ -64,2 +71,36 @@ | ||
* | ||
* @returns {function(Express.Request, Express.Response, Function): *} | ||
*/ | ||
export function paramsRequired(){ | ||
return function(req:Express.Request, res:Express.Response, next:Function){ | ||
req.paramsRequired = function(fields:ParamsRequired){ | ||
var a = []; | ||
for(var field in fields){ | ||
if(fields[field]){ | ||
for(var i = 0; i < fields[field].length; i++){ | ||
var key = fields[field][i]; | ||
if(this[field][key] === undefined){ | ||
a.push(key); | ||
} | ||
} | ||
} | ||
} | ||
if(a.length){ | ||
throw new BadRequest(a); | ||
} | ||
}; | ||
return next(); | ||
}; | ||
} | ||
/** | ||
* | ||
* @returns {function(any, Express.Request, Express.Response): undefined} | ||
@@ -66,0 +107,0 @@ */ |
{ | ||
"name": "httpexceptions", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "Provide exception for HTTP Rest API", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -10,3 +10,4 @@ # HTTPExceptions [![Build Status](https://travis-ci.org/Romakita/httpexceptions.svg?branch=master)](https://travis-ci.org/Romakita/httpexceptions) | ||
* Two middlewares are available. HTTPException.mime() and HTTPException.globalHandler() | ||
* Some HTTP code are implemented : | ||
Some HTTP Exception are already implemented : | ||
* BadRequest | ||
@@ -25,2 +26,4 @@ * Unauthorized | ||
* GatewayTimeout | ||
You can use HTTPException method to throw a custom Exception (see example). | ||
@@ -42,5 +45,6 @@ | ||
app.use(httpException.mime('application/json')); | ||
app.use(httpException.paramsRequired()); | ||
//or | ||
app.use(httpException.mime(['application/json', 'text/xml'])); | ||
// OR | ||
// app.use(httpException.mime(['application/json', 'text/xml'])); | ||
@@ -51,7 +55,15 @@ app.get('/my/route/exception', function(req, res){ | ||
//or | ||
throw new httpException.HTTPException(510, 'Custom Message'); | ||
// OR | ||
// throw new httpException.HTTPException(510, 'Custom Message'); | ||
}); | ||
app.get('/my/route/params', function(req, res){ | ||
req.paramsRequired({ | ||
query:['id'] | ||
}); //throw a BadRequest exception if the parameters id isn't defined in queryParams | ||
}); | ||
//at the end | ||
@@ -58,0 +70,0 @@ //GlobalHandler middleware catch exception and send response to the client |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
172748
3802
75