npm.io
1.0.1 • Published 8 years ago

jambon-router

Licence
MIT
Version
1.0.1
Deps
2
Vulns
1
Weekly
0
DeprecatedThis package is deprecated

jambon-router

$ npm install jambon-router

API

path

function path(path: string, ...reducers: (async state => state)[]) : async state => state

Import path function:

import {path} from 'jambon-router'

Match requests where path starts with /api:

const reducer = path('/api', ...reducers});

Match requests where path is exactly /api/foos:

const reducer = path('/api/foos

Using params:

const reducer = path('/api/foos/:fooId

method(method: string, ...reducers : AsyncReducerFunction[]) : AsyncReducerFunction

import {method} from 'jambon-router';

const methodReducer = method('POST', );


, async (state) => { const foos = await getFoos(); return { ...state, response: { ...state.response, body: foos, statusCode: 200 } }; });

Using params:

__CODE_BLOCK_5__

method(method: string, ...reducers : AsyncReducerFunction[]) : AsyncReducerFunction

__CODE_BLOCK_6__
, async (state) => { const {fooId} = state.request.params; const foo = await findFoo(fooId); return { ...state, response: { ...state.response, body: foo, statusCode: foo ? 200 : 404 } }; });

method(method: string, ...reducers : AsyncReducerFunction[]) : AsyncReducerFunction

__CODE_BLOCK_6__ , async (state) => { const foos = await getFoos(); return { ...state, response: { ...state.response, body: foos, statusCode: 200 } }; });

Using params:

__CODE_BLOCK_5__

method(method: string, ...reducers : AsyncReducerFunction[]) : AsyncReducerFunction

__CODE_BLOCK_6__