# http-assert

> assert with status codes

Latest version **1.5.0** (published 2021-08-25) · MIT license · 0 weekly downloads

## Install

```sh
npm install http-assert
pnpm add http-assert
yarn add http-assert
bun add http-assert
```

## Health

**Score 33/100 (F)** — status: abandoned.

Positive: has types package; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 1.5.0 |
| Published | 2021-08-25 |
| First published | 2013-12-24 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | separate (@types/http-assert) |
| Module format | CommonJS |
| Node | >= 0.8 |
| Dependencies | 2 |
| Unpacked size | 8.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 154 |
| Maintainers | eivifj, dougwilson, jongleberry |
| Keywords | assert, http |

## Links

- npm: https://www.npmjs.com/package/http-assert
- Repository: https://github.com/jshttp/http-assert
- Homepage: https://github.com/jshttp/http-assert#readme
- Issues: https://github.com/jshttp/http-assert/issues
- npm.io page: https://npm.io/package/http-assert

## Dependencies (2)

- [deep-equal](https://npm.io/package/deep-equal.md) ~1.0.1
- [http-errors](https://npm.io/package/http-errors.md) ~1.8.0

## Recent versions

- 1.5.0 (latest) — 2021-08-25
- 1.4.1 — 2019-04-28
- 1.4.0 — 2018-09-09
- 1.3.0 — 2017-05-08
- 1.2.0 — 2016-02-27
- 1.1.1 — 2015-02-15
- 1.1.0 — 2014-12-10
- 1.0.2 — 2014-09-10
- 1.0.1 — 2014-01-21
- 1.0.0 — 2014-01-21
- 0.1.1 — 2014-01-14
- 0.1.0 — 2014-01-05
- 0.0.1 — 2013-12-24

## README

# http-assert

[![NPM Version][npm-version-image]][npm-url]
[![NPM Downloads][npm-downloads-image]][npm-url]
[![Node.js Version][node-version-image]][node-version-url]
[![Build Status][ci-image]][ci-url]
[![Test Coverage][coveralls-image]][coveralls-url]

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

## Install

This is a [Node.js](https://nodejs.org/en/) module available through the
[npm registry](https://www.npmjs.com/). Installation is done using the
[`npm install` command](https://docs.npmjs.com/getting-started/installing-npm-packages-locally):

```bash
$ npm install http-assert
```

## Example
```js
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](https://nodejs.org/dist/latest/docs/api/assert.html).

Each function will throw an instance of `HttpError` from
[the `http-errors` module](https://www.npmjs.com/package/http-errors)
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](LICENSE)

[ci-image]: https://badgen.net/github/checks/jshttp/http-assert/master?label=ci
[ci-url]: https://github.com/jshttp/http-assert/actions?query=workflow%3Aci
[coveralls-image]: https://badgen.net/coveralls/c/github/jshttp/http-assert/master
[coveralls-url]: https://coveralls.io/r/jshttp/http-assert?branch=master
[node-version-image]: https://badgen.net/npm/node/http-assert
[node-version-url]: https://nodejs.org/en/download
[npm-downloads-image]: https://badgen.net/npm/dm/http-assert
[npm-url]: https://npmjs.org/package/http-assert
[npm-version-image]: https://badgen.net/npm/v/http-assert

---
_Source: https://npm.io/package/http-assert · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
