Comparing version 0.0.22 to 0.0.23
"use strict";var _interopRequireWildcard=require("@babel/runtime/helpers/interopRequireWildcard"),_interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(exports,"__esModule",{value:!0}),exports["default"]=void 0;var _classCallCheck2=_interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")),_defineProperty2=_interopRequireDefault(require("@babel/runtime/helpers/defineProperty")),fs=_interopRequireWildcard(require("fs")),path=_interopRequireWildcard(require("path")),_express=_interopRequireDefault(require("express")),http=_interopRequireWildcard(require("http")),_open=_interopRequireDefault(require("open")),chokidar=_interopRequireWildcard(require("chokidar")),_const=require("./const"),_utils=require("./utils"),_getRoute=require("./getRoute"),_internalApi=require("./internalApi"),_fakerHelpers=require("./fakerHelpers");function ownKeys(a,b){var c=Object.keys(a);if(Object.getOwnPropertySymbols){var d=Object.getOwnPropertySymbols(a);b&&(d=d.filter(function(b){return Object.getOwnPropertyDescriptor(a,b).enumerable})),c.push.apply(c,d)}return c}function _objectSpread(a){for(var b,c=1;c<arguments.length;c++)b=null==arguments[c]?{}:arguments[c],c%2?ownKeys(Object(b),!0).forEach(function(c){(0,_defineProperty2["default"])(a,c,b[c])}):Object.getOwnPropertyDescriptors?Object.defineProperties(a,Object.getOwnPropertyDescriptors(b)):ownKeys(Object(b)).forEach(function(c){Object.defineProperty(a,c,Object.getOwnPropertyDescriptor(b,c))});return a}var DEFAULT_PORT=6767,Restapify=function a(b){var c=this,d=b.rootDir,e=b.port,f=void 0===e?DEFAULT_PORT:e,g=b.baseUrl,h=void 0===g?"/api":g,i=b.states,j=void 0===i?[]:i,k=b.openDashboard,l=b.hotWatch;(0,_classCallCheck2["default"])(this,a),(0,_defineProperty2["default"])(this,"eventCallbacksStore",{}),(0,_defineProperty2["default"])(this,"app",void 0),(0,_defineProperty2["default"])(this,"server",void 0),(0,_defineProperty2["default"])(this,"chokidarWatcher",void 0),(0,_defineProperty2["default"])(this,"listedRouteFiles",{}),(0,_defineProperty2["default"])(this,"routes",{GET:{},POST:{},DELETE:{},PUT:{},PATCH:{}}),(0,_defineProperty2["default"])(this,"rootDir",void 0),(0,_defineProperty2["default"])(this,"port",void 0),(0,_defineProperty2["default"])(this,"apiBaseUrl",void 0),(0,_defineProperty2["default"])(this,"states",[]),(0,_defineProperty2["default"])(this,"hotWatch",void 0),(0,_defineProperty2["default"])(this,"autoOpenDashboard",void 0),(0,_defineProperty2["default"])(this,"listRouteFiles",function(){c.listedRouteFiles=(0,_utils.getRouteFiles)(c.rootDir)}),(0,_defineProperty2["default"])(this,"configHotWatch",function(){c.hotWatch&&(c.chokidarWatcher=chokidar.watch(c.rootDir,{ignoreInitial:!0}).on("all",function(){c.restartServer({hard:!0})}))}),(0,_defineProperty2["default"])(this,"configServer",function(){// Add middleware to parse request's body | ||
// Handle CORS | ||
c.app=(0,_express["default"])(),c.server=http.createServer(c.app),c.app.use(_express["default"].json()),c.app.use(function(a,b,c){b.append("Access-Control-Allow-Origin",["*"]),b.append("Access-Control-Allow-Methods","GET,PUT,POST,DELETE,PATCH"),b.append("Access-Control-Allow-Headers","Content-Type"),c()}),c.handleHttpServerErrors(),c.configRoutesFromListedFiles(),c.serveRoutes()}),(0,_defineProperty2["default"])(this,"configDashboard",function(){var a=path.resolve(__dirname,_const.DASHBOARD_FOLDER_PATH);c.app.use(_const.INTERNAL_BASEURL,_express["default"]["static"](a))}),(0,_defineProperty2["default"])(this,"configInternalApi",function(){var a=c.routes,b=c.states;c.app=(0,_internalApi.getInitialisedInternalApi)(c.app,{routes:a,states:b,setState:c.setState})}),(0,_defineProperty2["default"])(this,"handleHttpServerErrors",function(){c.server.on("error",function(a){switch(a.code){case"EADDRINUSE":{c.executeCallbacksForSingleEvent("error",{error:"MISS:PORT"});break}default:console.log("Unknow error ".concat(a.code));}})}),(0,_defineProperty2["default"])(this,"restartServer",function(a){c.executeCallbacks("server:restart"),c.closeServer(),c.customRun(_objectSpread(_objectSpread({},a),{},{hard:!1,openDashboard:!1}))}),(0,_defineProperty2["default"])(this,"checkApiBaseUrl",function(){if(c.apiBaseUrl.startsWith(_const.INTERNAL_BASEURL)){throw new Error(JSON.stringify({error:"INV:API_BASEURL"}))}}),(0,_defineProperty2["default"])(this,"checkRootDirectory",function(){var a=fs.existsSync(c.rootDir);if(!a){throw new Error(JSON.stringify({error:"MISS:ROOT_DIR"}))}}),(0,_defineProperty2["default"])(this,"checkJsonFiles",function(){Object.keys(c.listedRouteFiles).forEach(function(a){var b=c.listedRouteFiles[a],d=(0,_utils.isJsonString)(b),e=(0,_fakerHelpers.areFakerVarsSyntaxValidInContent)(b);if(!0!==d){var f={error:"INV:JSON_FILE",message:"Invalid json file ".concat(a,": ").concat(d)};throw new Error(JSON.stringify(f))}else if(!0!==e){var g=e.namespace,h=e.method,i={error:"INV:FAKER_SYNTAX",message:"The fakerjs method call `faker.".concat(g,".").concat(h,"()` is invalid")};throw new Error(JSON.stringify(i))}})}),(0,_defineProperty2["default"])(this,"configRoutesFromListedFiles",function(){Object.keys(c.listedRouteFiles).forEach(function(a){var b=(0,_getRoute.getRoute)(a,c.rootDir,c.listedRouteFiles[a]),d=b.route,e=b.method,f=b.stateVars,g=b.body,h=b.getBody,i=b.header,j=b.isExtended,k=b.statusCode,l=b.fileContent,m=c.routes[e][d]!==void 0,n=0<f.length;m||(c.routes[e][d]={}),n?(c.routes[e][d]===void 0&&(c.routes[e][d]={}),c.routes[e][d].states===void 0&&(c.routes[e][d].states={}),f.forEach(function(a){// @ts-ignore | ||
c.routes[e][d].states[a]=(0,_utils.withoutUndefinedFromObject)({body:g,fileContent:l,header:i,isExtended:j,statusCode:k,getBody:h})})):c.routes[e][d]=_objectSpread(_objectSpread({},c.routes[e][d]),b)})}),(0,_defineProperty2["default"])(this,"getRouteData",function(a,b){if(!c.routes[a][b])return null;var d=c.routes[a][b],e=c.states.find(function(c){return c.route===b&&(c.method===a||c.method===void 0&&"GET"===a)});if(e&&d.states){var f=e.state;return _objectSpread(_objectSpread({},d),d.states[f])}return d}),(0,_defineProperty2["default"])(this,"serveRoutes",function(){Object.keys(c.routes).forEach(function(a){var b=Object.keys(c.routes[a]),d=(0,_utils.getSortedRoutesSlug)(b);d.forEach(function(b){var d=c.getRouteData(a,b);d&&c.serveRoute(d)})})}),(0,_defineProperty2["default"])(this,"serveRoute",function(a){var b=a.normalizedRoute,d=a.routeVars,e=a.statusCode,f=a.header;b=(0,_utils.routeResolve)(c.apiBaseUrl,b);c.listenRoute(a.method,b,function responseCallback(b,c){c.status(e),f&&c.header(f);var g={};d.forEach(function(a){g[a]=b.params[a]});var h=a.getBody(g);h?c.send(JSON.stringify(h)):c.end()})}),(0,_defineProperty2["default"])(this,"listenRoute",function(a,b,d){switch(a){case"POST":c.app.post(b,d);break;case"DELETE":c.app["delete"](b,d);break;case"PUT":c.app.put(b,d);break;case"PATCH":c.app.patch(b,d);break;case"GET":default:c.app.get(b,d);}}),(0,_defineProperty2["default"])(this,"startServer",function(){c.server.listen(c.port)}),(0,_defineProperty2["default"])(this,"customRun",function(){var a=0<arguments.length&&arguments[0]!==void 0?arguments[0]:{},b=a.hard,d=!(void 0!==b)||b,e=a.startServer,f=!(void 0!==e)||e,g=a.openDashboard;try{d&&(c.configEventsCallbacks(),c.checkApiBaseUrl(),c.checkRootDirectory()),c.listRouteFiles(),c.checkJsonFiles(),c.configRoutesFromListedFiles(),f&&c.configServer(),f&&c.configDashboard(),f&&c.configInternalApi(),d&&c.configHotWatch(),d&&c.autoOpenDashboard&&f&&(!(void 0!==g)||g)&&c.openDashboard(),d&&f&&c.executeCallbacks("server:start"),c.startServer(),d&&c.executeCallbacks("start")}catch(a){var h=JSON.parse(a.message),i=h.error,j=h.message;c.executeCallbacks("error",{error:i,message:j})}}),(0,_defineProperty2["default"])(this,"configEventsCallbacks",function(){c.onError(function(a){var b=a.error;"MISS:PORT"===b&&(c.port+=1,c.restartServer({hard:!0}))})}),(0,_defineProperty2["default"])(this,"removeState",function(a,b){c.states=c.states.filter(function(c){return c.route!==a&&c.method!==b})}),(0,_defineProperty2["default"])(this,"createSingleEventStoreIfMissing",function(a){c.eventCallbacksStore[a]===void 0&&(c.eventCallbacksStore[a]=[])}),(0,_defineProperty2["default"])(this,"addSingleEventCallbackToStore",function(a,b){// @ts-ignore | ||
c.createSingleEventStoreIfMissing(a),c.eventCallbacksStore[a].push(b)}),(0,_defineProperty2["default"])(this,"addEventCallbackToStore",function(a,b){Array.isArray(a)?a.forEach(function(a){c.addSingleEventCallbackToStore(a,b)}):c.addSingleEventCallbackToStore(a,b)}),(0,_defineProperty2["default"])(this,"executeCallbacksForSingleEvent",function(a,b){var d=c.eventCallbacksStore[a];d&&d.forEach(function(a){b?a(b):a()})}),(0,_defineProperty2["default"])(this,"executeCallbacks",function(a,b){Array.isArray(a)?a.forEach(function(a){c.executeCallbacksForSingleEvent(a,b)}):c.executeCallbacksForSingleEvent(a,b)}),(0,_defineProperty2["default"])(this,"closeServer",function(){c.server.close()}),(0,_defineProperty2["default"])(this,"closeChokidarWatcher",function(){c.chokidarWatcher.close()}),(0,_defineProperty2["default"])(this,"setState",function(a){if(a.state){var b=c.states.findIndex(function(b){return b.route===a.route&&b.method===a.method});-1!==b?c.states[b]=a:c.states.push(a)}else c.removeState(a.route,a.method);c.restartServer()}),(0,_defineProperty2["default"])(this,"getServedRoutes",function(){return(0,_utils.getRoutesByFileOrder)(c.routes)}),(0,_defineProperty2["default"])(this,"openDashboard",function(){// open with delay so user has time to see the console output | ||
c.routes[e][d].states[a]=(0,_utils.withoutUndefinedFromObject)({body:g,fileContent:l,header:i,isExtended:j,statusCode:k,getBody:h})})):c.routes[e][d]=_objectSpread(_objectSpread({},c.routes[e][d]),b)})}),(0,_defineProperty2["default"])(this,"getRouteData",function(a,b){if(!c.routes[a][b])return null;var d=c.routes[a][b],e=c.states.find(function(c){return c.route===b&&(c.method===a||c.method===void 0&&"GET"===a)});if(e&&d.states){var f=e.state;return _objectSpread(_objectSpread({},d),d.states[f])}return d}),(0,_defineProperty2["default"])(this,"serveRoutes",function(){Object.keys(c.routes).forEach(function(a){var b=Object.keys(c.routes[a]),d=(0,_utils.getSortedRoutesSlug)(b);d.forEach(function(b){var d=c.getRouteData(a,b);d&&c.serveRoute(d)})})}),(0,_defineProperty2["default"])(this,"serveRoute",function(a){var b=a.normalizedRoute,d=a.routeVars,e=a.statusCode,f=a.header;b=(0,_utils.routeResolve)(c.apiBaseUrl,b);c.listenRoute(a.method,b,function responseCallback(b,c){c.status(e),f&&c.header(f);var g={};d.forEach(function(a){g[a]=b.params[a]});var h=a.getBody(g);h?c.send(JSON.stringify(h)):c.end()})}),(0,_defineProperty2["default"])(this,"listenRoute",function(a,b,d){switch(a){case"POST":c.app.post(b,d);break;case"DELETE":c.app["delete"](b,d);break;case"PUT":c.app.put(b,d);break;case"PATCH":c.app.patch(b,d);break;case"GET":default:c.app.get(b,d);}}),(0,_defineProperty2["default"])(this,"startServer",function(){c.server.listen(c.port)}),(0,_defineProperty2["default"])(this,"customRun",function(){var a=0<arguments.length&&arguments[0]!==void 0?arguments[0]:{},b=a.hard,d=!(void 0!==b)||b,e=a.startServer,f=!(void 0!==e)||e,g=a.openDashboard,h=a.hotWatch;try{d&&(c.configEventsCallbacks(),c.checkApiBaseUrl(),c.checkRootDirectory()),c.listRouteFiles(),c.checkJsonFiles(),c.configRoutesFromListedFiles(),f&&c.configServer(),f&&c.configDashboard(),f&&c.configInternalApi(),d&&(!(void 0!==h)||h)&&c.configHotWatch(),d&&c.autoOpenDashboard&&f&&(!(void 0!==g)||g)&&c.openDashboard(),d&&f&&c.executeCallbacks("server:start"),f&&c.startServer(),d&&c.executeCallbacks("start")}catch(a){var i=JSON.parse(a.message),j=i.error,k=i.message;c.executeCallbacks("error",{error:j,message:k})}}),(0,_defineProperty2["default"])(this,"configEventsCallbacks",function(){c.onError(function(a){var b=a.error;"MISS:PORT"===b&&(c.port+=1,c.restartServer({hard:!0}))})}),(0,_defineProperty2["default"])(this,"removeState",function(a,b){c.states=c.states.filter(function(c){return c.route!==a&&c.method!==b})}),(0,_defineProperty2["default"])(this,"createSingleEventStoreIfMissing",function(a){c.eventCallbacksStore[a]===void 0&&(c.eventCallbacksStore[a]=[])}),(0,_defineProperty2["default"])(this,"addSingleEventCallbackToStore",function(a,b){// @ts-ignore | ||
c.createSingleEventStoreIfMissing(a),c.eventCallbacksStore[a].push(b)}),(0,_defineProperty2["default"])(this,"addEventCallbackToStore",function(a,b){Array.isArray(a)?a.forEach(function(a){c.addSingleEventCallbackToStore(a,b)}):c.addSingleEventCallbackToStore(a,b)}),(0,_defineProperty2["default"])(this,"executeCallbacksForSingleEvent",function(a,b){var d=c.eventCallbacksStore[a];d&&d.forEach(function(a){b?a(b):a()})}),(0,_defineProperty2["default"])(this,"executeCallbacks",function(a,b){Array.isArray(a)?a.forEach(function(a){c.executeCallbacksForSingleEvent(a,b)}):c.executeCallbacksForSingleEvent(a,b)}),(0,_defineProperty2["default"])(this,"closeServer",function(){c.server.close()}),(0,_defineProperty2["default"])(this,"closeChokidarWatcher",function(){c.chokidarWatcher.close()}),(0,_defineProperty2["default"])(this,"setState",function(a){if(a.state){var b=c.states.findIndex(function(b){return b.route===a.route&&b.method===a.method});-1!==b?c.states[b]=a:c.states.push(a)}else c.removeState(a.route,a.method);c.restartServer()}),(0,_defineProperty2["default"])(this,"getServedRoutes",function(){return c.customRun({startServer:!1,openDashboard:!1,hotWatch:!1}),(0,_utils.getRoutesByFileOrder)(c.routes)}),(0,_defineProperty2["default"])(this,"openDashboard",function(){// open with delay so user has time to see the console output | ||
setTimeout(function(){(0,_open["default"])("http://localhost:".concat(c.port,"/restapify")),c.executeCallbacksForSingleEvent("dashboard:open")},_const.OPEN_DASHBOARD_TIMEOUT)}),(0,_defineProperty2["default"])(this,"close",function(){c.server&&c.closeServer(),c.hotWatch&&c.chokidarWatcher&&c.closeChokidarWatcher()}),(0,_defineProperty2["default"])(this,"on",function(a,b){c.addEventCallbackToStore(a,b)}),(0,_defineProperty2["default"])(this,"onError",function(a){c.addSingleEventCallbackToStore("error",a)}),(0,_defineProperty2["default"])(this,"run",function(){c.customRun()}),this.rootDir=d,this.port=f,this.apiBaseUrl=h,this.hotWatch=!(void 0!==l)||l,this.autoOpenDashboard=void 0!==k&&k,this.states=j.filter(function(a){return a.state!==void 0})},_default=Restapify;// I N T E R F A C E S | ||
exports["default"]=_default; |
{ | ||
"name": "restapify", | ||
"version": "0.0.22", | ||
"version": "0.0.23", | ||
"main": "dist/index.js", | ||
@@ -5,0 +5,0 @@ "files": [ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
3309495
5399