1.4.1 • Published 1 year ago

http-req-builder v1.4.1

Weekly downloads
253
License
MIT
Repository
github
Last release
1 year ago

http-req-builder

Wrapper around fabulous gotjs 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

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:

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
1.4.1

1 year ago

1.3.1

3 years ago

1.3.0

3 years ago

1.2.1

3 years ago

1.1.1

3 years ago

1.0.3

3 years ago

1.1.0

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago