storybook-addon-fake-api
Version:
This addon will send fake responses to the requests sent from your component
153 lines (132 loc) • 6.11 kB
JavaScript
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
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; }
import { API_METHODS } from "../models/constants";
import { ApiManagerService } from "./api-manager.service";
export var UIService = /*#__PURE__*/function () {
function UIService() {
_classCallCheck(this, UIService);
_defineProperty(this, "numberOfIndexDigits", 2);
_defineProperty(this, "apiManager", ApiManagerService.getInstance());
}
_createClass(UIService, [{
key: "getRows",
value: function getRows() {
var _this = this;
var apiList = this.apiManager.getList();
if (!apiList) {
return null;
}
var result = {};
apiList.forEach(function (api, index) {
var _objectSpread2;
var indexWithZeroPad = index.toString().padStart(_this.numberOfIndexDigits, "0");
result = _objectSpread(_objectSpread({}, result), {}, (_objectSpread2 = {}, _defineProperty(_objectSpread2, "enabled_".concat(indexWithZeroPad), {
name: "Enabled",
control: {
type: "boolean"
},
table: {
category: api.name
}
}), _defineProperty(_objectSpread2, "url_".concat(indexWithZeroPad), {
name: "URL",
control: {
type: "text"
},
table: {
category: api.name
}
}), _defineProperty(_objectSpread2, "method_".concat(indexWithZeroPad), {
name: "Method",
control: {
type: "select"
},
options: API_METHODS,
table: {
category: api.name
}
}), _objectSpread2), _this.getResponseControls(api.name, indexWithZeroPad));
});
return result;
}
}, {
key: "getResponseControls",
value: function getResponseControls(apiName, apiIndex) {
var _ref;
var subcategory = "Response";
return _ref = {}, _defineProperty(_ref, "type_".concat(apiIndex), {
name: "ResponseType",
control: {
type: "inline-radio"
},
options: ["Resolve", "Reject"],
table: {
category: apiName,
subcategory: subcategory
}
}), _defineProperty(_ref, "status_".concat(apiIndex), {
name: "Status",
control: {
type: "number"
},
table: {
category: apiName,
subcategory: subcategory
}
}), _defineProperty(_ref, "delay_".concat(apiIndex), {
name: "Delay (ms)",
control: {
type: "number"
},
table: {
category: apiName,
subcategory: subcategory
}
}), _defineProperty(_ref, "data_".concat(apiIndex), {
name: "Data",
control: {
type: "object"
},
table: {
category: apiName,
subcategory: subcategory
}
}), _ref;
}
}, {
key: "getArgs",
value: function getArgs() {
var _this2 = this;
var apiList = this.apiManager.getList();
if (!apiList) {
return null;
}
var result = {};
apiList.forEach(function (api, index) {
var _objectSpread3;
var indexWithZeroPad = index.toString().padStart(_this2.numberOfIndexDigits, "0");
result = _objectSpread(_objectSpread({}, result), {}, (_objectSpread3 = {}, _defineProperty(_objectSpread3, "enabled_".concat(indexWithZeroPad), api.enabled === undefined ? true : api.enabled), _defineProperty(_objectSpread3, "url_".concat(indexWithZeroPad), api.url), _defineProperty(_objectSpread3, "method_".concat(indexWithZeroPad), api.method), _objectSpread3), _this2.getResponseArgs(api.response, indexWithZeroPad));
});
return result;
}
}, {
key: "getResponseArgs",
value: function getResponseArgs(response, apiIndex) {
var _ref2;
return _ref2 = {}, _defineProperty(_ref2, "data_".concat(apiIndex), response.data), _defineProperty(_ref2, "status_".concat(apiIndex), response.status), _defineProperty(_ref2, "delay_".concat(apiIndex), response.delay ? response.delay : 0), _defineProperty(_ref2, "type_".concat(apiIndex), response.type), _ref2;
}
}], [{
key: "getInstance",
value: function getInstance() {
if (!this.instance) {
this.instance = new UIService();
}
return this.instance;
}
}]);
return UIService;
}();