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

tropa

v1.1.1

Published

Node.js routing library powered by Babel

Downloads

8

Readme

tropa

Tropa is a simple decorators based routing library for a Node.js applications, built using awesome Babel compiler.

Init App

You can easily create your tropa app using create-tropa generator:

npm init tropa my-app

It is possible to generate TypeScript app using following option --lang=ts

npm init tropa my-app-ts -- --lang=ts

Usage

Here is an example of Hello World app.

import { Get, listener } from 'tropa'
import http from 'http'

class Root {
  @Get('/') 
  hello() {
    return { Hello: 'World' }
  }
}

http.createServer(listener).listen(3000)

Routing decorators

import {
  Get,
  Post,
  Patch,
  Put,
  Delete,
} from 'tropa'

You are able to define your route handler by attaching one of the decorators mentioned above over your class method.

Route prefix can be added using Prefix decorator.

import { Prefix, Get } from 'tropa'

@Prefix('/meta') 
class Meta {
  @Get('/dictionaries') 
  getDictionaties() {
    return {
      foo: 'bar',
      baz: 42,
    }
  }
}

Api prefix can be added using setApiPrefix method.

import * as tropa from 'tropa'
import http from 'http'

tropa.setApiPrefix('/api/v1')

http.createServer(tropa.listener).listen(3000)

Head decorators

It is possible to defined default headers and default status code for particular route.

import { Post, Headers, StatusCode, Prefix, Body } from 'tropa'

@Prefix('/user')
class User {
  @StatusCode(201)
  @Headers({ 'Content-Type': 'text/plain' })
  @Post('/') 
  create(@Body() body) {
    return User.create(body)
  }
}

Redirect decorator exported as well, so you are able to redirect request after route handler

import { Get, Redirect, Prefix } from 'tropa'

@Prefix('/oauth')
class OAuth {
  @Redirect('https://www.facebook.com/')
  @Get('/facebook')
  facebook() {
    
  }
}

Parameter decorators

Parameter decorators provide an opportunity to parse and get query or path params or body only when it's needed.

It means that the body for example will be parsed only if Body decorator was set.

import { Post, Body, Param, Query } from 'tropa'

class Root {
  @Post('/{dynamicParam}') 
  echo(@Body() body, @Query() query, @Param() params) {
    return { body, query, params }
  }
}

All these decorators take path and map function.

class Root {
  @Post() 
  echo(@Body('name', doSomethingWithName) name) {
    return { name }
  }
}

It is possible to use only map function.

class Root {
  @Post() 
  echo(@Body(doSomethingWithBody) body) {
    return body
  }
}

There is an ability to get context entities using parameter decorators.

import { Request, Response, Context } from 'tropa'

class User {
  @Get('/') 
  hello(@Context() ctx, @Request() req, @Response() res) {
    res.raw.end(JSON.stringify({ Hello: 'World' }))
  }
}

Please note that if you retrieve res.raw field (instance of ServerResponse), tropa hand-overs responsibility of the response to you!

Class decoration and methods decoration

Decorate decorator accepts list of decorators to apply them to particular method or all class methods. (decorator defined over class)

import { Get, Decorate } from 'tropa'

const auth = fn => (...args) => {
  const { token } = getContext().request.headers

  if (token !== 'tropa') {
    throw new ApiError('Authorization failed')
  }

  return fn(...args)
}

@Decorate(auth)
class User {
  @Get() 
  hello() {
    return { Hello: 'World' }
  }
}

Hooks

Using hooks you are able to do something during the request life cycle.

All you need is to create TropaHooks and apply them using setHooks method.

import { TropaHooks, setHooks } from 'tropa'

class Hooks extends TropaHooks {
  onRequest(ctx) {
  }

  beforeParsing(ctx) {
  }

  beforeHandler(ctx) {
  }

  onResponse(ctx) {
  }

  errorHandler(err, ctx) {
  }
}

setHooks(Hooks)

Please note that in case you set errorHandler you also need to handle NotFoundError and InternalServerError by yourself.

Those error classes are exported too.

import { NotFoundError, InternalServerError } from 'tropa'

Middlewares

Tropa provides an ability to use middlewares too.

import * as tropa from 'tropa'
import http from 'http'
import cors from 'cors'

tropa.use(cors())

http.createServer(tropa.listener).listen(3000)

Current context

You are able to get current context using getContext method.

import { getContext } from 'tropa'

const ctx = getContext()

Controllers loading

Controllers can be loaded using loadControllers method. Provide absolute path to directory with controllers to load them.

import * as tropa from 'tropa'
import path from 'path'
import http from 'http'

async function bootstrap() {
  await tropa.loadControllers(path.resolve(__dirname, './controllers'))

  http.createServer(tropa.listener).listen(3000)
}

bootstrap()