Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@smallstack/api-server

Package Overview
Dependencies
Maintainers
3
Versions
54
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@smallstack/api-server - npm Package Compare versions

Comparing version 0.1.6 to 0.1.8

6

dist/bundle/dts/generated/services/GeneratedAPIService.d.ts

@@ -12,5 +12,5 @@ /**

getCollection(): Collection<ModelClass>;
save(apI: ModelClass): Promise<string>;
update(apI: ModelClass): Promise<number>;
delete(apI: ModelClass): Promise<number>;
save(api: ModelClass): Promise<string>;
update(api: ModelClass): Promise<number>;
delete(api: ModelClass): Promise<number>;
}

@@ -1,2 +0,2 @@

!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@smallstack/common"),require("@smallstack/data"),require("underscore"),require("@smallstack/user"),require("lodash")):"function"==typeof define&&define.amd?define(["exports","@smallstack/common","@smallstack/data","underscore","@smallstack/user","lodash"],t):t(e["@smallstack/data"]={},e["@smallstack/common"],e.data,e._,e.user,e.lodash)}(this,function(e,t,o,n,r,i){"use strict";var a=function(e,t,o,n){var r,i=arguments.length,a=i<3?t:null===n?n=Object.getOwnPropertyDescriptor(t,o):n;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)a=Reflect.decorate(e,t,o,n);else for(var c=e.length-1;c>=0;c--)(r=e[c])&&(a=(i<3?r(a):i>3?r(t,o,a):r(t,o))||a);return i>3&&a&&Object.defineProperty(t,o,a),a},c=function(e,t){if("object"==typeof Reflect&&"function"==typeof Reflect.metadata)return Reflect.metadata(e,t)},s=function(){function e(){}return e.instance=function(){return t.IOC.get("apiService")},e.prototype.getCollection=function(){return this.collectionsService.getCollectionByName("apis")},e.prototype.save=function(e){return this.getCollection().getMongoDBCollection().insert(e.toDocument())},e.prototype.update=function(e){var t=e.toDocument("id");return this.getCollection().getMongoDBCollection().update(t.id,{$set:{path:t.path,contentType:t.contentType,httpMethod:t.httpMethod,headers:t.headers}})},e.prototype.delete=function(e){return this.getCollection().getMongoDBCollection().remove(e.id)},a([t.Autowired(),c("design:type",Object)],e.prototype,"collectionsService",void 0),a([t.Autowired(),c("design:type",Object)],e.prototype,"dataBridge",void 0),e}(),u=function(){var e=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var o in t)t.hasOwnProperty(o)&&(e[o]=t[o])};return function(t,o){function n(){this.constructor=t}e(t,o),t.prototype=null===o?Object.create(o):(n.prototype=o.prototype,new n)}}(),l=function(e,t,o,n){var r,i=arguments.length,a=i<3?t:null===n?n=Object.getOwnPropertyDescriptor(t,o):n;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)a=Reflect.decorate(e,t,o,n);else for(var c=e.length-1;c>=0;c--)(r=e[c])&&(a=(i<3?r(a):i>3?r(t,o,a):r(t,o))||a);return i>3&&a&&Object.defineProperty(t,o,a),a},p=function(e,t){if("object"==typeof Reflect&&"function"==typeof Reflect.metadata)return Reflect.metadata(e,t)},d=function(e,t,o,n){return new(o||(o=Promise))(function(r,i){function a(e){try{s(n.next(e))}catch(e){i(e)}}function c(e){try{s(n.throw(e))}catch(e){i(e)}}function s(e){e.done?r(e.value):new o(function(t){t(e.value)}).then(a,c)}s((n=n.apply(e,t||[])).next())})},f=function(e,t){function o(o){return function(a){return function(o){if(n)throw new TypeError("Generator is already executing.");for(;c;)try{if(n=1,r&&(i=r[2&o[0]?"return":o[0]?"throw":"next"])&&!(i=i.call(r,o[1])).done)return i;switch(r=0,i&&(o=[0,i.value]),o[0]){case 0:case 1:i=o;break;case 4:return c.label++,{value:o[1],done:!1};case 5:c.label++,r=o[1],o=[0];continue;case 7:o=c.ops.pop(),c.trys.pop();continue;default:if(i=c.trys,!(i=i.length>0&&i[i.length-1])&&(6===o[0]||2===o[0])){c=0;continue}if(3===o[0]&&(!i||o[1]>i[0]&&o[1]<i[3])){c.label=o[1];break}if(6===o[0]&&c.label<i[1]){c.label=i[1],i=o;break}if(i&&c.label<i[2]){c.label=i[2],c.ops.push(o);break}i[2]&&c.ops.pop(),c.trys.pop();continue}o=t.call(e,c)}catch(e){o=[6,e],r=0}finally{n=i=0}if(5&o[0])throw o[1];return{value:o[0]?o[1]:void 0,done:!0}}([o,a])}}var n,r,i,a,c={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]};return a={next:o(0),throw:o(1),return:o(2)},"function"==typeof Symbol&&(a[Symbol.iterator]=function(){return this}),a},g=function(e){function a(){var o=e.call(this)||this;return t.Logger.debug("APIService","initializing..."),n.each(o.typesystem.getAllTypes(),function(e){t.Logger.debug("APIService","Checking Type: "+e.getModelName()),e.service&&e.service.queries instanceof Array&&(n.each(e.service.queries,function(t){void 0!==t.api&&o.createServiceQueryEndpoint(e,t.name)}),e.service.methods instanceof Array&&n.each(e.service.methods,function(t){t.api&&o.createSecuredMethodEndpoint(e,t)}))}),o}return u(a,e),a.prototype.createCallbackEndpoint=function(e,o){t.Logger.debug("APIService","Registering callback endpoint : ",e.toDocument()),this.registerEndpoint(e,o)},a.prototype.createSecuredMethodEndpoint=function(e,o){var n=this,r=t.Utils.getApiURL(e.getCollectionName(),o.name);t.Logger.debug("APIService","Registering service secured method endpoint for "+e.getServiceName()+"->"+o.name+"->"+r);var i=P.fromDocument({path:"api/"+r,httpMethod:"POST",headers:this.getTotallyOpenCORSHeaders()});this.registerEndpoint(i,function(t){var r=n.getUserId(t),i={};return r&&(i.userId=r),n.dataBridge.ddpCall.bind(i)(e.getCollectionName()+"-"+o.name,t.queryParams)})},a.prototype.getUserId=function(e){if(void 0!==e&&void 0!==e.headers&&void 0!==e.headers["x-smallstack-token"])return this.authService.getUserIdByStampedToken(e.headers["x-smallstack-token"]);t.Logger.debug("APIService","No x-smallstack-token found in parameters!")},a.prototype.getUser=function(e){var t=this;return new Promise(function(o){return d(t,void 0,void 0,function(){var t,n;return f(this,function(r){switch(r.label){case 0:return t=this.getUserId(e),n=o,[4,this.userService.getUserById({id:t}).getModel(0)];case 1:return n.apply(void 0,[r.sent()]),[2]}})})})},a.prototype.getTotallyOpenCORSHeaders=function(){return{"Access-Control-Allow-Origin":"*","Access-Control-Allow-Methods":"POST,PUT,GET,DELETE,HEAD,OPTIONS","Access-Control-Allow-Headers":"Authorization,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range","Access-Control-Expose-Headers":"Authorization,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range"}},a.prototype.createServiceQueryEndpoint=function(e,r){var a=this,c=t.Utils.getApiURL(e.getCollectionName(),r);t.Logger.debug("APIService","Registering service query endpoint for "+e.getServiceName()+"->"+r+"->"+c);var s=P.fromDocument({path:"api/"+c,httpMethod:"GET",headers:this.getTotallyOpenCORSHeaders()});this.registerEndpoint(s,function(c){return new Promise(function(s,u){var l,p=e.getServiceQueryByName(r),d=a.getUserId(c);if(void 0!==p.access){t.Logger.debug("APIService","Checking access for query: "+p.name);var f=a.collectionAccessService.checkAccess(p,{userId:d},c.queryParams);t.Logger.debug("APIService"," -> Result",f);for(var g=0,h=f;g<h.length;g++){var y=h[g];switch(y.type){case"boolean":if(!1===y.value)throw new Error("Denied by access rule!");break;case"selector":if(void 0!==l)throw new Error("Accessor Groups with more than one access method that changes the selector are not supported!");l=y.value;break;default:throw new Error("Unknown selector access type :"+y.type)}}}t.Logger.debug("APIService"," -> AccessSelector",l);var m={};void 0!==p.selector&&i.merge(m,p.selector);var v=o.TypesystemUtils.extractSelectorParameters(p.selector);n.each(v,function(e){if(void 0===c.queryParams[e]||"string"!=typeof c.queryParams[e])throw new Error("Parameter '"+e+"' is missing!");m[e]=c.queryParams[e],t.Logger.debug("APIService","Replaced "+e+" with "+c.queryParams[e])}),void 0!==l&&i.merge(m,l),t.Logger.debug("APIService","Executing service query: "+e.getServiceName()+"->"+r,m);var P={};c.queryParams.entriesPerPage?P.limit=parseInt(c.queryParams.entriesPerPage):P.limit=10,c.queryParams.currentPage?P.skip=(parseInt(c.queryParams.currentPage)-1)*P.limit:P.skip=0,void 0!==p.fields&&(P.fields=p.fields),n.each(c.queryParams,function(e,t){"["===e.charAt(0)&&(c.queryParams[t]=JSON.parse(e))}),t.Logger.debug("APIService"," with params: "+JSON.stringify(m)),t.Logger.debug("APIService"," with options: "+JSON.stringify(P));var b=a.collectionsService.getCollectionByName(e.getCollectionName()),S=a.collectionFind(b)(m,P).fetch(),A=[];n.each(S,function(e){A.push(e.toDocument("id"))}),s(JSON.stringify(A))})})},a.prototype.getPaginationParams=function(e){var t=10,o=1;e.queryParams&&e.queryParams.entriesPerPage&&(t=parseInt(e.queryParams.entriesPerPage)),e.queryParams&&e.queryParams.currentPage&&(o=parseInt(e.queryParams.currentPage));return{entriesPerPage:t,currentPage:o,skip:(o-1)*t}},a.prototype.createPaginationResult=function(e,t,o,n,r){-1===t.indexOf("http")&&("/"!==t.charAt(0)&&(t="/"+t),t=this.configurationService.get("smallstack.api.url","http://localhost:3000/api")+t);var i="?";-1!==t.indexOf("?")&&(i="&");var a={paging:{base:t,total:r,entriesPerPage:n,currentPage:o,totalPages:Math.ceil(r/n)},results:e};return r>o*n&&(a.paging.next=t+i+"entriesPerPage="+n+"&currentPage="+(o+1)),o>1&&(a.paging.previous=t+i+"entriesPerPage="+n+"&currentPage="+(o-1)),a},a.prototype.getPaginationParameters=function(e){var t=10,o=1;e.queryParams&&e.queryParams.entriesPerPage&&(t=parseInt(e.queryParams.entriesPerPage)),e.queryParams&&e.queryParams.currentPage&&(o=parseInt(e.queryParams.currentPage));return{entriesPerPage:t,skip:(o-1)*t,currentPage:o}},a.prototype.getUniqueCallbackName=function(e){return e.path+e.httpMethod},a.prototype.collectionFind=function(e){if("function"==typeof e.find)return e.find.bind(e);if("function"==typeof e.getMongoDBCollection){var t=e.getMongoDBCollection();return t.find.bind(t)}throw new Error("neither .find nor getMongoDBCollection().find are methods!")},l([t.Autowired(),p("design:type",o.Typesystem)],a.prototype,"typesystem",void 0),l([t.Autowired(),p("design:type",o.CollectionAccessService)],a.prototype,"collectionAccessService",void 0),l([t.Autowired(),p("design:type",Object)],a.prototype,"authService",void 0),l([t.Autowired(),p("design:type",r.UserService)],a.prototype,"userService",void 0),l([t.Autowired(),p("design:type",Object)],a.prototype,"configurationService",void 0),a}(s),h=function(e,t,o,n){var r,i=arguments.length,a=i<3?t:null===n?n=Object.getOwnPropertyDescriptor(t,o):n;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)a=Reflect.decorate(e,t,o,n);else for(var c=e.length-1;c>=0;c--)(r=e[c])&&(a=(i<3?r(a):i>3?r(t,o,a):r(t,o))||a);return i>3&&a&&Object.defineProperty(t,o,a),a},y=function(e,t){if("object"==typeof Reflect&&"function"==typeof Reflect.metadata)return Reflect.metadata(e,t)},m=function(){function e(){this.contentType="application/json",this.httpMethod="GET",this._hasSubDocuments=!1,this._isStored=!1}return e.fromDocument=function(e){if(void 0===e||null===e)throw new Error("doc cannot be undefined/null while calling GeneratedAPI.fromDocument(doc)!");var t=new(this.getModelClass());return void 0!==e._id&&(t._isStored=!0,t.id=e._id),void 0!==e.path&&(t.path=e.path),void 0!==e.contentType&&(t.contentType=e.contentType),void 0!==e.httpMethod&&(t.httpMethod=e.httpMethod),void 0!==e.headers&&(t.headers=e.headers),t},e.prototype.toDocument=function(e){void 0===e&&(e="_id");var t={};return t.path=this.path,t.contentType=this.contentType,t.httpMethod=this.httpMethod,t.headers=this.headers,this.id&&(t[e]=this.id),JSON.parse(JSON.stringify(t))},e.prototype.clone=function(){return P.fromDocument(this.toDocument())},e.prototype.saveSnapshot=function(){this._snapshot=this.toDocument()},e.prototype.restoreSnapshot=function(){if(!this._snapshot)throw new Error("Could not restore snapshot of model since no snapshot was saved before!");n.extend(this,this._snapshot)},e.prototype.hasSubDocuments=function(){return this._hasSubDocuments},e.prototype.isStored=function(){return this._isStored},e.getModelName=function(){return"API"},e.prototype.getModelName=function(){return"API"},e.prototype.getForeignLabel=function(){return this.getModelName()+" ID:"+this.id},e.getModelClass=function(){return t.IOC.get("API")},e.prototype.getModelClass=function(){return t.IOC.get("API")},e.prototype.delete=function(){return g.instance().delete(this)},e.prototype.update=function(){return g.instance().update(this)},e.prototype.save=function(){return g.instance().save(this)},e.asType=function(){return o.Type.fromDocument({collection:{name:"apis"},service:{name:"APIService",queries:[]},model:{name:"API",schema:[{name:"path",type:"string"},{name:"contentType",type:"string",defaultValue:"application/json",optional:!0},{name:"httpMethod",type:"string",defaultValue:"GET",allowedValues:["GET","POST","HEAD","PUT","DELETE","OPTIONS"]},{name:"headers",type:"object",blackbox:!0,optional:!0}]}})},e.enums={httpMethod:{GET:"GET",POST:"POST",HEAD:"HEAD",PUT:"PUT",DELETE:"DELETE",OPTIONS:"OPTIONS"}},h([t.Autowired(),y("design:type",Object)],e.prototype,"dataBridge",void 0),e}(),v=function(){var e=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var o in t)t.hasOwnProperty(o)&&(e[o]=t[o])};return function(t,o){function n(){this.constructor=t}e(t,o),t.prototype=null===o?Object.create(o):(n.prototype=o.prototype,new n)}}(),P=function(e){function t(){return null!==e&&e.apply(this,arguments)||this}return v(t,e),t}(m),b=function(){var e=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var o in t)t.hasOwnProperty(o)&&(e[o]=t[o])};return function(t,o){function n(){this.constructor=t}e(t,o),t.prototype=null===o?Object.create(o):(n.prototype=o.prototype,new n)}}(),S=function(e){function o(){var t=e.call(this)||this;return t.createMongoDBCollection(),t.applyAllowDenyRules(),t.createPublications(),t}return b(o,e),o.prototype.createPublications=function(){this.dataBridge.isServer()},o.prototype.createMongoDBCollection=function(){this.mongoDBCollection=this.collectionsService.createMongoDBCollection("apis",function(e){return P.fromDocument(e)}),this.collectionsService.registerCollection("apis",this)},o.prototype.applyAllowDenyRules=function(){var e=this.getMongoDBCollectionAllowRules(),t=this.getMongoDBCollectionDenyRules();e&&this.mongoDBCollection.allow(e),t&&this.mongoDBCollection.deny(t)},o.prototype.getMongoDBCollectionAllowRules=function(){},o.prototype.getMongoDBCollectionDenyRules=function(){return{insert:function(e,t){return!1},update:function(e,t,o,n){return!1},remove:function(e,t){return!1}}},o.prototype.getForeignCollection=function(e){},o.prototype.getCollectionName=function(){return o.COLLECTION_NAME},o.getCollection=function(){return t.IOC.get("collectionsService").getCollectionByName(o.COLLECTION_NAME)},o.COLLECTION_NAME="apis",o.queries={},o.expandables={},o}(o.Collection),A=function(){var e=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var o in t)t.hasOwnProperty(o)&&(e[o]=t[o])};return function(t,o){function n(){this.constructor=t}e(t,o),t.prototype=null===o?Object.create(o):(n.prototype=o.prototype,new n)}}(),C=function(e){function t(){return null!==e&&e.apply(this,arguments)||this}return A(t,e),t}(S);e.initializeTypesystem=function(e){t.IOC.onRegister("typesystem",function(n){n.addType(o.Type.fromDocument({collection:{name:"apis"},service:{name:"APIService",queries:[]},model:{name:"API",schema:[{name:"path",type:"string"},{name:"contentType",type:"string",defaultValue:"application/json",optional:!0},{name:"httpMethod",type:"string",defaultValue:"GET",allowedValues:["GET","POST","HEAD","PUT","DELETE","OPTIONS"]},{name:"headers",type:"object",blackbox:!0,optional:!0}]}})),t.IOC.register("API",P),void 0!==e&&void 0!==e.withCollections&&!0!==e.withCollections||t.IOC.register("apisCollection",new C)})},e.API=P,e.APIService=g,e.ApisCollection=C,Object.defineProperty(e,"__esModule",{value:!0})});
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@smallstack/common"),require("@smallstack/data"),require("underscore"),require("@smallstack/user"),require("lodash")):"function"==typeof define&&define.amd?define(["exports","@smallstack/common","@smallstack/data","underscore","@smallstack/user","lodash"],t):t(e["@smallstack/data"]={},e["@smallstack/common"],e.data,e._,e.user,e.lodash)}(this,function(e,t,o,r,n,i){"use strict";var a=function(e,t,o,r){var n,i=arguments.length,a=i<3?t:null===r?r=Object.getOwnPropertyDescriptor(t,o):r;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)a=Reflect.decorate(e,t,o,r);else for(var c=e.length-1;c>=0;c--)(n=e[c])&&(a=(i<3?n(a):i>3?n(t,o,a):n(t,o))||a);return i>3&&a&&Object.defineProperty(t,o,a),a},c=function(e,t){if("object"==typeof Reflect&&"function"==typeof Reflect.metadata)return Reflect.metadata(e,t)},s=function(){function e(){}return e.instance=function(){return t.IOC.get("apiService")},e.prototype.getCollection=function(){return this.collectionsService.getCollectionByName("apis")},e.prototype.save=function(e){return this.getCollection().getMongoDBCollection().insert(e.toDocument())},e.prototype.update=function(e){var t=e.toDocument("id");return this.getCollection().getMongoDBCollection().update(t.id,{$set:{path:t.path,contentType:t.contentType,httpMethod:t.httpMethod,headers:t.headers}})},e.prototype.delete=function(e){return this.getCollection().getMongoDBCollection().remove(e.id)},a([t.Autowired(),c("design:type",Object)],e.prototype,"collectionsService",void 0),a([t.Autowired(),c("design:type",Object)],e.prototype,"dataBridge",void 0),e}(),u=function(){var e=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var o in t)t.hasOwnProperty(o)&&(e[o]=t[o])};return function(t,o){function r(){this.constructor=t}e(t,o),t.prototype=null===o?Object.create(o):(r.prototype=o.prototype,new r)}}(),l=function(e,t,o,r){var n,i=arguments.length,a=i<3?t:null===r?r=Object.getOwnPropertyDescriptor(t,o):r;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)a=Reflect.decorate(e,t,o,r);else for(var c=e.length-1;c>=0;c--)(n=e[c])&&(a=(i<3?n(a):i>3?n(t,o,a):n(t,o))||a);return i>3&&a&&Object.defineProperty(t,o,a),a},p=function(e,t){if("object"==typeof Reflect&&"function"==typeof Reflect.metadata)return Reflect.metadata(e,t)},d=function(e,t,o,r){return new(o||(o=Promise))(function(n,i){function a(e){try{s(r.next(e))}catch(e){i(e)}}function c(e){try{s(r.throw(e))}catch(e){i(e)}}function s(e){e.done?n(e.value):new o(function(t){t(e.value)}).then(a,c)}s((r=r.apply(e,t||[])).next())})},f=function(e,t){function o(o){return function(a){return function(o){if(r)throw new TypeError("Generator is already executing.");for(;c;)try{if(r=1,n&&(i=n[2&o[0]?"return":o[0]?"throw":"next"])&&!(i=i.call(n,o[1])).done)return i;switch(n=0,i&&(o=[0,i.value]),o[0]){case 0:case 1:i=o;break;case 4:return c.label++,{value:o[1],done:!1};case 5:c.label++,n=o[1],o=[0];continue;case 7:o=c.ops.pop(),c.trys.pop();continue;default:if(i=c.trys,!(i=i.length>0&&i[i.length-1])&&(6===o[0]||2===o[0])){c=0;continue}if(3===o[0]&&(!i||o[1]>i[0]&&o[1]<i[3])){c.label=o[1];break}if(6===o[0]&&c.label<i[1]){c.label=i[1],i=o;break}if(i&&c.label<i[2]){c.label=i[2],c.ops.push(o);break}i[2]&&c.ops.pop(),c.trys.pop();continue}o=t.call(e,c)}catch(e){o=[6,e],n=0}finally{r=i=0}if(5&o[0])throw o[1];return{value:o[0]?o[1]:void 0,done:!0}}([o,a])}}var r,n,i,a,c={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]};return a={next:o(0),throw:o(1),return:o(2)},"function"==typeof Symbol&&(a[Symbol.iterator]=function(){return this}),a},g=function(e){function a(){var o=e.call(this)||this;return t.Logger.debug("APIService","initializing..."),r.each(o.typesystem.getAllTypes(),function(e){t.Logger.debug("APIService","Checking Type: "+e.getModelName()),e.service&&e.service.queries instanceof Array&&(r.each(e.service.queries,function(t){void 0!==t.api&&o.createServiceQueryEndpoint(e,t.name)}),e.service.methods instanceof Array&&r.each(e.service.methods,function(t){t.api&&o.createSecuredMethodEndpoint(e,t)}))}),o}return u(a,e),a.prototype.createCallbackEndpoint=function(e,o){t.Logger.debug("APIService","Registering callback endpoint : ",e.toDocument()),this.registerEndpoint(e,o)},a.prototype.createSecuredMethodEndpoint=function(e,o){var r=this,n=t.Utils.getApiURL(e.getCollectionName(),o.name);t.Logger.debug("APIService","Registering service secured method endpoint for "+e.getServiceName()+"->"+o.name+"->"+n);var i=P.fromDocument({path:"api/"+n,httpMethod:"POST",headers:this.getTotallyOpenCORSHeaders()});this.registerEndpoint(i,function(t){var n=r.getUserId(t),i={};return n&&(i.userId=n),r.dataBridge.ddpCall.bind(i)(e.getCollectionName()+"-"+o.name,t.queryParams)})},a.prototype.getUserId=function(e){if(void 0!==e&&void 0!==e.headers&&void 0!==e.headers["x-smallstack-token"])return this.authService.getUserIdByStampedToken(e.headers["x-smallstack-token"]);t.Logger.debug("APIService","No x-smallstack-token found in parameters!")},a.prototype.getUser=function(e){var t=this;return new Promise(function(o){return d(t,void 0,void 0,function(){var t,r;return f(this,function(n){switch(n.label){case 0:return t=this.getUserId(e),r=o,[4,this.userService.getUserById({id:t}).getModel(0)];case 1:return r.apply(void 0,[n.sent()]),[2]}})})})},a.prototype.getTotallyOpenCORSHeaders=function(){return{"Access-Control-Allow-Origin":"*","Access-Control-Allow-Methods":"POST,PUT,GET,DELETE,HEAD,OPTIONS","Access-Control-Allow-Headers":"Authorization,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range","Access-Control-Expose-Headers":"Authorization,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range"}},a.prototype.createServiceQueryEndpoint=function(e,n){var a=this,c=t.Utils.getApiURL(e.getCollectionName(),n);t.Logger.debug("APIService","Registering service query endpoint for "+e.getServiceName()+"->"+n+"->"+c);var s=P.fromDocument({path:"api/"+c,httpMethod:"GET",headers:this.getTotallyOpenCORSHeaders()});this.registerEndpoint(s,function(c){return new Promise(function(s,u){var l,p=e.getServiceQueryByName(n),d=a.getUserId(c);if(void 0!==p.access){t.Logger.debug("APIService","Checking access for query: "+p.name);var f=a.collectionAccessService.checkAccess(p,{userId:d},c.queryParams);t.Logger.debug("APIService"," -> Result",f);for(var g=0,h=f;g<h.length;g++){var y=h[g];switch(y.type){case"boolean":if(!1===y.value)throw new Error("Denied by access rule!");break;case"selector":if(void 0!==l)throw new Error("Accessor Groups with more than one access method that changes the selector are not supported!");l=y.value;break;default:throw new Error("Unknown selector access type :"+y.type)}}}t.Logger.debug("APIService"," -> AccessSelector",l);var m={};void 0!==p.selector&&i.merge(m,p.selector);var v=o.TypesystemUtils.extractSelectorParameters(p.selector);r.each(v,function(e){if(void 0===c.queryParams[e]||"string"!=typeof c.queryParams[e])throw new Error("Parameter '"+e+"' is missing!");m[e]=c.queryParams[e],t.Logger.debug("APIService","Replaced "+e+" with "+c.queryParams[e])}),void 0!==l&&i.merge(m,l),t.Logger.debug("APIService","Executing service query: "+e.getServiceName()+"->"+n,m);var P={};c.queryParams.entriesPerPage?P.limit=parseInt(c.queryParams.entriesPerPage):P.limit=10,c.queryParams.currentPage?P.skip=(parseInt(c.queryParams.currentPage)-1)*P.limit:P.skip=0,void 0!==p.fields&&(P.fields=p.fields),r.each(c.queryParams,function(e,t){"["===e.charAt(0)&&(c.queryParams[t]=JSON.parse(e))}),t.Logger.debug("APIService"," with params: "+JSON.stringify(m)),t.Logger.debug("APIService"," with options: "+JSON.stringify(P));var b=a.collectionsService.getCollectionByName(e.getCollectionName()),S=a.collectionFind(b)(m,P).fetch(),A=[];r.each(S,function(e){A.push(e.toDocument("id"))}),s(JSON.stringify(A))})})},a.prototype.getPaginationParams=function(e){var t=10,o=1;e.queryParams&&e.queryParams.entriesPerPage&&(t=parseInt(e.queryParams.entriesPerPage)),e.queryParams&&e.queryParams.currentPage&&(o=parseInt(e.queryParams.currentPage));return{entriesPerPage:t,currentPage:o,skip:(o-1)*t}},a.prototype.createPaginationResult=function(e,t,o,r,n){-1===t.indexOf("http")&&("/"!==t.charAt(0)&&(t="/"+t),t=this.configurationService.get("smallstack.api.url","http://localhost:3000/api")+t);var i="?";-1!==t.indexOf("?")&&(i="&");var a={paging:{base:t,total:n,entriesPerPage:r,currentPage:o,totalPages:Math.ceil(n/r)},results:e};return n>o*r&&(a.paging.next=t+i+"entriesPerPage="+r+"&currentPage="+(o+1)),o>1&&(a.paging.previous=t+i+"entriesPerPage="+r+"&currentPage="+(o-1)),a},a.prototype.getPaginationParameters=function(e){var t=10,o=1;e.queryParams&&e.queryParams.entriesPerPage&&(t=parseInt(e.queryParams.entriesPerPage)),e.queryParams&&e.queryParams.currentPage&&(o=parseInt(e.queryParams.currentPage));return{entriesPerPage:t,skip:(o-1)*t,currentPage:o}},a.prototype.getUniqueCallbackName=function(e){return e.path+e.httpMethod},a.prototype.collectionFind=function(e){if("function"==typeof e.find)return e.find.bind(e);if("function"==typeof e.getMongoDBCollection){var t=e.getMongoDBCollection();return t.find.bind(t)}throw new Error("neither .find nor getMongoDBCollection().find are methods!")},l([t.Autowired(),p("design:type",o.Typesystem)],a.prototype,"typesystem",void 0),l([t.Autowired(),p("design:type",o.CollectionAccessService)],a.prototype,"collectionAccessService",void 0),l([t.Autowired(),p("design:type",Object)],a.prototype,"authService",void 0),l([t.Autowired(),p("design:type",n.UserService)],a.prototype,"userService",void 0),l([t.Autowired(),p("design:type",Object)],a.prototype,"configurationService",void 0),a}(s),h=function(e,t,o,r){var n,i=arguments.length,a=i<3?t:null===r?r=Object.getOwnPropertyDescriptor(t,o):r;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)a=Reflect.decorate(e,t,o,r);else for(var c=e.length-1;c>=0;c--)(n=e[c])&&(a=(i<3?n(a):i>3?n(t,o,a):n(t,o))||a);return i>3&&a&&Object.defineProperty(t,o,a),a},y=function(e,t){if("object"==typeof Reflect&&"function"==typeof Reflect.metadata)return Reflect.metadata(e,t)},m=function(){function e(){this.contentType="application/json",this.httpMethod="GET",this._hasSubDocuments=!1,this._isStored=!1}return e.fromDocument=function(e){if(void 0===e||null===e)throw new Error("doc cannot be undefined/null while calling GeneratedAPI.fromDocument(doc)!");var t=new(this.getModelClass());return void 0!==e._id&&(t._isStored=!0,t.id=e._id),void 0!==e.path&&(t.path=e.path),void 0!==e.contentType&&(t.contentType=e.contentType),void 0!==e.httpMethod&&(t.httpMethod=e.httpMethod),void 0!==e.headers&&(t.headers=e.headers),t},e.prototype.toDocument=function(e){void 0===e&&(e="_id");var t={};return t.path=this.path,t.contentType=this.contentType,t.httpMethod=this.httpMethod,t.headers=this.headers,this.id&&(t[e]=this.id),JSON.parse(JSON.stringify(t))},e.prototype.clone=function(){return P.fromDocument(this.toDocument())},e.prototype.saveSnapshot=function(){this._snapshot=this.toDocument()},e.prototype.restoreSnapshot=function(){if(!this._snapshot)throw new Error("Could not restore snapshot of model since no snapshot was saved before!");r.extend(this,this._snapshot)},e.prototype.hasSubDocuments=function(){return this._hasSubDocuments},e.prototype.isStored=function(){return this._isStored},e.getModelName=function(){return"API"},e.prototype.getModelName=function(){return"API"},e.prototype.getForeignLabel=function(){return this.getModelName()+" ID:"+this.id},e.getModelClass=function(){return t.IOC.get("API")},e.prototype.getModelClass=function(){return t.IOC.get("API")},e.prototype.delete=function(){return g.instance().delete(this)},e.prototype.update=function(){return g.instance().update(this)},e.prototype.save=function(){return g.instance().save(this)},e.asType=function(){return o.Type.fromDocument({collection:{name:"apis"},service:{name:"APIService",abstract:!0,queries:[]},model:{name:"API",schema:[{name:"path",type:"string"},{name:"contentType",type:"string",defaultValue:"application/json",optional:!0},{name:"httpMethod",type:"string",defaultValue:"GET",allowedValues:["GET","POST","HEAD","PUT","DELETE","OPTIONS"]},{name:"headers",type:"object",blackbox:!0,optional:!0}]},packageName:"@smallstack/api-server"})},e.enums={httpMethod:{GET:"GET",POST:"POST",HEAD:"HEAD",PUT:"PUT",DELETE:"DELETE",OPTIONS:"OPTIONS"}},h([t.Autowired(),y("design:type",Object)],e.prototype,"dataBridge",void 0),e}(),v=function(){var e=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var o in t)t.hasOwnProperty(o)&&(e[o]=t[o])};return function(t,o){function r(){this.constructor=t}e(t,o),t.prototype=null===o?Object.create(o):(r.prototype=o.prototype,new r)}}(),P=function(e){function t(){return null!==e&&e.apply(this,arguments)||this}return v(t,e),t}(m),b=function(){var e=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var o in t)t.hasOwnProperty(o)&&(e[o]=t[o])};return function(t,o){function r(){this.constructor=t}e(t,o),t.prototype=null===o?Object.create(o):(r.prototype=o.prototype,new r)}}(),S=function(e){function o(){var t=e.call(this)||this;return t.createMongoDBCollection(),t.applyAllowDenyRules(),t.createPublications(),t}return b(o,e),o.prototype.createPublications=function(){this.dataBridge.isServer()},o.prototype.createMongoDBCollection=function(){this.mongoDBCollection=this.collectionsService.createMongoDBCollection("apis",function(e){return P.fromDocument(e)}),this.collectionsService.registerCollection("apis",this)},o.prototype.applyAllowDenyRules=function(){var e=this.getMongoDBCollectionAllowRules(),t=this.getMongoDBCollectionDenyRules();e&&this.mongoDBCollection.allow(e),t&&this.mongoDBCollection.deny(t)},o.prototype.getMongoDBCollectionAllowRules=function(){},o.prototype.getMongoDBCollectionDenyRules=function(){return{insert:function(e,t){return!1},update:function(e,t,o,r){return!1},remove:function(e,t){return!1}}},o.prototype.getForeignCollection=function(e){},o.prototype.getCollectionName=function(){return o.COLLECTION_NAME},o.getCollection=function(){return t.IOC.get("collectionsService").getCollectionByName(o.COLLECTION_NAME)},o.COLLECTION_NAME="apis",o.queries={},o.expandables={},o}(o.Collection),A=function(){var e=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var o in t)t.hasOwnProperty(o)&&(e[o]=t[o])};return function(t,o){function r(){this.constructor=t}e(t,o),t.prototype=null===o?Object.create(o):(r.prototype=o.prototype,new r)}}(),C=function(e){function t(){return null!==e&&e.apply(this,arguments)||this}return A(t,e),t}(S);e.initializeTypesystem=function(e){t.IOC.onRegister("typesystem",function(r){r.addType(o.Type.fromDocument({collection:{name:"apis"},service:{name:"APIService",abstract:!0,queries:[]},model:{name:"API",schema:[{name:"path",type:"string"},{name:"contentType",type:"string",defaultValue:"application/json",optional:!0},{name:"httpMethod",type:"string",defaultValue:"GET",allowedValues:["GET","POST","HEAD","PUT","DELETE","OPTIONS"]},{name:"headers",type:"object",blackbox:!0,optional:!0}]},packageName:"@smallstack/api-server"})),t.IOC.register("API",P),void 0!==e&&void 0!==e.withCollections&&!0!==e.withCollections||t.IOC.register("apisCollection",new C)})},e.API=P,e.APIService=g,e.ApisCollection=C,Object.defineProperty(e,"__esModule",{value:!0})});
//# sourceMappingURL=index.umd.js.map
{
"name": "@smallstack/api-server",
"version": "0.1.6",
"version": "0.1.8",
"description": "API server functionality for smallstack projects",

@@ -5,0 +5,0 @@ "main": "dist/bundle/index.umd.js",

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc