npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

nust-ts

v1.0.12

Published

Nust TS is a lightweight package designed to simplify the process of creating APIs in TypeScript. It leverages the power of [uWebSockets](https://github.com/uNetworking/uWebSockets.js), offering a fast and efficient web server. This package provides decor

Downloads

10

Readme

Nust TS

Nust TS is a lightweight package designed to simplify the process of creating APIs in TypeScript. It leverages the power of uWebSockets, offering a fast and efficient web server. This package provides decorators and utilities to easily define controllers and routes, offering a developer-friendly approach to building web APIs with minimal boilerplate and enhanced readability.

Features

  • High Performance: Built with uWebSockets, known for its high performance and efficiency. Nust TS is up to 8.5x faster than Fastify and 20x faster than Express, thanks to its use of C++ addons.
  • Lightweight: Only uses reflect-metadata, class-validator, and uWebsockets.
  • Parameter Scanning and DTO Validation: Optionally provide DTO classes for automatic validation of request parameters using class-validator.
  • Error Handling: Throw an object with status 400 and a message to handle errors gracefully.
  • Auto Controller Import: Automatically imports controllers for streamlined setup. Manual import is also supported.
  • JavaScript Modular Approach: Leverages JavaScript classes and importing for a clean and modular codebase.

Getting Started

Quick Start

  1. Create your project directory and navigate into it:
cd my-project
  1. Initialize a new Nust TS project:
npx nust-ts
  1. Install the dependencies:
npm install
  1. Start the development server with watch mode:
npm run watch

Your project will now be set up and running with hot-reloading enabled.

Installation

To install Nust TS, run:

npm install nust-ts

Example Usage

Create a simple Nust TS server with controller and routes.

src/index.ts

import { bootstrap, importControllers } from "nust-ts";
import path from "path";

const main = async () => {
  const basePath = path.resolve(__dirname, "./controllers");
  await importControllers(basePath);
  await bootstrap();
};

main();

src/controllers/user.controller.ts

import { IsMongoId, IsNotEmpty, IsString } from "class-validator";
import { Controller, Route } from "nust-ts";

class UserDto {
  @IsNotEmpty()
  @IsString()
  name: string;
}

class UserIdDto {
  @IsNotEmpty()
  @IsMongoId()
  userId: string;
}

@Controller("/users")
export class UserController {
  @Route("post", "/")
  createUser(body: UserDto) {
    return { body };
  }

  @Route("get", "/:userId")
  getUser(params: UserIdDto) {
    return { params };
  }

  @Route("put", "/:userId")
  updateUser(params: UserIdDto, body: UserDto) {
    return { params, body };
  }
  
  @Route("delete", "/:userId")
  deleteUser(params: UserIdDto) {
    return { params };
  }
}

Running the Server

Set the environment variable PORT and run your server:

npx ts-node src/index.ts

Your server will start running at http://localhost:9001.

Feature Highlights

  • High Performance: Built with uWebSockets, known for its high performance and efficiency. Nust TS is up to 8.5x faster than Fastify and 20x faster than Express, thanks to its use of C++ addons.
  • Lightweight: Only uses reflect-metadata, class-validator, and uWebsockets.
  • Parameter Scanning and DTO Validation: Optionally provide DTO classes for automatic validation of request parameters using class-validator.
  • Error Handling: Throw an object with status 400 and a message to handle errors gracefully.
  • Auto Controller Import: Automatically imports controllers for streamlined setup. Manual import is also supported.
  • JavaScript Modular Approach: Leverages JavaScript classes and importing for a clean and modular codebase.

Parameter Scanning and DTO Validation

To validate request parameters, define DTO classes with class-validator decorators. Nust TS automatically passes body, params, and query parameters to the handler methods. For example:

import { IsMongoId, IsNotEmpty, IsString } from "class-validator";
import { Controller, Route } from "nust-ts";

class UserDto {
  @IsNotEmpty()
  @IsString()
  name: string;
}

class UserIdDto {
  @IsNotEmpty()
  @IsMongoId()
  userId: string;
}

@Controller("/users")
export class UserController {
  @Route("post", "/")
  createUser(body: UserDto) {
    return { body };
  }

  @Route("get", "/:userId")
  getUser(params: UserIdDto) {
    return { params };
  }

  @Route("put", "/:userId")
  updateUser(params: UserIdDto, body: UserDto) {
    return { params, body };
  }

  @Route("delete", "/:userId")
  deleteUser(params: UserIdDto, query: any) {
    return { params, query };
  }
}

Error Handling

To handle errors, throw an object with a status property (set to 400 for bad requests) and a message property. For example:

import { IsNotEmpty, IsString } from "class-validator";
import { Controller, Route } from "nust-ts";

class UserDto {
  @IsNotEmpty()
  @IsString()
  name: string;
}

@Controller("/users")
export class UserController {
  @Route("post", "/")
  createUser(body: UserDto) {
    if (!body.name) {
      throw { status: 400, message: "Name is required" };
    }
    return { body };
  }
}

Auto Controller Import

Nust TS supports automatic import of controllers. To use it, place your controllers in a specific directory and call importControllers with the path to that directory. Manual import is also supported if you prefer.

Automatic Import

import { bootstrap, importControllers } from "nust-ts";
import path from "path";

const main = async () => {
  const basePath = path.resolve(__dirname, "./controllers");
  await importControllers(basePath);
  await bootstrap();
};

main();

Manual Import

import { bootstrap } from "nust-ts";
import { UserController } from "./controllers/user.controller";

const main = async () => {
  await bootstrap();
};

main();

JavaScript Modular Approach

Nust TS follows JavaScript's modular approach, using classes and imports to maintain a clean and organized codebase.

// user.controller.js
import { Controller, Route } from "nust-ts";

@Controller("/users")
class UserController {
  @Route("post", "/")
  createUser(body) {
    return { body };
  }

  @Route("get", "/:userId")
  getUser(params) {
    return { params };
  }
}

export default UserController;
// index.js
import { bootstrap, importControllers } from "nust-ts";
import path from "path";

const main = async () => {
  const basePath = path.resolve(__dirname, "./controllers");
  await importControllers(basePath);
  await bootstrap();
};

main();

By leveraging Nust TS, you can build efficient, scalable, and maintainable web APIs with ease. Enjoy the simplicity and performance boost that comes with this lightweight framework!