http-assert-plus
Advanced tools
Comparing version 1.1.2 to 2.0.1
{ | ||
"name": "http-assert-plus", | ||
"version": "1.1.2", | ||
"version": "2.0.1", | ||
"description": "More assertions with status codes", | ||
"main": "./assert.js", | ||
"main": "./index.cjs", | ||
"scripts": { | ||
"docs:preview": "docker run --rm -it -v $PWD/docs:/srv/jekyll -p 4000:4000 jekyll/jekyll:4.2.0 jekyll serve", | ||
"lint": "eslint --ext .js .", | ||
"lint": "eslint --ext .cjs .", | ||
"test": "nyc mocha", | ||
@@ -19,4 +19,4 @@ "preversion": "npm run lint && npm test", | ||
"files": [ | ||
"assert.js", | ||
"assert.d.ts" | ||
"index.cjs", | ||
"index.d.ts" | ||
], | ||
@@ -28,10 +28,15 @@ "keywords": [ | ||
"devDependencies": { | ||
"@babel/eslint-parser": "^7.21.8", | ||
"@types/mocha": "^10.0.1", | ||
"@typescript-eslint/eslint-plugin": "^5.26.0", | ||
"babel-eslint": "^10.1.0", | ||
"eslint": "^6.8.0", | ||
"eslint-plugin-mocha": "^5.3.0", | ||
"eslint": "^8.40.0", | ||
"eslint-plugin-mocha": "^10.1.0", | ||
"eslint-plugin-node": "^11.1.0", | ||
"eslint-plugin-sort-requires": "^2.1.0", | ||
"mocha": "^6.2.3", | ||
"module-alias": "^2.2.2", | ||
"nyc": "^15.1.0" | ||
"mocha": "^10.2.0", | ||
"nyc": "^15.1.0", | ||
"source-map-support": "^0.5.21", | ||
"ts-node": "^10.9.1", | ||
"tsconfig-paths": "^4.2.0", | ||
"typescript": "^5.0.4" | ||
}, | ||
@@ -44,15 +49,13 @@ "engines": { | ||
"defaults", | ||
"not IE", | ||
"maintained node versions" | ||
], | ||
"_moduleAliases": { | ||
"http-assert-plus": "./" | ||
}, | ||
"mocha": { | ||
"exit": true, | ||
"require": [ | ||
"module-alias/register" | ||
"ts-node/register", | ||
"source-map-support/register", | ||
"tsconfig-paths/register" | ||
], | ||
"spec": [ | ||
"./*.test.js" | ||
"./test/*.test.ts" | ||
], | ||
@@ -62,5 +65,8 @@ "ui": "bdd" | ||
"nyc": { | ||
"extension:": [ | ||
".ts" | ||
], | ||
"exclude": [ | ||
"./*.d.ts", | ||
"./*.test.js" | ||
"./*.test.ts" | ||
], | ||
@@ -67,0 +73,0 @@ "reporter": [ |
@@ -11,5 +11,5 @@ # http-assert-plus | ||
```js | ||
import assert from 'http-assert-plus'; | ||
// or | ||
const assert = require('http-assert-plus'); | ||
// or | ||
import assert from 'http-assert-plus'; | ||
@@ -23,3 +23,3 @@ const username = 'jdrydn'; | ||
// Error: Authorization failed | ||
// at http-assert-plus/README.md:15:7 { | ||
// at http-assert-plus/README.md:22:1 { | ||
// code: 'NOT_AUTHORIZED', | ||
@@ -73,30 +73,2 @@ // statusCode: 403, | ||
### Creating your own instance | ||
You can optionally create your own instance of `http-assert-plus`, useful if you want to add your own methods for common assertions: | ||
```js | ||
import assert from 'http-assert-plus'; | ||
ctx.assert = assert.create(); | ||
ctx.assert.isAuthenticated = (props = null) => { | ||
const { isAuthenticated } = ctx.state; | ||
assert(isAuthenticated, 401, 'You are not authenticated', { | ||
code: 'NOT_AUTHENTICATED', | ||
...props, | ||
}); | ||
}; | ||
// Later on: | ||
ctx.assert.isAuthenticated({ action: 'EditUser' }); | ||
// Error: You are not authenticated | ||
// at isAuthenticated (http-assert-plus/README.md:78:9) { | ||
// code: 'NOT_AUTHENTICATED', | ||
// statusCode: 401, | ||
// status: 401, | ||
// statusText: 'Unauthorized', | ||
// username: 'jdrydn' | ||
// } | ||
``` | ||
### What about deep equality? | ||
@@ -112,12 +84,4 @@ | ||
assert(deepEqual(a, b, { strict: true }), 400, 'These two are not entirely equal'); | ||
// Or create your own http-assert-plus instance: | ||
req.assert = assert.create(); | ||
req.assert.deepEqual = (a, b, ...args) => assert(deepEqual(a, b), ...args); | ||
req.assert.strictDeepEqual = (a, b, ...args) => assert(deepEqual(a, b, { strict: true }), ...args); | ||
req.assert.deepEqual([ 1, 2, 3 ], [ '1', 2, 3.0 ], 400, 'Array does not equal'); | ||
req.assert.strictDeepEqual([ 1, 2, 3 ], [ '1', 2, 3.0 ], 400, 'Array does not strict-equal'); | ||
// Error: Array does not strict-equal | ||
// at http-assert-plus/README.md:115:27 { | ||
// at http-assert-plus/README.md:72:1 { | ||
// statusCode: 400, | ||
@@ -135,5 +99,19 @@ // status: 400, | ||
const { origin } = window.location; | ||
assert(origin.startsWith('https://'), new Error('Expected origin to start with https://')); | ||
// In browsers, do this: | ||
assert(origin.startsWith('https://'), new Error('Expected origin to start with https://'), { origin }); | ||
// Error: Expected origin to start with https:// | ||
// at http-assert-plus/README.md:99:39 { | ||
// origin: 'http://localhost:4000', | ||
// } | ||
// Not this | ||
assert(origin.startsWith('https://'), 'Expected origin to start with https://'); | ||
// Error: Expected origin to start with https:// | ||
// at node_modules/http-assert-plus/index.cjs:56:38 { | ||
// at http-assert-plus/README.md:107:1 { | ||
// origin: 'http://localhost:4000', | ||
// } | ||
``` | ||
If you don't use a construct such as `new Error`, when reading stacktraces just ignore the first line as it'll always be the `assert` function :wink: |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
21340
353
13
113
1