1.6.1 • Published 2 years ago

extended-request v1.6.1

Weekly downloads
2
License
MIT
Repository
github
Last release
2 years ago

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:

PropertyDescription
detailsAn object with some connection details
optionsAn object containing request options
hostFQDN used for requests (eg. myapi.com)
portDefault = 80
443 enables https
pathURI path (eg. /get/users)
endpointhost endpoint/prefix for a path (eg. /api/v1)
typeThe HTTP Content-Type for the request. Check lib/util.js for valid types Default is BT.JSON
bodyPOST payload
bodyTypeThe type of the body payload. Check lib/util.js for valid types. Default is BT.FORM
authAuthentication 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
hostFQDN (eg. myapi.com)Yes
portPortNo
endpointPath prefixNo
authAuthentication detailsNo
const api = new ExtendedRequest({
  host: 'jsonplaceholder.typicode.com',
  port: 443
})

Performing a request

Available options:

Required
methodHTTP method (Default: GET)No
typeContent-Type (Default: 'application/json')No
bodyBody for a post requestIf method is 'POST'
bodyTypeSet specific type (Default: 'application/x-www-form-urlencoded')No
headersSet optional headers objectNo
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

1.6.1

2 years ago

1.6.0

4 years ago

1.5.0

4 years ago

1.4.0

5 years ago

1.3.0

5 years ago

1.2.0

5 years ago

1.1.0

6 years ago

1.0.9

6 years ago

1.0.8

6 years ago

1.0.7

6 years ago

1.0.6

6 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago