Giter Club home page Giter Club logo

koats's Introduction

koats

Experimenting with Koa and TypeScript

Stack

  • Koa
  • Koa router
  • TypeScript
  • Jest

Getting started

Running the app

  • npm install
  • npm start dev

Testing

  • npm run test
  • npm run test -- --watchAll
  • npm run test -- --coverage

Debugging

There's a launch.json available to use from VSCode, simply start the debugger and add breakpoints to the code.

Concepts

Dependencies

Dependencies are injected to the Context when the application starts.

Inside the context object you'll find a dependency container dc where all your services/dependencies will be instantiated.

Middleware

I've tried to keep middleware as simple as possible, without handling any errors or conditional logic in the main body, this makes them highly reusable between different routes or apps.

Instead, error handling and branching is achieved by composition of other middleware.

safe

Decouples error handling logic from middleware.

This runs your middleware code in the catch block of another middleware that has previously throw'ed.

This way we keep the "happy path" and the "error path" separated.

const safe = (a: Middleware) => (b: Middleware): Middleware => async (
  ctx,
  next
) => {
  let flag = false;
  const call = async () => {
    flag = true;
    await next();
  };

  try {
    await a(ctx, call);
  } catch (err) {
    ctx.err = err;

    if (!flag) {
      await b(ctx, next);
    } else {
      throw err;
    }
  }
};
Example
import { safe } from "@/middleware/safe";
import { Middleware } from "koa";

// Your heart tells you to keep it simple
const heart: Middleware = (ctx, next) => {
  throw new Error("But things can go wrong");
};

// Then you can fix it
const withYourMind: Middleware = (ctx, next) => {
  if (ctx.err.message.includes("wrong")) {
    // Our app keeps running
    return next();
  }
  // Sometimes there's nothing we can do about it!
  throw err;
};

const saveYourHeart = safe(heart);

// Combine them into one
app.use(saveYourHeart(withYourMind)).use((ctx, next) => {
  // To keep doing what you need
  return next();
});

branch

Decouples conditional logic from other middleware.

The middleware you pass in will only run when the predicate function returns true.

const branch = (p: Predicate) => (mw: Middleware): Middleware => async (
  ctx,
  next
) => {
  p(ctx) ? await mw(ctx, next) : await next();
};
Example
import { branch } from "@/middleware/branch";
import { Middleware } from "koa";

const html: Middleware = async (ctx, next) => {
  ctx.type = "html";
  ctx.body = "<h1>Hello world</h1>";
  await next();
};

const json: Middleware = async (ctx, next) => {
  ctx.type = "json";
  ctx.body = { hello: "world" };
  await next();
};

const htmlIf = branch(html);
const jsonIf = branch(json);

app
  .use(htmlIf(ctx => !!ctx.accepts("html"))
  .use(jsonIf(ctx => !!ctx.accepts("json"));

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.