Skip to content

2.8.0

Compare
Choose a tag to compare
@elbywan elbywan released this 30 Dec 09:58
· 48 commits to master since this release

2.8.0 (2023-12-30)

🏭 New feature(s)

  • addon.resolver can now be a function (0bf9aa8), closes #212
const fetcher = wretch("https://jsonplaceholder.typicode.com").addon({
  // resolver can now be a function and re-use the previously defined methods of the response chain:
  resolver: (chain) => ['res', 'json', 'text', 'blob', 'formData', 'arrayBuffer'].reduce((acc, method) => ({
    ...acc,
    // overrides .json, .text… methods to chain .then & .catch
    [method](cb) {
      return chain[method](cb)
        .then(ret => (console.log('[hook] ok')))
        .catch(error => {
          console.error('[hook] error', error.response.url, error.response.status)
          throw error
        });
    }
  }), {})
});

(async function () {
  await fetcher.get("/todos/1").json(console.log);

  // { userId: 1, id: 1, title: 'delectus aut autem', completed: false }
  // [hook] ok

  await fetcher.get("/bad-route").notFound(error => {
    console.log('handled error :)')
  }).text(console.log);

  // handled error :)
  // [hook] ok

  await fetcher.get("/bad-route").text(console.log).catch(() => console.log('unhandled error :('));

  // [hook] error https://jsonplaceholder.typicode.com/bad-route 404
  // unhandled error :(
})()