1.0.1 • Published 4 months ago

@stexcore/http-status v1.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
4 months ago

@stexcore/http-status

NPM Version License TypeScript

A collection of HTTP status codes for general use in any HTTP framework.
Simplifies the management of HTTP responses and errors with a clean and easy-to-use interface. Fully compatible with TypeScript and any HTTP framework.


🚀 Installation

Install the package from NPM using the following command:

npm install @stexcore/http-status

📦 Usage

Basic Import and Example

import httpStatus from "@stexcore/http-status";

// Create a successful response (200 OK)
const response = httpStatus.ok("Operation successful", { id: 123 });
console.log(response);

// Handle an error (404 Not Found)
const error = httpStatus.notFound("The requested resource was not found");
console.log(error);

// Validate if an object is an HttpError
if (httpStatus.isHttpError(error)) {
    console.error("This is an HTTP error!", error.message);
}

🌟 Main Methods

Success Responses (2xx)

MethodDescription
okCreates a 200 OK response.
createdCreates a 201 Created response.
acceptedCreates a 202 Accepted response.
nonAuthoritativeInfoCreates a 203 Non-Authoritative Information response.
noContentCreates a 204 No Content response.
resetContentCreates a 205 Reset Content response.
partialContentCreates a 206 Partial Content response.
multiStatusCreates a 207 Multi-Status response.
alreadyReportedCreates a 208 Already Reported response.
imUsedCreates a 226 IM Used response.

Redirections (3xx)

MethodDescription
movedPermanentlyCreates a 301 Moved Permanently response.
foundCreates a 302 Found response.
seeOtherCreates a 303 See Other response.
notModifiedCreates a 304 Not Modified response.
useProxyCreates a 305 Use Proxy response.
temporaryRedirectCreates a 307 Temporary Redirect response.
permanentRedirectCreates a 308 Permanent Redirect response.

Client Errors (4xx)

MethodDescription
badRequestCreates a 400 Bad Request error.
unauthorizedCreates a 401 Unauthorized error.
paymentRequiredCreates a 402 Payment Required error.
forbiddenCreates a 403 Forbidden error.
notFoundCreates a 404 Not Found error.
methodNotAllowedCreates a 405 Method Not Allowed error.
notAcceptableCreates a 406 Not Acceptable error.
proxyAuthRequiredCreates a 407 Proxy Authentication Required error.
requestTimeoutCreates a 408 Request Timeout error.
conflictCreates a 409 Conflict error.
goneCreates a 410 Gone error.
lengthRequiredCreates a 411 Length Required error.
preconditionFailedCreates a 412 Precondition Failed error.

Server Errors (5xx)

MethodDescription
internalServerErrorCreates a 500 Internal Server Error.
notImplementedCreates a 501 Not Implemented error.
badGatewayCreates a 502 Bad Gateway error.
serviceUnavailableCreates a 503 Service Unavailable error.
gatewayTimeoutCreates a 504 Gateway Timeout error.
httpVersionNotSupportedCreates a 505 HTTP Version Not Supported error.
variantAlsoNegotiatesCreates a 506 Variant Also Negotiates error.
insufficientStorageCreates a 507 Insufficient Storage error.
loopDetectedCreates a 508 Loop Detected error.
bandwidthLimitExceededCreates a 509 Bandwidth Limit Exceeded error.
notExtendedCreates a 510 Not Extended error.
networkAuthenticationRequiredCreates a 511 Network Authentication Required error.

⚙️ Configuration

This library is ready to use directly out of the box. TypeScript is optional and supported: declaration files (.d.ts) are included to enable seamless integration for TypeScript projects. If you're using JavaScript, simply install the library and start using it without additional setup.


📖 Technical Details

This package includes key classes for managing HTTP statuses and errors:

  • HttpStatus: A base class for representing HTTP responses.
  • HttpError: Extends Error to represent HTTP errors.

💡 Example with Express

Here's a possible use case for integrating this library with Express, a widely used Node.js framework:

import express from "express";
import httpStatus from "@stexcore/http-status";

const app = express();

// A sample route to return a successful response
app.get("/success", (req, res) => {
    res.json(httpStatus.ok("The operation was successful!", { data: "Example data" }));
});

// A route to simulate a server error
app.get("/error", (req, res) => {
    res.status(500).json(httpStatus.internalServerError("An unexpected error occurred.", { traceId: "123-abc" }));
});

// Start the server
const PORT = 3000;
app.listen(PORT, () => {
    console.log(`Server is running on http://localhost:\${PORT}`);
});

In this example:

  • The /success route demonstrates a typical 200 OK response with additional data.
  • The /error route showcases how you can return a structured 500 Internal Server Error.

🛠️ Development

If you'd like to contribute or report issues, visit our GitHub repository:
https://github.com/stexcore/http-status


📝 License

This project is licensed under the MIT license. See the LICENSE file for more details.


1.0.1

4 months ago

1.0.1-test

4 months ago

1.0.0

4 months ago