All files / ima/http HttpAgent.js

100% Statements 2/2
0% Branches 0/5
0% Functions 0/8
100% Lines 2/2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152    3x                                                                                                                                                                                                                                                                                                       3x  
import ns from '../namespace';
 
ns.namespace('ima.http');
 
/**
 * Options for a request sent using the HTTP agent.
 * @typedef {Object} HttpAgent~RequestOptions
 * @property {number} [timeout] Specifies the request timeout in milliseconds.
 * @property {number} [ttl] Specified how long the request may be cached in
 *           milliseconds.
 * @property {number} [repeatRequest] Specifies the maximum number of tries to
 *           repeat the request if the request fails.
 * @property {Object<string, string>} [headers] Sets the additional request
 *           headers (the keys are case-insensitive header names, the values
 *           are header values).
 * @property {boolean} [cache] Flag that enables caching the HTTP request
 *           (enabled by default, also applies to requests in progress).
 * @property {boolean} [withCredentials] Flag that indicates whether the
 *           request should be made using credentials such as cookies or
 *           authorization headers.
 * @property {{progress: function=}} [listeners] Listeners for request events.
 * @property {function(HttpAgent~Response)} [postProcessor] Response
 *           post-processor applied just before the response is stored in the
 *           cache and returned.
 */
 
/**
 * A response from the server.
 * @typedef {Object} HttpAgent~Response
 * @property {number} status The HTTP response status code.
 * @property {*} body The parsed response body, parsed as JSON.
 * @property {HttpProxy~RequestParams} params The original request params.
 * @property {Object<string, string>} headers The response HTTP headers.
 * @property {boolean} cached Whether or not the response has been cached.
 */
 
/**
 * The {@codelink HttpAgent} defines unifying API for sending HTTP requests at
 * both client-side and server-side.
 *
 * @interface
 */
export default class HttpAgent {
  /**
	 * Sends an HTTP GET request to the specified URL, sending the provided
	 * data as query parameters.
	 *
	 * @param {string} url The URL to which the request should be made.
	 * @param {Object<string, (boolean|number|string)>} data The data to send
	 *        to the server as query parameters.
	 * @param {HttpAgent~RequestOptions=} options Optional request options.
	 * @return {Promise<HttpAgent~Response>} A promise that resolves to the
	 *         response.
	 */
  get(url, data, options = {}) {}
 
  /**
	 * Sends an HTTP POST request to the specified URL, sending the provided
	 * data as the request body. If an object is provided as the request data,
	 * the data will be JSON-encoded. Sending other primitive non-string values
	 * as the request body is not supported.
	 *
	 * @param {string} url The URL to which the request should be made.
	 * @param {(string|Object<string, *>)} data The data to send to the server
	 *        as the request body.
	 * @param {HttpAgent~RequestOptions=} options Optional request options.
	 * @return {Promise<HttpAgent~Response>} A promise that resolves to the
	 *         response.
	 */
  post(url, data, options = {}) {}
 
  /**
	 * Sends an HTTP PUT request to the specified URL, sending the provided
	 * data as the request body. If an object is provided as the request data,
	 * the data will be JSON-encoded. Sending other primitive non-string values
	 * as the request body is not supported.
	 *
	 * @param {string} url The URL to which the request should be made.
	 * @param {(string|Object<string, *>)} data The data to send to the server
	 *        as the request body.
	 * @param {HttpAgent~RequestOptions=} options Optional request options.
	 * @return {Promise<HttpAgent~Response>} A promise that resolves to the
	 *         response.
	 */
  put(url, data, options = {}) {}
 
  /**
	 * Sends an HTTP PATCH request to the specified URL, sending the provided
	 * data as the request body. If an object is provided as the request data,
	 * the data will be JSON-encoded. Sending other primitive non-string values
	 * as the request body is not supported.
	 *
	 * @param {string} url The URL to which the request should be made.
	 * @param {(string|Object<string, *>)} data The data to send to the server
	 *        as the request body.
	 * @param {HttpAgent~RequestOptions=} options Optional request options.
	 * @return {Promise<HttpAgent~Response>} A promise that resolves to the
	 *         response.
	 */
  patch(url, data, options = {}) {}
 
  /**
	 * Sends an HTTP DELETE request to the specified URL, sending the provided
	 * data as the request body. If an object is provided as the request data,
	 * the data will be JSON-encoded. Sending other primitive non-string values
	 * as the request body is not supported.
	 *
	 * @param {string} url The URL to which the request should be made.
	 * @param {(string|Object<string, *>)} data The data to send to the server
	 *        as the request body.
	 * @param {HttpAgent~RequestOptions=} options Optional request options.
	 * @return {Promise<HttpAgent~Response>} A promise that resolves to the
	 *         response.
	 */
  delete(url, data, options = {}) {}
 
  /**
	 * Generates a cache key to use for identifying a request to the specified
	 * URL using the specified HTTP method, submitting the provided data.
	 *
	 * @param {string} method The HTTP method used by the request.
	 * @param {string} url The URL to which the request is sent.
	 * @param {Object<string, string>} data The data associated with the
	 *        request. These can be either the query parameters or request body
	 *        data.
	 * @return {string} The key to use for identifying such a request in the
	 *         cache.
	 */
  getCacheKey(method, url, data) {}
 
  /**
	 * Sets the specified header to be sent with every subsequent HTTP request,
	 * unless explicitly overridden by request options.
	 *
	 * @param {string} header The name of the header.
	 * @param {string} value The header value. To provide multiple values,
	 *        separate them with commas
	 *        (see http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2).
	 * @return {HttpAgent} This HTTP agent.
	 */
  setDefaultHeader(header, value) {}
 
  /**
	 * Clears all configured default headers.
	 *
	 * @return {HttpAgent} This HTTP agent.
	 */
  clearDefaultHeaders() {}
}
 
ns.ima.http.HttpAgent = HttpAgent;