wretch

A tiny wrapper built around fetch with an intuitive syntax.

MIT License

Downloads
309.4K
Stars
4.5K
Committers
14

Bot releases are hidden (Show)

wretch - 2.8.1 Latest Release

Published by elbywan 8 months ago

2.8.1 (2024-03-07)

πŸ› Bug fix(es)

  • Fix error callback type missing addons extensions (99a21a8), closes #222
export const apiClient = wretch('https://localhost:3000')
  .addon(QueryStringAddon)
  .resolve((chain) => {
    return chain.unauthorized((_error, request) => {
      // request type used to be wrong (missing .query() from the addon)
      // and it would clash with what was expected by the callback definition
      // now the type should be correct ✨ 
      return request
        .fetch()
        .unauthorized((error) => {
          throw error;
        })
        .json();
    });
  });

⬆️ Version update(s)

  • Bump follow-redirects from 1.15.1 to 1.15.4 (04fada6)
wretch - 2.8.0

Published by elbywan 10 months ago

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 to 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);
  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);
  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 :(
})()
wretch - 2.7.1

Published by elbywan 11 months ago

2.7.1 (2023-11-19)

πŸ› Bug fix(es)

  • Fix passing a HeadersInit (Header / array) argument to headers() (f32845a), closes #205
const headers1 = new Headers({ "hello": "world" });
const headers2 = new Headers({ "bonjour": "le monde" });
const headers3 = { "hola": "mundo " };
const headers4 = [["hallo", "welt"]]

let w = wretch().headers(headers1);
console.log(w._options.headers);
// Object { hello: "world" }
w = w.headers(headers2);
console.log(w._options.headers);
// Object { hello: "world", bonjour: "le monde" }
w = w.headers(headers3);
console.log(w._options.headers);
// Object { hello: "world", bonjour: "le monde", hola: "mundo " }
w = w.headers(headers4);
console.log(w._options.headers);
// Object { hello: "world", bonjour: "le monde", hola: "mundo ", hallo: "welt" }

⬆️ Version update(s)

  • Bump @babel/traverse from 7.19.4 to 7.23.2 (922005e)
  • Upgrade dependencies (d774826)
wretch - 2.7.0

Published by elbywan about 1 year ago

2.7.0 (2023-09-12)

🏭 New feature(s)

  • Support FileList objects in the form data addon (3582809), closes #201
<head>
  <script type="module">
    import wretch from 'https://cdn.skypack.dev/wretch';
    import FormDataAddon from 'https://cdn.skypack.dev/wretch/addons/formData';

    const fileInput = document.querySelector("#myfiles");

    fileInput.addEventListener("change", () => {
      wretch("/post-files")
        .addon(FormDataAddon)
        .formData({ files: fileInput.files })
        .post()
        .text(console.log)
    });
  </script>
</head>
<body>
  <input id="myfiles" multiple type="file" />
</body>

πŸ“ Documentation update(s)

  • Mention file names in the form data addon doc (411fb09), closes #197
wretch - 2.6.0

Published by elbywan over 1 year ago

2.6.0 (2023-06-28)

⬆️ Version update(s)

  • Bump engine.io and socket.io (3ef1738)
  • Bump socket.io-parser from 4.2.2 to 4.2.3 (b42aa0f)
  • Upgrade dependencies (b183cf7)

🏭 New feature(s)

wretch(url)
  .catcher(404, err => redirect("/routes/notfound", err.message))
  .catcher(500, err => flashMessage("internal.server.error"))
  // this fallback will trigger for any error except if already caught by other means (like above for 404s and 505s)
  .catcherFallback(err => {
    log("Uncaught error:", err)
    throw err
  })

πŸ“ Documentation update(s)

  • Update node-fetch code snippet (3b6ac7e), closes #179
wretch - 2.5.2

Published by elbywan over 1 year ago

2.5.2 (2023-04-11)

πŸ› Bug fix(es)

  • Fix catcher and resolve callback argument type. (76c295f), closes #177

πŸ“ Documentation update(s)

  • Node.js 18 section wording (b696b1c)
  • Warn to use a custom until function to avoid retrying on 4xx error (1812c73), closes #176
wretch - 2.5.1

Published by elbywan over 1 year ago

2.5.1 (2023-02-27)

πŸ› Bug fix(es)

  • Update resolver.ts - Add null verification to .get("Content-Type") (a68a524)
wretch - 2.5.0

Published by elbywan over 1 year ago

2.5.0 (2023-02-20)

🏭 New feature(s)

  • Parse error type as json on proper content-type (ea9adbf), closes #171

Setting the error type as json manually using .errorType("json") should now not be necessary if the server responds with a Content-Type header set as application/json.

The response body will be deserialized and the error.json will be set accordingly.

⬆️ Version update(s)

  • Bump @fastify/multipart from 7.3.0 to 7.4.1 (e6074c9)
  • Bump @sideway/formula from 3.0.0 to 3.0.1 (8208646)
  • Bump ua-parser-js from 0.7.31 to 0.7.33 (1694fe6)
wretch - 2.4.1

Published by elbywan almost 2 years ago

2.4.1 (2023-01-20)

πŸ› Bug fix(es)

  • Fix abort/progress addons state isolation issue (2b3a659)
wretch - 2.4.0

Published by elbywan almost 2 years ago

2.4.0 (2023-01-19)

🏭 New feature(s)

  • Add skip argument to the retry middleware (746f8c9)
wretch().middlewares([
  retry({
    skip(url, options) {
      return options.method != "GET"
    },
  })
])
wretch - 2.3.2

Published by elbywan almost 2 years ago

2.3.2 (2023-01-11)

πŸ› Bug fix(es)

Allows defining global error catchers using resolvers:

const request = wretch(baseURL)
  .errorType("json")
  .resolve((response) => {
    return (
      response
        .error("Error", (error) => {
          console.log("global catch (Error class)");
        })
        .error("TypeError", (error) => {
          console.log("global type error catch (TypeError class)");
        })
    );
  });

await request
  .get(`/carts/v3/${cartId}/payment/modes`)
  // Will override the global catcher now thanks to this fix.
  .notFound((error) => {
    console.log("not found");
  })
  .json();
wretch - 2.3.1

Published by elbywan almost 2 years ago

2.3.1 (2023-01-07)

πŸ› Bug fix(es)

  • Fix middlewares and addons subpath exports (bfd2542), closes #160

πŸ“ Documentation update(s)

  • Add limitations section to the readme (ad0a102), closes #159
wretch - 2.3.0

Published by elbywan almost 2 years ago

2.3.0 (2022-12-12)

🏭 New feature(s)

  • Add url property to WretchError (a1f6ac6), closes #157
wretch - 2.2.3

Published by elbywan almost 2 years ago

2.2.3 (2022-12-09)

πŸ› Bug fix(es)

  • Better error catching precedence (107fc71), closes #155
wretch - 2.2.2

Published by elbywan almost 2 years ago

2.2.2 (2022-12-03)

πŸ› Bug fix(es)

  • Fix compatibility issue between the perfs and progress addons (b70e8cd)
wretch - 2.2.1

Published by elbywan almost 2 years ago

2.2.1 (2022-12-03)

πŸ› Bug fix(es)

  • Add missing export for the progress addon (ebb0577)
  • Add missing progress addon rollup config (bd6c89b)
  • Register the progress addon in the .all entry point (b01e03d)
wretch - 2.2.0

Published by elbywan almost 2 years ago

2.2.0 (2022-12-03)

🏭 New feature(s)

Add progress addon (2bae524), closes #154

Wretch now accepts a callback to monitor download progress.

πŸ”— Documentation.

import ProgressAddon from "wretch/addons/progress"
 
wretch("some_url")
  // Register the addon
  .addon(ProgressAddon())
  .get()
  // Log the progress as a percentage of completion
  .progress((loaded, total) => console.log(`${(loaded / total * 100).toFixed(0)}%`))

⬆️ Version update(s)

  • Bump dependencies including outdated rollup plugins (bcbcdc5)
  • Bump engine.io from 6.2.0 to 6.2.1 (6a93854)
  • Bump fastify from 4.9.2 to 4.10.2 (8ae9122)

🎨 Code improvement(s)

πŸ› Bug fix(es)

  • Fix a minor Wretch type issue in addons (23ba7b1)
wretch - 2.1.5

Published by elbywan about 2 years ago

2.1.5 (2022-10-15)

⬆️ Version update(s)

  • Bump dependencies (6043c75)
  • Bump fastify from 4.3.0 to 4.8.1 (130ccc2)

πŸ› Bug fix(es)

  • Query addon should strip undefined values (ce395b5), closes #148

πŸ“ Documentation update(s)

  • Add timeout code sample in the readme (beb51c8)
  • Fix outdated code comments (d4c546d)
  • Fix unpkg url in the readme (07d4a00)
wretch - 2.1.4

Published by elbywan about 2 years ago

2.1.4 (2022-09-28)

πŸ› Bug fix(es)

  • Relax the typechecker when using resolve within defer (9e052c8), closes #146
wretch - 2.1.3

Published by elbywan about 2 years ago

2.1.3 (2022-09-28)

πŸ› Bug fix(es)

  • Fix the retry middleware crashing on network errors (95cbad5), closes #145