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-mapper

v1.0.0

Published

A better and smart router middleware for koa.

Downloads

8

Readme

koa-mapper

NPM version NPM downloads Dependency Status Build Status Coverage Status Greenkeeper badge

koa-mapper is a better and smart router middleware for koa. (Inspired by koa-router)

  • Support almost all features of koa-router
  • Support for parameters validation
  • Support parameters in path, header, query, cookie
  • Support body parser
  • Support request body validation
  • Support coerce data types of parameters and body
  • Support OpenAPI generation

Installation

npm install koa-mapper

Example

import Koa from 'koa';
import logger from 'koa-logger';
import Mapper from 'koa-mapper';

const app = new Koa();

app.use(logger());

const mapper = new Mapper();

mapper.get('/users/:id', {
  params: {
    id: { type: 'number' },
    info: { type: 'User', in: 'query' }
  }
}, (ctx) => {
  ctx.body = ctx.params;
});

mapper.post('/users', {
  body: {
    users: { type: 'array<User>' }
  }
}, (ctx) => {
  ctx.body = ctx.request.body;
});

mapper.define('User', {
  id: { type: 'number', required: true },
  name: { type: 'string', required: true }
});

app.use(mapper.routes());
app.use(mapper.allowedMethods());

app.listen(3000);

// open http://localhost:3000/users/123?info[id]=456&info[name]=hello
// open http://localhost:3000/openapi.json

API Reference

new Mapper([options])

Options

| Name | Type | Default | Description | | --- | --- | --- | --- | | prefix | string | '' | the path prefix for a Mapper | | openURL | string or false | /openapi.json | OpenAPI route, false to disable OpenAPI | | bodyparser | object or boolean | false | koa-body options, true|{} to enable body parser | | throwParamsError | function or boolean | utils.validateError | Throw error for params invalid | | throwBodyError | function or boolean | utils.validateError | Throw error for body invalid | | validator | object | {} | ajv options | | sensitive | boolean | false | sensitive option for path-to-regexp | | strict | boolean | false | strict option for path-to-regexp |

mapper.get|put|post|del(path, [options], ...middlewares) => Mapper

type options = {
  name: string, // route name, default null
  prefix: string, // route prefix, default ''
  bodyparser: object|boolean, // like Mapper options.bodyparser
  throwParamsError: function, // like Mapper options.throwParamsError
  throwBodyError: function, // like Mapper options.throwBodyError
  params: Params, // OpenAPI parameters definition
  body: Body, // OpenAPI requestBody schema definition
  ...others, // Fields of OpenAPI Operation Object
}

More fields of Operation Object

type Params = {
  in: string, // parameter in: `path`, `query`, `header`, `cookie`
  type: string, // parameter type
  ...others, // Fields of OpenAPI Parameter Object
}

More fields of OpenAPI Parameter Object

type support:

  • Basic type: array, boolean, integer, null, number, object, string, date, time, datetime, regex
  • Array type: array<string>, array<number|string>
  • Custom type: Pet, array<Pet>
type Body = string | {
  [property]: Schema
}

body examples:

  • body: 'Pet' => body: { $ref: 'Pet' }
  • body: { id: { type: 'number' } } => body: { type: 'object', properties: { id: { type: 'number' } }}

mapper.schema(name, properties, options) => Mapper alias mapper.define()

mapper.schema('Tag', {
  id: { type: 'integer', format: 'int64' },
  name: { type: 'string' }
});
mapper.schema('Category', {
  id: { type: 'integer', format: 'int64' },
  name: { type: 'string' }
});
mapper.schema('Pet', {
  id: { type: 'integer', format: 'int64' },
  category: { type: 'Category' },
  name: { type: 'string' },
  photoUrls: { type: 'array<string>' },
  tags: { type: 'array<Tag>' },
  status: { type: 'string', enum: ['available', 'pending', 'sold'] }
}, {
  required: ['name', 'photoUrls']
});

Support type extends:

mapper.schema('Model', {
  id: { type: 'number' },
  createdAt: { type: 'datetime' },
  updatedAt: { type: 'datetime' }
});
mapper.schema('User: Model', {
  name: { type: 'string' }
});

BodyParser

mapper.post('/users', {
  body: 'User'
}, (ctx) => {
  const { id, name } = ctx.request.body;
});

Multipart and file upload:

mapper.post('/uploadImage', {
  bodyparser: { multipart: true },
  body: {
    user: { type: 'number' },
    image: { type: 'file' }
  }
}, (ctx) => {
  const { user, image } = ctx.request.body;
});

License

MIT