Source: api/api/http.js

  1. /*
  2. * Copyright OpenSearch Contributors
  3. * SPDX-License-Identifier: Apache-2.0
  4. *
  5. * The OpenSearch Contributors require contributions made to
  6. * this file be licensed under the Apache-2.0 license or a
  7. * compatible open source license.
  8. *
  9. */
  10. 'use strict';
  11. /** @namespace API-HTTP */
  12. const { normalizeArguments, kConfigurationError } = require('../utils');
  13. function HttpApi(transport, ConfigurationError) {
  14. this.transport = transport;
  15. this[kConfigurationError] = ConfigurationError;
  16. }
  17. HttpApi.prototype.request = function (method, params, options, callback) {
  18. [params, options, callback] = normalizeArguments(params, options, callback);
  19. if (Array.isArray(params.body)) {
  20. const { path, querystring, headers, body } = params;
  21. params = { path, querystring, headers, bulkBody: body };
  22. }
  23. options = options || {};
  24. options.headers = params.headers || options.headers;
  25. return this.transport.request({ ...params, method }, options, callback);
  26. };
  27. /**
  28. * Make a customized CONNECT request.
  29. *
  30. * @memberOf API-HTTP
  31. *
  32. * @param {Object} params
  33. * @param {Object} params.path - The URL of the request
  34. * @param {Object} [params.querystring] - The querystring parameters
  35. * @param {Object} [params.headers] - The request headers
  36. * @param {Object} [params.body] - The request body
  37. *
  38. * @param {Object} [options] - Options for {@link Transport#request}
  39. * @param {function} [callback] - Callback that handles errors and response
  40. *
  41. * @returns {{abort: function(), then: function(), catch: function()}|Promise<never>|*}
  42. */
  43. HttpApi.prototype.connect = function (params, options, callback) {
  44. return this.request('CONNECT', params, options, callback);
  45. };
  46. /**
  47. * Make a customized DELETE request.
  48. *
  49. * @memberOf API-HTTP
  50. *
  51. * @param {Object} params
  52. * @param {Object} params.path - The URL of the request
  53. * @param {Object} [params.querystring] - The querystring parameters
  54. * @param {Object} [params.headers] - The request headers
  55. * @param {Object} [params.body] - The request body
  56. *
  57. * @param {Object} [options] - Options for {@link Transport#request}
  58. * @param {function} [callback] - Callback that handles errors and response
  59. *
  60. * @returns {{abort: function(), then: function(), catch: function()}|Promise<never>|*}
  61. */
  62. HttpApi.prototype.delete = function (params, options, callback) {
  63. return this.request('DELETE', params, options, callback);
  64. };
  65. /**
  66. * Make a customized GET request.
  67. *
  68. * @memberOf API-HTTP
  69. *
  70. * @param {Object} params
  71. * @param {Object} params.path - The URL of the request
  72. * @param {Object} [params.querystring] - The querystring parameters
  73. * @param {Object} [params.headers] - The request headers
  74. * @param {Object} [params.body] - The request body
  75. *
  76. * @param {Object} [options] - Options for {@link Transport#request}
  77. * @param {function} [callback] - Callback that handles errors and response
  78. *
  79. * @returns {{abort: function(), then: function(), catch: function()}|Promise<never>|*}
  80. */
  81. HttpApi.prototype.get = function (params, options, callback) {
  82. return this.request('GET', params, options, callback);
  83. };
  84. /**
  85. * Make a customized HEAD request.
  86. *
  87. * @memberOf API-HTTP
  88. *
  89. * @param {Object} params
  90. * @param {Object} params.path - The URL of the request
  91. * @param {Object} [params.querystring] - The querystring parameters
  92. * @param {Object} [params.headers] - The request headers
  93. * @param {Object} [params.body] - The request body
  94. *
  95. * @param {Object} [options] - Options for {@link Transport#request}
  96. * @param {function} [callback] - Callback that handles errors and response
  97. *
  98. * @returns {{abort: function(), then: function(), catch: function()}|Promise<never>|*}
  99. */
  100. HttpApi.prototype.head = function (params, options, callback) {
  101. return this.request('HEAD', params, options, callback);
  102. };
  103. /**
  104. * Make a customized OPTIONS request.
  105. *
  106. * @memberOf API-HTTP
  107. *
  108. * @param {Object} params
  109. * @param {Object} params.path - The URL of the request
  110. * @param {Object} [params.querystring] - The querystring parameters
  111. * @param {Object} [params.headers] - The request headers
  112. * @param {Object} [params.body] - The request body
  113. *
  114. * @param {Object} [options] - Options for {@link Transport#request}
  115. * @param {function} [callback] - Callback that handles errors and response
  116. *
  117. * @returns {{abort: function(), then: function(), catch: function()}|Promise<never>|*}
  118. */
  119. HttpApi.prototype.options = function (params, options, callback) {
  120. return this.request('OPTIONS', params, options, callback);
  121. };
  122. /**
  123. * Make a customized PATCH request.
  124. *
  125. * @memberOf API-HTTP
  126. *
  127. * @param {Object} params
  128. * @param {Object} params.path - The URL of the request
  129. * @param {Object} [params.querystring] - The querystring parameters
  130. * @param {Object} [params.headers] - The request headers
  131. * @param {Object} [params.body] - The request body
  132. *
  133. * @param {Object} [options] - Options for {@link Transport#request}
  134. * @param {function} [callback] - Callback that handles errors and response
  135. *
  136. * @returns {{abort: function(), then: function(), catch: function()}|Promise<never>|*}
  137. */
  138. HttpApi.prototype.patch = function (params, options, callback) {
  139. return this.request('PATCH', params, options, callback);
  140. };
  141. /**
  142. * Make a customized POST request.
  143. *
  144. * @memberOf API-HTTP
  145. *
  146. * @param {Object} params
  147. * @param {Object} params.path - The URL of the request
  148. * @param {Object} [params.querystring] - The querystring parameters
  149. * @param {Object} [params.headers] - The request headers
  150. * @param {Object} [params.body] - The request body
  151. *
  152. * @param {Object} [options] - Options for {@link Transport#request}
  153. * @param {function} [callback] - Callback that handles errors and response
  154. *
  155. * @returns {{abort: function(), then: function(), catch: function()}|Promise<never>|*}
  156. */
  157. HttpApi.prototype.post = function (params, options, callback) {
  158. return this.request('POST', params, options, callback);
  159. };
  160. /**
  161. * Make a customized PUT request.
  162. *
  163. * @memberOf API-HTTP
  164. *
  165. * @param {Object} params
  166. * @param {Object} params.path - The URL of the request
  167. * @param {Object} [params.querystring] - The querystring parameters
  168. * @param {Object} [params.headers] - The request headers
  169. * @param {Object} [params.body] - The request body
  170. *
  171. * @param {Object} [options] - Options for {@link Transport#request}
  172. * @param {function} [callback] - Callback that handles errors and response
  173. *
  174. * @returns {{abort: function(), then: function(), catch: function()}|Promise<never>|*}
  175. */
  176. HttpApi.prototype.put = function (params, options, callback) {
  177. return this.request('PUT', params, options, callback);
  178. };
  179. /**
  180. * Make a customized TRACE request.
  181. *
  182. * @memberOf API-HTTP
  183. *
  184. * @param {Object} params
  185. * @param {Object} params.path - The URL of the request
  186. * @param {Object} [params.querystring] - The querystring parameters
  187. * @param {Object} [params.headers] - The request headers
  188. * @param {Object} [params.body] - The request body
  189. *
  190. * @param {Object} [options] - Options for {@link Transport#request}
  191. * @param {function} [callback] - Callback that handles errors and response
  192. *
  193. * @returns {{abort: function(), then: function(), catch: function()}|Promise<never>|*}
  194. */
  195. HttpApi.prototype.trace = function (params, options, callback) {
  196. return this.request('TRACE', params, options, callback);
  197. };
  198. module.exports = HttpApi;