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

koa-ts-decorators

v0.0.8

Published

Decorators for koa

Downloads

13

Readme

Simple koa decorators

install: npm install koa-ts-decorators

Decorators:

  • @Controller
  • @Middleware
  • @Get
  • @Post
  • @Put
  • @Delete

Example

Basic:

import { KoaD, Get, Controller, Middleware, Post, Context, Next, IMiddleware, IKoaDConfig } from "koa-ts-decorators";
import * as koa_logger from "koa-logger";

const config: IKoaDConfig = {
    listening: "*:3001",
    enable: true,
    prefix: "/",
    proxy: false,
    subdomain_offset: 2,
    proxy_header: "X-Forwarded-For",
    ips_count: 0
}

@Controller()       // path /healthcheck
@Controller("/")    // path /
class Healthcheck {

    constructor (app_id: string, controller_name: string, prefix: string) {
        //console.log(app_id);
        //console.log(controller_name);
        //console.log(prefix);
    }

    @Middleware()
    middle (ctx: Context, next: Next) {
        ctx.state.flag = true;
        next();
    }

    @Get()              // route GET /
    @Get("/hello")      // route GET /hello
    @Post()             // route POST /
    @Post("/hello")     // route POST /hello
    get (ctx: Context): void {

        console.log(ctx.koad.config);

        if (ctx.state.flag === true) {
            ctx.body = "OK";
            ctx.status = 200;
        } else {
            ctx.body = "Error";
            ctx.status = 500;
        }
    }
}

@Middleware()
class Logger implements IMiddleware {

    constructor (app_id: string, middleware_name: string) {
        //console.log(app_id);
        //console.log(middleware_name);
    }

    use (config: IKoaDConfig): unknown {
        console.log(config);
        return koa_logger( (str: string) => {
            console.log(str);
        });
    }
}

const app = new KoaD(config); // create koad app (extended koa app)

// additional parameters and methods
console.log(app.config);
console.log(app.prefix);
console.log(app.id);
console.log(app.config);

app.disable();
app.enable();

app.listen( async () => {

    const response1 = await superagent.get("http://localhost:3001/");
    const response2 = await superagent.get("http://localhost:3001/hello");
    const response3 = await superagent.get("http://localhost:3001/healthcheck");
    const response4 = await superagent.get("http://localhost:3001/healthcheck/hello");

    app.close();

});

2 application:

import { KoaD, Get, Controller, Middleware, Post, Context, Next, IMiddleware, IKoaDConfig } from "koa-ts-decorators";
import * as koa_logger from "koa-logger";

const config: IKoaDConfig = {
    listening: "*:3001",                // listing host and port
    enable: true,                       // enable/disable application
    prefix: "/",                        // global prefix
    proxy: false,                       // koa app.proxy 
    subdomain_offset: 2,                // koa app.subdomainOffset
    proxy_header: "X-Forwarded-For",    // koa app.proxyHeader
    ips_count: 0                        // koa app.ipsCount
    //keys?: string[]                   // koa app.keys
    //env?: "development"               // koa app.env
}

@Controller("second")           // second app, path /healthcheck
@Controller("/", "second")      // second app, path /
@Controller()                   // default app, path /healthcheck
@Controller("/")                // default app, path /
class Healthcheck {

    @Middleware()
    @Middleware("second")
    middle (ctx: Context, next: Next) {
        ctx.state.flag = true;
        next();
    }

    @Get()              // route GET /
    @Get("/hello")      // route GET /hello
    @Get({
        app_id: "second"
    })
    @Get("/hello", "second")
    get (ctx: Context): void {

        console.log(ctx.koad.config);

        if (ctx.state.flag === true) {
            ctx.body = "OK";
            ctx.status = 200;
        } else {
            ctx.body = "Error";
            ctx.status = 500;
        }
    }
}

@Middleware()
@Middleware("second")
class Logger implements IMiddleware {
    use (config: IKoaDConfig): unknown {
        console.log(config);
        return koa_logger( (str: string) => {
            console.log(str);
        });
    }
}

const app1 = new KoaD(config); // create koad app1
const app2 = new KoaD(config, "second"); // create koad app2

app1.listen( "*:3001", async () => {

    const response1 = await superagent.get("http://localhost:3001/");
    const response2 = await superagent.get("http://localhost:3001/hello");
    const response3 = await superagent.get("http://localhost:3001/healthcheck");
    const response4 = await superagent.get("http://localhost:3001/healthcheck/hello");

    app.close();

});

app2.listen( "*:3002", async () => {

    const response1 = await superagent.get("http://localhost:3002/");
    const response2 = await superagent.get("http://localhost:3002/hello");
    const response3 = await superagent.get("http://localhost:3002/healthcheck");
    const response4 = await superagent.get("http://localhost:3002/healthcheck/hello");

    app.close();

});