index.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // Copyright 2010-2012 Mikeal Rogers
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. 'use strict'
  15. var extend = require('util')._extend
  16. , cookies = require('./lib/cookies')
  17. , helpers = require('./lib/helpers')
  18. var isFunction = helpers.isFunction
  19. , constructObject = helpers.constructObject
  20. , filterForCallback = helpers.filterForCallback
  21. , constructOptionsFrom = helpers.constructOptionsFrom
  22. , paramsHaveRequestBody = helpers.paramsHaveRequestBody
  23. // organize params for patch, post, put, head, del
  24. function initParams(uri, options, callback) {
  25. callback = filterForCallback([options, callback])
  26. options = constructOptionsFrom(uri, options)
  27. return constructObject()
  28. .extend({callback: callback})
  29. .extend({options: options})
  30. .extend({uri: options.uri})
  31. .done()
  32. }
  33. function request (uri, options, callback) {
  34. if (typeof uri === 'undefined') {
  35. throw new Error('undefined is not a valid uri or options object.')
  36. }
  37. var params = initParams(uri, options, callback)
  38. options = params.options
  39. options.callback = params.callback
  40. options.uri = params.uri
  41. return new request.Request(options)
  42. }
  43. function requester(params) {
  44. if(typeof params.options._requester === 'function') {
  45. return params.options._requester
  46. }
  47. return request
  48. }
  49. request.get = function (uri, options, callback) {
  50. var params = initParams(uri, options, callback)
  51. params.options.method = 'GET'
  52. return requester(params)(params.uri || null, params.options, params.callback)
  53. }
  54. request.head = function (uri, options, callback) {
  55. var params = initParams(uri, options, callback)
  56. params.options.method = 'HEAD'
  57. if (paramsHaveRequestBody(params)) {
  58. throw new Error('HTTP HEAD requests MUST NOT include a request body.')
  59. }
  60. return requester(params)(params.uri || null, params.options, params.callback)
  61. }
  62. request.post = function (uri, options, callback) {
  63. var params = initParams(uri, options, callback)
  64. params.options.method = 'POST'
  65. return requester(params)(params.uri || null, params.options, params.callback)
  66. }
  67. request.put = function (uri, options, callback) {
  68. var params = initParams(uri, options, callback)
  69. params.options.method = 'PUT'
  70. return requester(params)(params.uri || null, params.options, params.callback)
  71. }
  72. request.patch = function (uri, options, callback) {
  73. var params = initParams(uri, options, callback)
  74. params.options.method = 'PATCH'
  75. return requester(params)(params.uri || null, params.options, params.callback)
  76. }
  77. request.del = function (uri, options, callback) {
  78. var params = initParams(uri, options, callback)
  79. params.options.method = 'DELETE'
  80. return requester(params)(params.uri || null, params.options, params.callback)
  81. }
  82. request.jar = function (store) {
  83. return cookies.jar(store)
  84. }
  85. request.cookie = function (str) {
  86. return cookies.parse(str)
  87. }
  88. request.defaults = function (options, requester) {
  89. var self = this
  90. var wrap = function (method) {
  91. var headerlessOptions = function (options) {
  92. options = extend({}, options)
  93. delete options.headers
  94. return options
  95. }
  96. var getHeaders = function (params, options) {
  97. return constructObject()
  98. .extend(options.headers)
  99. .extend(params.options.headers)
  100. .done()
  101. }
  102. return function (uri, opts, callback) {
  103. var params = initParams(uri, opts, callback)
  104. params.options = extend(headerlessOptions(options), params.options)
  105. if (options.headers) {
  106. params.options.headers = getHeaders(params, options)
  107. }
  108. if (isFunction(requester)) {
  109. if (method === self) {
  110. method = requester
  111. } else {
  112. params.options._requester = requester
  113. }
  114. }
  115. return method(params.options, params.callback)
  116. }
  117. }
  118. var defaults = wrap(self)
  119. defaults.get = wrap(self.get)
  120. defaults.patch = wrap(self.patch)
  121. defaults.post = wrap(self.post)
  122. defaults.put = wrap(self.put)
  123. defaults.head = wrap(self.head)
  124. defaults.del = wrap(self.del)
  125. defaults.cookie = wrap(self.cookie)
  126. defaults.jar = self.jar
  127. defaults.defaults = self.defaults
  128. return defaults
  129. }
  130. request.forever = function (agentOptions, optionsArg) {
  131. var options = constructObject()
  132. if (optionsArg) {
  133. options.extend(optionsArg)
  134. }
  135. if (agentOptions) {
  136. options.agentOptions = agentOptions
  137. }
  138. options.extend({forever: true})
  139. return request.defaults(options.done())
  140. }
  141. // Exports
  142. module.exports = request
  143. request.Request = require('./request')
  144. request.debug = process.env.NODE_DEBUG && /\brequest\b/.test(process.env.NODE_DEBUG)
  145. request.initParams = initParams