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

koa-clay

v6.5.0

Published

An ultra convenient minimal framework for building Koa apps

Downloads

195

Readme

Clay is an ultra convenient minimal framework for building Koa apps.

Features

  • Expose API routes with minimal config
  • Validate requests against conditions or an entity's required values
  • Secure your endpoints with permission preflight checks
  • Automatic documentation

Examples

Docs: docs

Tests: tests

Installation

yarn add koa-clay
// OR
npm i koa-clay --save

Lightweight route configuration

// app.ts

const app = new Koa()

app.use(service('/users', new UserService()))

app.listen(3000, () => console.log('Listening...'))

Clients will now have access to handler functions you implement in your service:

// UserService.ts

async get(req: Request) {
  ...
}

async patch(req: Request) {
  ...
}

By default you'll get a method for each HTTP method (GET/POST/PUT/PATCH/DELETE). You can also define your own routes:

// AlbumService.ts
@Routes([
  // e.g. http://example.me/albums/1/personnel/3
  {
    method: 'GET',
    path: '/:id/personnel/:personnelId',
    handler: 'getPersonnel'
  },
  // i.e. http://example.me/albums
  {
    method: 'GET',
    path: '', // can be omitted
    handler: 'getAll'
  }
])
class AlbumService extends Service {
  async getPersonnel(req: Request) {
    ...
  }

  async getAll(req: Request) {
    ...
  }
}

When a request is made, the specified handler function will be invoked.

Permissions and validation

Secure your endpoints using the @HasPermission decorator, which will run a function to determine if the route can be accessed:

class SecretsPolicy extends Policy {
  async get(req: Request): Promise<PolicyResponse> {
    return req.ctx.user.hasScope('get')
  }
}

class SecretService extends Service {
  @HasPermission(SecretsPolicy, 'get')
  async get(req: Request): Promise<Response> { ... }
}

Validate incoming request data (body, query and headers) using the @Validate decorator:

@Validate({
  body: ['username', 'age'], // these keys are required
})
async post(req: Request): Promise<Response> { ... }

You can also validate requests against an entity's required properties. Additionally, you can make keys required based on the request method:

class User {
  @Required() // required in POST and PUT requests
  username: string

  @Required({ methods: ['PUT'] }) // required in PUT requests
  age: number
}

@Validate({
  body: [User]
})
async post(req: Request): Promise<Response> { ... }

Automatic documentation

Services, routes and parameters are automatically documented. Descriptions can be added for routes and parameters for extra clarity.