Giter Site home page Giter Site logo

codesuki / superagent-intercept Goto Github PK

View Code? Open in Web Editor NEW
23.0 3.0 1.0 3 KB

Add functions that will be called during end() e.g. for handling error conditions without having the same code all over the place.

License: MIT License

JavaScript 100.00%
javascript superagent intercept proxy interceptor error-handling

superagent-intercept's Introduction

superagent-intercept

Add interceptors that will be called during end() e.g. for handling error conditions without having the same code all over the place.

Support

Please consider supporting the project by starring it on GitHub :)

https://github.com/codesuki/superagent-intercept

Install

npm install superagent-intercept

Example

let AuthIntercept = require('superagent-intercept')((err, res) => {
	if (res.status == 401) {
	   // route to login
	}
});

request.get('/api/something/' + someId).use(AuthIntercept).end((err, res) {
	// AuthIntercept will be called here.
	// ... code ...
});

superagent-intercept's People

Contributors

codesuki avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

jdrew1303

superagent-intercept's Issues

Add typings file?

I was wondering if it would be possible to get typings added for this module.

This is what I added for now (may not be the best solution as new to typescript)

declare module 'superagent-intercept' {
	import * as request from 'superagent'

	export default function Intercept( req:( err:request.Response, res:request.Response ) => void ): () => {}
}

Thanks!

request2.js:36 Uncaught Error: 登录失败

When i want to throw erros in the function, it shows "Uncaught Error"

const intercept = require('superagent-intercept')((err, res) => {
  if (err) {
    throw new Error(err)
  }

  if (res) {
    /**
     * code为非0是抛错 可结合自己业务进行修改
     */
    const data = res.body
    if (data.code !== 0) {
      throw new Error(data.message)
    }
  }
})
request2.js:36 Uncaught Error: 登录失败
    at Request.<anonymous> (request2.js:36)
    at index.js:8
    at Array.forEach (<anonymous>)
    at Request.req.callback (index.js:8)
    at Request.<anonymous> (client.js:465)
    at Request.Emitter.emit (index.js:145)
    at XMLHttpRequest.xhr.onreadystatechange (client.js:761)

Q: Response argument could be undefined

Is it possible to ensure that err and res arguments are always object references so we can manipulate them? In some cases (ie. request timeout) res can be undefined.

As the arguments are Object references, receiving 'undefined' doesn't allow us to modify the object which eventually gets passed to the "real" superagent .end.

This may be an issue for the superagent npm.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.