# http-req-builder

> Builder for API requests, i use it in API testing projects

Latest version **1.4.1** (published 2023-03-17) · MIT license · 0 weekly downloads

## Install

```sh
npm install http-req-builder
pnpm add http-req-builder
yarn add http-req-builder
bun add http-req-builder
```

## Health

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

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

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.4.1 |
| Published | 2023-03-17 |
| First published | 2021-01-13 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 4 |
| Unpacked size | 24.6 KB |
| Known vulnerabilities | 0 (+2 in 2 direct dependencies) |
| Install scripts | no |
| GitHub stars | 10 |
| Author | Oleksandr Khotemskyi |
| Maintainers | xotabu4 |

## Links

- npm: https://www.npmjs.com/package/http-req-builder
- Repository: https://github.com/Xotabu4/http-req-builder
- Homepage: https://github.com/Xotabu4/http-req-builder#readme
- Issues: https://github.com/Xotabu4/http-req-builder/issues
- npm.io page: https://npm.io/package/http-req-builder

## Dependencies (4)

- [got](https://npm.io/package/got.md) 11.8.1
- [form-data](https://npm.io/package/form-data.md) ^4.0.0
- [tough-cookie](https://npm.io/package/tough-cookie.md) 4.0.0
- [@types/tough-cookie](https://npm.io/package/@types/tough-cookie.md) 4.0.0

## Recent versions

- 1.4.1 (latest) — 2023-03-17
- 1.3.1 — 2021-05-04
- 1.3.0 — 2021-05-04
- 1.2.1 — 2021-03-31
- 1.1.1 — 2021-03-27
- 1.0.3 — 2021-03-27
- 1.1.0 — 2021-02-21
- 1.0.2 — 2021-01-13
- 1.0.1 — 2021-01-13
- 1.0.0 — 2021-01-13

## README

# http-req-builder
 
Wrapper around fabulous [gotjs](https://github.com/sindresorhus/got) library. 

This lib provides builder pattern, and simplifies and hides some of the gotjs features.

I personaly use this library for my API tests.

# Usage

```typescript
import { JsonRequest } from 'http-req-builder'

const resp = await new JsonRequest()
    .url('https://httpbin.org/post')
    .method('POST')
    .searchParams({
        "hello": "world"
    })
    .body({
        "testing": true
    })
    .send()

assert(resp.body.headers['User-Agent'].includes('got'))
assert(resp.body.json.testing)  
assert(resp.body.args.hello == 'world')  
```

- Start building your requests with `new JsonRequest()` or `new FormRequest()`
- Specify parameters with methods:
`prefixUrl` `url` `cookieJar` `method` `headers` `bearerToken` `searchParams` `body`
- Send constructed request with `.send()`, optionaly specify response body type with `.send<MyResponseBodyType>`
- `await` returned promise, and do whatever you need


# Extending

Feel free to `extend` `BaseHttpRequest` or `FormRequest, JsonRequest`, and add/override methods. For example, lets add simplest logging on each request:

```typescript
import { JsonRequest } from 'http-req-builder'

class MyRequest extends JsonRequest {
    constructor() {
        super()
        // See https://github.com/sindresorhus/got#hooks
        this.options = {
            // Keeping options from JsonRequest 
            ...this.options,
            hooks: {
                beforeRequest: [options => {
                    console.time(`Request took`)
                }],
                afterResponse: [response => {
                    console.timeEnd(`Request took`)
                    return response;
                }]
            }
        }
    }
}

await new MyRequest()
    .url('https://httpbin.org/get')
    .send()

await new MyRequest()
    .url('https://httpbin.org/get')
    .send()

// Request took: 125.032ms
// Request took: 123.292ms
```

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