thin-request

1.0.2 • Public • Published

Thin request

Travis

Thin http(s) request promise wrapper.

Installation

$ npm i --save thin-request

Usage

GET

request('https://www.endpoint.com', {
    path: '/user/1337',
    json: true // Sets application header to json. Also json parses the result.
})
.then(data => {
    // do whatever with the json result.
})
.catch(err => {
    throw new Error(err);
});

POST

request('https://www.endpoint.com', {
    path: '/user',
    method: 'POST',
    body: {
        data: 'posting'
    }
})
.then(data => {
    // do whatever with the result
})
.catch(err => {
    throw new Error(err);
});

PUT

request('https://www.endpoint.com', {
    path: '/user',
    method: 'PUT',
    body: {
        whatever: 'update/replace'
    }
})
.then(data => {
    // do whatever with the result
})
.catch(err => {
    throw new Error(err);
});

DELETE

request('https://www.endpoint.com', {
    path: '/user/1337'
})
.then(data => {
    // do whatever with the result
})
.catch(err => {
    throw new Error(err);
});

Options

This module takes care of these behind the scenes. You can easily override by defining the ones you want in the input options.

// parses the endpoint you provide
const url = require('url').parse(endpoint);
 
const opts = {
    hostname: url.hostname || params.hostname,
    port: url.port || params.port,
    path: url.path || params.path,
    protocol: url.protocol || params.protocol,
    json: false || params.json, // Take a look at GET example.
    method: params.method || 'GET',
    family: params.family || null,
    localAddress: params.localAddress || null,
    auth: params.auth || null,
    agent: params.agent || null,
    createConnection: params.createConnection || null
};

Read more about options

Tests

$ npm test

Contribution

Contributions are appreciated.

License

MIT-licensed. See LICENSE.

Dependents (1)

Package Sidebar

Install

npm i thin-request

Weekly Downloads

2

Version

1.0.2

License

MIT

Last publish

Collaborators

  • bjarneo