# async-json-request

> Base request methods based on promises for async/await ussage

Latest version **4.1.0** (published 2019-08-14) · ISC license · 0 weekly downloads

## Install

```sh
npm install async-json-request
pnpm add async-json-request
yarn add async-json-request
bun add async-json-request
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 4.1.0 |
| Published | 2019-08-14 |
| First published | 2018-02-26 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 6 |
| Unpacked size | 13.1 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Antonin Bouchal |
| Maintainers | scorpio1337 |

## Links

- npm: https://www.npmjs.com/package/async-json-request
- Repository: https://github.com/bouchal/node-async-json-request
- Homepage: https://github.com/bouchal/node-async-json-request#readme
- Issues: https://github.com/bouchal/node-async-json-request/issues
- npm.io page: https://npm.io/package/async-json-request

## Dependencies (6)

- [methods](https://npm.io/package/methods.md) ^1.1.2
- [request](https://npm.io/package/request.md) ^2.88.0
- [deepmerge](https://npm.io/package/deepmerge.md) ^2.1.1
- [@types/methods](https://npm.io/package/@types/methods.md) ^1.1.0
- [@types/request](https://npm.io/package/@types/request.md) ^2.48.1
- [@types/caseless](https://npm.io/package/@types/caseless.md) ^0.12.1

## Recent versions

- 4.1.0 (latest) — 2019-08-14
- 4.0.5 — 2019-02-12
- 4.0.4 — 2019-02-12
- 4.0.3 — 2019-02-12
- 4.0.2 — 2019-02-12
- 4.0.1 — 2019-02-12
- 4.0.0 — 2019-02-06
- 3.1.0 — 2019-02-05
- 3.0.0 — 2019-01-21
- 2.2.0 — 2018-09-19
- 2.1.0 — 2018-08-20
- 2.0.3 — 2018-08-06
- 2.0.2 — 2018-08-06
- 2.0.1 — 2018-08-06
- 1.0.1 — 2018-05-09
- … 1 more at https://npm.io/package/async-json-request/versions

## README

Simple sending JSON requests via async/await or promises.

# Async JSON Request

[![Coverage Status](https://coveralls.io/repos/github/bouchal/node-async-json-request/badge.svg?branch=master)](https://coveralls.io/github/bouchal/node-async-json-request?branch=master)

## Installation

```
npm i async-json-request --save
```

## Usage

### Basic request sending

```javascript
import JsonRequest from 'async-json-request';

const apiRequest = new JsonRequest('https://jsonplaceholder.typicode.com');

const printData = async () => {
	const result = await apiRequest.get('/posts/1');
	
	console.log(result.body);
};

printData();
```

### Methods

- __.get(URI, PARAMETERS)__
- __.post(URI, PARAMETERS, BODY)__
- __.put(URI, PARAMETERS, BODY)__
- ...and every other possible methods

### Custom default request options

```javascript
import JsonRequest from 'async-json-request';

const apiRequest = new JsonRequest('https://jsonplaceholder.typicode.com', {
	headers: {
		'x-token': 'TOKEN'
	}
});
```

### Custom additional options for specific request

```javascript
import JsonRequest from 'async-json-request';

const apiRequest = new JsonRequest('https://jsonplaceholder.typicode.com', {
	headers: {
		'x-token': 'TOKEN'
	}
});

const result = apiRequest
    .wrap({
        headers: {
            'authorization': 'Bearer XYZ'
        }
    })
    .get('/posts/1');
```

All options are deep merged together.

### Typescript

If you know schema which return in body, you can use generic types for better suggestions.

```typescript
import JsonRequest from 'async-json-request';

interface IPost {
    userId: number;
    id: number;
    title: string;
    body: string;
}

const apiRequest = new JsonRequest('https://jsonplaceholder.typicode.com');

const printFirstPostTitle = async (): Promise<void> => {
    const response = await apiRequest.get<IPost>('/posts/1');

    const responseBody = response.body; // Now body will be suggest you IPost interface.
    
    console.log(`Title of first post is: ${responseBody.title}`);
}

printFirstPostTitle();
```

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