Socket
Socket
Sign inDemoInstall

response-callbacks

Package Overview
Dependencies
0
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    response-callbacks

Simple middleware class to provide callbacks from responses


Version published
Maintainers
1
Install size
253 kB
Created

Readme

Source

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

Keywords

FAQs

Last updated on 08 Dec 2016

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc