index.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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('extend')
  16. , cookies = require('./lib/cookies')
  17. , helpers = require('./lib/helpers')
  18. var paramsHaveRequestBody = helpers.paramsHaveRequestBody
  19. // organize params for patch, post, put, head, del
  20. function initParams(uri, options, callback) {
  21. if (typeof options === 'function') {
  22. callback = options
  23. }
  24. var params = {}
  25. if (typeof options === 'object') {
  26. extend(params, options, {uri: uri})
  27. } else if (typeof uri === 'string') {
  28. extend(params, {uri: uri})
  29. } else {
  30. extend(params, uri)
  31. }
  32. params.callback = callback || params.callback
  33. return params
  34. }
  35. function request (uri, options, callback) {
  36. if (typeof uri === 'undefined') {
  37. throw new Error('undefined is not a valid uri or options object.')
  38. }
  39. var params = initParams(uri, options, callback)
  40. if (params.method === 'HEAD' && paramsHaveRequestBody(params)) {
  41. throw new Error('HTTP HEAD requests MUST NOT include a request body.')
  42. }
  43. return new request.Request(params)
  44. }
  45. function verbFunc (verb) {
  46. var method = verb.toUpperCase()
  47. return function (uri, options, callback) {
  48. var params = initParams(uri, options, callback)
  49. params.method = method
  50. return request(params, params.callback)
  51. }
  52. }
  53. // define like this to please codeintel/intellisense IDEs
  54. request.get = verbFunc('get')
  55. request.head = verbFunc('head')
  56. request.post = verbFunc('post')
  57. request.put = verbFunc('put')
  58. request.patch = verbFunc('patch')
  59. request.del = verbFunc('delete')
  60. request['delete'] = verbFunc('delete')
  61. request.jar = function (store) {
  62. return cookies.jar(store)
  63. }
  64. request.cookie = function (str) {
  65. return cookies.parse(str)
  66. }
  67. function wrapRequestMethod (method, options, requester, verb) {
  68. return function (uri, opts, callback) {
  69. var params = initParams(uri, opts, callback)
  70. var target = {}
  71. extend(true, target, options, params)
  72. target.pool = params.pool || options.pool
  73. if (verb) {
  74. target.method = verb.toUpperCase()
  75. }
  76. if (typeof requester === 'function') {
  77. method = requester
  78. }
  79. return method(target, target.callback)
  80. }
  81. }
  82. request.defaults = function (options, requester) {
  83. var self = this
  84. options = options || {}
  85. if (typeof options === 'function') {
  86. requester = options
  87. options = {}
  88. }
  89. var defaults = wrapRequestMethod(self, options, requester)
  90. var verbs = ['get', 'head', 'post', 'put', 'patch', 'del', 'delete']
  91. verbs.forEach(function(verb) {
  92. defaults[verb] = wrapRequestMethod(self[verb], options, requester, verb)
  93. })
  94. defaults.cookie = wrapRequestMethod(self.cookie, options, requester)
  95. defaults.jar = self.jar
  96. defaults.defaults = self.defaults
  97. return defaults
  98. }
  99. request.forever = function (agentOptions, optionsArg) {
  100. var options = {}
  101. if (optionsArg) {
  102. extend(options, optionsArg)
  103. }
  104. if (agentOptions) {
  105. options.agentOptions = agentOptions
  106. }
  107. options.forever = true
  108. return request.defaults(options)
  109. }
  110. // Exports
  111. module.exports = request
  112. request.Request = require('./request')
  113. request.initParams = initParams
  114. // Backwards compatibility for request.debug
  115. Object.defineProperty(request, 'debug', {
  116. enumerable : true,
  117. get : function() {
  118. return request.Request.debug
  119. },
  120. set : function(debug) {
  121. request.Request.debug = debug
  122. }
  123. })