npm.io
0.4.8 • Published 1 year ago

return-fetch-json

Licence
MIT
Version
0.4.8
Deps
1
Size
20 kB
Vulns
0
Weekly
0
Stars
165
Install scriptsThis package runs scripts during installation (preinstall/install/postinstall)

️return-fetch-json

An extended function of [return-fetch](https://github.com/myeongjae-kim/return-fetch) to serialize request body and deserialize response body as json.
See Documentation or See Demo

CI Test Coverage npm version Bundle Size MIT license

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.