https://github.com/checkernetwork/assert-ok-response
A tiny helper to assert that response from `fetch()` is ok
https://github.com/checkernetwork/assert-ok-response
Last synced: 10 months ago
JSON representation
A tiny helper to assert that response from `fetch()` is ok
- Host: GitHub
- URL: https://github.com/checkernetwork/assert-ok-response
- Owner: CheckerNetwork
- License: other
- Created: 2025-03-17T13:46:35.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2025-08-11T12:04:37.000Z (10 months ago)
- Last Synced: 2025-08-11T12:33:40.541Z (10 months ago)
- Language: JavaScript
- Size: 330 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README
# assert-ok-response
A tiny helper to assert that response from `fetch()` is ok.
## Usage
```js
import { assertOkResponse } from 'assert-ok-response'
const response = await fetch('https://example.com/not-found')
await assertOkResponse(response)
//
// Throws:
//
// const err = new Error(`${errorMsg ?? `Cannot fetch ${res.url}`} (${res.status}): ${body}`)
// ^
//
// Error: Cannot fetch https://example.com/not-found (404):
// [...]
// at assertOkResponse (file:///Users/bajtos/src/assert-ok-response/index.js:15:15)
// at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
// at async file:///Users/bajtos/src/assert-ok-response/x.js:4:1 {
// statusCode: 404,
// serverMessage: '\n' +
// [...]
```
Error properties:
- `statusCode` (number) - The HTTP status code of the response.
- `serverMessage` (string) - The response body returned by the server.
## Development
- `npm test` to run the tests.
- `npm run lint:fix` to fix linting issues.
- `npx np` to publish a new version.