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.1 to 0.1.3

1

dist/bundle/dts/services/APIService.d.ts

@@ -11,2 +11,3 @@ import { APIPaginationParameters, APIPaginationResult } from "@smallstack/api-common";

private collectionAccessService;
private authService;
private userService;

@@ -13,0 +14,0 @@ private configurationService;

2

dist/bundle/index.umd.js

@@ -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,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},h=function(e){function a(){var o=e.call(this)||this;return o.apiCallbacks={},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());var r=this.getUniqueCallbackName(e);if(this.apiCallbacks[r])throw new Error("Callback API for path "+e.path+" and method "+e.httpMethod+" already exists!");this.apiCallbacks[r]=o,this.registerEndpoint(e,this.apiCallbacks[r])},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"])throw new Error("AuthService is not migrated yet!");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){var s,u=e.getServiceQueryByName(n),l=a.getUserId(c);if(void 0!==u.access){t.Logger.debug("APIService","Checking access for query: "+u.name);var p=a.collectionAccessService.checkAccess(u,{userId:l},c.queryParams);t.Logger.debug("APIService"," -> Result",p);for(var d=0,f=p;d<f.length;d++){var h=f[d];switch(h.type){case"boolean":if(!1===h.value)throw new Error("Denied by access rule!");break;case"selector":if(void 0!==s)throw new Error("Accessor Groups with more than one access method that changes the selector are not supported!");s=h.value;break;default:throw new Error("Unknown selector access type :"+h.type)}}}t.Logger.debug("APIService"," -> AccessSelector",s);var g={};void 0!==u.selector&&i.merge(g,u.selector);var y=o.TypesystemUtils.extractSelectorParameters(u.selector);r.each(y,function(e){if(void 0===c.queryParams[e]||"string"!=typeof c.queryParams[e])throw new Error("Parameter '"+e+"' is missing!");g[e]=c.queryParams[e],t.Logger.debug("APIService","Replaced "+e+" with "+c.queryParams[e])}),void 0!==s&&i.merge(g,s),t.Logger.debug("APIService","Executing service query: "+e.getServiceName()+"->"+n,g);var m={};c.queryParams.entriesPerPage?m.limit=parseInt(c.queryParams.entriesPerPage):m.limit=10,c.queryParams.currentPage?m.skip=(parseInt(c.queryParams.currentPage)-1)*m.limit:m.skip=0,void 0!==u.fields&&(m.fields=u.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(g)),t.Logger.debug("APIService"," with options: "+JSON.stringify(m));var v=a.collectionsService.getCollectionByName(e.getCollectionName()),P=a.collectionFind(v)(g,m).fetch(),b=[];return r.each(P,function(e){b.push(e.toDocument("id"))}),JSON.stringify(b)})},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",n.UserService)],a.prototype,"userService",void 0),l([t.Autowired(),p("design:type",Object)],a.prototype,"configurationService",void 0),a}(s),g=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 h.instance().delete(this)},e.prototype.update=function(){return h.instance().update(this)},e.prototype.save=function(){return h.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"}},g([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)}}(),C=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)}}(),S=function(e){function t(){return null!==e&&e.apply(this,arguments)||this}return A(t,e),t}(C);e.initializeTypesystem=function(e){t.IOC.onRegister("typesystem",function(r){r.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 S)})},e.API=P,e.APIService=h,e.ApisCollection=S,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},h=function(e){function a(){var o=e.call(this)||this;return o.apiCallbacks={},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());var r=this.getUniqueCallbackName(e);if(this.apiCallbacks[r])throw new Error("Callback API for path "+e.path+" and method "+e.httpMethod+" already exists!");this.apiCallbacks[r]=o,this.registerEndpoint(e,this.apiCallbacks[r])},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){var s,u=e.getServiceQueryByName(n),l=a.getUserId(c);if(void 0!==u.access){t.Logger.debug("APIService","Checking access for query: "+u.name);var p=a.collectionAccessService.checkAccess(u,{userId:l},c.queryParams);t.Logger.debug("APIService"," -> Result",p);for(var d=0,f=p;d<f.length;d++){var h=f[d];switch(h.type){case"boolean":if(!1===h.value)throw new Error("Denied by access rule!");break;case"selector":if(void 0!==s)throw new Error("Accessor Groups with more than one access method that changes the selector are not supported!");s=h.value;break;default:throw new Error("Unknown selector access type :"+h.type)}}}t.Logger.debug("APIService"," -> AccessSelector",s);var g={};void 0!==u.selector&&i.merge(g,u.selector);var y=o.TypesystemUtils.extractSelectorParameters(u.selector);r.each(y,function(e){if(void 0===c.queryParams[e]||"string"!=typeof c.queryParams[e])throw new Error("Parameter '"+e+"' is missing!");g[e]=c.queryParams[e],t.Logger.debug("APIService","Replaced "+e+" with "+c.queryParams[e])}),void 0!==s&&i.merge(g,s),t.Logger.debug("APIService","Executing service query: "+e.getServiceName()+"->"+n,g);var m={};c.queryParams.entriesPerPage?m.limit=parseInt(c.queryParams.entriesPerPage):m.limit=10,c.queryParams.currentPage?m.skip=(parseInt(c.queryParams.currentPage)-1)*m.limit:m.skip=0,void 0!==u.fields&&(m.fields=u.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(g)),t.Logger.debug("APIService"," with options: "+JSON.stringify(m));var v=a.collectionsService.getCollectionByName(e.getCollectionName()),P=a.collectionFind(v)(g,m).fetch(),b=[];return r.each(P,function(e){b.push(e.toDocument("id"))}),JSON.stringify(b)})},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),g=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 h.instance().delete(this)},e.prototype.update=function(){return h.instance().update(this)},e.prototype.save=function(){return h.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"}},g([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)}}(),C=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),S=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)}}(),A=function(e){function t(){return null!==e&&e.apply(this,arguments)||this}return S(t,e),t}(C);e.initializeTypesystem=function(e){t.IOC.onRegister("typesystem",function(r){r.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 A)})},e.API=P,e.APIService=h,e.ApisCollection=A,Object.defineProperty(e,"__esModule",{value:!0})});
//# sourceMappingURL=index.umd.js.map
{
"name": "@smallstack/api-server",
"version": "0.1.1",
"version": "0.1.3",
"description": "API server functionality for smallstack projects",

@@ -35,6 +35,6 @@ "main": "dist/bundle/index.umd.js",

"dependencies": {
"@smallstack/api-common": "^0.1.0",
"@smallstack/api-common": "^0.1.1",
"@smallstack/common": "^0.1.26",
"@smallstack/data": "^0.3.2",
"@smallstack/user": "^0.1.9",
"@smallstack/user": "^0.1.10",
"lodash": "^4.17.4",

@@ -41,0 +41,0 @@ "pluralize": "^7.0.0",

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