An extended function of [return-fetch](https://github.com/deer-develop/return-fetch) to serialize request body and
deserialize response body as json.
See Documentation
or
See Demo
import returnFetchJson from "return-fetch-json";
// Create an extended fetch function and use it instead of the global fetch.
export const fetchExtended = returnFetchJson({
jsonParser: JSON.parse, // `jsonParser` property is omittable. You can use your custom parser.
baseUrl: "https://jsonplaceholder.typicode.com"
});
//////////////////// Use it somewhere ////////////////////
fetchExtended<{ id: number }>("/posts", {
method: "POST",
body: { message: "Hello, world!" },
}).then(it => it.body)
.then(console.log);
Via npm
npm install return-fetch-json
Via yarn
yarn add return-fetch-json
Via pnpm
pnpm add return-fetch-json
<!--
Pick your favourite CDN:
- https://unpkg.com/return-fetch-json
- https://cdn.jsdelivr.net/npm/return-fetch-json
- https://www.skypack.dev/view/return-fetch-json
- …
-->
<!-- UMD import as window.returnFetchJson -->
<script src="https://unpkg.com/return-fetch-json"></script>
<!-- Modern import -->
<script type="module">
import returnFetchJson from 'https://cdn.skypack.dev/return-fetch-json/dist/index.js'
// ... //
</script>
Run on Stickblitz.