# @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: ```sh 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: ```sh 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: ```typescript 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 ```typescript 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: ```typescript 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: ```typescript 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. ## License and Legal Information 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](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.