# ad-http

> Another Rest JS Library.

Latest version **0.0.4** (published 2019-05-12) · MIT license · 0 weekly downloads

## Install

```sh
npm install ad-http
pnpm add ad-http
yarn add ad-http
bun add ad-http
```

## Health

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

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.4 |
| Published | 2019-05-12 |
| First published | 2019-04-13 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 252.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Ahmad Dehnavi |
| Maintainers | a.dehnavi |

## Links

- npm: https://www.npmjs.com/package/ad-http
- Repository: https://github.com/ahmaddehnavi/ad-http
- npm.io page: https://npm.io/package/ad-http

## Dependencies (3)

- [tslib](https://npm.io/package/tslib.md) ^1.9.3
- [ad-logger](https://npm.io/package/ad-logger.md) 0.0.10
- [autobind-decorator](https://npm.io/package/autobind-decorator.md) ^2.4.0

## Recent versions

- 0.0.4 (latest) — 2019-05-12
- 0.0.3 — 2019-04-18
- 0.0.2 — 2019-04-13
- 0.0.1 — 2019-04-13

## README

#ad-http
[![Build Status](https://travis-ci.com/ahmaddehnavi/ad-http.svg?branch=master)](https://travis-ci.com/ahmaddehnavi/ad-http)

#Install :
```
yarn add ad-http
```

#Simple Usage
```typescript
import ADHttp, {Request} from 'ad-http'

 ADHttp.process('https://jsonplaceholder.typicode.com/todos/1')
        .then(response => response.body())
        .then(body => body.json())
        .then(json => {
            console.log(json)
        })
        .catch(error=>{
            console.error(error)
        })
```
#Advanced Usage
**import**
```typescript
import {
    PrepareRequestInterceptor,
    StatusCheckerHttpInterceptor,
    BaseUrlHttpInterceptor,
    HttpError,
    StatusError,
    Rest,
    Request,
    NormalClient
} from 'ad-http'
```
**create rest instance**
```
type BaseResponse = { status: boolean, message: string }
let rest = new Rest<BaseResponse>();
```
***or use custom client***
```
type BaseResponse = { status: boolean, message: string }
let rest = new Rest<BaseResponse>(new NormalClient());
```

**change request or response if need**
```
rest.interceptors.push(new PrepareRequestInterceptor());
rest.interceptors.push(new StatusCheckerHttpInterceptor());
rest.interceptors.push(new BaseUrlHttpInterceptor('https://httpstat.us/'));
```
**write custom interceptors**
```
// make new request
rest.interceptors.push({
    name: 'MakeNewRequest',
    intercept: async (chain) => {
        console.log('check status to make new request ' + chain.request.url);
        let response = await chain.proceed();
        if (response.status === 404) {
            let url = 'https://jsonplaceholder.typicode.com/todos';
            console.log('try make new request to' + url);
            return (
                chain.reset()
                    .proceed(
                        chain.request
                            .edit()
                            .url(url)
                            .build()
                    )
            );
        }
        return response;
    }
});
```

**create request**
```
let request = new Request.Builder()
        .url('/404')
        .build();
```

**process request**
```
rest.process(request)
    .then(async (response) => {
       console.log('Status : ',response.status);
       console.log('Headers : ',response.headers);
       let body=await response.body();
       let model=await body.json();
       console.log(model);
    })
    .catch((e) => {
       console.error(e);
       console.error(e instanceof StatusError);
       console.error(e instanceof HttpError);
       console.error(e instanceof Error);
    });
```

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