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

kox

v0.2.16

Published

koa module addon, make write api server more easier. Design an api style, combine with swagger(inspired by hapi-swagger).

Downloads

36

Readme

KOX

NPM version build status

koa module addon, make write api server more easier. Design an api style, combine with swagger(inspired by hapi-swagger).

Installation

 $ npm i kox

Example

// this will return an koa instance, so all koa properties are supported by kox.
const app = require('kox')
const apis = [{
  method: 'get',
  path: '/',
  handler: function (ctx) {
    ctx.set('x-powered-by', 'kox')
    return "hello world"
  }
}]
app.loadApis(api)
app.listen(3838)

Getting started

API

app

Because kox is just koa addon, so all koa app api are supported. You can read koa docs.

In kox, i use koa-body to handle request body parsing, so you dont need to add this middleware again in your project, the default body parser setting as follow:

{
  jsonLimit: '100KB',
  formLimit: '50KB',
  multipart: true,
  formidable: {
  uploadDir: join(process.cwd(), 'runtime')
}

You can modify body parser setting when initial kox instance:

const app = kox({bodyParser: {...}})

Controller

Kox add two controller related api to koa instance:

  • app.controller
  • app.loadCtrls

app.controller

This api is used to add an controller.

const fooController = {name: 'foo', ...}
// this operation add controller to app._controllers, we will using these controllers in api definitions.
app.controller(fooController)

app.loadCtrls

This api is used to add multiple controllers. I usually use it with require-dir module. You can see code in example.

const requireDir = require('require-dir')
const controllers = require('../controllers')
app.loadCtrls(controllers)

Controller Class

app.controller and app.loadCtrls are all used to create Controller, and most design of Controller are inspired by sundae which based on express. But I make action to be more functional, and handle the context as an argument in action. And I prefer put all dependencies in context, rather then require them in the begin of a code file. I think that will make action easy to test in real project.

  • Controller#mixin
    mixin will add actions to this._actions of controlller. And actions can be invoked as api handler or used to be a hook

    const mixins = requireDir('../mixins')
    ctrl.mixin(minins)
  • Controller#action
    action will add an action to this._actions

    ctrl.action('action1', function(ctx) {...})
  • Controller#before
    before will set an action as pre hook of other action, and it take options to indicate actions to hook

    // this will hook `hello` action before `foo` and `bar`
    ctrl.before('hello', {only: 'foo bar'})
    // this operation will return an generator combine hello and foo
    ctrl.invoke('foo') 
  • Controller#after
    The same as before, but add post hook to actions

    // this operation will add post hook to every actions except action named married
    ctrl.after('bye', {except: 'married'})
      
  • Controller#invoke
    Will return a generator, used in koa-router module.

    ctrl.invoke('hello')

Swagger API

I used to write business code and document of code separately. I think that way to write code cant make me more productive. I want to Document can decribe code and code can be document. And Swagger have done this. I found hapi-swagger is really awesome project to do this. But the way to write code in hapi is not my tea, so i rebuild kox

api describe

{
	method: 'get',
	path: '/hello',
	handler: 'controller.action' // or you can write an function directly
	tags: ['Hello'],
	summary: 'say hello',
	description: 'this api will return hello world',
	// validate definition is the same as hapi, using joi as validator
	validate: {
		payload: Joi.object({...}),
		params: ...,
		query: ...,
		headers: ...
	},
	responses: {
		200: {
			description: 'success',
			schema: Joi.object({}) // we validate response by schema
		}
	}
}

LICENSE

MIT

TODO

  • [x] Generate Swagger Json Route
  • [x] Auto route api
  • [ ] Full Example
  • [ ] Full documents
  • [ ] Rendering API