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

elsombrero

v1.0.7

Published

It's a simple library easy to use that allow you to create HttpController on **Express.js** Node Typescript project.

Downloads

10

Readme

ElSombrero Typescript Http Decorator Library

It's a simple library easy to use that allow you to create HttpController on Express.js Node Typescript project.

Note Important

Do not use 1.0.3 and 1.0.4 version due to a bug.

Installation

You can install elsombrero library with npm

npm install --save elsombrero

Or with yarn

yarn add elsombrero

Example

First create your Express.js app

 import express from 'express'

 const app = express()

Create your Main class that take the Register decorator and pass your controllers, services and your Express.js app in your context.

import { Register } from 'elsombrero/core'

@Register({
  controllers: [],
  services: [],
  context: app
})
class Main{
  public async static main(){
    app.listen(8080, () => console.log('Servier is running!'))
  }
}

Main.main()

Create Your first Controller on index.controller.ts. You can pass on your controller parameter your base url string. If you pass nothing, your controller's base url is on the root (/). You can use the Methods Decorators to specify your HTTP Method and take url on argument. Like controller if you pass nothing, your url is on the root(/). You can use Express.js dynamic routing expression on your Method

  • @Get : Get HTTP
  • @Post : Post HTTP
  • @Put : Put HTTP
  • @Patch : Patch HTTP
  • @Delete : Delete HTTP
import { Controller, Get } from 'elsombrero/core'

@Controller()
export class Index{
  @Get()
  public index(): any{
    return { message: 'Hello World' }
  } 

  @Get('/article'): any{
    return { title: 'My Article' }
  }
}

Then Register your Controller on your Main class

import { Register } from 'elsombrero/core'
import { Index } from './index.controller.ts'

@Register({
  controllers: [Index],
  services: [],
  context: app
})
class Main{
  public async static main(){
    app.listen(8080, () => console.log('Servier is running!'))
  }
}

Main.main()

You can get your HttpContext on your controller method by specifying the HttpContext argument on your Method.

import { Controller, Post, HttpContext } from 'elsombrero/core'

@Controller('/article')
export class Article{
  @Get('/:id')
  public index({params, query}): any{
    return { message: 'Hello World' }
  } 

  @Post()
  public create({body}: HttpContext): any{
    return { body }
  }

  @Post('/not-found')
  public create({request, response}: HttpContext): any{
    // Do something
    response.status(404).json({message: 'Not Found'})
  }
}

Here is the HttpContext interface declaration

export interface HttpContext{
  request: Request 
  response: Response,
  body: any, 
  params: any,
  query: any,
  file: any,
  files: any[],
  headers: IncomingHttpHeaders
}

Create Middleware

You can create Middleware by implementing HttpHandler in your class which take HttpContext argument. You can pass it in your Method Decorator on second argument or on your controller second argument. You can pass as much middlewares as you want on your Controller or method decorator.

Example

import { Controller, Post, HttpContext, HttpHandler } from 'elsombrero/core'

class FirstHandler implements HttpHandler{
  public handle(context: HttpContext): void{
    console.log('First Middleware')
    context.next()
  }
}

class SecondHandler implements HttpHandler{
  public handle(context: HttpContext): void{
    console.log('Second Middleware')
    context.next()
  }
}

@Controller('/article', FirstHandler, SecondHandler)
export class Article{
  @Get('/:id', FirstHandler, SecondHandler)
  public index({params, query}): any{
    return { message: 'Hello World' }
  } 

  @Post(null, FirstHandler, SecondHandler)
  public create({body}: HttpContext): any{
    return { body }
  }

  @Post('/not-found', FirstHandler, SecondHandler)
  public create({request, response}: HttpContext): any{
    // Do something
    response.status(404).json({message: 'Not Found'})
  }
}

Change Http Response Status

You can create a custom response by using response.status or by using an instance of HttpResponse<T>. By default, data response is undefined and Http Status is 200. You can find all HttpStatus on HttpResponse.StatusCode.

Example

import { Controller, Post, HttpContext } from 'elsombrero/core'
import { HttpResponse } from 'elsombrero/common/response'

@Controller('/article')
export class Article{
  @Get('/:id')
  public index({params, query}): HttpResponse<any>{
    return new HttpResponse<any>({name: 'John Doe'}, 200);
  } 

  @Post('/not-found')
  public create({request, response}: HttpContext): HttpResponse<any>{
    // Do something
    return new HttpResponse<any>({name: 'John Doe'}, HttpResponse.StatusCode.Created);
  }
}

Exception

New Feature, You can know Handle Exception directly from ElSombrero Library, All you have to do is throwing a new exception on your method. HttpException is the base class and you can pass your own Exception or use all implemented Exception. By default HttpException class send code 500 and Internal Server Error message.

Example

import { Controller, Get, HttpContext } from 'elsombrero/core'
import { NotFoundException, HttpException } from 'elsombrero/common/exceptions'

@Controller('/article')
export class Article{
  @Get('/:id')
  public index({params, query}: HttpContext): any{
    const data = {my: 'custom data'}
    throw new NotFoundException<any>(data, 'Ressource Not Found')
  } 

  @Get('all/')
  public index({params, query}: HttpContext): any[]{
    throw new HttpException<any>({my: 'custom data'}, 400, 'Forbidden')
  }
  
  @Get('collection/:id')
  public index({params, query}: HttpContext): any[]{
    throw new NotFoundException<any>()
  }
}

You can throw Exception direclty on your HttpHandler.

Example

import { Controller, Get, HttpContext, HttpHandler } from 'elsombrero/core'
import { UnauthorizedException } from 'elsombrero/common/exceptions'

class AuthHandler implements HttpHandler{
  handle(context: HttpContext): void{
    throw new UnauthorizedException()
  }
}

@Controller('/article', AuthHandler)
export class Article{
  @Get('/:id')
  public index({params, query}: HttpContext): any{
    const data = {my: 'custom data'}
    return data
  } 
}

View Engine

By default ElSombrero Support Templating by returning View object on your controller method. You can put your views on your express view directory and use your favorite view engine. Second Optionnal Parameter is the data you to pass to your view.

import { Controller, View, Get, HttpContext } from 'elsombrero/core'

@Controller('/article')
export class Article{
  @Get('/:id')
  public index({query}: HttpContext): View{
    return new View('index', {query})
  } 
}

Injectable and Services (Dependencies Injection)

You can Inject dependencies on your class by using the @Injectable decorator then you can access to your dependencies by adding them on your constructor argument

Example

Create a file article.service.ts

import { Injectable } from 'elsombrero/core'

@Injectable()
export class Article{

  public findOne(id: string){
    return {id, name: 'My Article'}
  }
  
}

On Your article.controller.ts :

import { Controller, View, Get, HttpContext } from 'elsombrero/core'
import { ArticleService } from './article.service.ts'

@Controller('/article')
export class Article{

  public constructor(public article: ArticleService){}

  @Get('/:id')
  public index({params}: HttpContext): any{
    return this.article.findOne(params.id)
  } 
}

And don't forget to register your Services and Controllers

import { Register } from 'elsombrero/core'
import { Index } from './index.controller.ts'
import { ArticleService } from './article.service.ts'

@Register({
  controllers: [Article],
  services: [ArticleService],
  context: app
})
class Main{
  public async static main(){
    app.listen(8080, () => console.log('Servier is running!'))
  }
}

Main.main()

Enjoy ❤👌