@burnett01/extended-request

1.6.1 • Public • Published

extended-request

GitHub license Build Status npm version

An enhanced request module for node.js for REST-/API servers.

Features:

  • Supports various auth providers (Basic, Bearer, Token, PostToken, Custom ..)
  • Supports HTTP and HTTP/S (TLS) protocols
  • Auto response content-type negotiation (JSON, HTML, Plain, ...)
  • GZIP compression
  • ES6 & ES5 support
  • Promises & classic nodeback
  • Debugging

Table of contents


API Reference

ExtendedRequest(
    [Object {
        host: String='',
        port: Number='',
        endpoint: String='',
        auth: Object { 
            provider: String='basic|bearer|token|postToken|custom',
            username: String='',
            password: String='',
            token: String='',
            custom: String=''
        }
    } details]
) -> Object {
    /* Constants */
    this:  Object=this,

    /* Methods */
    request:  [String=path, Object {
        method: String='GET|POST|HEAD|...',
        type: String='text/plain|application/json|...',
        bodyType: String='application/x-www-form-urlencoded|application/json|...',
        body: String='',
    } options, function(class ErrorClass err, null ok) cb] | Promise
}

/* Statics */

ExtendedRequest.DEBUG = true/false

Property reference:

Property Description
details An object with some connection details
options An object containing request options
host FQDN used for requests (eg. myapi.com)
port Default = 80
443 enables https
path URI path (eg. /get/users)
endpoint host endpoint/prefix for a path (eg. /api/v1)
type The HTTP Content-Type for the request. Check lib/util.js for valid types Default is BT.JSON
body POST payload
bodyType The type of the body payload. Check lib/util.js for valid types. Default is BT.FORM
auth Authentication details
.provider = Either 'basic, bearer, token, postToken, custom'
.username = Set HTTP basic auth username when provider is 'basic'
.password = Set HTTP basic auth password when provider is 'basic'
.token =
Set HTTP auth token (header) when provider is 'token' or 'bearer'.
Set POST token when provider is 'postToken' and request.method is 'POST'
.custom =
Set HTTP auth header when provider is 'custom'

Function reference:

Creating a request container

Available options:

Required
host FQDN (eg. myapi.com) Yes
port Port No
endpoint Path prefix No
auth Authentication details No
const api = new ExtendedRequest({
  host: 'jsonplaceholder.typicode.com',
  port: 443
})

Performing a request

Available options:

Required
method HTTP method (Default: GET) No
type Content-Type (Default: 'application/json') No
body Body for a post request If method is 'POST'
bodyType Set specific type
(Default: 'application/x-www-form-urlencoded')
No
headers Set optional headers object No
api.request('/posts/1', (err, response) => {
  console.log(err, response)
})

api.request('/store/post', {
  method: 'POST',
  body: 'My new Post!'
}, (err, response) => {
  console.log(err, response)
})

/* Promises */

api.request('/posts/1')
.then((response) => {
  console.log(response)
})
.catch((err) => {
  console.log(err)
})

api.request('/store/post', {
  method: 'POST',
  body: 'My new Post!'
})
.then((response) => {
  console.log(response)
})
.catch((err) => {
  console.log(err)
})

Setup / Install

Use npm install @burnett01/extended-request

// ES6
import ExtendedRequest from '@burnett01/extended-request'

// ES5
var ExtendedRequest = require('@burnett01/extended-request')

Build

NPM

npm run build

Make

make build


Unit-Tests

The testing-framework used by this module is Mocha with the BDD / TDD assertion library Chai.

  • test/test.default.js Performs 9 tests | Source

Default reporter: list

Make

make test

NPM

npm test


Contributing

You're very welcome and free to contribute. Thank you.


License

MIT

Package Sidebar

Install

npm i @burnett01/extended-request

Weekly Downloads

2

Version

1.6.1

License

MIT

Unpacked Size

25 kB

Total Files

12

Last publish

Collaborators

  • krew