tgvashworth / fetch-engine

A smart request-making library
24 stars 6 forks source link

Make it possible to intercept request and return response early #35

Closed tgvashworth closed 8 years ago

tgvashworth commented 8 years ago

Cache and that?

tgvashworth commented 8 years ago

Idea is to rename the current fetch callback to fetching, then allow plugins to replace the internal fetch implementation. They could be passed the 'next' fetch in the chain (where the final one is our internal fetch) to call if they'd like to.

class CachePlugin {
  fetch({ request, fetch }) {
    if (this.isCached(request)) {
      return this.get(request);
    }
    return fetch(request);
  }
  isCached(request) {
    // ...
  }
  get(request) {
    // ...
  }
}