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

@4lch4/koa-oto

v2.0.0

Published

A library for responding to requests within the Koa framework.

Downloads

26

Readme

Drone (self-hosted) with branch Codecov Codacy Badge

NPM Standard-Js

@4lch4/koa-oto

This library is a responder library for handling responses within the Koa framework.

!!! warning v2.0.0 Upgrade With the release of v2.0.0, this package is now a pure ESM package. If you're unsure what that means or would simply like some more information then I highly recommend you read this GitHub Gist (Pure ESM Package) from the legendary Sindresorhus.

If you still want to use this package before the ESM switch, you'll want the [`v1.3.2`](https://www.npmjs.com/package/@4lch4/koa-oto/v/1.3.2) release.

If you have any questions, comments, or concerns, please don't hesitate to [file an issue](https://git.4lch4.io/4lch4/Koa-Oto/issues) and I'll reply ASAP.

The Name

The name "Koa-Ōtō" is a combination of Koa (duh), and the Japanese Romaji version of 応答 (Ōtō) which means "Respond".

NOTE: According to Google Translate at least 😅

Example Usage

It isn't much, but this is the most basic way to use the library within your API:

import { ClientErrors, Successful } from '@4lch4/koa-oto'
import Router from '@koa/router'
import Koa from 'koa'

const app = new Koa()
const router = new Router()

router.get('/', async ctx => Successful.ok(ctx, 'Hello World'))
router.get('/error', ctx => ClientErrors.badRequest(ctx, 'Bad Request'))

app.use(router.routes())
app.use(router.allowedMethods())

app.listen(3000, () => {
  console.log('Server is running on port 3000')
})

Simple Auth Helper Usage

I recently added the SimpleAuthHelper class, also as part of the v2.0.0 release, that is designed to be used by the author (@4lch4) so I do not recommend anyone else use it, but I figured I'd document it just in case.

Using the same example server from above, if we want to secure just one of the routes, we'd do something like so:

router.get('/secure', async ctx => {
  const authenticated = new SimpleAuthHelper().authenticateRequest(ctx)

  if (authenticated) Successful.ok(ctx, 'Request was authenticated!')
  else ClientErrors.unauthorized(ctx)
})

Auth Check Details

All the authenticateRequest method does is the following:

  • Check the request for an API token.
    • Checks for an Authorization header with a value that starts with Token.
    • If no header is found, it checks for an apiToken query parameter.
  • If an API token is found in the previous step, compare it to the API token that was set when instantiating the SimpleAuthHelper class.
    • If you don't provide an API token to the constructor then it'll attempt to pull the value from the API_REQUEST_TOKEN environment variable.
    • If the environment variable isn't set then the token will be set to a random generated string generated in order to avoid any unintentional authorization.

Testing and Coverage

I went a really weird route for testing this library, but in the end I've achieved a solid coverage percentage in CodeCov. The following images are an icicle and grid representation of the current test coverage:

Code-Coverage-Icicle

Code-Coverage-Tree

Icicle: The top section represents the entire project. Proceeding with folders and finally individual files. The size and color of each slice is representing the number of statements and the coverage, respectively.

Grid: Each block represents a single file in the project. The size and color of each block is represented by the number of statements and the coverage, respectively.