openui5-smart-mockserver
Version:
An extended version of UI5 MockServer which generates meaningful mock data.
22 lines (21 loc) • 9.02 kB
JavaScript
//@ui5-bundle openui5/smartmockserver/library-preload.js
sap.ui.require.preload({
"openui5/smartmockserver/SmartMockServer.js":function(){
"use strict";
/*
* openui5-smart-mockserver
* (c) Copyright 2018-2023 Mauricio Lauffer
* Licensed under the MIT license. See LICENSE file in the project root for full license information.
*/sap.ui.define(["openui5/smartmockserver/library","sap/base/Log","sap/ui/thirdparty/jquery","sap/ui/core/util/MockServer"],function(t,e,r,n){const o=n;o.prototype._generateDataFromEntityOriginal=o.prototype._generateDataFromEntity;o.prototype._generateDataFromEntity=function(t,e,r){const n=o.prototype._generateDataFromEntityOriginal.apply(this,arguments);return this._generateDataWithSmartRules(t.name,n)};o.prototype._generateDataWithSmartRules=function(t,r){try{if(!this._smartRules){this._smartRules=[]}let e=structuredClone(r);e=this._generateDataFromEntityWithSapSemanticsAnnotations(t,e);e=this._generateDataFromEntityWithSmartMockServerAnnotations(t,e);e=this._generateDataFromEntityWithSmartRules(t,e);return e}catch(t){e.error(t);return r}};o.prototype._generateDataFromEntityWithSmartRules=function(t,e){if(this._hasSmartRulesEntity(t)){const r=structuredClone(e);for(const e in r){if(this._hasSmartRulesEntityProperty(t,e)){r[e]=this._generatePropertyValueWithSmartRules(t,e)}}return r}else{return e}};o.prototype._getEntityPropertiesWithSapSemanticsAnnotations=function(t){const e='EntityType[Name="'+t+'"]';const n="Property[sap\\:semantics]";return r(this._oMetadata).find(e).find(n)};o.prototype._generateDataFromEntityWithSapSemanticsAnnotations=function(t,e){const n=this._getEntityPropertiesWithSapSemanticsAnnotations(t);if(n&&n.length&&n.length>0){const t=structuredClone(e);n.each(function(e,n){const o=r(n);const i=this._getFakerMethodFromSapSemantics(o.attr("sap:semantics"));if(i){t[o.attr("Name")]=this._callFakerMethod(i)}}.bind(this));return t}else{return e}};o.prototype._getFakerMethodFromSapSemantics=function(e){const r=t.SAP_SEMANTICS_TO_FAKER_METHOD_MAPPING.find(function(t){return t.sapSemantics===e});if(r){return r.fakerMethod}else{return r}};o.prototype._getEntityPropertiesWithSmartMockServerAnnotations=function(t){const e='EntityType[Name="'+t+'"]';const n="Property[smartmockserver\\:rule]";return r(this._oMetadata).find(e).find(n)};o.prototype._generateDataFromEntityWithSmartMockServerAnnotations=function(t,e){const n=this._getEntityPropertiesWithSmartMockServerAnnotations(t);if(n&&n.length&&n.length>0){const t=structuredClone(e);n.each(function(e,n){const o=r(n);const i=o.attr("smartmockserver:rule");if(i){t[o.attr("Name")]=this._callFakerMethod(i)}}.bind(this));return t}else{return e}};o.prototype._generatePropertyValueWithSmartRules=function(t,e){const r=this._getSmartRulesEntityProperty(t,e);return this._callFakerMethod(r.fakerMethod)};o.prototype._callFakerMethod=function(e){return t.faker.helpers.fake("{{"+e+"}}")};o.prototype._getSmartRulesEntity=function(t){return this._smartRules.filter(function(e){return e.entityName===t})[0]};o.prototype._getSmartRulesEntityProperty=function(t,e){const r=this._getSmartRulesEntity(t);if(!r){return r}return r.properties.find(function(t){return t.name===e})};o.prototype._hasSmartRulesEntity=function(t){return!!this._getSmartRulesEntity(t)};o.prototype._hasSmartRulesEntityProperty=function(t,e){return!!this._getSmartRulesEntityProperty(t,e)};o.prototype.setSmartRules=function(t){this._smartRules=typeof t==="object"&&t.length?t:[]};const i={REQUEST:"REQUEST",MOCK_RESPONSE:"MOCK_RESPONSE",REGISTER_MOCK_SERVER:"REGISTER_MOCK_SERVER",START_MOCK_SERVER:"START_MOCK_SERVER",STOP_MOCK_SERVER:"STOP_MOCK_SERVER",UNKNOWN_MESSAGE:"UNKNOWN_MESSAGE"};o.getAll=function(){return n._aServers};o.registerServiceWorker=function(t){const r=o.getAll();for(const t of r){t.start();t.stop()}navigator.serviceWorker.addEventListener("message",u);return navigator.serviceWorker.register(t).then(function(){s();const t=S();return f(i.REGISTER_MOCK_SERVER,t)}).catch(function(t){e.error("ServiceWorker not registered!");e.error(t)})};o.prototype.isServiceWorkerStarted=function(){return this._isServiceWorkerStarted};o.prototype.startServiceWorker=function(){this._isServiceWorkerStarted=true;const t={rootUri:this.getRootUri(),active:this.isServiceWorkerStarted()};return f(i.START_MOCK_SERVER,t)};o.prototype.stopServiceWorker=function(){this._isServiceWorkerStarted=false;const t={rootUri:this.getRootUri(),active:this.isServiceWorkerStarted()};return f(i.STOP_MOCK_SERVER,t)};function s(){r.ajaxPrefilter(function(t,r,n){if(t.async){return}const o=a(t.url);if(!o){return}const i={url:t.url,method:t.type,mockServer:{path:"JUST_TO_AVOID_TO_BE_FOUND"}};const s=c(i,o);if(!s){return}try{t.beforeSend=function(t,e){o.start();e.url=e.url.replace(window.location.origin,"")};n.complete(function(){o.stop()})}catch(t){e.error(t)}})}function a(t){return o.getAll().filter(function(e){const r=new RegExp(e.getRootUri());return r.test(t)})[0]}function c(t,e){if(!e){return}return e.getRequests().filter(function(e){if(e.method.toLowerCase()!==t.method.toLowerCase()){return false}const r=e.path.toString()==="/$/";if(r&&t.mockServer.path==="$"){return r}else if(!r){return e.path instanceof RegExp?e.path.test(t.url):e.path===t.url}})[0]}function u(t){if(t.data.type!==i.REQUEST){return}p(t.data.payload).then(function(e){t.ports[0].postMessage({type:i.MOCK_RESPONSE,payload:JSON.stringify(e)})}).catch(function(r){e.error(r);t.ports[0].postMessage({type:i.MOCK_RESPONSE,error:true,payload:JSON.stringify(r)})})}function p(t){return new Promise(function(e,r){const n=JSON.parse(t);const o=a(n.url);const i=c(n,o);if(!i){r(new Error("No request was found in MockServer"))}const s=Object.assign({},n);const u=typeof i.path.exec==="function"?i.path.exec(s.url).slice(1):[];s.respond=function(){e(arguments)};s.respondFile=s.respondJSON=s.respondXML=s.respond;i.response(s,u[0],u[1])})}function S(){return o.getAll().map(function(t){const e=t.getRequests().map(function(t){const e={method:t.method,path:t.path,isRegExp:t.path instanceof RegExp};if(e.isRegExp){const t=e.path.toString();const r=t.substring(t.indexOf("/")+1,t.lastIndexOf("/"));const n=t==="/$/";e.path=n?"$":r}return e});return{active:false,rootUri:t.getRootUri(),requests:e}})}function f(t,e){return navigator.serviceWorker.ready.then(function(r){r.active.postMessage({type:t,payload:JSON.stringify(e)})})}return o});
},
"openui5/smartmockserver/library.js":function(){
"use strict";
/*
* openui5-smart-mockserver
* (c) Copyright 2018-2023 Mauricio Lauffer
* Licensed under the MIT license. See LICENSE file in the project root for full license information.
*/sap.ui.define(["sap/ui/core/Lib","sap/ui/core/library"],function(e){(async()=>{const e=`${sap.ui.require.toUrl("openui5/smartmockserver")}/thirdparty/faker/locale/en.mjs`;const{faker:t}=await import(e);a.faker=t})();const a=e.init({name:"openui5.smartmockserver",dependencies:["sap.ui.core"],controls:["openui5.smartmockserver.SmartMockServer"],noLibraryCSS:true,version:"1.0.0"});a.SAP_SEMANTICS_TO_FAKER_METHOD_MAPPING=[{sapSemantics:"city",fakerMethod:"location.city"},{sapSemantics:"country",fakerMethod:"location.country"},{sapSemantics:"geo-lat",fakerMethod:"location.latitude"},{sapSemantics:"geo-lon",fakerMethod:"location.longitude"},{sapSemantics:"region",fakerMethod:"location.state"},{sapSemantics:"street",fakerMethod:"location.streetAddress"},{sapSemantics:"zip",fakerMethod:"location.zipCode"},{sapSemantics:"org",fakerMethod:"company.name"},{sapSemantics:"currency-code",fakerMethod:"finance.currencyCode"},{sapSemantics:"photo",fakerMethod:"image.avatar"},{sapSemantics:"bcc",fakerMethod:"internet.email"},{sapSemantics:"cc",fakerMethod:"internet.email"},{sapSemantics:"email",fakerMethod:"internet.email"},{sapSemantics:"from",fakerMethod:"internet.email"},{sapSemantics:"sender",fakerMethod:"internet.email"},{sapSemantics:"to",fakerMethod:"internet.email"},{sapSemantics:"url",fakerMethod:"internet.url"},{sapSemantics:"body",fakerMethod:"lorem.paragraphs"},{sapSemantics:"subject",fakerMethod:"lorem.sentence"},{sapSemantics:"name",fakerMethod:"person.fullName"},{sapSemantics:"givenname",fakerMethod:"person.firstName"},{sapSemantics:"middlename",fakerMethod:"person.firstName"},{sapSemantics:"title",fakerMethod:"person.jobTitle"},{sapSemantics:"familyname",fakerMethod:"person.lastName"},{sapSemantics:"honorific",fakerMethod:"person.prefix"},{sapSemantics:"suffix",fakerMethod:"person.suffix"},{sapSemantics:"tel",fakerMethod:"phone.number"}];return a});
},
"openui5/smartmockserver/manifest.json":'{"sap.app":{"id":"openui5.smartmockserver","type":"library","applicationVersion":{"version":"1.0.0"},"title":"An extended version of UI5 MockServer which generates meaningful mock data."},"sap.ui":{"technology":"UI5","deviceTypes":{"desktop":true,"tablet":true,"phone":true}},"sap.ui5":{"dependencies":{"minUI5Version":"1.120.0","libs":{"sap.ui.core":{}}},"contentDensities":{"compact":true,"cozy":true}}}'
});
//# sourceMappingURL=library-preload.js.map