1.0.1 • Published 6 years ago

jambon-router v1.0.1

Weekly downloads
-
License
MIT
Repository
-
Last release
6 years ago

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$', async (state) => {
	const foos = await getFoos();

	return {
		...state,
		response: {
			...state.response,
			body: foos,
			statusCode: 200
		}
	};
});

Using params:

const reducer = path('/api/foos/:fooId$',  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

import {method} from 'jambon-router';

const methodReducer = method('POST', );