return-fetch-json
v0.4.6
Published
An extended function of return-fetch ot serialize request body and deserialize response body as object.
Downloads
209
Maintainers
Readme
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);
Installation
Package Manager
Via npm
npm install return-fetch-json
Via yarn
yarn add return-fetch-json
Via pnpm
pnpm add return-fetch-json
<script> tag
<!--
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>
Demo
Run on Stickblitz.