# koa-ts-decorators

> Decorators for koa

Latest version **0.0.8** (published 2020-07-29) · ISC license · 0 weekly downloads

## Install

```sh
npm install koa-ts-decorators
pnpm add koa-ts-decorators
yarn add koa-ts-decorators
bun add koa-ts-decorators
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.8 |
| Published | 2020-07-29 |
| First published | 2020-07-22 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 30.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Nuzhnov A.A. |
| Maintainers | samuray |
| Keywords | koa, typescript, decorator |

## Links

- npm: https://www.npmjs.com/package/koa-ts-decorators
- Repository: https://github.com/samurayii/koa-ts-decorators
- Homepage: https://github.com/samurayii/koa-ts-decorators.git#readme
- Issues: https://github.com/samurayii/koa-ts-decorators/issues
- npm.io page: https://npm.io/package/koa-ts-decorators

## Dependencies (2)

- [koa](https://npm.io/package/koa.md) ^2.13.0
- [@koa/router](https://npm.io/package/@koa/router.md) ^9.3.1

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 0.0.8 (latest) — 2020-07-29
- 0.0.7 — 2020-07-28
- 0.0.6 — 2020-07-23
- 0.0.5 — 2020-07-23
- 0.0.4 — 2020-07-23
- 0.0.3 — 2020-07-23
- 0.0.2 — 2020-07-22
- 0.0.1 — 2020-07-22

## README

## Simple koa decorators

install: `npm install koa-ts-decorators`

Decorators:
- @Controller
- @Middleware
- @Get
- @Post
- @Put
- @Delete

### Example

Basic:
```js
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:
```js
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();

});

```

---
_Source: https://npm.io/package/koa-ts-decorators · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
