storybook-addon-fake-api
Version:
This addon will send fake responses to the requests sent from your component
97 lines (74 loc) • 3.4 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.OverridingService = void 0;
var _mockXmlhttprequest = require("mock-xmlhttprequest");
var _apiManager = require("./api-manager.service");
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
var OverridingService = /*#__PURE__*/function () {
function OverridingService() {
_classCallCheck(this, OverridingService);
_defineProperty(this, "fakeFetch", function (input, init) {
var apiMethod;
var apiUrl;
if (typeof input === "string") {
apiUrl = input;
apiMethod = init ? init.method || "GET" : "GET";
} else {
apiUrl = input.url;
apiMethod = input.method;
}
var api = _apiManager.ApiManagerService.getInstance().getMatch(apiUrl, apiMethod);
if (api && api.enabled) {
return new Promise(function (res, rej) {
setTimeout(function () {
var response = new Response(JSON.stringify(api.response.data));
if (api.response.type === "Resolve") return res(response);
return rej(response);
}, api.response.delay || 0);
});
}
return window.originalFetch(input, init);
});
_defineProperty(this, "fakeXhr", function (xhr) {
var apiMethod = xhr.method;
var apiUrl = xhr.url;
var api = _apiManager.ApiManagerService.getInstance().getMatch(apiUrl, apiMethod);
if (api && api.enabled) {
setTimeout(function () {
xhr.respond(api.response.status, {}, api.response.data);
}, api.response.delay || 0);
} else {
// eslint-disable-next-line new-cap
var realXhr = new window.originalXhr();
realXhr.open(apiMethod, apiUrl);
realXhr.onreadystatechange = function () {
if (realXhr.readyState === XMLHttpRequest.DONE) {
xhr.respond(realXhr.status, {}, realXhr.responseText);
}
};
realXhr.send();
}
});
window.originalFetch = window.fetch;
window.fetch = this.fakeFetch;
window.originalXhr = window.XMLHttpRequest;
window.XMLHttpRequest = (0, _mockXmlhttprequest.newMockXhr)();
window.XMLHttpRequest.onSend = this.fakeXhr;
}
_createClass(OverridingService, null, [{
key: "getInstance",
value: function getInstance() {
if (!this.instance) {
this.instance = new OverridingService();
}
return this.instance;
}
}]);
return OverridingService;
}();
exports.OverridingService = OverridingService;
;