es6/http/index.js
/**
 * Prepare an http client promise with options.
 * @param {string} url The url for the http request i.e. http://localhost:4001/db/query
 * @param {object} options Options for the HTTP client.
 * @param {string=} options.httpMethod The HTTP method for the request i.e. get or post.
 * @param {object=} options.query An object with the query to send with the HTTP request.
 * @param {object=} options.body The body of the HTTP request for all non get requests.
 * @param {object=} options.agent Agent to replace the default agent i.e. keepalive.
 * @param {object=} options.timeout Optional timeout to override default.
 * @param {number=} options.timeout.response Milliseconds to wait for the server to start
 * sending data.
 * @param {number=} options.timeout.deadline Milliseconds to wait for the data to finish being sent.
 * @param {object=} options.headers HTTP headers to send with the request.
 */
Prepare an http client promise with options.
@param {string} url The url for the http request i.e.
Prepare an http client promise with options . @param { string } url The url for the http request i . e .
function prepare(url, options = {}) {
  const {
    httpMethod = HTTP_METHOD_GET,
    query,
    body,
    agent,
    auth,
    buffer,
    responseParserType,
  } = options
  let { headers = {}, timeout = {} } = options
  const client = getHttpLibrary()[httpMethod](url)
  if (httpMethod !== HTTP_METHOD_GET) {
    headers = createJsonHeaders(headers)
  }
  if (query) {
    client.query(queryString.stringify(query))
  }
  if (_isObject(auth)) {
    const { user, pass, authOptions } = auth
    client.auth(user, pass, authOptions)
  }
  if (body && httpMethod !== HTTP_METHOD_GET) {
    client.send(body)
  }
  if (agent) {
    client.agent(agent)
  }
  if (buffer) {
    client.buffer(true)
  }
  if (responseParserType === CONTENT_TYPE_APPLICATION_OCTET_STREAM) {
    const parser = superagent.parse[responseParserType]
    client.parse(parser)
  }
  if (timeout && !_isObject(timeout)) {
    timeout = { deadline: timeout }
  }
  client.timeout(_assign({}, timeout, DEAULT_TIMEOUT))
  client.set(createDefaultHeaders(headers))
  return client
}
function prepare ( url , options = { } ) { const { httpMethod = HTTP_METHOD_GET , query , body , agent , auth , buffer , responseParserType , } = options let { headers = { } , timeout = { } } = options const client = getHttpLibrary ( ) [ httpMethod ] ( url ) if ( httpMethod !== HTTP_METHOD_GET ) { headers = createJsonHeaders ( headers ) } if ( query ) { client . query ( queryString . stringify ( query ) ) } if ( _isObject ( auth ) ) { const { user , pass , authOptions } = auth client . auth ( user , pass , authOptions ) } if ( body && httpMethod !== HTTP_METHOD_GET ) { client . send ( body ) } if ( agent ) { client . agent ( agent ) } if ( buffer ) { client . buffer ( true ) } if ( responseParserType === CONTENT_TYPE_APPLICATION_OCTET_STREAM ) { const parser = superagent . parse [ responseParserType ] client . parse ( parser ) } if ( timeout && ! _isObject ( timeout ) ) { timeout = { deadline : timeout } } client . timeout ( _assign ( { } , timeout , DEAULT_TIMEOUT ) ) client . set ( createDefaultHeaders ( headers ) ) return client }
Prepare an http client promise with options.
Prepare an http client promise with options .
// Add headers for the JSON requests which are all non-get requests. // Add a query to the request // Add a query to the request // Convert timeout to object if it is set and not an object