muzu
TypeScript icon, indicating that this package has built-in type declarations

0.1.0 • Public • Published

Muzu: TypeScript HTTP Request Handling and Routing Library

Muzu is a powerful TypeScript npm library designed for server-side HTTP request handling and routing. It provides developers with a seamless and efficient way to handle and route HTTP requests in their Node.js applications.

Installation

To incorporate Muzu into your project, you can easily install it via npm using the following command:

npm install muzu

Usage

Here is an in-depth example demonstrating how to leverage the capabilities of Muzu in your application:

import { MuzuServer, Request, Response } from 'muzu';

// Create a new instance of the MuzuServer
const app = new MuzuServer();
const { Post, Controller } = app;

@Controller('auth')
class TestController {

  @Post('login')
  login(req: Request, res: Response) {
    const { username, password } = req.body;
    
    // Implement your login logic here

    return {
      status: true
    };
  }

}

// Start the server and listen on port 8080
app.listen(8080);

In this example, we instantiate a new MuzuServer and utilize the @Post and @Controller decorators to define a route for handling POST requests at the '/login' endpoint within the 'auth' base route.

You can also handle other HTTP methods by using decorators such as @Get, @Put, @Delete, and @Patch accordingly.

Inside the login method, you can implement the necessary logic to handle the incoming request. In this case, we return a simple object { status: true } as the response.

The server is started and set to listen on port 8080 using the listen method of the MuzuServer instance.

Exception Handling

Muzu provides a comprehensive exception handling mechanism. You can create custom exception classes by extending the HttpException class and utilize them within your application. Let's see an example:

import { MuzuServer, Request, Response, HttpException } from 'muzu';

// Custom Exception Class
class UserNotFoundException extends HttpException {
  constructor(details?: string) {
    super('User Not Found!', 404, details);
  }
}

const app = new MuzuServer();
const { Post, Controller } = app;

@Controller('auth')
class TestController {

  @Get('user')
  getUser(req: Request, res: Response) {
    const username = req.body.username;
    
    // Throw a UserNotFoundException
    throw new UserNotFoundException({
      username
    });
    
    // The code below will not be executed because an exception is thrown.

    return {
      status: true
    };
  }

}

app.listen(8080);

In this example, we create a custom exception class UserNotFoundException that extends the HttpException class. Inside the getUser method of the TestController class, we throw an instance of this custom exception class. The thrown exception will be automatically caught and handled by the handleException method of the MuzuServer instance.

Exception Response

When an exception is thrown and caught, Muzu automatically generates an exception response with relevant details. Here is an example response for the UserNotFoundException:

{
  "status": 404,
  "message": "User Not Found!",
  "details": {
    "username": "muzu"
  }
}

Contributing

We welcome contributions from the community to enhance and improve Muzu. If you're interested in contributing, please visit our GitHub repository: Muzu GitHub Repo

License

Muzu is released under the MIT License. For more information, please refer to the License File.

If you have any questions or need further assistance, feel free to reach out to us. Happy coding with Muzu!

Readme

Keywords

none

Package Sidebar

Install

npm i muzu

Weekly Downloads

1

Version

0.1.0

License

ISC

Unpacked Size

44.6 kB

Total Files

54

Last publish

Collaborators

  • yldrmzffr