/ archiveofbelonging.org / back / node_modules / @request / interface /

[ICO]NameLast modifiedSizeDescription
[PARENTDIR]Parent Directory  -  
[DIR]config/2 years ago -  
[   ]LICENSE9 years ago8.9K7375cab EXHIBTION: fix overflow ellipsis cutoff [كارل مبارك]
[TXT]README.md9 years ago 11Kf12eb36 documentaiton updates [كارل مبارك]
[   ]index.js9 years ago 93  
[   ]package.json2 years ago1.5K7375cab EXHIBTION: fix overflow ellipsis cutoff [كارل مبارك]
README.md

Common Interface for HTTP Clients

A module conforming to this specification is:

  1. A function that expects the common options object outlined in this specification
  2. A function that initiates the actual HTTP request while consuming the options outlined in this specification
module.exports = (options) => {
  // do something with options
  // and make the actual HTTP request
}

Given the above module definition, a client application can use it like this:

var request = require('my-http-client')
// make request
request({
  // any common option defined in this specification
})

HTTP Client Wrappers

var http = require('http')

module.exports = (options) => {
  // do something with the common interface options
  var resultOptions = {}
  // implement various HTTP features
  return http.request(resultOptions)
}

var request = require('request')

module.exports = (options) => {
  // do something with the common interface options
  var resultOptions = {}
  // implement various HTTP features
  return request(resultOptions)
}

// use the native fetch API in the browser

module.exports = (options) => {
  // do something with the common interface options
  var resultOptions = {}
  // implement various HTTP features
  return fetch(new Request(url, resultOptions))
}

Either way the client application should be able to make requests in a consistent way:

var request = require('my-http-client')
// make request
request({
  // any common option defined in this specification
})

Optional Dependencies

A module conforming to this specification while having optional dependencies may look like this:

module.exports = (deps) => (options) => {
  var resultOptions = {}
  if (options.oauth) {
    resultOptions.oauth = deps.oauth(options.oauth)
  }
  return request(resultOptions)
}

Given the above module definition, a client application can use it like this:

var request = require('my-http-client')({
  oauth: require('my-oauth-implementation')
})
// make request
request({
  // any common option defined in this specification
})

Bundled Dependencies

A module conforming to this specification while having hardcoded dependencies may look like this:

module.exports = require('my-http-client')({
  oauth: require('my-oauth-implementation')
})

Given the above module definition, a client application can use it like this:

var request = require('my-http-client')
// make request
request({
  // any common option defined in this specification
})

Basic API

A module using the common @request/api may look like this:

var request = require('my-http-client')
var api = require('@request/api')

module.exports = api({
  type: 'basic',
  request: request
})

Given the above module definition, a client application can use it like this:

var request = require('my-http-client')
// make request
request('url', {options}, (err, res, body) => {})
// or
request.[HTTP_VERB]('url', {options}, (err, res, bdoy) => {})
// + any combination of the above arguments

Chain API

A module using the common @request/api may look like this:

var request = require('my-http-client')
var api = require('@request/api')

module.exports = api({
  type: 'chain',
  config: {
    method: {
      get: [],
      // ...
    },
    option: {
      qs: [],
      // ...
    },
    custom: {
      submit: [],
      // ...
    }
  },
  define: {
    submit: function (callback) {
      if (callback) {
        this._options.callback = callback
      }
      return request(this._options)
    }
  }
})

Given the above module definition, a client application can use it like this:

var request = require('my-http-client')
// make request
request
  .get('url')
  .qs({a: 1})
  .submit((err, res, body) => {})

Promises

A module utilizing Promises may look like this:

module.exports = (deps) => (options) => {
  var request = deps.request

  if (deps.promise) {
    var Promise = deps.promise
    var promise = new Promise((resolve, reject) => {
      options.callback = (err, res, body) => {
        if (err) {
          reject(err)
        }
        else {
          resolve([res, body])
        }
      }
    })
    request(options)
    return promise
  }
  else {
    return request(options)
  }
}

Given the above module definition, a client application can use it like this:

var request = require('my-http-client')({
  request: require('request'),
  promise: Promise
})
// or
var request = require('my-http-client')({
  request: require('request'),
  promise: require('bluebird')
})
// make request
request({options})
  .catch((err) => {})
  .then((result) => {})

Promises can be combined with the @request/api.

Interface

option type
method String
URL
url/uri String, Object
qs Object, String
Body
form Object, String
json Object, String
body Stream, Buffer, Array, String
multipart Object, Array
Authentication
auth Object
basic, oauth, hawk, httpSignature, aws
Modifiers
gzip Boolean, String
encoding Boolean, String
stringify Object
parse Object
Proxy
proxy String, Object
tunnel Boolean
Misc
headers Object
cookie Boolean, Object
length Boolean
callback Function
redirect Boolean, Object
timeout Number
har Object
end Boolean

Method

method String


URL

url/uri String | Object

qs Object | String


Body

form Object | String

json Object | String

body String | Buffer | Array | Stream

multipart Object | Array

Each item's body can be either: Stream, Request, Buffer or String.


Authentication

auth Object


Modifiers

gzip Boolean | String

encoding Boolean | String

parse Object

stringify Object


Proxy

proxy String | Object

{
  proxy: 'http://localhost:6767'
  //
  proxy: url.parse('http://localhost:6767')
  //
  proxy: {
    url: 'http://localhost:6767',
    headers: {
      allow: ['header-name'],
      exclusive: ['header-name']
    }
  }
}

tunnel Boolean


Misc

headers Object

length Boolean

callback Function

redirect Boolean | Object

timeout Number

har Object

end Boolean


Apache/2.4.38 (Debian) Server at www.karls.computer Port 80