response-callbacks

1.1.1 • Public • Published

response-callbacks Build Status

Manually dealing with responses can be tedious and memorizing what every code means isnt something i enjoy doing. So I put together a list of common callbacks that can be used with the response object.

Install

npm install --save response-callbacks

Example Usage

response-callbacks takes in two parameters. Response and an object containing methods you want called back to.

import RepsonseCallbacks from 'response-callbacks'
 
let callBacks = {
  success: (response) => { console.log('success', response) },
  validation: (response) => { console.log('form validation errors', response.json()) },
  server_error: (response) => { console.log('server error', response.status) },
 
  // You can use the status code directly as well.
  420: (response) => { console.log('Enhance Your Calm') }
}
 
let request = fetch('/endpoint').then((response) => {
  return (new ResponseCallbacks(response, callBacks)).run()
})
 
// response-callbacks returns the original response so you can continue using promises like normal
request.catch(function (err) { console.error('error', err.message) })

Callback Methods

I put together a list of most used methods. Optionally you can just have the actual status code in your object and it will be called as well.

200's

success: 2** // Every 200 request
ok: 200
created: 201
no_content: 204

300's

redirection: 3** // Every 300 request
moved_permanently: 301
not_modified: 304

400's

client_error: 4** // Every 400 request
bad_request: 400
unauthorized: 401
forbidden: 403
not_found: 404
conflict: 409
unprocessable_entity: 422
validation: 422 // Used for things like form errors

500's

server_error: 5** // Every 500 request
internal_server_error: 500
bad_gateway: 502
service_unavailable: 503
gateway_timeout: 504

Package Sidebar

Install

npm i response-callbacks

Weekly Downloads

7

Version

1.1.1

License

MIT

Last publish

Collaborators

  • brianvoe