@smallstack/api-server
Advanced tools
Comparing version 0.1.8 to 0.2.0
@@ -1,4 +0,4 @@ | ||
import { Collection, MongoDBAllowDenyOptions } from "@smallstack/data"; | ||
import { BaseCollection, Collection, MongoDBAllowDenyOptions } from "@smallstack/common"; | ||
import { API } from "../../models/API"; | ||
export declare class GeneratedApisCollection extends Collection<API> { | ||
export declare class GeneratedApisCollection extends BaseCollection<API> implements Collection<API> { | ||
static COLLECTION_NAME: string; | ||
@@ -10,5 +10,5 @@ static queries: {}; | ||
createMongoDBCollection(): void; | ||
protected applyAllowDenyRules(): void; | ||
protected getMongoDBCollectionAllowRules(): MongoDBAllowDenyOptions<API>; | ||
protected getMongoDBCollectionDenyRules(): MongoDBAllowDenyOptions<API>; | ||
applyAllowDenyRules(): void; | ||
getMongoDBCollectionAllowRules(): MongoDBAllowDenyOptions<API>; | ||
getMongoDBCollectionDenyRules(): MongoDBAllowDenyOptions<API>; | ||
getForeignCollection(typeName: string): string; | ||
@@ -15,0 +15,0 @@ getCollectionName(): string; |
import { DataBridge } from "@smallstack/common"; | ||
import { SmallstackModel, Type } from "@smallstack/data"; | ||
import { SmallstackModel, Type } from "@smallstack/common"; | ||
import { API } from "../../models/API"; | ||
@@ -4,0 +4,0 @@ export interface IAPI { |
@@ -5,3 +5,3 @@ /** | ||
import { DataBridge } from "@smallstack/common"; | ||
import { CollectionsService, Collection, SmallstackModel } from "@smallstack/data"; | ||
import { CollectionsService, Collection, SmallstackModel } from "@smallstack/common"; | ||
import { APIService } from "../../services/APIService"; | ||
@@ -8,0 +8,0 @@ export declare class GeneratedAPIService<ModelClass extends SmallstackModel> { |
import { APIPaginationParameters, APIPaginationResult } from "@smallstack/api-common"; | ||
import { Type, TypeSecuredMethod } from "@smallstack/data"; | ||
import { Type, TypeSecuredMethod } from "@smallstack/common"; | ||
import { User } from "@smallstack/user"; | ||
@@ -19,3 +19,3 @@ import { APIParams } from "../APIParams"; | ||
createSecuredMethodEndpoint(type: Type, method: TypeSecuredMethod): void; | ||
getUserId(params: APIParams): string; | ||
getUserId(params: APIParams): Promise<string>; | ||
getUser(params: APIParams): Promise<User>; | ||
@@ -32,6 +32,2 @@ getTotallyOpenCORSHeaders(): any; | ||
private getUniqueCallbackName(api); | ||
/** | ||
* proxy method to route to @smallstack/data/Collection/MongoDBCollection if new datalayer is being used | ||
*/ | ||
private collectionFind(collection); | ||
} |
@@ -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},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+"¤tPage="+(o+1)),o>1&&(a.paging.previous=t+i+"entriesPerPage="+r+"¤tPage="+(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})}); | ||
!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.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())})},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 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=t.Utils.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:"api/"+n,httpMethod:"POST",headers:this.getTotallyOpenCORSHeaders()});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=t.Utils.getApiURL(e.getCollectionName(),r);t.Logger.debug("APIService","Registering service query endpoint for "+e.getServiceName()+"->"+r+"->"+a);var c=v.fromDocument({path:"api/"+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,f,h,g,y,m,v,P,b;return d(this,function(d){switch(d.label){case 0:return i=e.getServiceQueryByName(r),[4,this.getUserId(a)];case 1:if(s=d.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,f=l;p<f.length;p++)switch((h=f[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=d.sent(),b=[],o.each(P,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/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+"¤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.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 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}]},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)}}(),b=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),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}(b);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 A)})},e.API=v,e.APIService=f,e.ApisCollection=A,Object.defineProperty(e,"__esModule",{value:!0})}); | ||
//# sourceMappingURL=index.umd.js.map |
{ | ||
"name": "@smallstack/api-server", | ||
"version": "0.1.8", | ||
"version": "0.2.0", | ||
"description": "API server functionality for smallstack projects", | ||
@@ -20,3 +20,3 @@ "main": "dist/bundle/index.umd.js", | ||
"type": "git", | ||
"url": "https://gitlab.com/smallstack/data" | ||
"url": "https://gitlab.com/smallstack/products/smallstack-api-server" | ||
}, | ||
@@ -36,11 +36,10 @@ "author": "smallstack GmbH", | ||
"dependencies": { | ||
"@smallstack/api-common": "^0.1.3", | ||
"@smallstack/common": "^0.1.27", | ||
"@smallstack/data": "^0.3.3", | ||
"@smallstack/user": "^0.1.10", | ||
"@smallstack/api-common": "^0.2.1", | ||
"@smallstack/common": "^0.6.2", | ||
"@smallstack/user": "^0.2.4", | ||
"lodash": "^4.17.4", | ||
"pluralize": "^7.0.0", | ||
"underscore": "1.8.3", | ||
"window-or-global": "1.0.1" | ||
"window-or-global": "^1.0.1" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
73647
7
202
+ Added@smallstack/common@0.6.14(transitive)
+ Added@smallstack/files@0.2.2(transitive)
+ Added@smallstack/i18n@0.3.3(transitive)
+ Added@smallstack/oms-common@0.2.3(transitive)
+ Added@smallstack/user@0.2.10(transitive)
+ Addedrxjs@5.5.12(transitive)
+ Addedsymbol-observable@1.0.1(transitive)
- Removed@smallstack/data@^0.3.3
- Removed@smallstack/common@0.1.28(transitive)
- Removed@smallstack/i18n@0.2.24(transitive)
- Removed@smallstack/user@0.1.26(transitive)
Updated@smallstack/common@^0.6.2
Updated@smallstack/user@^0.2.4
Updatedwindow-or-global@^1.0.1