koa-kit
A simple framework with functional tools for koa.
Support Koa@2.x from 1.1.0
Usage
const kk = ;const app = ; app;
Options
const app = ;
APIs
app.listen
app.listen([port], [callback], [onException]);
Deprecated
app.listen([port], [callback], [onException]);
port: 9030 by default, or set process.env.PORT;callback: call by http.Server.listen;onException: handler for Event: 'uncaughtException'.
app.listen();
- Same as
koa.listen
;
ctx.assertOK
ctx.assertOK(value[, statusCode][, statusMeassage][, errorProperties])
Just like the ctx.assert, but throw error when value instanceof Error
;
ctx.assertEqual
ctx.assertEqual(actual, expected[, statusCode][, statusMeassage][, errorProperties])
Throw HTTPError
if actual
is not match the type or value of expected
.
- actual: required, throw error when it is undefined;
- expected:
- RegExp
- return expected.test(actual)
- Function
- return expected(actual);
- expected must return a Boolean.
- Object
- Wrapper Object, check the type of the actual. (For example,
{userName: String, age: Number}
.) - Normal Object, deeply checking by
expected
. (For example,{userName: 'abc', age: 18, address: /^No\./i}
.)
- Wrapper Object, check the type of the actual. (For example,
- RegExp
var obj = userName: 'abc' age: 18 address: 'No.123 xyz Road.'; this;// => HTTPError, 18 != 19this;// => true, /^No\./i.test('No.123 xyz Road.')this;// => Do nothing, type matched.// String, Number, Array, Object, Boolean, Functionthis;// => HTTPError, using Strict Equal, 18 !== '18'.this;// => Do nothing, age function return true.
ctx.assertQuery
ctx.assertQuery(actual, expected[, statusCode][, statusMeassage][, errorProperties])
Almost the same as ctx.assertEqual
, but:
- actual:
- undefined
- return HTTP 400 by default
- undefined
- expected:
- String
- Tests required properties when actual is an object, property names should set off by ONE
(space) . (For example,
"userName aget"
.)
- Tests required properties when actual is an object, property names should set off by ONE
- String
var obj = userName: 'abc' age: 18 address: 'No.123 xyz Road.'; this;// => Do nothing.this;// => HTTPError, fullName was missing.