An enhanced request module for node.js for REST-/API servers.
npm install extended-request
javascript
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:
$3
Available options:
| | | Required |
| ------ | ----------- | ------ |
| host | FQDN (eg. myapi.com) | Yes |
| port | Port | No |
| endpoint | Path prefix | No |
| auth | Authentication details | No |
`javascript
const api = new ExtendedRequest({
host: 'jsonplaceholder.typicode.com',
port: 443
})
`
---
$3
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|
`javascript
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
`javascript
// ES6
import ExtendedRequest from '@burnett01/extended-request'
// ES5
var ExtendedRequest = require('@burnett01/extended-request')
`
---
Build
$3
`npm run build`
$3
`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
$3
`make test`
$3
`npm test``