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

lightyjs

v0.0.6

Published

An expressjs decorator framework

Downloads

7

Readme

lightyjs

A decorator based expressjs utility library

This library provide a simple interface to class-transformer and class-validator with a decorator based system like nestjs

This also adds support for async routes and the responses from the routers can be provided with a simple return statement

How to install

yarn add reflect-metadata class-transformer class-validator lightyjs

How to use

Configuration

This library uses the experimental decorator feature provided by typescript add the following, to your typescript configuration

{
    "compilerOptions": {
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true
    }
}

Create a controller

// example.ts
import { Controller, Post } from "lightyjs";

@Controller("/example")
class ExampleController {
    @Post("/hello")
    async HelloWorld() {
        // this will call res.json() under the hood
        return { message: "Hello World" };
    }
}
POST /example/hello => { message: "Hello World" }

Add the controllers to your app

// index.ts
import express from "express";
import { addController } from "lightyjs";
import ExampleController from "./example";

const app = express();
addControllers(app, [ExampleController]);

Middlewares

Its possible to create custom middlewares in decorator form

It uses the same syntax as a classic expressjs middleware

import { Request, Response, NextFunction } from "express";
import { createMiddleware } from "lightyjs";

const CustomDecorator = createMiddleware((req: Request, res: Response, next: NextFunction) => {
    // Your middleware function

    return next(); // This must be called when the middleware passes
});

const CustomAsyncDecorator = createMiddleware(async (req: Request, res: Response, next: NextFunction) => {
    // Your middleware function

    return next(); // This must be called when the middleware passes
});

How to use custom decorators/middlewares

The order of execution of the middleware is based on the order of the declaration (pretty standard)

// example2.ts
@Controller("/example/v2")
class Example2Controller {
    @Get("/test")
    @CustomDecorator() // This will be called first
    @CustomAsyncDecorator() // This will be called second
    async Test() {}
}

Parameters decorators

This library provides a simple way to get possible parameters and values from the request using decorators

This is the list of possible decorators :

  • @Body - Equivalent to req.body
  • @Params - Equivalent to req.params
  • @Query - Equivalent to req.query
  • @Headers - Equivalent to req.headers
  • @Request - Equivalent to req
  • @Response - Equivalent to res
// example2.ts
@Controller("/example/v3")
class Example2Controller {
    @Post("/test")
    async Test(@Body() body: any) {}
}

It's possible to create DataTransferObjects (DTOs) in combination with this parameter decorators to perform validation and cleanup of the data

Create a DTO

Using the library class-validator is possible to create a dto using a simple class

Check the repo class-validator for the usage of this package

// test.dto.ts
const {IsString , IsNotEmpty} from "class-validator"

export class TestDTO{
    @IsString()
    @IsNotEmpty()
    text : string

}

Using Dto with parameters decorator

Getting back to the previous example is possible to integrate a dto simply by importing the class not the type and passing it as a typescript type

// example2.ts

import TestDTO from "./test.dto";

@Controller("/example/v3")
class Example2Controller {
    @Post("/test")
    async Test(@Body() body: TestDTO) {}
}

This will use hunder the hood class-transformer library and will perform the validation of the data

In this first release is not possible to customize the error handling process of the validation, the error thrown is a ValidationFailed that can be handled with an express global error handler