# koa-metarouter

> this project is use Typescript ‘reflect-metadata’ defined koa-router

Latest version **2.3.0** (published 2022-03-27) · MIT license · 0 weekly downloads

## Install

```sh
npm install koa-metarouter
pnpm add koa-metarouter
yarn add koa-metarouter
bun add koa-metarouter
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.3.0 |
| Published | 2022-03-27 |
| First published | 2021-10-09 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 34 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | gaoyi |
| Maintainers | gaoyia |
| Keywords | koa, koa-router, typescript |

## Links

- npm: https://www.npmjs.com/package/koa-metarouter
- Repository: https://github.com/TsBoot/koa-metarouter
- Homepage: https://github.com/TsBoot/koa-metarouter#readme
- Issues: https://github.com/TsBoot/koa-metarouter/issues
- npm.io page: https://npm.io/package/koa-metarouter

## Dependencies (1)

- [methods](https://npm.io/package/methods.md) ^1.1.2

## 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

- 2.3.0 (latest) — 2022-03-27
- 2.2.9 — 2022-03-25
- 2.2.8 — 2022-01-19
- 2.2.5 — 2021-12-28
- 2.2.4 — 2021-12-06
- 2.2.3 — 2021-10-30
- 2.2.2 — 2021-10-29
- 2.2.1 — 2021-10-28
- 2.2.0 — 2021-10-24
- 2.1.4 — 2021-10-17
- 2.1.3 — 2021-10-17
- 2.0.1 — 2021-10-12
- 1.1.0 — 2021-10-09

## README

# koa-metarouter

- 💡 Simplified route definition
- 🔑 Non-invasive
- ⚙️ Multiple router instance
- 🔌 Extensible
- 📦 Extremely light

[中文文档](https://github.com/TsBoot/koa-metarouter/blob/main/README.zh.md)

```json
// tsconfig.json
{
  "compilerOptions": {
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true
  }
}
```

# Example
## Basic example

`npm i koa-metarouter`

```typescript
// ./router/metaRouter.ts
import Router from "@koa/router";
import MetaRouterClass from "koa-metarouter";

const router = new Router();
const metaRouter: MetaRouterClass = new MetaRouterClass(router);
export default metaRouter
const { Controller,Get } = metaRouter
export {
    Controller,
    Get,
}

// ./router/index.ts
import metaRouter from "./metaRouter";
import "../controller/DemoController";
// or import("../controller/DemoController");


// DemoController
import { Controller, Get } from "./metaRouter";

@Controller()
export default class DemoController {
  @Get() // url: /Demo/index
  async index (): Promise<any> {}
}

// ./App.ts
import Koa from "koa";
import router from "./router";
const koa = new Koa();
koa.use(router.routes());
koa.listen(3000)
```

### you can use change-case format default part
```cmd
 // https://www.npmjs.com/package/change-case

 // https://lodash.com/docs/4.17.15#lowerCase // recommend

  npm i lodash
  // or
  npm i change-case
```

```typescript
// ✨ this is default, you can cover it 
metaRouter.classNameFormat = (className : string) : string => {
  const reg = /controller$/i;
    className = className.replace(reg, "");
    return className;
};

// ✨ this is default, you can cover it 
metaRouter.functionNameFormat = (functionName : string) : string => {
  return functionName;
};
export default metaRouter;

```

  You can use the following decorators in Controller


| decorators ||||||
|   :----:   |   :----:   |    :----:  |    :----: | :----:  |  :----: |
|    Post    |     Get    |    Put     |  Delete   |   Del   |  Patch  |
|    Link    |    Unlink  |    Head    |  Options  |   All   |         |


  If you want to respond to any methods, you can use `All`

```typescript
// DemoController
import { Get, All } from "./metaRouter";
import { Context, Next } from "koa";

// ✨ Controller is necessary
@Controller({path:"/public"}, ...middleware) 
export default class DemoController {
  @Get()
  async test () : Promise<any> {}

  @Get("/stringPath")
  async stringPath () : Promise<any> {}

  // ✨ if you want defined router name
  @All({name :"requestArgument"})
  async requestArgument () : Promise<any> {}

  // ✨ if you want add middleware
  @All(middleware1,middleware2,...)
  async middleware () : Promise<any> {}
  // or
  @All({path:"/middleware"},middleware1,middleware2,...)
  async middleware () : Promise<any> {}

  // static method 
  @All()
  static async staticTest (ctx:Context,next:Next) : Promise<any> {}
}

```

```typescript
interface MethodOptions {
  name ?: string,

  path ?: UrlPath | null | undefined
  method ?: string | Array<string>

  sensitive ?: boolean | undefined;
  strict ?: boolean | undefined;
  end ?: boolean | undefined;
  prefix ?: string | undefined;
  ignoreCaptures ?: boolean | undefined;
}
```

## Redirect

```typescript
@Controller({})
export default class DemoController {
  // ✨ default statusCode is 301
  @Redirect("/url_c")
  async url_a () : Promise<any> {}

  // ✨ if you want use code 302
  @Redirect("/url_b","/url_c",302)
  async url_b () : Promise<any> {}

  @Get()
  async url_c () : Promise<any> {}


}
```
>   ✨ more example please look test file

## custom usage
  use MetaRouter him self

  if you want realize custom http Methods, you can use like this

```typescript
// ./router/metaRouter.ts
import Router from "@koa/router";
import MetaRouterClass, { RouterMethodDecorator } from "koa-metarouter";
const router = new Router({
  methods: [
    "HEAD",
    "OPTIONS",
    "GET",
    "PUT",
    "PATCH",
    "POST",
    "DELETE",
    "PURGE", // add method 这里添加自定义的方法
  ],
});

const metaRouter: MetaRouterClass = new MetaRouterClass(router);

const { All, Redirect, Post, Get, Head, Patch, Del, Delete, MetaRouter, Controller, Options, Link, Unlink, Put } = metaRouter;

// custom Method Name
const Purge: RouterMethodDecorator = (optionsOrMiddleware, ..._middleware) => {
  const { options, middleware } = MetaRouterClass.argumentsFormat(optionsOrMiddleware, ..._middleware);
  if (Array.isArray(options.method)) {
    options.method = [ ...options.method, "purge" ];
  } else if (typeof options.method === "string") {
    options.method = [ options.method, "purge" ];
  }
  return metaRouter.MetaRouter(options, ...middleware);
};

export default metaRouter;
export {
  All,
  Redirect,
  Get,
  Post,
  Del,
  Delete,
  Options,
  Link,
  Unlink,
  Put,
  MetaRouter,
  Controller,
  Head,
  Patch,
  Purge,
};


```

# more detial please see

[https://github.com/TsBoot/koa-metarouter/blob/main/__test__/TestController.ts](https://github.com/TsBoot/koa-metarouter/blob/main/__test__/TestController.ts)

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