Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
akeren committed Oct 1, 2020
0 parents commit 949cf0f
Show file tree
Hide file tree
Showing 8 changed files with 292 additions and 0 deletions.
30 changes: 30 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"extends": ["airbnb", "prettier", "plugin:node/recommended"],
"plugins": ["prettier"],
"rules": {
"prettier/prettier": "error",
"spaced-comment": "off",
"no-console": "warn",
"consistent-return": "off",
"func-names": "off",
"object-shorthand": "off",
"no-process-exit": "off",
"no-param-reassign": "off",
"no-return-await": "off",
"no-underscore-dangle": "off",
"class-methods-use-this": "off",
"prefer-destructuring": [
"error",
{
"object": true,
"array": false
}
],
"no-unused-vars": [
"error",
{
"argsIgnorePattern": "req|res|next|val"
}
]
}
}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
5 changes: 5 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"trailingComma": "es5",
"singleQuote": true,
"semi": true
}
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 Kater Akeren

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
128 changes: 128 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
# Http Status Codes

A collection of extensive easy access and resuable HTTP status codes as defined by Request for comments (RFC).

## Install

```shell
npm install http-status-codez --save
```

## Usage

```javascript
const express = require('express');
const { Response } = require('http-status-codez');
const User = require('./models/user');
const app = express();

app.get('/api/v1/users', catchAsync(async (req, res, next) => {
const users = await User.find();

res.status(Response.HTTP_OK).json({
status: 'success',
results: users.length,
data: {
users,
},
});

}));

app.get('/api/v1/users/:id', catchAsync(async (req, res, next) => {
const user = await User.findById(req.params.id);

if (!user) {
return next(
new AppError('User not found with that ID', Response.HTTP_NOT_FOUND);
);
}

res.status(Response.HTTP_OK).json({
status: 'success',
data: {
user,
},
});

}));
```

## Codes

| Code | Instance Properties | Phrase |
| ---- | ----------------------------------------- | ------------------------------- |
| 100 | HTTP_CONTINUE | Continue |
| 101 | HTTP_SWITCHING_PROTOCOLS | Switching Protocols |
| 102 | HTTP_PROCESSING | Processing |
| 103 | HTTP_EARLY_HINTS | Early Hints |
| 200 | HTTP_OK | OK |
| 201 | HTTP_CREATED | Created |
| 202 | HTTP_ACCEPTED | Accepted |
| 203 | HTTP_NON_AUTHORITATIVE_INFORMATION | Non Authoritative Information |
| 204 | HTTP_NO_CONTENT | No Content |
| 205 | HTTP_RESET_CONTENT | Reset Content |
| 206 | HTTP_PARTIAL_CONTENT | Partial Content |
| 207 | HTTP_MULTI_STATUS | Multi-Status |
| 208 | HTTP_ALREADY_REPORTED | Already Reported |
| 226 | HTTP_IM_USED | IM Used |
| 300 | HTTP_MULTIPLE_CHOICES | Multiple Choices |
| 301 | HTTP_MOVED_PERMANENTLY | Moved Permanently |
| 302 | HTTP_FOUND | Found |
| 303 | HTTP_SEE_OTHER | See Other |
| 304 | HTTP_NOT_MODIFIED | Not Modified |
| 305 | HTTP_USE_PROXY | Use Proxy |
| 306 | HTTP_RESERVED | Reserved |
| 307 | HTTP_TEMPORARY_REDIRECT | Temporary Redirect |
| 308 | HTTP_PERMANENTLY_REDIRECT | Permanent Redirect |
| 400 | HTTP_BAD_REQUEST | Bad Request |
| 401 | HTTP_UNAUTHORIZED | Unauthorized |
| 402 | HTTP_PAYMENT_REQUIRED | Payment Required |
| 403 | HTTP_FORBIDDEN | Forbidden |
| 404 | HTTP_NOT_FOUND | Not Found |
| 405 | HTTP_METHOD_NOT_ALLOWED | Method Not Allowed |
| 406 | HTTP_NOT_ACCEPTABLE | Not Acceptable |
| 407 | HTTP_PROXY_AUTHENTICATION_REQUIRED | Proxy Authentication Required |
| 408 | HTTP_REQUEST_TIMEOUT | Request Timeout |
| 409 | HTTP_CONFLICT | Conflict |
| 410 | HTTP_GONE | Gone |
| 411 | HTTP_LENGTH_REQUIRE | Length Required |
| 412 | HTTP_PRECONDITION_FAILED | Precondition Failed |
| 413 | HTTP_REQUEST_ENTITY_TOO_LARGE | Request Entity Too Large |
| 414 | HTTP_REQUEST_URI_TOO_LONG | Request-URI Too Long |
| 415 | HTTP_UNSUPPORTED_MEDIA_TYPE | Unsupported Media Type |
| 416 | HTTP_REQUESTED_RANGE_NOT_SATISFIABLE | Requested Range Not Satisfiable |
| 417 | HTTP_EXPECTATION_FAILED | Expectation Failed |
| 418 | HTTP_I_AM_A_TEAPOT | I'm a teapot |
| 419 | HTTP_INSUFFICIENT_SPACE_ON_RESOURCE | Insufficient Space on Resource |
| 420 | HTTP_METHOD_FAILURE | Method Failure |
| 421 | HTTP_MISDIRECTED_REQUEST | Misdirected Request |
| 422 | HTTP_UNPROCESSABLE_ENTITY | Unprocessable Entity |
| 423 | HTTP_LOCKED | Locked |
| 424 | HTTP_FAILED_DEPENDENCY | Failed Dependency |
| 425 | HTTP_TOO_EARLY | Too Early |
| 426 | HTTP_UPGRADE_REQUIRED | Upgrade Required |
| 428 | HTTP_PRECONDITION_REQUIRED | Precondition Required |
| 429 | HTTP_TOO_MANY_REQUESTS | Too Many Requests |
| 431 | HTTP_REQUEST_HEADER_FIELDS_TOO_LARGE | Request Header Fields Too Large |
| 451 | HTTP_UNAVAILABLE_FOR_LEGAL_REASONS | Unavailable For Legal Reasons |
| 500 | HTTP_INTERNAL_SERVER_ERROR | Internal Server Error |
| 501 | HTTP_NOT_IMPLEMENTED | Not Implemented |
| 502 | HTTP_BAD_GATEWAY | Bad Gateway |
| 503 | HTTP_SERVICE_UNAVAILABLE | Service Unavailable |
| 504 | HTTP_GATEWAY_TIMEOUT | Gateway Timeout |
| 505 | HTTP_VERSION_NOT_SUPPORTED | HTTP Version Not Supported |
| 506 | HTTP_VARIANT_ALSO_NEGOTIATES_EXPERIMENTAL | Variant Also Negotiates |
| 507 | HTTP_INSUFFICIENT_STORAGE | Insufficient Storage |
| 508 | HTTP_LOOP_DETECTED | Loop Detecte |
| 5010 | HTTP_NOT_EXTENDED | Not Extended |
| 511 | HTTP_NETWORK_AUTHENTICATION_REQUIRED | Network Authentication Required |

## Contributing

If you discover a security vulnerability, please create an issue. All security vulnerabilities will be promptly addressed and appreciated.

```shell
npm version [major | minor | patch]
npm publish
```
32 changes: 32 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "http-status-codez",
"version": "1.1.0",
"description": "A collection of easy access to HTTP codes status response as defined by Request for comments (RFC)",
"main": "src/index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/akeren/http-status-codez.git"
},
"keywords": [
"http",
"api",
"node",
"HttpStatus",
"clinet",
"server",
"response",
"status",
"codes",
"codez"
],
"author": "Kater Akeren",
"license": "MIT",
"bugs": {
"url": "https://github.com/akeren/http-status-codez/issues",
"email": "[email protected]"
},
"homepage": "https://github.com/akeren/http-status-codez#readme"
}
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./utils/Response');
74 changes: 74 additions & 0 deletions src/utils/Response.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/**
*
* @author Kater, Akeren
*/

class Response {
static HTTP_CONTINUE = 100;
static HTTP_SWITCHING_PROTOCOLS = 101;
static HTTP_PROCESSING = 102;
static HTTP_EARLY_HINTS = 103;
static HTTP_OK = 200;
static HTTP_CREATED = 201;
static HTTP_ACCEPTED = 202;
static HTTP_NON_AUTHORITATIVE_INFORMATION = 203;
static HTTP_NO_CONTENT = 204;
static HTTP_RESET_CONTENT = 205;
static HTTP_PARTIAL_CONTENT = 206;
static HTTP_MULTI_STATUS = 207;
static HTTP_ALREADY_REPORTED = 208;
static HTTP_IM_USED = 226;
static HTTP_MULTIPLE_CHOICES = 300;
static HTTP_MOVED_PERMANENTLY = 301;
static HTTP_FOUND = 302;
static HTTP_SEE_OTHER = 303;
static HTTP_NOT_MODIFIED = 304;
static HTTP_USE_PROXY = 305;
static HTTP_RESERVED = 306;
static HTTP_TEMPORARY_REDIRECT = 307;
static HTTP_PERMANENTLY_REDIRECT = 308;
static HTTP_BAD_REQUEST = 400;
static HTTP_UNAUTHORIZED = 401;
static HTTP_PAYMENT_REQUIRED = 402;
static HTTP_FORBIDDEN = 403;
static HTTP_NOT_FOUND = 404;
static HTTP_METHOD_NOT_ALLOWED = 405;
static HTTP_NOT_ACCEPTABLE = 406;
static HTTP_PROXY_AUTHENTICATION_REQUIRED = 407;
static HTTP_REQUEST_TIMEOUT = 408;
static HTTP_CONFLICT = 409;
static HTTP_GONE = 410;
static HTTP_LENGTH_REQUIRED = 411;
static HTTP_PRECONDITION_FAILED = 412;
static HTTP_REQUEST_ENTITY_TOO_LARGE = 413;
static HTTP_REQUEST_URI_TOO_LONG = 414;
static HTTP_UNSUPPORTED_MEDIA_TYPE = 415;
static HTTP_REQUESTED_RANGE_NOT_SATISFIABLE = 416;
static HTTP_EXPECTATION_FAILED = 417;
static HTTP_I_AM_A_TEAPOT = 418;
static HTTP_INSUFFICIENT_SPACE_ON_RESOURCE = 419;
static HTTP_METHOD_FAILURE = 420;
static HTTP_MISDIRECTED_REQUEST = 421;
static HTTP_UNPROCESSABLE_ENTITY = 422;
static HTTP_LOCKED = 423;
static HTTP_FAILED_DEPENDENCY = 424;
static HTTP_TOO_EARLY = 425;
static HTTP_UPGRADE_REQUIRED = 426;
static HTTP_PRECONDITION_REQUIRED = 428;
static HTTP_TOO_MANY_REQUESTS = 429;
static HTTP_REQUEST_HEADER_FIELDS_TOO_LARGE = 431;
static HTTP_UNAVAILABLE_FOR_LEGAL_REASONS = 451;
static HTTP_INTERNAL_SERVER_ERROR = 500;
static HTTP_NOT_IMPLEMENTED = 501;
static HTTP_BAD_GATEWAY = 502;
static HTTP_SERVICE_UNAVAILABLE = 503;
static HTTP_GATEWAY_TIMEOUT = 504;
static HTTP_VERSION_NOT_SUPPORTED = 505;
static HTTP_VARIANT_ALSO_NEGOTIATES_EXPERIMENTAL = 506;
static HTTP_INSUFFICIENT_STORAGE = 507;
static HTTP_LOOP_DETECTED = 508;
static HTTP_NOT_EXTENDED = 510;
static HTTP_NETWORK_AUTHENTICATION_REQUIRED = 511;
}

module.exports = { Response };

0 comments on commit 949cf0f

Please sign in to comment.