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

koax

v0.6.4

Published

Control flow inspired by koa and redux and in pursuit of free monads.

Downloads

18

Readme

koax

Build status Git tag NPM version Code style

Build apps by decoupling effects from application logic. Inspired by co, koa, redux and cycle.

The basic idea is that your main can yield effects to your interperter stack. Actions are dispatched to main by either dispatching directly or by adding drivers to the effects stack, which dispatch to main.

The basic building block of a koax app is a koax. A koax is a generator that processes actions and is composed of koax middleware.

At the outer most level, koax apps should basically have the same form.

import {run} from 'koax'
import effects from './effects'
import main from './app'

let dispatch = run(effects, main)

Effects and main are composed of koax middleware.

import koax from 'koax'
import {fetchEffect} from '@koax/fetch'
import {awsEffect} from '@koax/aws'

let effects = koax()
  .use(fetchEffect())
  .use(awsEffect())
  ...

Koax middleware looks like koa middleware, with three params: action, next, and ctx.

function (action, next, ctx) {
  if (action.type === FETCH) {
    return fetch(action.payoad.ur, action.payload.params)
  }
  return next()
}

Effects will be added to the interpreter stack, which includes default control flow middlware:

The action creators for these middleware are exposed by koax. They include: fork, delay, join, and cancel.

Now we can create our main and process it using the interpreter. Remember we want our app to be a koax. We can create it using koax to compose middleware or with a simple generator

function * main (evt) {
  if (evt.type === REQUEST && evt.method ==== 'get') {
    return yield aws('DynamoDB', 'getItem', {Key: evt.payload, TableName: 'Stuff')
  } else if (evt.type === REQUEST && evt.method ==== 'set') {
    return yield aws('DynamoDB', 'setItem', {Item: evt.payload, TableName: 'Stuff'})
  }
}

Putting it all together (again)...


let dispatch = run(effects, main)
dispatch({type: REQUEST, method: 'get'}).then(res => res) // results

Installation

$ npm install koax

Router

A router example.

app.js

import koax, {run} from 'koax'
import {route, request} from '@koax/route'
import {fetchEffect} from '@koax/fetch'
import {awsEffect, aws} from '@koax/aws'
import {get} from '@koax/fetch-json'


let effects = koax()
  .use(fetchEffect())
  .use(awsEffect())

let main = koax()
  .use(route('/pets.get', getPets))
  .use(route('/pets.add', addPet))

module.exports = run(effects, main)

function * getPets ({params}) {
  return yield aws('DynamoDB', 'getItem', {Key: {owner: params.owner}, TableName: 'Pets'})
}

function * addPet({param}) {
  yield aws('DynamoDB', 'updateItem', {})
}

For an http server, we could do:

server.js

import app from './app'
import koa from 'koa'

let server = koa()
server.use(function * () {
  this.body = yield app(request(this.request.url, this.request.params, this.request.headers))
})

API

koax() (default)

Returns: a koax middleware stack

.use(middleware)

  • middleware - add middleware to koax app

Returns: koax app

run(effects, main, ctx)

  • effects - effects processing stack
  • main - main function or generator, signature: main(action)
  • ctx - ctx to pass to effects and main middleware

Returns: a function that interprets data passed to it. data can be an action or a koax app.

Concepts

middleware(action, next, ctx)

  • action - an action that middleware can process (preferably treat as immutable)
  • next - a function that passes execution to next middleware (can yield or return)
  • ctx - global shared context for all middleware

Returns: whatever your heart desires or next() to defer to the next middleware


function * middleware (action, next, ctx) {
  // return a simple value
  if (action.type === 'FOO') return 'bar'
  // dispatch FETCH and return its result
  if (action.type === 'QUX') return yield {type: 'FETCH', payload: 'google'}
  // pass execution to next middleware
  return next()
}

next()

next is simply a function that calls the next middleware with action and next already bound. Since koax handles generators that are yielded or returned, each middleware can be either a function or generator and they will work as expected.

yield

yield dispatches actions to the interpreter. The interpreter is composed of the default control flow middleware and the effects stack.

License

MIT