1.0.4 • Published 3 years ago

statushttp v1.0.4

Weekly downloads
7
License
BSD-3-Clause
Repository
github
Last release
3 years ago

HTTP Status Codes (statushttp)

A JavaScript library for working with HTTP status codes.

All status codes are taken from the IANA Status Code Registry.

The library works both in the front-end (VanillaJS) and the back-end (NodeJS), supporting a variety of imports. It can also be used as a command-line tool to work with HTTP status codes.

Installation

Install it via NPM.

# Local
npm i statushttp

# Global
npm i -g statushttp

Quick Start

const { sc } = require('statushttp');
// Or import { sc } from 'statushttp'; for modules

// Passing case-insensitive text with spaces
console.log(sc('not found').Code); // 404
console.log(sc(201).Desc); // "Created"

// Identifier
console.log(sc(405).Text); // "METHOD_NOT_ALLOWED"

Importing

  • CommonJS
    const { statusCode, statusText, ... } = require('statushttp');
  • ES6
    import { statusCode, statusText, ... } from 'statushttp';
  • AMD (require.js)
    require(['main.js'], () => {
      // Assuming path for 'statushttp' configured at 'node_modules/statushttp/statushttp' inside main.js
    	require(['statushttp'], ({
    		statusCode,
    		statusText,
    		...
    	}) => {
    		...
    	});
    });
  • HTML + VanillaJS

    <!-- Minified -->
    <script src='node_modules/statushttp/statushttp.min.js'></script>
    
    <!-- Not minified -->
    <script src='node_modules/statushttp/statushttp.js'></script>
    
    <script>
    			// statusText, statusCode, et cetera, are accessible now.
    </script>

List of Identifiers Used

The following identifiers are used in statushttp to identify various HTTP statuses.

IdentifierCode
CONTINUE100
SWITCHING_PROTOCOLS101
PROCESSING102
EARLY_HINTS103
OK200
CREATED201
ACCEPTED202
NON_AUTHORITATIVE_INFORMATION203
NO_CONTENT204
RESET_CONTENT205
PARTIAL_CONTENT206
MULTI_STATUS207
ALREADY_REPORTED208
IM_USED226
MULTIPLE_CHOICES300
MOVED_PERMANENTLY301
FOUND302
SEE_OTHER303
NOT_MODIFIED304
USE_PROXY305
UNUSED306
TEMPORARY_REDIRECT307
PERMANENT_REDIRECT308
BAD_REQUEST400
UNAUTHORIZED401
PAYMENT_REQUIRED402
FORBIDDEN403
NOT_FOUND404
METHOD_NOT_ALLOWED405
NOT_ACCEPTABLE406
PROXY_AUTHENTICATION_REQUIRED407
REQUEST_TIMEOUT408
CONFLICT409
GONE410
LENGTH_REQUIRED411
PRECONDITION_FAILED412
PAYLOAD_TOO_LARGE413
URI_TOO_LONG414
UNSUPPORTED_MEDIA_TYPE415
RANGE_NOT_SATISFIABLE416
EXPECTATION_FAILED417
MISDIRECTED_REQUEST421
UNPROCESSABLE_ENTITY422
LOCKED423
FAILED_DEPENDENCY424
TOO_EARLY425
UPGRADE_REQUIRED426
PRECONDITION_REQUIRED428
TOO_MANY_REQUESTS429
REQUEST_HEADER_FIELDS_TOO_LARGE431
UNAVAILABLE_FOR_LEGAL_REASONS451
INTERNAL_SERVER_ERROR500
NOT_IMPLEMENTED501
BAD_GATEWAY502
SERVICE_UNAVAILABLE503
GATEWAY_TIMEOUT504
HTTP_VERSION_NOT_SUPPORTED505
VARIANT_ALSO_NEGOTIATES506
INSUFFICIENT_STORAGE507
LOOP_DETECTED508
NOT_EXTENDED510
NETWORK_AUTHENTICATION_REQUIRED511

Usage (JavaScript)

The following objects are accessible from statushttp:

  • statusCode : Textual identifiers mapped to numeric codes.
  • statusText : Numeric codes mapped to textual identifiers.
  • statusDesc : User-friendly status messages/descriptions.
  • statusClass : Enumerations for status classes.
  • StatusCode : A class implementation for status codes to provide ease in working with status codes.
  • sc : An alias for the StatusCode class constructor.

Examples:

console.log(statusCode.NOT_FOUND); // 404
console.log(statusText[502]); // BAD_GATEWAY
console.log(statusDesc[301]); // Moved Permanently
console.log(statusClass.SUCCESS); // 2

The StatusCode class

The StatusCode class provides functionality to facilitate working with HTTP status codes.

The class itself is inherited from String, and behaves as a user-friendly message string by default.

Constructor

The constructor receives one parameter, which may be a number representing the status code, or the status text (case-insensitive, with or without the hyphens and parentheses).

Examples:

let   successStatus = new StatusCode(200);
      successStatus = sc(200); // Using the alias
const createdStatus = sc('created');
const continueStatus = sc('ConTinUe');

Object Properties

  • Code : The numerical code for the status.
  • Text : A textual identifier for the status.
  • Desc : A user-friendly status message.
  • Class : The status class enumeration for the status (can be matched from statusClass properties).

Examples:

  • console.log(successStatus.Text); // OK
    
    console.log(createdStatus); // Created
    console.log(createdStatus.Code); // 201
    
    console.log(continueStatus.Desc); // Continue
    console.log(continueStatus.Class); // 1

Usage (Command-Line)

statushttp can directly be used from the command-line if installed globally. Alterntively, in a project-specific installation, it may be executed using npx.

statushttp <value> [options] # Global installation
npx statushttp <value> [options] # Local installation

The value here may be a number representing the status code, or the status text (case-insensitive, with or without the hyphens and parentheses).

Even if value contains spaces, it doesn't need to be sent as one parameter enclosed in quotes.

Options: | Option | Description | | --- | --- | | -l, --list | Show all status codes. | | -?, --help | Show help. | | -v, --version | Displays the version. |

Examples:

> statushttp ok

Status Code: 200
Textual Identifier: OK
Message: OK
Class: SUCCESS

> statushttp 304

Status Code: 304
Textual Identifier: NOT_MODIFIED
Message: Not Modified
Class: REDIRECTIONAL

> statushttp temporary redirect

Status Code: 307
Textual Identifier: TEMPORARY_REDIRECT
Message: Temporary Redirect
Class: REDIRECTIONAL

> statushttp 'not found'

Status Code: 404
Textual Identifier: NOT_FOUND
Message: Not Found
Class: CLIENT_ERROR

> statushttp --list

┌──────┬─────────────────────────────────┐
│ Code │ Message                         │
├──────┼─────────────────────────────────┤
│ 100  │ Continue                        │
├──────┼─────────────────────────────────┤
│ 101  │ Switching Protocols             │
├──────┼─────────────────────────────────┤
│ ...  │ ...                             │
├──────┼─────────────────────────────────┤
│ 510  │ Not Extended                    │
├──────┼─────────────────────────────────┤
│ 511  │ Network Authentication Required │
└──────┴─────────────────────────────────┘

Examples

  • An Express server:

    const express = require('express');
    const cors = require('cors');
    const { statusCode, statusDesc } = require('statushttp');
    
    const app = express();
    
    app.use(cors());
    
    app.route('/*')
    .all((req, res) => {
    	res.status(statusCode.NOT_FOUND).end(statusDesc[statusCode.NOT_FOUND]);
    });
    
    app.listen(5000);
  • A React application:

    import react, { useState } from 'react';
    import axios from 'axios';
    import { sc } from 'statushttp';
    
    const App = () => {
      const [resp, setResp] = useState(null);
    
      if (resp == null) {
        axios.get('http://localhost:5000')
          .then(r => setResp(r))
          .catch(e => setResp(e.response));
      }
    
      return <h1>{sc(resp?.status || 200)}</h1>;
    };
    
    export default App;

Development

  • Clone the Git repository.
    git clone https://github.com/paramsiddharth/statushttp.git
  • Install the dependencies.
    cd statushttp
    npm ci
  • Install the development code as a global module.
    npm i -g .
  • Build the ESM wrapper and the minified form of the development code.
    npm run build
  • Test the library.
    npm test
    Test with your own values.
    cd tests
    node test 'service unavailable'
    Test the minified web version by running tests/Web.html.

Contribution

Contributions are welcome via GitHub.

  • Raise issues via GitHub.
  • Fork the repository.
  • Clone the fork.
  • Setup the development environment as described above.
  • Commit and push changes to the forked repository.
  • Create a pull request.

Made with ❤ by Param.