You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

155 lines
3.9 KiB

  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. var cookies = require('./lib/cookies')
  17. var 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 (options !== null && 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.options = verbFunc('options')
  57. request.post = verbFunc('post')
  58. request.put = verbFunc('put')
  59. request.patch = verbFunc('patch')
  60. request.del = verbFunc('delete')
  61. request['delete'] = verbFunc('delete')
  62. request.jar = function (store) {
  63. return cookies.jar(store)
  64. }
  65. request.cookie = function (str) {
  66. return cookies.parse(str)
  67. }
  68. function wrapRequestMethod (method, options, requester, verb) {
  69. return function (uri, opts, callback) {
  70. var params = initParams(uri, opts, callback)
  71. var target = {}
  72. extend(true, target, options, params)
  73. target.pool = params.pool || options.pool
  74. if (verb) {
  75. target.method = verb.toUpperCase()
  76. }
  77. if (typeof requester === 'function') {
  78. method = requester
  79. }
  80. return method(target, target.callback)
  81. }
  82. }
  83. request.defaults = function (options, requester) {
  84. var self = this
  85. options = options || {}
  86. if (typeof options === 'function') {
  87. requester = options
  88. options = {}
  89. }
  90. var defaults = wrapRequestMethod(self, options, requester)
  91. var verbs = ['get', 'head', 'post', 'put', 'patch', 'del', 'delete']
  92. verbs.forEach(function (verb) {
  93. defaults[verb] = wrapRequestMethod(self[verb], options, requester, verb)
  94. })
  95. defaults.cookie = wrapRequestMethod(self.cookie, options, requester)
  96. defaults.jar = self.jar
  97. defaults.defaults = self.defaults
  98. return defaults
  99. }
  100. request.forever = function (agentOptions, optionsArg) {
  101. var options = {}
  102. if (optionsArg) {
  103. extend(options, optionsArg)
  104. }
  105. if (agentOptions) {
  106. options.agentOptions = agentOptions
  107. }
  108. options.forever = true
  109. return request.defaults(options)
  110. }
  111. // Exports
  112. module.exports = request
  113. request.Request = require('./request')
  114. request.initParams = initParams
  115. // Backwards compatibility for request.debug
  116. Object.defineProperty(request, 'debug', {
  117. enumerable: true,
  118. get: function () {
  119. return request.Request.debug
  120. },
  121. set: function (debug) {
  122. request.Request.debug = debug
  123. }
  124. })