/koa-if-else

Koa router that works like an if/else statement

Primary LanguageTypeScriptMIT LicenseMIT

koa-if-else

if

import koa from "koa";
import { ify } from "koa-if-else";

const app = new Koa();

app.use(
  ify(
    (ctx) => ctx.query.one,
    (ctx) => (ctx.state.result = "One!")
  ),
  (ctx) => (ctx.body = ctx.state.result)
);

if/else

import koa from "koa";
import { ify } from "koa-if-else";

const app = new Koa();

app.use(
  ify(
    (ctx) => ctx.query.one,
    (ctx) => (ctx.state.result = "One!")
  )
    .elsey((ctx) => (ctx.state.result = "Something else!"),
  (ctx) => (ctx.body = ctx.state.result)
);

if/elseif/else

import koa from "koa";
import { ify } from "koa-if-else";

const app = new Koa();

app.use(
  ify(
    (ctx) => ctx.query.one,
    (ctx) => (ctx.state.result = "One!")
  )
    .elseyIfy(
      (ctx) => ctx.query.two,
      (ctx) => (ctx.state.result = "Two!")
    )
    .elsey((ctx) => (ctx.state.result = "Something else!"),
  (ctx) => (ctx.body = ctx.state.result)
);