Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/haydn/json-api-store. An isomorphic JavaScript library that acts as an in memory data store for JSON API data. particlesystem.com/json-api-store
json-api
Something went wrong. Try again.
8.7 kB · 298 lines
HTML
at master
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299<!DOCTYPE html><html><head> <meta charset="utf-8"> <base data-ice="baseUrl" href="../../"> <title data-ice="title">src/ajax.js | API Document</title> <link type="text/css" rel="stylesheet" href="css/style.css"> <link type="text/css" rel="stylesheet" href="css/prettify-tomorrow.css"> <script src="script/prettify/prettify.js"></script> </head><body class="layout-container">
<header> <a href="./">Home</a> <a href="identifiers.html">Identifier</a> <a href="source.html">Source</a> <a data-ice="repoURL" href="https://github.com/haydn/json-api-store" class="repo-url-github">Repository</a> <div class="search-box"> <span> <img src="./image/search.png"> <span class="search-input-edge"></span><input class="search-input"><span class="search-input-edge"></span> </span> <ul class="search-result"></ul> </div></header>
<nav class="navigation" data-ice="nav"><div data-ice="classWrap"> <h2>Class</h2> <ul> <li data-ice="classDoc"><span><a href="class/src/ajax-adapter.js~AjaxAdapter.html">AjaxAdapter</a></span></li><li data-ice="classDoc"><span><a href="class/src/store.js~Store.html">Store</a></span></li></ul></div>
<div data-ice="functionWrap"> <h2><a href="function/">Function</a></h2> <ul> <li data-ice="functionDoc"><span><a href="function/index.html#static-function-ajax">ajax</a></span></li></ul></div>
</nav>
<div class="content" data-ice="content"><h1 data-ice="title">src/ajax.js</h1><pre class="source-code line-number raw-source-code"><code class="prettyprint linenums" data-ice="content">import Rx from "rx";
/** * * Derived from RxJS-DOM, Copyright (c) Microsoft Open Technologies, Inc: * * https://github.com/Reactive-Extensions/RxJS-DOM * * The original source file can be viewed here: * * https://github.com/Reactive-Extensions/RxJS-DOM/blob/fdb169c8bd1612318d530e6e54074b1c9e537906/src/ajax.js * * Licensed under the Apache License, Version 2.0: * * http://www.apache.org/licenses/LICENSE-2.0 * * Modifications from original: * * - extracted from "Rx.DOM" namespace * - minor eslinter cleanup ("var" to "let" etc) * - addition of "auto" responseType * */
var root = (typeof window !== "undefined" && window) || this;
// Gets the proper XMLHttpRequest for support for older IEfunction getXMLHttpRequest() { if (root.XMLHttpRequest) { return new root.XMLHttpRequest(); } else { let progId; try { let progIds = ['Msxml2.XMLHTTP', 'Microsoft.XMLHTTP', 'Msxml2.XMLHTTP.4.0']; for(let i = 0; i < 3; i++) { try { progId = progIds[i]; if (new root.ActiveXObject(progId)) { break; } } catch(e) { } } return new root.ActiveXObject(progId); } catch (e) { throw new Error('XMLHttpRequest is not supported by your browser'); } }}
// Get CORS support even for older IEfunction getCORSRequest() { var xhr = new root.XMLHttpRequest(); if ('withCredentials' in xhr) { return xhr; } else if (!!root.XDomainRequest) { return new XDomainRequest(); } else { throw new Error('CORS is not supported by your browser'); }}
function normalizeAjaxSuccessEvent(e, xhr, settings) { var response = ('response' in xhr) ? xhr.response : xhr.responseText; if (settings.responseType === 'auto') { try { response = JSON.parse(response); } catch (e) {} } else { response = settings.responseType === 'json' ? JSON.parse(response) : response; } return { response: response, status: xhr.status, responseType: xhr.responseType, xhr: xhr, originalEvent: e };}
function normalizeAjaxErrorEvent(e, xhr, type) { return { type: type, status: xhr.status, xhr: xhr, originalEvent: e };}
/** * Creates an observable for an Ajax request with either a settings object with url, headers, etc or a string for a URL. * * @example * source = ajax('/products'); * source = ajax({ url: 'products', method: 'GET' }); * * @param {Object} settings Can be one of the following: * * A string of the URL to make the Ajax call. * An object with the following properties * - url: URL of the request * - body: The body of the request * - method: Method of the request, such as GET, POST, PUT, PATCH, DELETE * - async: Whether the request is async * - headers: Optional headers * - crossDomain: true if a cross domain request, else false * - responseType: "text" (default), "json" or "auto" * * @returns {Observable} An observable sequence containing the XMLHttpRequest.*/export default function (options) { var settings = { method: 'GET', crossDomain: false, async: true, headers: {}, responseType: 'text', createXHR: function(){ return this.crossDomain ? getCORSRequest() : getXMLHttpRequest(); }, normalizeError: normalizeAjaxErrorEvent, normalizeSuccess: normalizeAjaxSuccessEvent };
if(typeof options === 'string') { settings.url = options; } else { for(let prop in options) { if(hasOwnProperty.call(options, prop)) { settings[prop] = options[prop]; } } }
let normalizeError = settings.normalizeError; let normalizeSuccess = settings.normalizeSuccess;
if (!settings.crossDomain && !settings.headers['X-Requested-With']) { settings.headers['X-Requested-With'] = 'XMLHttpRequest'; } settings.hasContent = settings.body !== undefined;
return new Rx.AnonymousObservable(function (observer) { var isDone = false; var xhr;
var processResponse = function(xhr, e){ var status = xhr.status === 1223 ? 204 : xhr.status; if ((status >= 200 && status <= 300) || status === 0 || status === '') { observer.onNext(normalizeSuccess(e, xhr, settings)); observer.onCompleted(); } else { observer.onError(normalizeError(e, xhr, 'error')); } isDone = true; };
try { xhr = settings.createXHR();; } catch (err) { observer.onError(err); }
try { if (settings.user) { xhr.open(settings.method, settings.url, settings.async, settings.user, settings.password); } else { xhr.open(settings.method, settings.url, settings.async); }
let headers = settings.headers; for (let header in headers) { if (hasOwnProperty.call(headers, header)) { xhr.setRequestHeader(header, headers[header]); } }
if(!!xhr.upload || (!('withCredentials' in xhr) && !!root.XDomainRequest)) { xhr.onload = function(e) { if(settings.progressObserver) { settings.progressObserver.onNext(e); settings.progressObserver.onCompleted(); } processResponse(xhr, e); };
if(settings.progressObserver) { xhr.onprogress = function(e) { settings.progressObserver.onNext(e); }; }
xhr.onerror = function(e) { if (settings.progressObserver) { settings.progressObserver.onError(e); } observer.onError(normalizeError(e, xhr, 'error')); isDone = true; };
xhr.onabort = function(e) { if (settings.progressObserver) { settings.progressObserver.onError(e); } observer.onError(normalizeError(e, xhr, 'abort')); isDone = true; }; } else {
xhr.onreadystatechange = function (e) { if (xhr.readyState === 4) { processResponse(xhr, e); } }; }
xhr.send(settings.hasContent && settings.body || null); } catch (e) { observer.onError(e); }
return function () { if (!isDone && xhr.readyState !== 4) { xhr.abort(); } }; });};</code></pre>
</div>
<footer class="footer"> Generated by <a href="https://esdoc.org">ESDoc<span data-ice="esdocVersion">(0.2.6)</span></a></footer>
<script src="script/search_index.js"></script><script src="script/search.js"></script><script src="script/pretty-print.js"></script><script src="script/inherited-summary.js"></script><script src="script/test-summary.js"></script><script src="script/inner-link.js"></script><script src="script/patch-for-local.js"></script></body></html>