@smallstack/api-server
Advanced tools
Comparing version 0.2.2 to 0.2.3
@@ -10,2 +10,3 @@ import { DataBridge } from "@smallstack/common"; | ||
headers?: any; | ||
type?: "query" | "method" | "manual"; | ||
} | ||
@@ -19,2 +20,3 @@ export declare class GeneratedAPI implements IAPI, SmallstackModel { | ||
headers: any; | ||
type: "query" | "method" | "manual"; | ||
private _hasSubDocuments; | ||
@@ -32,2 +34,7 @@ private _isStored; | ||
}; | ||
"type": { | ||
"QUERY": string; | ||
"METHOD": string; | ||
"MANUAL": string; | ||
}; | ||
}; | ||
@@ -34,0 +41,0 @@ static fromDocument<T>(doc: IAPI): T; |
@@ -19,3 +19,3 @@ import { APIPaginationParameters, APIPaginationResult } from "@smallstack/api-common"; | ||
createCallbackEndpoint(api: API, callback: (params: APIParams) => Promise<any>): void; | ||
createSecuredMethodEndpoint(type: Type, method: TypeSecuredMethod): void; | ||
createMethodEndpoint(type: Type, method: TypeSecuredMethod): void; | ||
getUserId(params: APIParams): Promise<string>; | ||
@@ -22,0 +22,0 @@ getUser(params: APIParams): Promise<User>; |
@@ -1,2 +0,2 @@ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@smallstack/common"),require("underscore"),require("@smallstack/user"),require("lodash")):"function"==typeof define&&define.amd?define(["exports","@smallstack/common","underscore","@smallstack/user","lodash"],t):t(e["@smallstack/api-server"]={},e["@smallstack/common"],e._,e["@smallstack/user"],e.lodash)}(this,function(e,t,o,r,n){"use strict";var i=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},a=function(e,t){if("object"==typeof Reflect&&"function"==typeof Reflect.metadata)return Reflect.metadata(e,t)},c=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)},i([t.Autowired(),a("design:type",Object)],e.prototype,"collectionsService",void 0),i([t.Autowired(),a("design:type",Object)],e.prototype,"dataBridge",void 0),e}(),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)}}(),u=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},l=function(e,t){if("object"==typeof Reflect&&"function"==typeof Reflect.metadata)return Reflect.metadata(e,t)},p=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},d=function(e){function i(){var r=e.call(this)||this;return r.registeredEndpoints=[],t.Logger.debug("APIService","initializing..."),o.each(r.typesystem.getAllTypes(),function(e){t.Logger.debug("APIService","Checking Type: "+e.getModelName()),e.service&&e.service.queries instanceof Array&&(o.each(e.service.queries,function(t){void 0!==t.api&&r.createServiceQueryEndpoint(e,t.name)}),e.service.methods instanceof Array&&o.each(e.service.methods,function(t){t.api&&r.createSecuredMethodEndpoint(e,t)}))}),r}return s(i,e),i.prototype.createCallbackEndpoint=function(e,o){t.Logger.debug("APIService","Registering callback endpoint : ",e.toDocument()),this.registerEndpoint(e,o)},i.prototype.createSecuredMethodEndpoint=function(e,o){var r=this,n=this.getApiURL(e.getCollectionName(),o.name);t.Logger.debug("APIService","Registering service secured method endpoint for "+e.getServiceName()+"->"+o.name+"->"+n);var i=v.fromDocument({path:n,httpMethod:"POST",headers:this.getTotallyOpenCORSHeaders()});this.registerEndpoint(i,function(t){return p(r,void 0,void 0,function(){var r,n;return f(this,function(i){switch(i.label){case 0:return[4,this.getUserId(t)];case 1:return r=i.sent(),n={},r&&(n.userId=r),[2,this.dataBridge.ddpCall.bind(n)(e.getCollectionName()+"-"+o.name,t.queryParams)]}})})})},i.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!")},i.prototype.getUser=function(e){var t=this;return new Promise(function(o){return p(t,void 0,void 0,function(){var t,r;return f(this,function(n){switch(n.label){case 0:return[4,this.getUserId(e)];case 1:return t=n.sent(),r=o,[4,this.userService.getUserById({id:t}).getModel(0)];case 2:return r.apply(void 0,[n.sent()]),[2]}})})})},i.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"}},i.prototype.createServiceQueryEndpoint=function(e,r){var i=this,a=this.getApiURL(e.getCollectionName(),r);t.Logger.debug("APIService","Registering service query endpoint for "+e.getServiceName()+"->"+r+"->"+a);var c=v.fromDocument({path:a,httpMethod:"GET",headers:this.getTotallyOpenCORSHeaders()});this.registerEndpoint(c,function(a){return new Promise(function(c,s){return p(i,void 0,void 0,function(){var i,s,u,l,p,d,h,g,y,m,v,P,A;return f(this,function(f){switch(f.label){case 0:return i=e.getServiceQueryByName(r),[4,this.getUserId(a)];case 1:if(s=f.sent(),void 0!==i.access)for(t.Logger.debug("APIService","Checking access for query: "+i.name),l=this.collectionAccessService.checkAccess(i,{userId:s},a.queryParams),t.Logger.debug("APIService"," -> Result",l),p=0,d=l;p<d.length;p++)switch((h=d[p]).type){case"boolean":if(!1===h.value)throw new Error("Denied by access rule!");break;case"selector":if(void 0!==u)throw new Error("Accessor Groups with more than one access method that changes the selector are not supported!");u=h.value;break;default:throw new Error("Unknown selector access type :"+h.type)}return t.Logger.debug("APIService"," -> AccessSelector",u),g={},void 0!==i.selector&&n.merge(g,i.selector),y=t.TypesystemUtils.extractSelectorParameters(i.selector),o.each(y,function(e){if(void 0===a.queryParams[e]||"string"!=typeof a.queryParams[e])throw new Error("Parameter '"+e+"' is missing!");g[e]=a.queryParams[e],t.Logger.debug("APIService","Replaced "+e+" with "+a.queryParams[e])}),void 0!==u&&n.merge(g,u),t.Logger.debug("APIService","Executing service query: "+e.getServiceName()+"->"+r,g),m={},a.queryParams.entriesPerPage?m.limit=parseInt(a.queryParams.entriesPerPage):m.limit=10,a.queryParams.currentPage?m.skip=(parseInt(a.queryParams.currentPage)-1)*m.limit:m.skip=0,void 0!==i.fields&&(m.fields=i.fields),o.each(a.queryParams,function(e,t){"["===e.charAt(0)&&(a.queryParams[t]=JSON.parse(e))}),t.Logger.debug("APIService"," with params: "+JSON.stringify(g)),t.Logger.debug("APIService"," with options: "+JSON.stringify(m)),v=this.collectionsService.getCollectionByName(e.getCollectionName()),[4,v.getMongoDBCollection().find(g,m).fetch()];case 2:return P=f.sent(),A=[],o.each(P,function(e){A.push(e.toDocument("id"))}),c(JSON.stringify(A)),[2]}})})})})},i.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}},i.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/"+this.getAPIPathPrefix())+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+"¤tPage="+(o+1)),o>1&&(a.paging.previous=t+i+"entriesPerPage="+r+"¤tPage="+(o-1)),a},i.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}},i.prototype.getAPIPathPrefix=function(){var e=process.env.SMALLSTACK_API_PREFIX;if(e){if("string"!=typeof e)throw new Error("SMALLSTACK_API_PREFIX has to be typof string!");if("/"!==e.charAt(0))throw new Error("SMALLSTACK_API_PREFIX has to start with a forward slash! (e.g. '/api')");return e.substr(1)}return"api"},i.prototype.getApiURL=function(e,o){return e+"/"+t.Utils.decapitalize(o.replace("get","")).toLowerCase()},i.prototype.getRegisteredAPIEndpoints=function(){return this.registeredEndpoints},i.prototype.getUniqueCallbackName=function(e){return e.path+e.httpMethod},u([t.Autowired(),l("design:type",t.Typesystem)],i.prototype,"typesystem",void 0),u([t.Autowired(),l("design:type",t.CollectionAccessService)],i.prototype,"collectionAccessService",void 0),u([t.Autowired(),l("design:type",Object)],i.prototype,"authService",void 0),u([t.Autowired(),l("design:type",r.UserService)],i.prototype,"userService",void 0),u([t.Autowired(),l("design:type",Object)],i.prototype,"configurationService",void 0),i}(c),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},g=function(e,t){if("object"==typeof Reflect&&"function"==typeof Reflect.metadata)return Reflect.metadata(e,t)},y=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 v.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!");o.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 d.instance().delete(this)},e.prototype.update=function(){return d.instance().update(this)},e.prototype.save=function(){return d.instance().save(this)},e.asType=function(){return t.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(),g("design:type",Object)],e.prototype,"dataBridge",void 0),e}(),m=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)}}(),v=function(e){function t(){return null!==e&&e.apply(this,arguments)||this}return m(t,e),t}(y),P=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 o(){var t=e.call(this)||this;return t.createMongoDBCollection(),t.applyAllowDenyRules(),t.createPublications(),t}return P(o,e),o.prototype.createPublications=function(){this.dataBridge.isServer()},o.prototype.createMongoDBCollection=function(){this.mongoDBCollection=this.collectionsService.createMongoDBCollection("apis",function(e){return v.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}(t.BaseCollection),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 t(){return null!==e&&e.apply(this,arguments)||this}return b(t,e),t}(A);e.initializeTypesystem=function(e){t.IOC.onRegister("typesystem",function(o){o.addType(t.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",v),void 0!==e&&void 0!==e.withCollections&&!0!==e.withCollections||t.IOC.register("apisCollection",new S)})},e.API=v,e.APIService=d,e.ApisCollection=S,Object.defineProperty(e,"__esModule",{value:!0})}); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@smallstack/common"),require("underscore"),require("@smallstack/user"),require("lodash")):"function"==typeof define&&define.amd?define(["exports","@smallstack/common","underscore","@smallstack/user","lodash"],t):t(e["@smallstack/api-server"]={},e["@smallstack/common"],e._,e["@smallstack/user"],e.lodash)}(this,function(e,t,o,r,n){"use strict";var i=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},a=function(e,t){if("object"==typeof Reflect&&"function"==typeof Reflect.metadata)return Reflect.metadata(e,t)},c=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,type:t.type}})},e.prototype.delete=function(e){return this.getCollection().getMongoDBCollection().remove(e.id)},i([t.Autowired(),a("design:type",Object)],e.prototype,"collectionsService",void 0),i([t.Autowired(),a("design:type",Object)],e.prototype,"dataBridge",void 0),e}(),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)}}(),u=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},l=function(e,t){if("object"==typeof Reflect&&"function"==typeof Reflect.metadata)return Reflect.metadata(e,t)},p=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())})},d=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},f=function(e){function i(){var r=e.call(this)||this;return r.registeredEndpoints=[],t.Logger.debug("APIService","initializing..."),o.each(r.typesystem.getAllTypes(),function(e){t.Logger.debug("APIService","Checking Type: "+e.getModelName()),e.service&&e.service.queries instanceof Array&&(o.each(e.service.queries,function(t){void 0!==t.api&&r.createServiceQueryEndpoint(e,t.name)}),e.service.methods instanceof Array&&o.each(e.service.methods,function(t){t.api&&r.createMethodEndpoint(e,t)}))}),r}return s(i,e),i.prototype.createCallbackEndpoint=function(e,o){t.Logger.debug("APIService","Registering callback endpoint : ",e.toDocument()),this.registerEndpoint(e,o)},i.prototype.createMethodEndpoint=function(e,o){var r=this,n=this.getApiURL(e.getCollectionName(),o.name);t.Logger.debug("APIService","Registering service secured method endpoint for "+e.getServiceName()+"->"+o.name+"->"+n);var i=v.fromDocument({path:n,httpMethod:"POST",headers:this.getTotallyOpenCORSHeaders(),type:"method"});this.registerEndpoint(i,function(t){return p(r,void 0,void 0,function(){var r,n;return d(this,function(i){switch(i.label){case 0:return[4,this.getUserId(t)];case 1:return r=i.sent(),n={},r&&(n.userId=r),[2,this.dataBridge.ddpCall.bind(n)(e.getCollectionName()+"-"+o.name,t.queryParams)]}})})})},i.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!")},i.prototype.getUser=function(e){var t=this;return new Promise(function(o){return p(t,void 0,void 0,function(){var t,r;return d(this,function(n){switch(n.label){case 0:return[4,this.getUserId(e)];case 1:return t=n.sent(),r=o,[4,this.userService.getUserById({id:t}).getModel(0)];case 2:return r.apply(void 0,[n.sent()]),[2]}})})})},i.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"}},i.prototype.createServiceQueryEndpoint=function(e,r){var i=this,a=this.getApiURL(e.getCollectionName(),r);t.Logger.debug("APIService","Registering service query endpoint for "+e.getServiceName()+"->"+r+"->"+a);var c=v.fromDocument({path:a,httpMethod:"GET",headers:this.getTotallyOpenCORSHeaders(),type:"query"});this.registerEndpoint(c,function(a){return new Promise(function(c,s){return p(i,void 0,void 0,function(){var i,u,l,p,f,h,y,g,m,v,P,A,b;return d(this,function(d){switch(d.label){case 0:return i=e.getServiceQueryByName(r),[4,this.getUserId(a)];case 1:if(u=d.sent(),void 0!==i.access)for(t.Logger.debug("APIService","Checking access for query: "+i.name),p=this.collectionAccessService.checkAccess(i,{userId:u},a.queryParams),t.Logger.debug("APIService"," -> Result",p),f=0,h=p;f<h.length;f++)switch((y=h[f]).type){case"boolean":if(!1===y.value)return s("Denied by access rule!"),[2];break;case"selector":if(void 0!==l)return s("Accessor Groups with more than one access method that changes the selector are not supported!"),[2];l=y.value;break;default:return s("Unknown selector access type :"+y.type),[2]}return t.Logger.debug("APIService"," -> AccessSelector",l),g={},void 0!==i.selector&&n.merge(g,i.selector),m=t.TypesystemUtils.extractSelectorParameters(i.selector),o.each(m,function(e){void 0!==a.queryParams[e]&&"string"==typeof a.queryParams[e]?(g[e]=a.queryParams[e],t.Logger.debug("APIService","Replaced "+e+" with "+a.queryParams[e])):s("Parameter '"+e+"' is missing!")}),void 0!==l&&n.merge(g,l),t.Logger.debug("APIService","Executing service query: "+e.getServiceName()+"->"+r,g),v={},a.queryParams.entriesPerPage?v.limit=parseInt(a.queryParams.entriesPerPage):v.limit=10,a.queryParams.currentPage?v.skip=(parseInt(a.queryParams.currentPage)-1)*v.limit:v.skip=0,void 0!==i.fields&&(v.fields=i.fields),o.each(a.queryParams,function(e,t){"["===e.charAt(0)&&(a.queryParams[t]=JSON.parse(e))}),t.Logger.debug("APIService"," with params: "+JSON.stringify(g)),t.Logger.debug("APIService"," with options: "+JSON.stringify(v)),P=this.collectionsService.getCollectionByName(e.getCollectionName()),[4,P.getMongoDBCollection().find(g,v).fetch()];case 2:return A=d.sent(),b=[],o.each(A,function(e){b.push(e.toDocument("id"))}),c(JSON.stringify(b)),[2]}})})})})},i.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}},i.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/"+this.getAPIPathPrefix())+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+"¤tPage="+(o+1)),o>1&&(a.paging.previous=t+i+"entriesPerPage="+r+"¤tPage="+(o-1)),a},i.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}},i.prototype.getAPIPathPrefix=function(){var e=process.env.SMALLSTACK_API_PREFIX;if(e){if("string"!=typeof e)throw new Error("SMALLSTACK_API_PREFIX has to be typof string!");if("/"!==e.charAt(0))throw new Error("SMALLSTACK_API_PREFIX has to start with a forward slash! (e.g. '/api')");return e.substr(1)}return"api"},i.prototype.getApiURL=function(e,o){return e+"/"+t.Utils.decapitalize(o.replace("get","")).toLowerCase()},i.prototype.getRegisteredAPIEndpoints=function(){return this.registeredEndpoints},i.prototype.getUniqueCallbackName=function(e){return e.path+e.httpMethod},u([t.Autowired(),l("design:type",t.Typesystem)],i.prototype,"typesystem",void 0),u([t.Autowired(),l("design:type",t.CollectionAccessService)],i.prototype,"collectionAccessService",void 0),u([t.Autowired(),l("design:type",Object)],i.prototype,"authService",void 0),u([t.Autowired(),l("design:type",r.UserService)],i.prototype,"userService",void 0),u([t.Autowired(),l("design:type",Object)],i.prototype,"configurationService",void 0),i}(c),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)},g=function(){function e(){this.contentType="application/json",this.httpMethod="GET",this.type="manual",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),void 0!==e.type&&(t.type=e.type),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,t.type=this.type,this.id&&(t[e]=this.id),JSON.parse(JSON.stringify(t))},e.prototype.clone=function(){return v.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!");o.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 f.instance().delete(this)},e.prototype.update=function(){return f.instance().update(this)},e.prototype.save=function(){return f.instance().save(this)},e.asType=function(){return t.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},{name:"type",type:"string",allowedValues:["query","method","manual"],optional:!0,defaultValue:"manual"}]},packageName:"@smallstack/api-server"})},e.enums={httpMethod:{GET:"GET",POST:"POST",HEAD:"HEAD",PUT:"PUT",DELETE:"DELETE",OPTIONS:"OPTIONS"},type:{QUERY:"query",METHOD:"method",MANUAL:"manual"}},h([t.Autowired(),y("design:type",Object)],e.prototype,"dataBridge",void 0),e}(),m=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)}}(),v=function(e){function t(){return null!==e&&e.apply(this,arguments)||this}return m(t,e),t}(g),P=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 o(){var t=e.call(this)||this;return t.createMongoDBCollection(),t.applyAllowDenyRules(),t.createPublications(),t}return P(o,e),o.prototype.createPublications=function(){this.dataBridge.isServer()},o.prototype.createMongoDBCollection=function(){this.mongoDBCollection=this.collectionsService.createMongoDBCollection("apis",function(e){return v.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}(t.BaseCollection),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 t(){return null!==e&&e.apply(this,arguments)||this}return b(t,e),t}(A);e.initializeTypesystem=function(e){t.IOC.onRegister("typesystem",function(o){o.addType(t.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},{name:"type",type:"string",allowedValues:["query","method","manual"],optional:!0,defaultValue:"manual"}]},packageName:"@smallstack/api-server"})),t.IOC.register("API",v),void 0!==e&&void 0!==e.withCollections&&!0!==e.withCollections||t.IOC.register("apisCollection",new S)})},e.API=v,e.APIService=f,e.ApisCollection=S,Object.defineProperty(e,"__esModule",{value:!0})}); | ||
//# sourceMappingURL=index.umd.js.map |
{ | ||
"name": "@smallstack/api-server", | ||
"version": "0.2.2", | ||
"version": "0.2.3", | ||
"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
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
78026
222