# lski-fetch-json

> Fetch wrapper to handle JSON automatically

Latest version **1.0.1** (published 2018-09-16) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install lski-fetch-json
pnpm add lski-fetch-json
yarn add lski-fetch-json
bun add lski-fetch-json
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2018-09-16 |
| First published | 2017-08-19 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 24.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Lee Cooper |
| Maintainers | lski |
| Keywords | fetch, ajax, json |

## Links

- npm: https://www.npmjs.com/package/lski-fetch-json
- Repository: https://github.com/lski/lski-fetch-json
- Homepage: https://github.com/lski/lski-fetch-json#readme
- Issues: https://github.com/lski/lski-fetch-json/issues
- npm.io page: https://npm.io/package/lski-fetch-json

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 1.0.1 (latest) — 2018-09-16
- 1.0.0 — 2017-08-19

## README

lski-fetch-json
===============

***Deprecated:** Package didnt have proper focus, plus the main function 'fetchJson' did not handle real world errors that well. So recommended you dont use it going forward.*

This library provides a few functions that wrap parts of the fetch API to default headers and the data to sending and/or receiving JSON, rather than having to write it each time.

This library is all about convenience, easy of use and flexibility, you can choose whether you just want to send, or just want to receive JSON or in fact both in the same call.

This library does not modify the fetch API and it uses pure functions.

### Example - Get JSON

```js
import fetchJson, { postJson } from 'lski-fetch-json';

fetchJson('http://an-api.com')
    .then(({ data, response }) => {
        console.log(data, response); 
        // data: a JSON object not a string
        // response: the original fetch response object
    });

```

The fetchJson function only deals with getting JSON, it does not care if you are sending JSON, you can send any data type you want and can accept options (or a Request object) the same as standard fetch. The only difference between fetch and fetchJson is the parameter to the first 'then' is an object that includes two properties 'data' that is the JSON data returned and 'response' which is the original raw response given to fetch.

### Example - Send JSON

```js
const myData = { hello: "world" };

fetch('http://an-api.com', postJson(myData))
    .then(response => {
        // A normal fetch response object
    });
```

The postJson (and putJson) function deals purely with sending a JSON object directly to the url. It does not care if you are receiving JSON back, just that you are sending it, so can be used with the standard fetch function. Therefore it handles the conversion to a JSON string and setting the appropriate headers. 

### Example - Send and Get JSON

```js
fetchJson('http://an-api.com', postJson(myData))
    .then(({ data, response }) => {
        console.log(data, response); 
    });
```

### Example - Request

```js
import fetchJson from 'lski-fetch-json';

const request = new Request('http://an-api.com', postJson({ hello: "world" }));

fetchJson(request)
    .then(({ data, response }) => {
        console.log(data, response); 
    });
```

If you prefer to use and reuse a Request object fetchJson works in the same way as fetch, just with the

## Install

npm i lski-fetch-json

### Test

npm i
npm test

## Prerequisites

There are only two things needed for this to work, fetch and Object.assign. Both of which are easily polyfilled in needed.

---
_Source: https://npm.io/package/lski-fetch-json · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
