A TypeScript library for managing HTTP status information, with detailed status classes.
Go to file
2024-05-29 14:16:36 +02:00
.vscode fix(core): update 2021-08-16 15:36:41 +02:00
docs fix(core): update 2019-01-02 01:00:53 +01:00
test fix(core): update 2019-01-02 02:08:12 +01:00
ts fix(core): update 2021-08-19 18:42:52 +02:00
.gitignore fix(core): update 2021-08-16 15:36:41 +02:00
.gitlab-ci.yml fix(core): update 2021-08-16 15:36:41 +02:00
npmextra.json update tsconfig 2024-04-14 18:24:47 +02:00
package-lock.json 1.0.12 2021-08-19 18:42:52 +02:00
package.json update description 2024-05-29 14:16:36 +02:00
readme.hints.md update tsconfig 2024-04-14 18:24:47 +02:00
readme.md update tsconfig 2024-04-14 18:24:47 +02:00
tsconfig.json update tsconfig 2024-04-01 21:41:24 +02:00

@push.rocks/smartstatus

status information in TypeScript

Install

To install @push.rocks/smartstatus, you can use npm (or yarn, or pnpm) by running the following command in your terminal:

npm install @push.rocks/smartstatus --save

Ensure you have TypeScript and a package to work with TypeScript in your project. If not, you might want to add TypeScript and ts-node (for a start) to your project:

npm install typescript ts-node --save-dev

Usage

Using @push.rocks/smartstatus allows you to handle HTTP status codes more effectively in TypeScript. Below are examples detailing how to utilize this module in various scenarios.

Getting Started

Ensure you import the module into your TypeScript file:

import * as smartstatus from '@push.rocks/smartstatus';

Retrieving a Specific Status

You can retrieve specific HTTP status information by using its respective class or a convenient method for fetching it by the status code string.

Example: Fetching 404 Not Found Status

import { HttpStatus } from '@push.rocks/smartstatus';

const notFoundStatus = smartstatus.HttpStatus.getHttpStatusByString('404');
console.log(notFoundStatus.code); // 404
console.log(notFoundStatus.text); // Not Found
console.log(notFoundStatus.description); // The requested resource could not be found but may be available in the future. Subsequent requests by the client are permissible.

This method getHttpStatusByString dynamically returns an instance of the respective status class filled with relevant data such as the HTTP code, text, and a descriptive message.

Handling Errors with HTTP Statuses

When building a web application or an API, handling different HTTP statuses becomes crucial. Here's an example of how you could use smartstatus to enrich error handling in your Express.js app:

import express from 'express';
import { HttpStatus } from '@push.rocks/smartstatus';

const app = express();

app.get('/some/endpoint', (req, res) => {
    // Some logic that might fail
    if (someConditionNotMet) {
        const unauthorizedStatus = HttpStatus.getHttpStatusByString('401');
        res.status(unauthorizedStatus.code).json({
            error: unauthorizedStatus.text,
            message: unauthorizedStatus.description,
        });
    }
});

In the above scenario, if a specific condition is not met (implying some authorization failure), we respond with a 401 Unauthorized status, along with a message detailing the issue.

Extending with Custom Statuses

While @push.rocks/smartstatus provides a structured manner to handle known HTTP statuses, there might be scenarios where you need custom statuses for internal signaling or specialized clients.

Extending or adding new statuses is straightforward:

import { HttpStatus } from '@push.rocks/smartstatus';

class Status418 extends HttpStatus {
    constructor() {
        super({ code: 418, text: 'I'm a teapot', description: 'The requested entity body is short and stout. Tip me over and pour me out.' });
    }
}

// Register the custom status
HttpStatus.addStatus('418', Status418);

// Retrieve and use the custom status
const customStatus = HttpStatus.getHttpStatusByString('418');
console.log(`${customStatus.code} ${customStatus.text}`); // 418 I'm a teapot

This approach ensures that your application can manage both standard and custom status codes effectively, maintaining a clear and expressive way to handle HTTP responses.

Conclusion

By integrating @push.rocks/smartstatus into your TypeScript applications, you gain a powerful tool to manage HTTP status codes, improving the readability and maintainability of your code when dealing with HTTP responses. Whether you are building web applications, APIs, or services, smartstatus offers a structured approach to handling success and error states across your application.

This repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the license file within this repository.

Please note: The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.

Trademarks

This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.

Company Information

Task Venture Capital GmbH
Registered at District court Bremen HRB 35230 HB, Germany

For any legal inquiries or if you require further information, please contact us via email at hello@task.vc.

By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.