Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

httpexceptions

Package Overview
Dependencies
Maintainers
1
Versions
19
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

httpexceptions - npm Package Compare versions

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 @@ */

@@ -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 @@ */

2

package.json
{
"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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc