Skip to content

Commit

Permalink
Merge pull request #3 from SteffenLanger/master
Browse files Browse the repository at this point in the history
Improved grammar and changed some structure.
  • Loading branch information
fireridlle authored Aug 10, 2016
2 parents d1407c5 + 3b356e0 commit f449e6f
Show file tree
Hide file tree
Showing 12 changed files with 168 additions and 156 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
.idea
99 changes: 50 additions & 49 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -12,91 +12,92 @@ 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
*
* 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.
13 changes: 13 additions & 0 deletions lib/default/badRequest.js
Original file line number Diff line number Diff line change
@@ -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
}
});
};
24 changes: 10 additions & 14 deletions lib/default/forbidden.js
Original file line number Diff line number Diff line change
@@ -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
}
});
}
24 changes: 10 additions & 14 deletions lib/default/notFound.js
Original file line number Diff line number Diff line change
@@ -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
}
});
}
10 changes: 3 additions & 7 deletions lib/default/ok.js
Original file line number Diff line number Diff line change
@@ -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 || {});
}
this.send(extraData || undefined);
};
29 changes: 14 additions & 15 deletions lib/default/serverError.js
Original file line number Diff line number Diff line change
@@ -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
}
});
}
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
}
});
};
11 changes: 11 additions & 0 deletions lib/default/unauthorized.js
Original file line number Diff line number Diff line change
@@ -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
}
});
}
12 changes: 12 additions & 0 deletions lib/default/unprocessableEntity.js
Original file line number Diff line number Diff line change
@@ -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
}
});
};
17 changes: 0 additions & 17 deletions lib/default/validation.js

This file was deleted.

Loading

0 comments on commit f449e6f

Please sign in to comment.