Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add response events #15

Open
coreybutler opened this issue Nov 13, 2022 · 0 comments
Open

Add response events #15

coreybutler opened this issue Nov 13, 2022 · 0 comments
Labels
enhancement New feature or request

Comments

@coreybutler
Copy link
Member

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.

@coreybutler coreybutler added the enhancement New feature or request label Nov 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant