diff --git a/.gitignore b/.gitignore index 3c3629e..34977ee 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ node_modules +.idea \ No newline at end of file diff --git a/README.md b/README.md index 6e0d274..5865921 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,9 @@ # express-custom-response -Create custom response methods for express.js +Create custom response methods for express.js. ## Getting Started -Install the module with: `npm install express-custom-response` +Install the module with: `npm install https://github.com/SteffenLanger/express-custom-response`. ##Quick Start ```javascript @@ -12,80 +12,81 @@ require('express-custom-response')(); ## Documentation -Module contain pre-defined custom responses: +The module contains pre-defined custom responses: -**res.serverError (extra)** -- *mixed* **extra** - - Any data that have to be returned to user +**res.serverError (message, extraData)** +- *mixed* **data** +- Any data that have to be returned to the user -> return reponse with http code 500 and body:``` +return response with http code 500 and body: +```javascript error: { - error_message: 'We're sorry, a server error occurred. Please wait a bit and try again', - error_code: 500, - error_extra: null or extra + message: 'The database failed to respond.', + extraData: null or extraData } ``` -**res.validation (message, extra)** +**res.badRequest (message, extraData)** - *string* **message** - - Message to show user why his request were forbidden -- *mixed* **extra** - - Any data that have to be returned to user +- Message why the user's request could not be processed +- *mixed* **extraData** +- Any data that have to be returned to the user -> return reponse with http code 405 and body:``` +return response with http code 400 and body: +```javascript error: { - error_message: 'Validation failed', - error_code: 405, - fields: {email: 'Email has to be uniq'}, - error_extra: null or extra + message: 'Validation failed', + fields: {email: 'Email has to be unique'}, + extraData: null or extraData } ``` -**res.forbidden (fields, extra)** -- *object* **fields** - - Object which contain validation error, where key is field name and value is message -- *mixed* **extra** - - Any data that have to be returned to user +**res.forbidden (message, extraData)** +- *object* **message** +- Message why the user's request was forbidden +- *mixed* **extraData** +- Any data that have to be returned to the user -> return reponse with http code 405 and body:``` +return response with http code 403 and body: +```javascript error: { - error_message: 'Validation failed', - error_code: 405, - fields: {email: 'Email has to be uniq'}, - error_extra: null or extra + message: 'The user does not have enough permissions to access this file.', + extraData: null or extraData } ``` -**res.notFound (name, extra)** -- *string* **name** - - What server can't find -- *mixed* **extra** - - Any data that have to be returned to user +**res.notFound (uri, extraData)** +- *string* **uri** +- The URI the server cannot find +- *mixed* **extraData** +- Any data that have to be returned to the user -> return reponse with http code 404 and body:``` +return response with http code 404 and body: +```javascript error: { - error_message: 'User not found', - error_code: 404, - error_extra: null or extra + message: 'User not found', + code: 404, + extraData: null or extraData } ``` -**res.ok (extra)** -- *mixed* **extra** - - Any data that have to be returned to user +**res.ok (extraData)** +- *mixed* **extraData** +- Any data that have to be returned to the user -> return reponse with http code 404 and body:``` -{} or extra +return response with http code 200 and body: +```javascript +{} or extraData ``` -## Create own response -If you want to add you own responses you should pass absolute path to module constructor +## Create your own response +If you want to add your own responses you can pass the absolute path of the directory to the module constructor: ```javascript require('express-custom-response')(__dirname+ '/api/responses'); ``` -Then each script in this directory will be available as response method by filename. Example: +Then each script in this directory will be available as a response method. The method's name equals the file name. Example: ```javascript /** * api/responses/accepted.js @@ -93,10 +94,10 @@ Then each script in this directory will be available as response method by filen * This will be available in controllers as res.accepted(message); */ -module.exports = function(message){ - //this is points to express.response +module.exports = function(message) { + //"this" points to express.response this.status(202); this.send(message); } ``` -If you want to override exists pre-defined response simple add new script to your directory with name of response which you want to override +If you want to override existing pre-defined responses, simply add a new script to your directory with the name of the response which you want to override. diff --git a/lib/default/badRequest.js b/lib/default/badRequest.js new file mode 100644 index 0000000..a681aab --- /dev/null +++ b/lib/default/badRequest.js @@ -0,0 +1,13 @@ + +module.exports = function (message, code, fields, extraData) { + // Set status code + this.status(400); + this.send({ + error : { + message : message || 'Request could not be handled.', + code : code || undefined, + fields : fields || undefined, + extraData : extraData || undefined + } + }); +}; \ No newline at end of file diff --git a/lib/default/forbidden.js b/lib/default/forbidden.js index 5d2a0e9..9bef2c5 100644 --- a/lib/default/forbidden.js +++ b/lib/default/forbidden.js @@ -1,16 +1,12 @@ -/** - * Created by Sergii on 26.04.2015. - */ - -module.exports = function(message, extra){ - // Set status code - this.status(403); - this.send({ - error: { - error_message: message, - error_code: 403, - error_extra: extra || null - } - }); +module.exports = function (message, code, extraData) { + // Set status code + this.status(403); + this.send({ + error : { + message : message || 'This request is forbidden.', + code : code || undefined, + extraData : extraData || undefined + } + }); } \ No newline at end of file diff --git a/lib/default/notFound.js b/lib/default/notFound.js index 5851726..10052a4 100644 --- a/lib/default/notFound.js +++ b/lib/default/notFound.js @@ -1,16 +1,12 @@ -/** - * Created by Sergii on 26.04.2015. - */ - -module.exports = function(name, extra){ - // Set status code - this.status(404); - this.send({ - error: { - error_message: name + ' not found', - error_code: 404, - error_extra: extra || null - } - }); +module.exports = function (uri, extraData) { + // Set status code + this.status(404); + this.send({ + error : { + message : uri + ' not found.', + code : 404, + extraData : extraData || undefined + } + }); } \ No newline at end of file diff --git a/lib/default/ok.js b/lib/default/ok.js index 19e273f..f9445c8 100644 --- a/lib/default/ok.js +++ b/lib/default/ok.js @@ -1,10 +1,6 @@ -/** - * Created by Sergii on 26.04.2015. - */ - -module.exports = function(extra){ +module.exports = function(extraData){ // Set status code this.status(200); - this.send(extra || {}); -} \ No newline at end of file + this.send(extraData || undefined); +}; \ No newline at end of file diff --git a/lib/default/serverError.js b/lib/default/serverError.js index b0fbc6e..1eaf8ce 100644 --- a/lib/default/serverError.js +++ b/lib/default/serverError.js @@ -1,16 +1,15 @@ -/** - * Created by Sergii on 26.04.2015. - */ - -module.exports = function(extra){ - // Set status code - this.status(500); - this.send({ - error: { - error_message: 'We\'re sorry, a server error occurred. Please wait a bit and try again', - error_code: 500, - error_extra: extra || null - } - }); -} \ No newline at end of file +module.exports = function (message, code, extraData) { + // Set status code + this.status(500); + if(extraData instanceof Error) { + extraData = extraData.toString(); + } + this.send({ + error : { + message : message || 'We\'re sorry, a server error occurred. Please wait a bit and try again.', + code : code || undefined, + extraData : extraData || undefined + } + }); +}; diff --git a/lib/default/unauthorized.js b/lib/default/unauthorized.js new file mode 100644 index 0000000..be6a6b3 --- /dev/null +++ b/lib/default/unauthorized.js @@ -0,0 +1,11 @@ +module.exports = function (message, code, extraData) { + // Set status code + this.status(401); + this.send({ + error : { + message : message || 'Unauthorized..', + code : code || undefined, + extraData : extraData || undefined + } + }); +} diff --git a/lib/default/unprocessableEntity.js b/lib/default/unprocessableEntity.js new file mode 100644 index 0000000..085c59e --- /dev/null +++ b/lib/default/unprocessableEntity.js @@ -0,0 +1,12 @@ +"use strict"; + +module.exports = function (message, extraData) { + // Set status code + this.status(422); + this.send({ + error : { + message : message || 'The request is logically intact. However, it asks the server to execute an action which is not allowed by application logic.', + extraData : extraData || undefined + } + }); +}; diff --git a/lib/default/validation.js b/lib/default/validation.js deleted file mode 100644 index 05abfdc..0000000 --- a/lib/default/validation.js +++ /dev/null @@ -1,17 +0,0 @@ -/** - * Created by Sergii on 26.04.2015. - */ - - -module.exports = function(fields, extra){ - // Set status code - this.status(405); - this.send({ - error: { - error_message: 'Validation failed', - error_code: 405, - fields: fields, - error_extra: extra || null - } - }); -} \ No newline at end of file diff --git a/lib/express-custom-response.js b/lib/express-custom-response.js index 6065880..a223852 100644 --- a/lib/express-custom-response.js +++ b/lib/express-custom-response.js @@ -1,44 +1,47 @@ /* * express-custom-response - * https://github.com/fireridlle/express-custom-response + * https://github.com/SteffenLanger/express-custom-response * - * Copyright (c) 2015 Sergii Zinkevych + * Based on work by Sergii Zinkevych @https://github.com/fireridlle/express-custom-response + * + * Copyright (c) 2016 Steffen Langer * Licensed under the MIT license. */ 'use strict'; -var express = require('express'), - fs = require('fs') - - -var defaultReponse = ['serverError', 'validation', 'forbidden', 'notFound', 'ok']; - - -module.exports = function(reponse_path){ - - //add default reponses - defaultReponse.forEach(function(reponseName){ - - //if user didn't override default reponse - if(!express.response[reponseName]){ - express.response[reponseName] = require(__dirname + '/default/' + reponseName + '.js'); +var express = require('express'), + fs = require('fs'), + /** + * Adds all files to the express reponse object + * @param responsesDir Path to directory containing the response files + */ + addResponses = function (responsesDir) { + // Check if directory exists + if (fs.existsSync(responsesDir)) { + + // Get user responses + var userResponses = fs.readdirSync(responsesDir); + + // Add user responses to the express response object + userResponses.forEach(function (reponseFileName) { + var responseName = reponseFileName.split('.')[0]; + express.response[responseName] = require(responsesDir + '/' + reponseFileName); + }); + + } + else { + throw Error('The custom responses directory does not exist.'); + } + }; + +module.exports = function (responsesDir) { + + // Add default responses + addResponses(__dirname + '/default'); + + // Add custom responses + if(responsesDir) { + addResponses(responsesDir); } - - }); - - //check if directory exists - if(fs.existsSync(reponse_path)){ - - //get user reponses - var userRes = fs.readdirSync(reponse_path); - - //add user reponses - userRes.forEach(function(response){ - var reponseName = response.split('.')[0]; - express.response[reponseName] = require(reponse_path + '/' + response); - }); - - } - -} +}; diff --git a/package.json b/package.json index 5ff7e3a..48e22f0 100644 --- a/package.json +++ b/package.json @@ -2,22 +2,23 @@ "name": "express-custom-response", "description": "Create custom response methods for express.js", "version": "0.1.1", - "homepage": "https://github.com/fireridlle/express-custom-response", + "license" : "MIT", + "homepage": "https://github.com/SteffenLanger/express-custom-response", "author": { "name": "Sergii Zinkevych", "email": "s.zinkevych@gmail.com" }, "repository": { "type": "git", - "url": "git@github.com:fireridlle/express-custom-response.git" + "url": "git@github.com:SteffenLanger/express-custom-response.git" }, "bugs": { - "url": "https://github.com/fireridlle/express-custom-response/issues" + "url": "https://github.com/SteffenLanger/express-custom-response/issues" }, "licenses": [ { "type": "MIT", - "url": "https://github.com/fireridlle/express-custom-response/blob/master/LICENSE-MIT" + "url": "https://github.com/SteffenLanger/express-custom-response/blob/master/LICENSE-MIT" } ], "main": "lib/express-custom-response", @@ -25,4 +26,4 @@ "node": ">= 0.8.0" }, "keywords": [] -} \ No newline at end of file +}