1.1.1 • Published 2 years ago

typedkoa v1.1.1

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

TypedKoa

Koa route handler with great Typescript support

InstallationExample

Installation

npm i typedkoa    # npm
yarn add typedkoa # yarn
pnpm i typedkoa   # pnpm

Example

import { Route } from "typedkoa";

interface Student {
  classroomId: string;
  name: string;
  age: number;
}

interface PathParams {
  classroomId: string;
}

interface ReqBody {
  name: string;
  age: number;
}

interface QueryParams {}

type ResBody = Student;

// `Route` function accepts a handler function as an argument and returns a Koa middleware
const createStudent = Route<PathParams, ResBody, ReqBody, QueryParams>(
  async (req, res, ctx) => {
    // Request `params`, `body` and `query` are type inferred
    const { params, body, query } = req;

    const student = new Student({
      ...body,
      classroomId: params.classroomId,
    });

    // response body is type checked
    return student;
  }
);
1.1.1

2 years ago

1.1.0

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago