diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000..a657cce --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,14 @@ +version: 2 +updates: +- package-ecosystem: npm + directory: "/" + schedule: + interval: daily + open-pull-requests-limit: 10 + ignore: + - dependency-name: sinon + versions: + - 10.0.0 + - dependency-name: youch + versions: + - 2.2.0 diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml new file mode 100644 index 0000000..aafa9b3 --- /dev/null +++ b/.github/workflows/run-tests.yml @@ -0,0 +1,29 @@ +name: Run tests + +on: [push, pull_request] + +jobs: + test: + runs-on: ubuntu-latest + + strategy: + matrix: + node-version: [12.x, 14.x, 15.x] + + name: Node ${{ matrix.node-version }} + + steps: + - uses: actions/checkout@v2 + + - name: Use Node.js ${{ matrix.node-version }} + uses: actions/setup-node@v1 + with: + node-version: ${{ matrix.node-version }} + + - name: Install dependencies + run: npm install + + - name: Run tests + run: npm test + env: + CI: true diff --git a/.travis.yml b/.travis.yml index 6c7bd82..fbebcfa 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,18 +1,6 @@ language: node_js + node_js: - - 8 - - 10 - - 11 + - 12 + - 14 - node - -env: - - HAPI_VERSION="" - -matrix: - include: - - node_js: "8" - env: HAPI_VERSION="@17" - -install: - - npm install - - npm install hapi$HAPI_VERSION diff --git a/CHANGELOG.md b/CHANGELOG.md index 3c25bab..d77cf00 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,43 @@ # Changelog +## Version [4.0.0](https://github.com/futurestudio/hapi-dev-errors/compare/v3.3.0...v4.0.0) (2020-01-10) + +### Breaking Changes +- require Node.js v12 + - this change aligns with the hapi ecosystem requiring Node.js v12 with the release of hapi 19 + + +## Version [3.3.0](https://github.com/futurestudio/hapi-dev-errors/compare/v3.2.6...v3.3.0) (2019-10-17) + +### Added +- basic TypeScript declarations in `lib/index.d.ts` + + +## Version [3.2.6](https://github.com/futurestudio/hapi-dev-errors/compare/v3.2.5...v3.2.6) (2019-10-11) + +### Updated +- refine Readme +- bump dependencies +- remove Node.js v11 from Travis testing +- minor refactorings (a mobile app update would say: “performance improvements”) + + +## Version [3.2.5](https://github.com/futurestudio/hapi-dev-errors/compare/v3.2.4...v3.2.5) (2019-05-19) + +### Updated +- fix `.travis.yml` to properly test the hapi version matrix +- fix `@hapi/hapi` import in examples +- minor example refinements +- bump dependencies + + +## Version [3.2.4](https://github.com/futurestudio/hapi-dev-errors/compare/v3.2.3...v3.2.4) (2019-04-27) + +### Updated +- bump dependencies +- update to hapi scoped dependencies + + ## Version [3.2.3](https://github.com/futurestudio/hapi-dev-errors/compare/v3.2.2...v3.2.3) (2019-02-18) ### Updated diff --git a/README.md b/README.md index f9afd0d..cfecf1e 100644 --- a/README.md +++ b/README.md @@ -16,8 +16,8 @@

Build Status Known Vulnerabilities - hapi-dev-errors Version - Greenkeeper badge + Latest Version + Monthly downloads

Follow @marcuspoehls for updates! @@ -47,30 +47,37 @@ Besides the web view, `hapi-dev-errors` prints pretty error details to the termi ## Requirements -This plugin uses async/await which requires **Node.js v8 or newer**. +This plugin uses async/await which requires **Node.js v12 or newer**. + + +### Compatibility +| Major Release | [hapi.js](https://github.com/hapijs/hapi) version | Node.js version | +| --- | --- | --- | +| `v4` | `>=17 hapi` | `>=12` | +| `v3` | `>=17 hapi` | `>=8` | +| `v2` | `>=17 hapi` | `>=8` | ## Installation Add `hapi-dev-errors` as a dependency to your project: ```bash -# NPM 5: this way is yours npm i hapi-dev-errors - -# NPM 4: -npm i -S hapi-dev-errors ``` -### Do you use hapi v16 (or lower)? +### Using hapi v17 or v18? +Use the `3.x` release of `hapi-dev-errors`: + +```bash +npm i hapi-dev-errors@3 +``` + +### Using hapi v16 (or lower)? Use the `1.3.2` release of `hapi-dev-errors` with hapi v16. Later versions are only compatible with hapi v17. ```bash -# NPM 5: this way is yours npm i hapi-dev-errors@1.3.2 - -# NPM 4: use NPM shortcuts to (i)nstall and (-S)ave the module as a dependency -npm i -S hapi-dev-errors@1.3.2 ``` @@ -117,7 +124,8 @@ await server.register({ return ` Search hapi-dev-errors on GitHub - ` + + ` } ] } diff --git a/examples/default.js b/examples/default.js index a104e30..39f768c 100644 --- a/examples/default.js +++ b/examples/default.js @@ -1,6 +1,6 @@ 'use strict' -const Hapi = require('hapi') +const Hapi = require('@hapi/hapi') const server = new Hapi.Server({ host: 'localhost', port: 3000 }) @@ -15,9 +15,7 @@ async function launchIt () { server.route({ method: '*', path: '/{path*}', - handler: (_, h) => { - return h.notAvailable() - } + handler: (_, h) => h.notAvailable() }) await server.start() diff --git a/examples/template.js b/examples/template.js index 04c8f50..be28dc3 100644 --- a/examples/template.js +++ b/examples/template.js @@ -1,6 +1,6 @@ 'use strict' -const Hapi = require('hapi') +const Hapi = require('@hapi/hapi') const Path = require('path') const server = new Hapi.Server({ host: 'localhost', port: 3000 }) @@ -8,7 +8,7 @@ const server = new Hapi.Server({ host: 'localhost', port: 3000 }) async function launchIt () { await server.register([ { - plugin: require('vision') + plugin: require('@hapi/vision') }, { plugin: require('../'), @@ -20,9 +20,7 @@ async function launchIt () { ]) server.views({ - engines: { - html: require('handlebars') - }, + engines: { html: require('handlebars') }, path: Path.resolve(__dirname, 'views'), layout: 'layout', isCached: process.env.NODE_ENV !== 'production' @@ -31,9 +29,7 @@ async function launchIt () { server.route({ method: '*', path: '/{path*}', - handler: (_, reply) => { - reply.notAvailable() - } + handler: (_, h) => h.notAvailable() }) await server.start() diff --git a/examples/with-links.js b/examples/with-links.js index 4dd255e..5c29a5b 100644 --- a/examples/with-links.js +++ b/examples/with-links.js @@ -1,6 +1,6 @@ 'use strict' -const Hapi = require('hapi') +const Hapi = require('@hapi/hapi') const server = new Hapi.Server({ host: 'localhost', port: 3000 }) @@ -23,9 +23,7 @@ async function launchIt () { server.route({ method: 'GET', path: '/{path*}', - handler: (_, h) => { - h.notAvailable() - } + handler: (_, h) => h.notAvailable() }) await server.start() diff --git a/lib/error-handler.js b/lib/error-handler.js index dbca6f8..1bfb8b9 100644 --- a/lib/error-handler.js +++ b/lib/error-handler.js @@ -128,7 +128,9 @@ class ErrorHandler { * @returns {Response} */ async resolveError (request, h) { - await this.logToTerminal(request) + if (this.shouldLogToTerminal()) { + await this.logToTerminal(request) + } if (this.wantsJson(request)) { return this.sendJson(request, h) @@ -141,18 +143,26 @@ class ErrorHandler { return this.sendHtml(request, h) } + /** + * Returns a boolean whether to log + * the error to the terminal. + * + * @returns {Boolean} + */ + shouldLogToTerminal () { + return this.toTerminal + } + /** * Logs the error to terminal. * * @param {Request} request */ async logToTerminal (request) { - if (this.toTerminal) { - const youch = this.createYouch(request) - const json = await youch.toJSON() + const youch = this.createYouch(request) + const json = await youch.toJSON() - console.log(ForTerminal(json)) - } + console.log(ForTerminal(json)) } /** diff --git a/lib/index.d.ts b/lib/index.d.ts new file mode 100644 index 0000000..d391c4e --- /dev/null +++ b/lib/index.d.ts @@ -0,0 +1 @@ +declare module 'hapi-dev-errors'; diff --git a/package.json b/package.json index 00e5219..b41bda1 100644 --- a/package.json +++ b/package.json @@ -1,33 +1,33 @@ { "name": "hapi-dev-errors", "description": "Return better error details and skip the look at command line to catch the issue.", - "version": "3.2.3", - "author": "Future Studio ", + "version": "4.0.0", + "author": "Marcus Pöhls ", "bugs": { "url": "https://github.com/futurestudio/hapi-dev-errors/issues" }, "dependencies": { - "youch": "~2.0.10", - "youch-terminal": "~1.0.0" + "youch": "~3.3.2", + "youch-terminal": "~2.2.0" }, "devDependencies": { - "boom": "~7.3.0", - "code": "~5.2.4", - "eslint": "~5.14.0", - "eslint-config-standard": "~12.0.0", - "eslint-plugin-import": "~2.16.0", - "eslint-plugin-node": "~8.0.1", - "eslint-plugin-promise": "~4.0.1", - "eslint-plugin-standard": "~4.0.0", - "hapi": "~18.1.0", - "husky": "~1.3.1", - "joi": "~14.3.1", - "lab": "~18.0.2", - "sinon": "~7.2.3", - "vision": "~5.4.4" + "@hapi/boom": "~10.0.0", + "@hapi/code": "~9.0.0", + "@hapi/hapi": "~21.4.0", + "@hapi/joi": "~17.1.0", + "@hapi/lab": "~25.3.0", + "@hapi/vision": "~7.0.0", + "eslint": "~7.32.0", + "eslint-config-standard": "~16.0.2", + "eslint-plugin-import": "~2.32.0", + "eslint-plugin-node": "~11.1.0", + "eslint-plugin-promise": "~5.2.0", + "eslint-plugin-standard": "~5.0.0", + "husky": "~9.1.1", + "sinon": "~21.0.0" }, "engines": { - "node": ">=8" + "node": ">=12" }, "homepage": "https://github.com/futurestudio/hapi-dev-errors#readme", "keywords": [ @@ -41,12 +41,19 @@ ], "license": "MIT", "main": "lib/index.js", + "types": "lib/index.d.ts", "repository": { "type": "git", "url": "git+https://github.com/futurestudio/hapi-dev-errors.git" }, "scripts": { - "test": "lab --assert code --leaks --coverage --lint", - "prepush": "npm test" + "lint": "eslint .", + "lint:fix": "eslint . --fix", + "test": "lab --assert @hapi/code --leaks --coverage --lint" + }, + "husky": { + "hooks": { + "pre-push": "npm run lint" + } } } diff --git a/test/plugin-falls-back-to-json.js b/test/plugin-falls-back-to-json.js index dc9ff10..8ca8422 100644 --- a/test/plugin-falls-back-to-json.js +++ b/test/plugin-falls-back-to-json.js @@ -1,8 +1,8 @@ 'use strict' -const Lab = require('lab') -const Code = require('code') -const Hapi = require('hapi') +const Lab = require('@hapi/lab') +const Code = require('@hapi/code') +const Hapi = require('@hapi/hapi') let server diff --git a/test/plugin-handles-failed-response-validations.js b/test/plugin-handles-failed-response-validations.js index d8fdde9..43bf503 100644 --- a/test/plugin-handles-failed-response-validations.js +++ b/test/plugin-handles-failed-response-validations.js @@ -1,9 +1,9 @@ 'use strict' -const Lab = require('lab') -const Joi = require('joi') -const Code = require('code') -const Hapi = require('hapi') +const Joi = require('@hapi/joi') +const Lab = require('@hapi/lab') +const Code = require('@hapi/code') +const Hapi = require('@hapi/hapi') let server @@ -26,9 +26,9 @@ experiment('hapi-dev-error handles failed response validations', () => { method: 'GET', config: { response: { - schema: { + schema: Joi.object({ user: Joi.object().required() - } + }) } }, handler: () => { diff --git a/test/plugin-is-disabled-by-default.js b/test/plugin-is-disabled-by-default.js index 451c64e..6300852 100644 --- a/test/plugin-is-disabled-by-default.js +++ b/test/plugin-is-disabled-by-default.js @@ -1,9 +1,9 @@ 'use strict' -const Lab = require('lab') -const Code = require('code') -const Hapi = require('hapi') -const Boom = require('boom') +const Lab = require('@hapi/lab') +const Code = require('@hapi/code') +const Hapi = require('@hapi/hapi') +const Boom = require('@hapi/boom') const server = new Hapi.Server() diff --git a/test/plugin-is-disabled-in-production.js b/test/plugin-is-disabled-in-production.js index 597d320..4684f54 100644 --- a/test/plugin-is-disabled-in-production.js +++ b/test/plugin-is-disabled-in-production.js @@ -1,8 +1,8 @@ 'use strict' -const Lab = require('lab') -const Code = require('code') -const Hapi = require('hapi') +const Lab = require('@hapi/lab') +const Code = require('@hapi/code') +const Hapi = require('@hapi/hapi') const server = new Hapi.Server() diff --git a/test/plugin-is-enabled-when-not-in-production.js b/test/plugin-is-enabled-when-not-in-production.js index 294a057..e5c460d 100644 --- a/test/plugin-is-enabled-when-not-in-production.js +++ b/test/plugin-is-enabled-when-not-in-production.js @@ -1,10 +1,10 @@ 'use strict' -const Lab = require('lab') -const Code = require('code') -const Boom = require('boom') -const Hapi = require('hapi') const Path = require('path') +const Lab = require('@hapi/lab') +const Code = require('@hapi/code') +const Hapi = require('@hapi/hapi') +const Boom = require('@hapi/boom') let server @@ -30,7 +30,8 @@ experiment('hapi-dev-error register plugin', () => { const routeOptions = { path: '/showErrorsForWeb', method: 'GET', - handler: () => Boom.badImplementation('a fancy server error') } + handler: () => Boom.badImplementation('a fancy server error') + } server.route(routeOptions) @@ -139,7 +140,7 @@ experiment('hapi-dev-error renders a custom template', () => { // fake dev env, no process.env.NODE_ENV defined await server.register([ { - plugin: require('vision') + plugin: require('@hapi/vision') }, { plugin: require('../lib/index'), diff --git a/test/plugin-skips-non-500-responses.js b/test/plugin-skips-non-500-responses.js index 6ccd58d..6505aae 100644 --- a/test/plugin-skips-non-500-responses.js +++ b/test/plugin-skips-non-500-responses.js @@ -1,9 +1,9 @@ 'use strict' -const Lab = require('lab') -const Code = require('code') -const Hapi = require('hapi') -const Boom = require('boom') +const Lab = require('@hapi/lab') +const Code = require('@hapi/code') +const Hapi = require('@hapi/hapi') +const Boom = require('@hapi/boom') let server diff --git a/test/plugin-uses-links.js b/test/plugin-uses-links.js index 139be43..3151137 100644 --- a/test/plugin-uses-links.js +++ b/test/plugin-uses-links.js @@ -1,9 +1,9 @@ 'use strict' -const Lab = require('lab') -const Code = require('code') -const Hapi = require('hapi') const Sinon = require('sinon') +const Lab = require('@hapi/lab') +const Code = require('@hapi/code') +const Hapi = require('@hapi/hapi') const { experiment, it, beforeEach, afterEach } = (exports.lab = Lab.script()) @@ -54,7 +54,7 @@ experiment('hapi-dev-error handles custom user links', () => { }) it('throws if the links are strings', async () => { - const server = await createServer({ links: [ 'error' ] }) + const server = await createServer({ links: ['error'] }) const response = await server.inject({ url: '/', @@ -84,7 +84,7 @@ experiment('hapi-dev-error handles custom user links', () => { }) it('works fine with a link function', async () => { - const server = await createServer({ links: () => `link` }) + const server = await createServer({ links: () => 'link' }) const response = await server.inject({ url: '/', diff --git a/test/prints-error-to-terminal.js b/test/prints-error-to-terminal.js index c219a69..d693ffd 100644 --- a/test/prints-error-to-terminal.js +++ b/test/prints-error-to-terminal.js @@ -1,9 +1,9 @@ 'use strict' -const Lab = require('lab') -const Code = require('code') -const Hapi = require('hapi') const Sinon = require('sinon') +const Lab = require('@hapi/lab') +const Code = require('@hapi/code') +const Hapi = require('@hapi/hapi') let server