Skip to content

kakengloh/typedkoa

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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;
  }
);

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published