mockforme 1.0.3 → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.html +1 -9
- package/dist/mockforme.min.js +1 -128
- package/package.json +1 -1
package/dist/index.html
CHANGED
|
@@ -1,9 +1 @@
|
|
|
1
|
-
<!
|
|
2
|
-
<html>
|
|
3
|
-
<head>
|
|
4
|
-
<meta charset="utf-8">
|
|
5
|
-
<title>mockforme - The best way to mock your APIs</title>
|
|
6
|
-
<meta name="viewport" content="width=device-width, initial-scale=1"><script defer src="mockforme.min.js"></script></head>
|
|
7
|
-
<body>
|
|
8
|
-
</body>
|
|
9
|
-
</html>
|
|
1
|
+
<!doctype html><html><head><meta charset="utf-8"><title>mockforme - The best way to mock your APIs</title><meta name="viewport" content="width=device-width,initial-scale=1"><script defer="defer" src="mockforme.min.js"></script></head><body></body></html>
|
package/dist/mockforme.min.js
CHANGED
|
@@ -1,128 +1 @@
|
|
|
1
|
-
|
|
2
|
-
* ATTENTION: The "eval" devtool has been used (maybe by default in mode: "development").
|
|
3
|
-
* This devtool is neither made for production nor for readable output files.
|
|
4
|
-
* It uses "eval()" calls to create a separate source file in the browser devtools.
|
|
5
|
-
* If you are trying to read the output file, select a different devtool (https://webpack.js.org/configuration/devtool/)
|
|
6
|
-
* or disable the default devtool with "devtool: false".
|
|
7
|
-
* If you are looking for production-ready output files, see mode: "production" (https://webpack.js.org/configuration/mode/).
|
|
8
|
-
*/
|
|
9
|
-
(function webpackUniversalModuleDefinition(root, factory) {
|
|
10
|
-
if(typeof exports === 'object' && typeof module === 'object')
|
|
11
|
-
module.exports = factory();
|
|
12
|
-
else if(typeof define === 'function' && define.amd)
|
|
13
|
-
define([], factory);
|
|
14
|
-
else {
|
|
15
|
-
var a = factory();
|
|
16
|
-
for(var i in a) (typeof exports === 'object' ? exports : root)[i] = a[i];
|
|
17
|
-
}
|
|
18
|
-
})(this, function() {
|
|
19
|
-
return /******/ (() => { // webpackBootstrap
|
|
20
|
-
/******/ "use strict";
|
|
21
|
-
/******/ var __webpack_modules__ = ({
|
|
22
|
-
|
|
23
|
-
/***/ "./src/base.js":
|
|
24
|
-
/*!*********************!*\
|
|
25
|
-
!*** ./src/base.js ***!
|
|
26
|
-
\*********************/
|
|
27
|
-
/***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {
|
|
28
|
-
|
|
29
|
-
eval("__webpack_require__.r(__webpack_exports__);\n/* harmony export */ __webpack_require__.d(__webpack_exports__, {\n/* harmony export */ \"mockforme\": () => (/* binding */ mockforme)\n/* harmony export */ });\n/* harmony import */ var _utils_toLowerCase__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./utils/toLowerCase */ \"./src/utils/toLowerCase.js\");\n/* harmony import */ var _utils_httpService__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./utils/httpService */ \"./src/utils/httpService.js\");\n\n\n\nconst mockforme = (token) => {\n const BASE_URL = \"https://dashboard.mockforme.com\";\n const API_PROXY_URL = `${BASE_URL}${\"/mockforme\"}`\n const LIST_ALL_APIS_FOR_MOCK_URL = `${BASE_URL}${\"/gateway/op/getEnabledApisForSdk\"}`;\n const API_HEADER_KEY = \"mockforme\";\n\n let apisToBeMocked = [];\n\n const currentLocationObject = window.location;\n const protocol = currentLocationObject.protocol;\n const host = currentLocationObject.host;\n const port = currentLocationObject.port;\n\n if (!token) {\n throw \"mockforme token cannot be null\";\n }\n /**\n * \n * @param {*} apis [\n * {\n * url: '<string>',\n * method: '<strong>',\n * }\n * ]\n */\n const setMockApis = (apis) => {\n apisToBeMocked = apisToBeMocked.concat(apis);\n };\n\n const checkIfApiToBeMocked = (pathname, method='get') => {\n let o = null;\n if (!Array.isArray(apisToBeMocked)) {\n return o;\n }\n for (let i=0; i<apisToBeMocked.length; i++) {\n const v = apisToBeMocked[i];\n if ((0,_utils_toLowerCase__WEBPACK_IMPORTED_MODULE_0__.toLowerCase)(v.apiMethod) === (0,_utils_toLowerCase__WEBPACK_IMPORTED_MODULE_0__.toLowerCase)(method) && v.apiEndpoint === pathname) {\n o = {\n url: v.apiEndpoint,\n method: v.apiMethod,\n };\n break;\n }\n }\n return o\n };\n\n const addInterceptor = () => {\n const xhrInterceptor = () => {\n let oldXHROpen = window.XMLHttpRequest.prototype.open;\n\n window.XMLHttpRequest.prototype.open = function(method, url, async, user, password) {\n try {\n if (!url.match(/https?[:]\\/\\//)) { // url contains no protocol and host(assuming)\n url = `${protocol}//${host}${url}`;\n }\n\n const parsedUrl = new URL(url);\n const pathname = parsedUrl && parsedUrl.pathname;\n\n let arg = arguments;\n\n const o = checkIfApiToBeMocked(pathname, method);\n if (o) {\n arg[1] = `${API_PROXY_URL}${o.url}`;\n }\n\n this.onreadystatechange = function() {\n const OPENED_STATE = 1;\n if (this.readyState == OPENED_STATE) {\n /**\n * setRequestHeader appends token in existing keys rather than empty it\n * so added a hack to check \n * if arg[1] not match with LIST_ALL_APIS_FOR_MOCK_URL then only add mockforme header\n * otherwise it will append two tokens in url comma separated.\n */\n this.setRequestHeader(API_HEADER_KEY, token);\n }\n }\n return oldXHROpen.apply(this, arg);\n } catch (e) {\n console.log('Error in mock api, could be invalid url', e);\n return oldXHROpen.apply(this, arguments);\n }\n }\n };\n\n const fetchInterceptor = () => {\n const oFetch = window.fetch;\n window.fetch = function(url, config={}) {\n // Get the parameter in arguments\n // Intercept the parameter here\n let method = null;\n if (!config.method) {\n config.method = 'get';\n }\n let arg = arguments;\n\n if (url && url.startsWith('/')) {\n url = `${window.location.origin}${url}`;\n }\n\n const parsedUrl = new URL(url);\n const pathname = parsedUrl && parsedUrl.pathname;\n \n const o = checkIfApiToBeMocked(pathname, config.method);\n if (o) {\n url = `${API_PROXY_URL}${o.url}`;\n arg[0] = url;\n config.headers = {\n [API_HEADER_KEY]: token\n };\n }\n return oFetch.call(this, url, config)\n }\n };\n\n fetchInterceptor();\n xhrInterceptor();\n };\n\n\n const fetchApisToBeMockedUsingSDK = (successCallback) => {\n (0,_utils_httpService__WEBPACK_IMPORTED_MODULE_1__.httpService)({\n method: 'get',\n url: LIST_ALL_APIS_FOR_MOCK_URL,\n async: false,\n }, (res) => {\n if (res) {\n setMockApis(res);\n }\n successCallback(res);\n }, () => {\n // error, show message\n console.log('Error in loading mocked apis');\n }).request();\n }\n\n const initialize = (callback) => {\n addInterceptor();\n fetchApisToBeMockedUsingSDK((res) => {\n if (callback) { // callback\n callback(res);\n }\n });\n };\n \n return {\n initialize,\n };\n};\n\n//# sourceURL=webpack://mockforme/./src/base.js?");
|
|
30
|
-
|
|
31
|
-
/***/ }),
|
|
32
|
-
|
|
33
|
-
/***/ "./src/index.js":
|
|
34
|
-
/*!**********************!*\
|
|
35
|
-
!*** ./src/index.js ***!
|
|
36
|
-
\**********************/
|
|
37
|
-
/***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {
|
|
38
|
-
|
|
39
|
-
eval("__webpack_require__.r(__webpack_exports__);\n/* harmony export */ __webpack_require__.d(__webpack_exports__, {\n/* harmony export */ \"default\": () => (__WEBPACK_DEFAULT_EXPORT__)\n/* harmony export */ });\n/* harmony import */ var _base__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./base */ \"./src/base.js\");\n\n\nwindow.mockforme = _base__WEBPACK_IMPORTED_MODULE_0__.mockforme;\n\n/**\n * How to use\n * mockforme('<ACCESS_TOKEN>').initialize();\n */\n\n/* harmony default export */ const __WEBPACK_DEFAULT_EXPORT__ = (_base__WEBPACK_IMPORTED_MODULE_0__.mockforme);\n\n//# sourceURL=webpack://mockforme/./src/index.js?");
|
|
40
|
-
|
|
41
|
-
/***/ }),
|
|
42
|
-
|
|
43
|
-
/***/ "./src/utils/httpService.js":
|
|
44
|
-
/*!**********************************!*\
|
|
45
|
-
!*** ./src/utils/httpService.js ***!
|
|
46
|
-
\**********************************/
|
|
47
|
-
/***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {
|
|
48
|
-
|
|
49
|
-
eval("__webpack_require__.r(__webpack_exports__);\n/* harmony export */ __webpack_require__.d(__webpack_exports__, {\n/* harmony export */ \"httpService\": () => (/* binding */ httpService)\n/* harmony export */ });\nconst httpService = (function() {\n function http(options, successCallback, errorCallback) {\n var httpRequest;\n (function __construct() {\n if (window.XMLHttpRequest) {\n httpRequest = new XMLHttpRequest();\n } else if (window.ActiveXObject) {\n httpRequest = new ActiveXObject(\"Microsoft.XMLHTTP\");\n }\n })();\n \n function getResponse(){\n var responseHeader = httpRequest.getResponseHeader('Content-Type');\n var responseText = httpRequest.responseText;\n if(responseHeader){\n var splitHeader = responseHeader.split(';');\n try{\n var header = splitHeader[0];\n switch(header){\n case 'application/json':\n return JSON.parse(responseText);\n break;\n default:\n return responseText;\n break;\n }\n } catch(e){\n throw \"Unable to convert response header\";\n }\n }\n \n }\n function httpResponseHandler() {\n if (httpRequest.readyState == 4) {\n if (httpRequest.status.toString().match(/^20[0-9]$/)) {\n var response = getResponse();\n successCallback.call(this, response, httpRequest)\n } else { // error\n errorCallback.call(this, httpRequest.responseText, httpRequest);\n }\n }\n }\n \n function setRequestHeaders(headers) {\n for (let i in headers) {\n httpRequest.setRequestHeader(i, headers[i]);\n }\n }\n \n function serialize(obj, prefix) {\n var str = [];\n for (var p in obj) {\n if (obj.hasOwnProperty(p)) {\n var k = prefix ? prefix + \"[\" + p + \"]\" : p,\n v = obj[p];\n str.push(typeof v == \"object\" ?\n serialize(v, k) :\n encodeURIComponent(k) + \"=\" + encodeURIComponent(v));\n }\n }\n return str.join(\"&\");\n }\n \n function request() {\n if(window.XMLHttpRequest){\n httpRequest.onload = httpResponseHandler;\n } else if(window.ActiveXObject){\n httpRequest.onreadystatechange = httpResponseHandler;\n } else {\n throw \"unable to process ajax\";\n }\n \n var params = serialize(options.params);\n if(options.method.toLowerCase() == 'get'){\n if(typeof options.params == 'object'){\n if(options.url.indexOf(\"?\") == -1){\n options.url += \"?\";\n } else {\n var split = options.url.split(\"?\");\n if(split[1]){\n var isQueryStringInUrl = split[1].split(\"=\");\n if(isQueryStringInUrl[1]){\n options.url += \"&\"\n }\n }\n }\n options.url += params;\n }\n }\n if (!options.hasOwnProperty('async')) {\n options.async = true;\n }\n httpRequest.open(options.method, options.url, options.async);\n if (options.headers) {\n setRequestHeaders(options.headers);\n }\n httpRequest.send(params);\n }\n return {\n request: request\n }\n };\n return http;\n})();\n\n//# sourceURL=webpack://mockforme/./src/utils/httpService.js?");
|
|
50
|
-
|
|
51
|
-
/***/ }),
|
|
52
|
-
|
|
53
|
-
/***/ "./src/utils/toLowerCase.js":
|
|
54
|
-
/*!**********************************!*\
|
|
55
|
-
!*** ./src/utils/toLowerCase.js ***!
|
|
56
|
-
\**********************************/
|
|
57
|
-
/***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {
|
|
58
|
-
|
|
59
|
-
eval("__webpack_require__.r(__webpack_exports__);\n/* harmony export */ __webpack_require__.d(__webpack_exports__, {\n/* harmony export */ \"toLowerCase\": () => (/* binding */ toLowerCase)\n/* harmony export */ });\nconst toLowerCase = (v) => {\n try {\n if (v) {\n return v.toLowerCase();\n }\n return v;\n } catch (e) {\n console.log(e);\n return null;\n }\n};\n\n//# sourceURL=webpack://mockforme/./src/utils/toLowerCase.js?");
|
|
60
|
-
|
|
61
|
-
/***/ })
|
|
62
|
-
|
|
63
|
-
/******/ });
|
|
64
|
-
/************************************************************************/
|
|
65
|
-
/******/ // The module cache
|
|
66
|
-
/******/ var __webpack_module_cache__ = {};
|
|
67
|
-
/******/
|
|
68
|
-
/******/ // The require function
|
|
69
|
-
/******/ function __webpack_require__(moduleId) {
|
|
70
|
-
/******/ // Check if module is in cache
|
|
71
|
-
/******/ var cachedModule = __webpack_module_cache__[moduleId];
|
|
72
|
-
/******/ if (cachedModule !== undefined) {
|
|
73
|
-
/******/ return cachedModule.exports;
|
|
74
|
-
/******/ }
|
|
75
|
-
/******/ // Create a new module (and put it into the cache)
|
|
76
|
-
/******/ var module = __webpack_module_cache__[moduleId] = {
|
|
77
|
-
/******/ // no module.id needed
|
|
78
|
-
/******/ // no module.loaded needed
|
|
79
|
-
/******/ exports: {}
|
|
80
|
-
/******/ };
|
|
81
|
-
/******/
|
|
82
|
-
/******/ // Execute the module function
|
|
83
|
-
/******/ __webpack_modules__[moduleId](module, module.exports, __webpack_require__);
|
|
84
|
-
/******/
|
|
85
|
-
/******/ // Return the exports of the module
|
|
86
|
-
/******/ return module.exports;
|
|
87
|
-
/******/ }
|
|
88
|
-
/******/
|
|
89
|
-
/************************************************************************/
|
|
90
|
-
/******/ /* webpack/runtime/define property getters */
|
|
91
|
-
/******/ (() => {
|
|
92
|
-
/******/ // define getter functions for harmony exports
|
|
93
|
-
/******/ __webpack_require__.d = (exports, definition) => {
|
|
94
|
-
/******/ for(var key in definition) {
|
|
95
|
-
/******/ if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
|
|
96
|
-
/******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
|
|
97
|
-
/******/ }
|
|
98
|
-
/******/ }
|
|
99
|
-
/******/ };
|
|
100
|
-
/******/ })();
|
|
101
|
-
/******/
|
|
102
|
-
/******/ /* webpack/runtime/hasOwnProperty shorthand */
|
|
103
|
-
/******/ (() => {
|
|
104
|
-
/******/ __webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))
|
|
105
|
-
/******/ })();
|
|
106
|
-
/******/
|
|
107
|
-
/******/ /* webpack/runtime/make namespace object */
|
|
108
|
-
/******/ (() => {
|
|
109
|
-
/******/ // define __esModule on exports
|
|
110
|
-
/******/ __webpack_require__.r = (exports) => {
|
|
111
|
-
/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
|
|
112
|
-
/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
113
|
-
/******/ }
|
|
114
|
-
/******/ Object.defineProperty(exports, '__esModule', { value: true });
|
|
115
|
-
/******/ };
|
|
116
|
-
/******/ })();
|
|
117
|
-
/******/
|
|
118
|
-
/************************************************************************/
|
|
119
|
-
/******/
|
|
120
|
-
/******/ // startup
|
|
121
|
-
/******/ // Load entry module and return exports
|
|
122
|
-
/******/ // This entry module can't be inlined because the eval devtool is used.
|
|
123
|
-
/******/ var __webpack_exports__ = __webpack_require__("./src/index.js");
|
|
124
|
-
/******/
|
|
125
|
-
/******/ return __webpack_exports__;
|
|
126
|
-
/******/ })()
|
|
127
|
-
;
|
|
128
|
-
});
|
|
1
|
+
!function(e,t){if("object"==typeof exports&&"object"==typeof module)module.exports=t();else if("function"==typeof define&&define.amd)define([],t);else{var o=t();for(var n in o)("object"==typeof exports?exports:e)[n]=o[n]}}(this,(function(){return(()=>{"use strict";var e={d:(t,o)=>{for(var n in o)e.o(o,n)&&!e.o(t,n)&&Object.defineProperty(t,n,{enumerable:!0,get:o[n]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t),r:e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})}},t={};e.r(t),e.d(t,{default:()=>a});const o=e=>{try{return e?e.toLowerCase():e}catch(e){return console.log(e),null}},n=function(e,t,o){var n;function r(){if(4==n.readyState)if(n.status.toString().match(/^20[0-9]$/)){var e=function(){var e=n.getResponseHeader("Content-Type"),t=n.responseText;if(e){var o=e.split(";");try{switch(o[0]){case"application/json":return JSON.parse(t);default:return t}}catch(e){throw"Unable to convert response header"}}}();t.call(this,e,n)}else o.call(this,n.responseText,n)}function a(e,t){var o=[];for(var n in e)if(e.hasOwnProperty(n)){var r=t?t+"["+n+"]":n,i=e[n];o.push("object"==typeof i?a(i,r):encodeURIComponent(r)+"="+encodeURIComponent(i))}return o.join("&")}return window.XMLHttpRequest?n=new XMLHttpRequest:window.ActiveXObject&&(n=new ActiveXObject("Microsoft.XMLHTTP")),{request:function(){if(window.XMLHttpRequest)n.onload=r;else{if(!window.ActiveXObject)throw"unable to process ajax";n.onreadystatechange=r}var t=a(e.params);if("get"==e.method.toLowerCase()&&"object"==typeof e.params){if(-1==e.url.indexOf("?"))e.url+="?";else{var o=e.url.split("?");o[1]&&o[1].split("=")[1]&&(e.url+="&")}e.url+=t}e.hasOwnProperty("async")||(e.async=!0),n.open(e.method,e.url,e.async),e.headers&&function(e){for(let t in e)n.setRequestHeader(t,e[t])}(e.headers),n.send(t)}}},r=e=>{const t="https://dashboard.mockforme.com",r=`${t}/mockforme`,a=`${t}/gateway/op/getEnabledApisForSdk`,i="mockforme";let s=[];const c=window.location,l=c.protocol,p=c.host;if(c.port,!e)throw"mockforme token cannot be null";const d=(e,t="get")=>{let n=null;if(!Array.isArray(s))return n;for(let r=0;r<s.length;r++){const a=s[r];if(o(a.apiMethod)===o(t)&&a.apiEndpoint===e){n={url:a.apiEndpoint,method:a.apiMethod};break}}return n},u=()=>{(()=>{const t=window.fetch;window.fetch=function(o,n={}){n.method||(n.method="get");let a=arguments;o&&(o.startsWith("//")?o=`${window.location.protocol}${o}`:o.startsWith("/")&&(o=`${window.location.origin}${o}`));const s=new URL(o),c=s&&s.pathname,l=d(c,n.method);return l&&(o=`${r}${l.url}`,a[0]=o,n.headers={[i]:e}),t.call(this,o,n)}})(),(()=>{let t=window.XMLHttpRequest.prototype.open;window.XMLHttpRequest.prototype.open=function(o,n,a,s,c){try{n.match(/https?[:]\/\//)||(n=`${l}//${p}${n}`);const a=new URL(n),s=a&&a.pathname;let c=arguments;const u=d(s,o);return u&&(c[1]=`${r}${u.url}`),this.onreadystatechange=function(){1==this.readyState&&this.setRequestHeader(i,e)},t.apply(this,c)}catch(e){return console.log("Error in mock api, could be invalid url",e),t.apply(this,arguments)}}})()};return{initialize:e=>{var t;u(),t=t=>{e&&e(t)},n({method:"get",url:a,async:!1},(e=>{var o;e&&(o=e,s=s.concat(o)),t(e)}),(()=>{console.log("Error in loading mocked apis")})).request()}}};window.mockforme=r;const a=r;return t})()}));
|
package/package.json
CHANGED