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

@cedjj/magnus-back

v1.2.17

Published

Microservice framework

Downloads

5

Readme

Type Controllers

Allows to create controllers classes that handle requests. Simple wrapper over express.js routing.

Usage

Simply create a class and put annotations on its methods:

import {Request, Response} from "express";
import {Controller, Get, Post, Put, Patch, Delete} from "t-controllers/Annotations";

@Controller()
export class BlogController {

    @Get('/blogs')
    getAll(request: Request, response: Response) {
        // use request and response like you always do with express.js ...
    }

    @Get('/blogs/:id')
    getOne(request: Request, response: Response) {
    }

    @Post('/blogs')
    post(request: Request, response: Response) {
    }

    @Put('/blogs/:id')
    put(request: Request, response: Response) {
    }

    @Patch('/blogs/:id')
    patch(request: Request, response: Response) {
    }

    @Delete('/blogs/:id')
    remove(request: Request, response: Response) {
    }

}

// and you need to require controllers and register actions in express app:

import {defaultActionRegistry} from "t-controllers/ActionRegistry";

require('./BlogController'); // require your controller
let app = express(); // create express application
app.use(bodyParser.json()); // setup body parser
defaultActionRegistry.registerActions(app); // register actions in the app
app.listen(3000); // run express app

If you want to include the directory with controllers do this:

import {defaultActionRegistry} from "t-controllers/ActionRegistry";
import {ControllerUtils} from "t-controllers/ControllerUtils";

ControllerUtils.requireAll([__dirname + '/controllers']); // includes all controllers
let app = express(); // create express application
app.use(bodyParser.json()); // setup body parser
defaultActionRegistry.registerActions(app); // register actions in the app
app.listen(3000); // run express app

You can also return result right from the controller, and this result will be pushed via response.send()


@Get('/blogs')
getAll(request: Request, response: Response) {
    return [
        { id: 1, name: 'first blog' },
        { id: 2, name: 'second blog' }
    ];
}

// its the same as:
// request.send([{ id: 1, name: 'first blog' }, { id: 2, name: 'second blog' }]);

You can also return a Promise (object that contains .then method), and its result will be pushed to response.send after promise is resolved.


@Get('/blogs')
getAll(request: Request, response: Response) {
    return SomeService.loadData();
}

// its the same as SomeService.loadData().then(
//     result => request.send(result),
//     error  => { request.status(500); request.send(error) }
// );

You can inject some common express params you may need right into the controller action method:


@Get('/blogs/:id')
getAll( request: Request,
        response: Response,
        @Body() requestBody: any,
        @Param('id') id: number,
        @QueryParam('name') name: string) {
    // now you can use your parameters
}

// its the same as:
// let requestBody = response.body;
// let id = response.params.id;
// let name = response.query.name;

Take a look on samples in ./sample for more examples of usages.

Todos

  • cover with tests
  • integration with other frameworks (other then express.js) can be easily added, so PRs are welcomed