Détail du package

http-assert

jshttp12.9mMIT1.5.0

assert with status codes

assert, http

readme

http-assert

NPM Version NPM Downloads Node.js Version Build Status Test Coverage

Assert with status codes. Like ctx.throw() in Koa, but with a guard.

Install

This is a Node.js module available through the npm registry. Installation is done using the npm install command:

$ npm install http-assert

Example

var assert = require('http-assert')
var ok = require('assert')

var username = 'foobar' // username from request

try {
  assert(username === 'fjodor', 401, 'authentication failed')
} catch (err) {
  ok(err.status === 401)
  ok(err.message === 'authentication failed')
  ok(err.expose)
}

API

The API of this module is intended to be similar to the Node.js assert module.

Each function will throw an instance of HttpError from the http-errors module when the assertion fails.

assert(value, [status], [message], [properties])

Tests if value is truthy. If value is not truthy, an HttpError is thrown that is constructed with the given status, message, and properties.

assert.deepEqual(a, b, [status], [message], [properties])

Tests for deep equality between a and b. Primitive values are compared with the Abstract Equality Comparison (==). If a and b are not equal, an HttpError is thrown that is constructed with the given status, message, and properties.

assert.equal(a, b, [status], [message], [properties])

Tests shallow, coercive equality between a and b using the Abstract Equality Comparison (==). If a and b are not equal, an HttpError is thrown that is constructed with the given status, message, and properties.

assert.fail([status], [message], [properties])

Always throws an HttpError that is constructed with the given status, message, and properties.

assert.notDeepEqual(a, b, [status], [message], [properties])

Tests for deep equality between a and b. Primitive values are compared with the Abstract Equality Comparison (==). If a and b are equal, an HttpError is thrown that is constructed with the given status, message, and properties.

assert.notEqual(a, b, [status], [message], [properties])

Tests shallow, coercive equality between a and b using the Abstract Equality Comparison (==). If a and b are equal, an HttpError is thrown that is constructed with the given status, message, and properties.

assert.notStrictEqual(a, b, [status], [message], [properties])

Tests strict equality between a and b as determined by the SameValue Comparison (===). If a and b are equal, an HttpError is thrown that is constructed with the given status, message, and properties.

assert.ok(value, [status], [message], [properties])

Tests if value is truthy. If value is not truthy, an HttpError is thrown that is constructed with the given status, message, and properties.

assert.strictEqual(a, b, [status], [message], [properties])

Tests strict equality between a and b as determined by the SameValue Comparison (===). If a and b are not equal, an HttpError is thrown that is constructed with the given status, message, and properties.

Licence

MIT

changelog

1.5.0 / 2021-08-25

  • Add assert.fail()
  • deps: http-errors@~1.8.0
    • deps: inherits@2.0.4
    • deps: setprototypeof@1.2.0

1.4.1 / 2019-04-28

  • deps: http-errors@~1.7.2
    • deps: setprototypeof@1.1.1

1.4.0 / 2018-09-09

  • Add assert.ok()
  • deps: http-errors@~1.7.1
    • Set constructor name when possible
    • deps: depd@~1.1.2
    • deps: setprototypeof@1.1.0
    • deps: statuses@'>= 1.5.0 < 2'

1.3.0 / 2017-05-07

  • deps: deep-equal@~1.0.1
    • Fix null == undefined for non-strict compares
  • deps: http-errors@~1.6.1
    • Accept custom 4xx and 5xx status codes in factory
    • Deprecate using non-error status codes
    • Make message property enumerable for HttpErrors
    • Support new code 421 Misdirected Request
    • Use setprototypeof module to replace __proto__ setting
    • deps: inherits@2.0.3
    • deps: setprototypeof@1.0.3
    • deps: statuses@'>= 1.3.1 < 2'
    • perf: enable strict mode

1.2.0 / 2016-02-27

  • deps: http-errors@~1.4.0

1.1.1 / 2015-02-13

  • deps: deep-equal@~1.0.0
  • dpes: http-errors@~1.3.1

1.1.0 / 2014-12-10

  • Add equality methods
    • assert.deepEqual()
    • assert.equal()
    • assert.notDeepEqual()
    • assert.notEqual()
    • assert.notStrictEqual()
    • assert.strictEqual()

1.0.2 / 2014-09-10

  • Fix setting err.expose on invalid status
  • Use http-errors module
  • perf: remove duplicate status check

1.0.1 / 2014-01-20

  • Fix typo causing err.message to be undefined

1.0.0 / 2014-01-20

  • Default status to 500
  • Set err.expose to false for 5xx codes