npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@ninsho/koa-restapi

v1.0.1

Published

A simple, lightweight, robust and versatile framework for building REST Api services on top of Koa with the developer experience at heart.

Downloads

3

Readme

Koa-Restapi

Build Status Coverage Status

A simple, lightweight, robust and versatile framework for building REST Api services on top of Koa with the developer experience at heart.

The Koa-Restapi framework comes shipped with a built in json schema validator for validating http requests and service layer responses at runtime using AJV. Which is currently the fastest json schema validator.

By leveraging Typescripts abstract classes and generic types, it provides compile time type checking and auto generation of json schemas.

Install

yarn add @ninsho/koa-restapi

Usage

Write an api endpoint class that extends the RestApiEndpoint abstract class.

  • CreateUser - http request object type
  • User - http response object type

Now create a resource service class that extends the ResourceService abstract class.

  • Database - data prvoider class type
  • User - type of object the provider will return
export class CreateUserApi extends RestApiEndpoint<CreateUser, User>{
  public routePath: string = '/users';
  public httpAction: HttpAction = HttpAction.POST;

  constructor(service: CreateUserService) {
    super(service, 'CreateUserApi');
  }
}

export class CreateUserService extends ResourceService<Database, User>{
  constructor(userService: Database) {
    super(userService);
  }

  public async run(ctx: Context){
    return await this.provider.createUser(ctx.params);
  }

  public async handleError(error: Error) {
    const httpError = error as HttpError;
    httpError.statusCode = 500;
    return httpError;
  }
}

Load the api middlware into a Koa application

import { getRouter } from '@ninsho/koa-restapi'

const provider = new Database();
const create_user_service = new CreateUserService(provider);
const create_user_api  = new CreateUserApi(create_user_service);

app.use(bodyParser());
app.use(getRouter(create_user_api));
server = app.listen(3000);

Configure

Next create a .restapi.json file in the same directory as the package.json

{
    "include": "./users/api/*.ts",       // api files
    "models": "./users/model/models.ts", // model files
    "outDir": "./"                       // output for the api.schema.json file
}

Finally parse the api file to determine the request and response types that the rest api is 'constrained' by and generate the json schemas for the validators

./package.json

"scripts": {
    "parse": restapi
}

yarn parse

And thats all to it! The framework has created a request and response validator based on the types provided to the RestApiEndpoint class

There are more examples in the examples folder!

Benefits

Speed up development

  • By abstracting out the common functionality of restful api endpoints, developers just have to focus on whats most important.
  • Combining the abstraction with the type constraints provides request and response validation out of the box which will help catch bugs early on and save on development time.

Improve code structure and maintainability

  • This framework was built to improve the development experience. It encourages writing readable, reusable and maintainable code.

Isolate functionality

  • Hone in on core functionality for a given api endpoint. Helps with developing microservices.

Expandable and portable

  • Create and add your own custom middlware for a particular endpoint

Robust

  • Using abstraction, contraints and validation, you can ben sure that your endpoint will work correctly.

Some things to know

The Context Object

  • The "koa-restapi" framework takes advantage of Koa's context object. Request parameters are stored in ctx.params after validation and responses are stored in ctx.state.response after validation.

The Middleware Stack

  • If a class extends the 'RestApiEndpoint' class, it already has 3 layers of stacked middleware.
  • When adding custom middleware, it is inserting in a 'stacked' fashion. First in, last out.
  • Middleware is ran starting from the top of the stack towerds the bottom and then back up.
[
  requestValidator,
        .
        .            <-- custom middleware added here
  resourceService,
  responseHandler,
]

DO NOT provide a primitive type for a 'RestApiEndpoint' class

  • It will cause the API parser to throw an error. Instead use a type alias
  • e.g. type Response = { result: boolean }