Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add options in opts for consolidate[engine] #36

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
65 changes: 65 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ $ npm install koa-error

- `template` path to template written with your template engine
- `engine` template engine name passed to [consolidate](https://github.com/tj/consolidate.js)
- `options` template engine options passed to [consolidate](https://github.com/tj/consolidate.js), [more-consolidate-options](https://github.com/tj/consolidate.js/blob/master/lib/consolidate.js)
- `cache` cached compiled functions, default: `NODE_ENV != 'development'`
- `env` force a NODE_ENV, default: `development`
- `accepts` mimetypes passed to [ctx.accepts](https://github.com/koajs/koa/blob/master/docs/api/request.md#requestacceptstypes), default: `[ 'html', 'text', 'json' ]`
Expand Down Expand Up @@ -99,6 +100,70 @@ app.use(error({
</html>
```

#### Custom filters, use macro,block in Nunjucks

koa-error engine tool base on [consolidate](https://github.com/tj/consolidate.js), you can also set consolidate options

> [more-nunjucks-options](https://github.com/tj/consolidate.js/blob/master/lib/consolidate.js#L1317-L1370)

`app.js`:

```js
//...
const app = new Koa();
const nunjucks = require('nunjucks');
const nunjucksEnv = new nunjucks.Environment(new nunjucks.FileSystemLoader(path.join(__dirname, 'tpl')));

// add filters
const filters = require('./filters');
for(let [k, v] of Object.entries(filters)){
nunjucksEnv.addFilter(k, v);
}

//...
app.use(koaError({
//...
template: path.join(__dirname, 'tpl/error.html'),
options: {
nunjucksEnv // custom nunjucks env
}
}));
```

`filters.js`:

```js
module.exports = {
// define filters function here
stringify(..args){
return JSON.stringify(...args);
}
//...
};
```

`tpl/error.html`:

```html
<!DOCTYPE html>
<html>
<head>
{% include "./com.html" %}
</head>
<body>
<p>{{ request | stringify }}</p> {# use filters here #}
<!-- ... -->
</body>
</htm>
```

`tpl/com.html`:

```html
<link rel="stylesheet" href="/css/normalize.css" />
```


## License

MIT
6 changes: 4 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ module.exports = error
function error (opts) {
opts = opts || {}

let { options } = opts

const engine = opts.engine || 'lodash'

const accepts = opts.accepts || [ 'html', 'text', 'json' ]
Expand Down Expand Up @@ -68,7 +70,7 @@ function error (opts) {

case 'html':
ctx.type = 'text/html'
ctx.body = await consolidate[engine](path, {
ctx.body = await consolidate[engine](path, Object.assign({
originalError: err,
cache: cache,
env: env,
Expand All @@ -79,7 +81,7 @@ function error (opts) {
stack: err.stack,
status: ctx.status,
code: err.code
})
}, options))
Froguard marked this conversation as resolved.
Show resolved Hide resolved
break
}
}
Expand Down