UNPKG

storybook-addon-fake-api

Version:

This addon will send fake responses to the requests sent from your component

169 lines (126 loc) 5.97 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ApiManagerService = void 0; var _pathToRegexp = require("path-to-regexp"); function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); } function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); } function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } 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 ApiManagerService = /*#__PURE__*/function () { function ApiManagerService() { var _this = this; _classCallCheck(this, ApiManagerService); _defineProperty(this, "setList", function (list) { if (_this.validateList(list)) { _this.apiList = list; return true; } return false; }); _defineProperty(this, "getList", function () { return _this.apiList; }); _defineProperty(this, "validateList", function (ApiList) { if (!ApiList) { return false; } if (!Array.isArray(ApiList) || ApiList.length === 0) { return false; } var invalidItem = ApiList.find(function (api, index) { return !_this.validateAPIItem(api, index); }); if (invalidItem) { return false; } return true; }); _defineProperty(this, "validateAPIItem", function (api, index) { if (!api) { console.error("API #".concat(index + 1, " is falsy")); return false; } if (!api.method) { console.error("API #".concat(index + 1, " doesn't have a valid method")); return false; } if (!api.url) { console.error("API #".concat(index + 1, " doesn't have any url")); return false; } if (!_this.validateResponse(api.response, index)) { return false; } return true; }); _defineProperty(this, "validateResponse", function (response, index) { if (!response) { console.error("API #".concat(index + 1, " doesn't have any responses")); return false; } if (!response.data) { console.error("API #".concat(index + 1, " doesn't have any responses data")); return false; } if (!response.status) { console.error("API #".concat(index + 1, " doesn't have any responses status")); return false; } if (!response.type) { console.error("API #".concat(index + 1, " doesn't have any responses type")); return false; } return true; }); _defineProperty(this, "updateItem", function (keyName, index, value) { var newList = _toConsumableArray(_this.apiList); if (keyName === "name" || keyName === "url" || keyName === "enabled" || keyName === "method") { newList[index][keyName] = value; return newList; } if (keyName === "delay" || keyName === "data" || keyName === "status" || keyName === "type") { newList[index].response[keyName] = value; return newList; } return null; }); _defineProperty(this, "getMatch", function (url, method) { if (!_this.apiList) { return undefined; } var passedUrl = url; return _this.apiList.find(function (api) { if (url.indexOf("http") !== 0) { passedUrl = "".concat(window.location.protocol, "//").concat(window.location.host).concat(url); } var parsedApiUrl = new URL(api.url); var parsedUrl = new URL(passedUrl); var matcher = (0, _pathToRegexp.match)(parsedApiUrl.host + parsedApiUrl.pathname, { encode: encodeURI, decode: decodeURIComponent }); var matched = matcher(parsedUrl.host + parsedUrl.pathname); return matched && method.toUpperCase() === api.method.toUpperCase() && api; }); }); } _createClass(ApiManagerService, null, [{ key: "getInstance", value: function getInstance() { if (!this.instance) { this.instance = new ApiManagerService(); } return this.instance; } }]); return ApiManagerService; }(); exports.ApiManagerService = ApiManagerService;