Skip to content

Add response events #15

Description

@coreybutler

Similar to #14, response events should be available on the client/resource modules. For example:

API = new Resource(...)

// Listen for all responses
API.on('response', response => {
  console.log(`Recevied (${response.status}) ${response.responseText}`)
})

// Listen for 401 response status 
API.on('401', response => {
  console.warn('unauthorized access')
})

// Listen for 400 level status codes
API.on('4XX', response => {
  console.warn(`Recevied (${response.status}) ${response.responseText}`)
})

In most cases, responses will be handled directly by the request caller. Events allow global listeners to log responses, trigger additional response handlers, etc. For instance, many people are used to the axios HTTP library, which throws an error on 4XX responses. 4XX are a successful HTTP response indicating there was a some sort of network or api related problem, such as a 401 indicating a user can contact and endpoint but isn't allowed to do so. This library does not consider an HTTP response status code to be a JavaScript error, but it can still be useful to throw an application error when one of these types of responses occurs.

This can also be used to identify problems, modify the client/resource instance, and retry a request automatically (such as renewing an access token before retrying a failed request).

Finally, these events can be very useful for logging.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions