Skip to content

Commit 2aa020c

Browse files
committed
feat: exports is now a function accepting added cookiesKey, logger, useCtxLogger, and stringify arguments (breaking change)
1 parent 700c8ed commit 2aa020c

File tree

5 files changed

+796
-995
lines changed

5 files changed

+796
-995
lines changed

README.md

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,19 @@ npm install --save koa-better-error-handler
4646

4747
> You should probably be using this in combination with [koa-404-handler][] too!
4848
49+
The package exports a function which accepts four arguments (in order):
50+
51+
* `cookiesKey` - defaults to `false`
52+
* `logger` - defaults to `console`
53+
* `useCtxLogger` - defaults to `true`
54+
* `stringify` - defaults to `fast-safe-stringify` (you can also use `JSON.stringify` or another option here if preferred)
55+
56+
If you pass a `cookiesKey` then support for sessions will be added. You should always set this argument's value if you are using cookies and sessions (e.g. web server).
57+
58+
We recommend to use [Cabin][] for your `logger` and also you should use its middleware too, as it will auto-populate `ctx.logger` for you to make context-based logs easy.
59+
60+
Note that this package only supports `koa-generic-session`, and does not yet support `koa-session-store` (see the code in [index.js](index.js) for more insight, pull requests are welcome).
61+
4962
### API
5063

5164
> No support for sessions, cookies, or flash messaging:
@@ -60,7 +73,7 @@ const koa404Handler = require('koa-404-handler');
6073
const app = new Koa();
6174

6275
// override koa's undocumented error handler
63-
app.context.onerror = errorHandler;
76+
app.context.onerror = errorHandler();
6477

6578
// specify that this is our api
6679
app.context.api = true;
@@ -115,9 +128,11 @@ const redisStore = new RedisStore({
115128
});
116129

117130
// add sessions to our app
131+
const cookiesKey = 'lad.sid';
118132
app.use(
119133
convert(
120134
session({
135+
key: cookiesKey,
121136
store: redisStore
122137
})
123138
)
@@ -127,7 +142,7 @@ app.use(
127142
app.use(convert(flash()));
128143

129144
// override koa's undocumented error handler
130-
app.context.onerror = errorHandler;
145+
app.context.onerror = errorHandler({ cookiesKey });
131146

132147
// use koa-404-handler
133148
app.use(koa404Handler);
@@ -244,3 +259,5 @@ You can also specify a base URI in the environment variable for rendering as `pr
244259
[koa]: http://koajs.com/
245260

246261
[koa-404-handler]: https://github.com/ladjs/koa-404-handler
262+
263+
[cabin]: https://cabinjs.com

0 commit comments

Comments
 (0)