import xhr from './xhr'; import fetch from './fetch'; import type { NetworkResponse, RequestOptions } from '../types'; function hasFetchSupport(): boolean { return 'fetch' in window && typeof window.fetch === 'function'; } function hasCorsSupport(): boolean { return 'XMLHttpRequest' in window && 'withCredentials' in new XMLHttpRequest(); } /** * http client for browsers. Will attempt to use a CORS capable implementation * if available. * * @param {Object} options * @param {string} options.url The url of the request. * @param {string} [options.method] The HTTP method to use, ie, GET, POST, etc. * @param {Object} [options.data] The data to send with the request. For JSONP * and methods that do not support bodies, data * will be converted to URL parameters, otherwise * sent as the body for applicable HTTP methods. * @param {Object} [options.headers] HTTP headers to be sent with the request. * @param {boolean} [options.envelope=false] Default false. Fallback to using envelope * based POST requests (for all HTTP methods). * Use this to avoid leaking sensitive data in query * strings for envelope and JSONP requests. */ export default function request( options: RequestOptions, ): Promise> { if (hasCorsSupport()) { return xhr(options); } if (hasFetchSupport()) { return fetch(options); } throw new Error('Unable to determine ajax implementation to use for request.'); }