UNPKG

ngx-sails-socketio

Version:

An Angular module for connecting SailsJs backend through SocketIO.

1 lines 17.6 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("sails.io.js"),require("socket.io-client"),require("@angular/core"),require("rxjs/Observable"),require("json-object-mapper"),require("rxjs/add/operator/map")):"function"==typeof define&&define.amd?define(["exports","sails.io.js","socket.io-client","@angular/core","rxjs/Observable","json-object-mapper","rxjs/add/operator/map"],e):e((t.ng=t.ng||{},t.ng.amazing={}),null,null,t.ng.core,t.Rx,null)}(this,function(n,c,h,t,e,r){"use strict";c=c&&c.hasOwnProperty("default")?c.default:c;var s=function(){function t(t){this.JWR=t}return t.prototype.isOk=function(){return 200<=this.getStatusCode()&&this.getStatusCode()<300},t.prototype.isCreated=function(){return 201===this.getStatusCode()},t.prototype.isUnauthorized=function(){return 401===this.getStatusCode()},t.prototype.isForbidden=function(){return 403===this.getStatusCode()},t.prototype.isNotFound=function(){return 404===this.getStatusCode()},t.prototype.isBadRequest=function(){return 400===this.getStatusCode()},t.prototype.isError=function(){return this.isClientError()||this.isServerError()},t.prototype.isClientError=function(){return 400<=this.getStatusCode()&&!this.isServerError()},t.prototype.isServerError=function(){return 500<=this.getStatusCode()},t.prototype.getCode=function(){return this.getBody().code},t.prototype.getData=function(){return this.getBody().data||this.getBody()},t.prototype.getMessage=function(){return this.getBody().message},t.prototype.getBody=function(){return this.JWR.body},t.prototype.getHeaders=function(){return this.JWR.headers},t.prototype.getError=function(){return this.JWR.error},t.prototype.getStatusCode=function(){return this.JWR.statusCode},t.prototype.pipe=function(){return this.JWR.pipe()},t.prototype.toPOJO=function(){return this.JWR.toPOJO()},t.prototype.toString=function(){return this.JWR.toString()},t}(),p=function(t){this.url="ws://localhost:1337",this.query="__sails_io_sdk_version=0.11.0&__sails_io_sdk_platform=windows&__sails_io_sdk_language=javascript",this.reconnection=!0,this.autoConnect=!0,this.transports=["websocket"],this.useCORSRouteToGetCookie=!1,this.headers={},this.timeout=2e4,this.path="",this.prefix="",Object.assign(this,t)},i=function(){function t(t,e){this.next=t,this.interceptor=e}return t.prototype.handle=function(t){return this.interceptor.intercept(t,this.next)},t}();function o(t){return void 0===t}function a(t){return"string"==typeof t}function u(t){return!Object.keys(t).length}function f(t){return t instanceof Object}var d={CREATED:"created",UPDATED:"updated",ADDED:"added",DESTROYED:"destroyed",REMOVED:"removed"},l=function(){function t(t){this.JWR=t}return t.prototype.isCreated=function(){return this.getVerb()===d.CREATED},t.prototype.isUpdated=function(){return this.getVerb()===d.UPDATED},t.prototype.isDestroyed=function(){return this.getVerb()===d.DESTROYED},t.prototype.isAdded=function(){return this.getVerb()===d.ADDED},t.prototype.isRemoved=function(){return this.getVerb()===d.REMOVED},t.prototype.getVerb=function(){return this.JWR.verb},t.prototype.getData=function(){return this.JWR.data},t.prototype.getId=function(){return this.JWR.id},t}(),y=new t.InjectionToken("SAILS_OPTIONS"),E=new t.InjectionToken("SAILS_INTERCEPTORS"),g={DEV:"development",PROD:"production"},C={ERROR:"error",CONNECT:"connect",RECONNECT:"reconnect",CONNECTING:"connecting",DISCONNECT:"disconnect",RECONNECTING:"reconnecting",CONNECT_ERROR:"connect_error",CONNECT_TIMEOUT:"connect_timeout"},O=function(){function t(t,e,r){void 0===r&&(r=[]);var i=this;this.injector=t,this.Interceptors=r,this.Listeners=((a={})[C.CONNECT]=[],a[C.CONNECT_ERROR]=[],a[C.CONNECT_TIMEOUT]=[],a[C.CONNECTING]=[],a[C.RECONNECT]=[],a[C.RECONNECTING]=[],a[C.DISCONNECT]=[],a[C.ERROR]=[],a);var n=c(h),o=n.socket,s=function(t){return function(e){i.Listeners[t].forEach(function(t){return t(e)})}};o.on(C.CONNECT,s(C.CONNECT)),o.on(C.CONNECT_ERROR,s(C.CONNECT_ERROR)),o.on(C.CONNECT_TIMEOUT,s(C.CONNECT_TIMEOUT)),o.on(C.CONNECTING,s(C.CONNECTING)),o.on(C.RECONNECT,s(C.RECONNECT)),o.on(C.ERROR,s(C.ERROR)),o.on(C.RECONNECTING,s(C.RECONNECTING)),o.on(C.DISCONNECT,s(C.DISCONNECT));var a,u=new p(e);Object.assign(n.sails,u),this.socket=o,this.Config=u}return Object.defineProperty(t.prototype,"socket",{get:function(){return this.Socket},set:function(t){this.Socket=t},enumerable:!0,configurable:!0}),t.prototype.connect=function(){return this.connected()||this.socket._connect(),this},t.prototype.connected=function(){return this.socket.isConnected()},t.prototype.isConnecting=function(){return this.socket.isConnecting(),this},t.prototype.disconnect=function(){return this.connected()&&this.socket.disconnect(),this},t.prototype.addEventListener=function(t,e){if(!this.Listeners[t])throw new Error("The event ["+t+"] has not yet been supported by this library.");return this.Listeners[t].push(e),this},t.prototype.removeEventListener=function(t,e){if(!this.Listeners[t])throw new Error("The event ["+t+"] has not yet been supported by this library.");return-1<this.Listeners[t].indexOf(e)&&this.Listeners[t].splice(this.Listeners[t].indexOf(e),1),this},t.prototype.on=function(i){var n=this;return new e.Observable(function(r){return n.socket.on(i,function(t){if(t){var e=new l(t);r.next(e),n.debugReqRes(i,e)}}),function(){return n.socket.off(i,function(){})}})},t.prototype.off=function(i){var n=this;return new e.Observable(function(r){n.socket.off(i,function(t){if(t){var e=new l(t);r.next(e),n.debugReqRes(i,e)}return function(){}})})},t.prototype.request=function(t){var e=t.clone({url:this.Config.prefix+t.url});return this.intercept(e)},t.prototype.intercept=function(t,e){var r=this;return void 0===e&&(e=this),this.Interceptors.reduceRight(function(t,e){return new i(t,r.injector.get(e))},e).handle(t)},t.prototype.handle=function(n){var o=this;return new e.Observable(function(i){o.socket.request(n.serialize(),function(t,e){var r=new s(e);r.isError()?i.error(r.getError()):i.next(r.getBody()),i.complete(),o.debugReqRes(n,r)})})},t.prototype.debugReqRes=function(t,e){this.Config.environment===g.DEV&&(console.groupCollapsed("[SailsSocketIO] > Debug Output"),a(t)?console.log(t):console.dir(t),console.dir(e),console.groupEnd())},t}();function m(t,e){return r.ObjectMapper.deserialize(t,e)}function b(e){return void 0===e&&(e=""),function(t){t.prototype.getEndPoint=function(){return e}}}O.ctorParameters=function(){return[{type:t.Injector,decorators:[{type:t.Inject,args:[t.Injector]}]},{type:void 0,decorators:[{type:t.Inject,args:[y]}]},{type:Array,decorators:[{type:t.Inject,args:[E]}]}]};var R,S=function(t,e,r,i){var n,o=arguments.length,s=o<3?e:null===i?i=Object.getOwnPropertyDescriptor(e,r):i;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)s=Reflect.decorate(t,e,r,i);else for(var a=t.length-1;0<=a;a--)(n=t[a])&&(s=(o<3?n(s):3<o?n(e,r,s):n(e,r))||s);return 3<o&&s&&Object.defineProperty(e,r,s),s},w=function(t,e){if("object"==typeof Reflect&&"function"==typeof Reflect.metadata)return Reflect.metadata(t,e)};n.SailsModel=R=function(){function t(){this.id=""}return t.prototype.getEndPoint=function(){return this.getEndPoint()},t.serialize=function(t){return function(t){for(var e in t){var r=t[e];null!==r&&"function"!=typeof r||delete t[e],r&&r instanceof R&&null!==r.id&&(t[e]=r.id),r&&r instanceof R&&(t[e]=R.serialize(r)),r&&r instanceof Array&&(t[e]=r.map(function(t){return t instanceof R?R.serialize(t):t}))}return t}(Object.assign({},t))},t.unserialize=function(e,t){var r=function(t){return m(e,t)};if(Array.isArray(t))return t.map(r);if(f(t))return r(t);throw new Error("SailsModel.unserialize requires a data parameter of either a Literal Object or an Array of Literal Objects")},t}(),S([r.JsonProperty(),w("design:type",String)],n.SailsModel.prototype,"id",void 0),S([r.JsonProperty({type:Date}),w("design:type",Date)],n.SailsModel.prototype,"createdAt",void 0),S([r.JsonProperty({type:Date}),w("design:type",Date)],n.SailsModel.prototype,"updatedAt",void 0),n.SailsModel=R=S([b()],n.SailsModel);var v=function(){function t(t){var e=t.url,r=t.method,i=t.params,n=t.headers;this.options={url:e,method:r,params:this.toMap(i),headers:this.toMap(n)}}return t.prototype.clone=function(t){for(var e in t)this.options.hasOwnProperty(e)&&t[e]||delete t[e];return Object.assign(this.options,t),this},t.prototype.toMap=function(e){void 0===e&&(e={});var r=new Map;return Object.keys(e).forEach(function(t){return r.set(t,e[t])}),r},t.prototype.toObject=function(t){void 0===t&&(t=new Map);var r={};return t.forEach(function(t,e){return r[e]=t}),r},Object.defineProperty(t.prototype,"method",{get:function(){return this.options.method},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"url",{get:function(){return this.options.url},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"params",{get:function(){return this.options.params},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"headers",{get:function(){return this.options.headers},enumerable:!0,configurable:!0}),t.prototype.serialize=function(){return{url:this.url,method:this.method,params:this.toObject(this.params),headers:this.toObject(this.headers)}},t}(),N=function(){function t(t){void 0===t&&(t=""),this.query=t}return t.prototype.append=function(t){return"string"==typeof t&&(this.query.length&&(this.query+="&"),this.query+=t),this},t.prototype.toString=function(){return this.query&&"?"!==this.query.charAt(0)&&(this.query="?"+this.query),this.query},t}(),T={GET:"get",POST:"post",PUT:"put",DELETE:"delete",PATCH:"patch"},q=function(){function t(t){this.sails=t,this.parameters=[]}return t.prototype.get=function(t,e){return this._request(T.GET,t,e)},t.prototype.post=function(t,e,r){return this._request(T.POST,t,e,r)},t.prototype.put=function(t,e,r){return this._request(T.PUT,t,e,r)},t.prototype.delete=function(t,e){return this._request(T.DELETE,t,e)},t.prototype.patch=function(t,e){return this._request(T.PATCH,t,e)},t.prototype._request=function(t,e,r,i){var n=new v({method:t,url:this.buildQuery(e),params:r,headers:i});return this.sails.request(n)},t.prototype.addParam=function(t,e){return e.toString().length&&this.parameters.push(t+"="+e),this},t.prototype.getParams=function(){return this.parameters.join("&")},t.prototype.buildQuery=function(t){return t+new N(this.getParams())},t}(),D=function(){function t(){this.criteria={},this.orCriteria={}}return t.prototype.or=function(){return o(this.orCriteria.or)?this.orCriteria.or=[this.criteria]:Array.isArray(this.orCriteria.or)?this.orCriteria.or.push(this.criteria):f(this.criteria.or)&&(this.orCriteria.or=[this.criteria]),this.criteria={},this},t.prototype.whereNotEqualTo=function(t,e){if(o(this.criteria[t])||a(this.criteria[t]))return this.criteria[t]={"!":e},this;if(o(this.criteria[t]["!"]))return this.criteria[t]["!"]=e,this;throw new Error("DuplicateError: ! clause, use whereNotIn instead")},t.prototype.whereLike=function(t,e){if(o(this.criteria[t])||a(this.criteria[t]))return this.criteria[t]={like:e},this;if(o(this.criteria[t].like))return this.criteria[t].like=e,this;throw new Error("DuplicateError: like clause has already been used in this query")},t.prototype.whereEqualTo=function(t,e){if(o(this.criteria[t])||a(this.criteria[t]))return this.criteria[t]=e,this;throw new Error("DuplicateError: contains clause has already been used in this query")},t.prototype.whereContains=function(t,e){if(o(this.criteria[t])||a(this.criteria[t]))return this.criteria[t]={contains:e},this;if(o(this.criteria[t].contains))return this.criteria[t].contains=e,this;throw new Error("DuplicateError: contains clause has already been used in this query")},t.prototype.whereIn=function(t){for(var e=[],r=1;r<arguments.length;r++)e[r-1]=arguments[r];if(o(this.criteria[t])||a(this.criteria[t]))return this.criteria[t]=e,this;throw new Error("DuplicateError: contains clause has already been used in this query")},t.prototype.whereStartsWith=function(t,e){if(a(this.criteria[t]))return this.criteria[t]={startsWith:e},this;if(o(this.criteria[t].startsWith))return this.criteria[t].startsWith=e,this;throw new Error("DuplicateError: startsWith clause has already been used in this query")},t.prototype.whereEndsWith=function(t,e){if(o(this.criteria[t])||a(this.criteria[t]))return this.criteria[t]={endsWith:e},this;if(o(this.criteria[t].endsWith))return this.criteria[t].endsWith=e,this;throw new Error("DuplicateError: endsWith clause has already been used in this query")},t.prototype.whereNotIn=function(t,e){return o(this.criteria[t])||a(this.criteria[t])?this.criteria[t]={"!":[e]}:o(this.criteria[t]["!"])?this.criteria[t]["!"]=[e]:Array.isArray(this.criteria[t]["!"])?this.criteria[t]["!"].push(e):this.criteria[t]["!"]=[this.criteria[t]["!"],e],this},t.prototype.whereLessThan=function(t,e){if(o(this.criteria[t])||a(this.criteria[t]))return this.criteria[t]={"<":e},this;if(o(this.criteria[t]["<"]))return this.criteria[t]["<"]=e,this;throw new Error("DuplicateError: < clause has already been used in this query")},t.prototype.whereLessThanOrEqualTo=function(t,e){if(o(this.criteria[t])||a(this.criteria[t]))return this.criteria[t]={"<=":e},this;if(o(this.criteria[t]["<="]))return this.criteria[t]["<="]=e,this;throw new Error("DuplicateError: <= clause has already been used in this query")},t.prototype.whereGreaterThan=function(t,e){if(o(this.criteria[t])||a(this.criteria[t]))return this.criteria[t]={">":e},this;if(o(this.criteria[t][">"]))return this.criteria[t][">"]=e,this;throw new Error("DuplicateError: > clause has already been used in this query")},t.prototype.whereGreaterThanOrEqualTo=function(t,e){if(o(this.criteria[t])||a(this.criteria[t]))return this.criteria[t]={">=":e},this;if(o(this.criteria[t][">="]))return this.criteria[t][">="]=e,this;throw new Error("DuplicateError: >= clause has already been used in this query")},t.prototype.build=function(){if(u(this.criteria))return"";var t=function(t){return JSON.stringify(t)};return u(this.orCriteria)?t(this.criteria):(Array.isArray(this.orCriteria.or)&&this.orCriteria.or.push(this.criteria),t(this.orCriteria))},t.prototype.toString=function(){return this.build()},t}(),P=function(){function t(t,e){this.modelClass=e,this.errorMsg="[SailsSocketIO]: the data is not an instance of "+this.modelClass.name+".\n You could SailsModel.unserialize("+this.modelClass.name+", data) as "+this.modelClass.name+"[] (Array of Models), Or\n SailsModel.unserialize("+this.modelClass.name+", data) as "+this.modelClass.name+" (Single Models)\n after fetching the data with SailsRequest.",this.request=new q(t),this.model=new e}return t.prototype.find=function(){var e=this;return this.request.addParam("where",this.getRequestCriteria()),this.request.get("/"+this.model.getEndPoint()).map(function(t){if(t.isOk())return n.SailsModel.unserialize(e.modelClass,t.getData());throw t})},t.prototype.findById=function(t){var e=this;return this.request.addParam("where",this.getRequestCriteria()),this.request.get("/"+this.model.getEndPoint()+"/"+t).map(function(t){if(t.isOk())return n.SailsModel.unserialize(e.modelClass,t.getData());throw t})},t.prototype.save=function(t){var e=this;if(!(t instanceof this.modelClass))throw new TypeError(this.errorMsg);var r=n.SailsModel.serialize(t),i="/"+t.getEndPoint();return null===t.id?this.request.post(i,r).map(function(t){if(t.isOk())return n.SailsModel.unserialize(e.modelClass,t.getData());throw t}):this.request.put(i.concat("/",t.id),r).map(function(t){if(t.isOk())return n.SailsModel.unserialize(e.modelClass,t.getData());throw t})},t.prototype.update=function(t,e){var r=this;e.createdAt&&delete e.createdAt,e.updatedAt&&delete e.updatedAt;var i=e instanceof n.SailsModel?n.SailsModel.serialize(e):Object.assign({},e);return this.request.put("/"+this.model.getEndPoint()+"/"+t,i).map(function(t){if(t.isOk())return n.SailsModel.unserialize(r.modelClass,t.getData());throw t})},t.prototype.remove=function(t){var e=this;return this.request.delete("/"+this.model.getEndPoint()+"/"+t).map(function(t){if(t.isOk())return n.SailsModel.unserialize(e.modelClass,t.getData());throw t})},t.prototype.setLimit=function(t){return this.request.addParam("limit",t),this},t.prototype.setSort=function(t){return this.request.addParam("sort",t),this},t.prototype.setSkip=function(t){return this.request.addParam("skip",t),this},t.prototype.setPopulation=function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];return this.request.addParam("populate","["+t.join(",")+"]"),this},t.prototype.setRequestCriteria=function(t){return this.criteria=t,this},t.prototype.getRequestCriteria=function(){return this.criteria||new D},t}(),j=function(){function r(){}return r.forRoot=function(t,e){return void 0===e&&(e=[]),{ngModule:r,providers:[O].concat(e,[{provide:y,useValue:t},{provide:E,useValue:e}])}},r}();j.decorators=[{type:t.NgModule}],j.ctorParameters=function(){return[]};var I=function(){function t(t){this.sails=t}return t.prototype.on=function(t){return this.sails.on(t.toLowerCase())},t.prototype.off=function(t){return this.sails.off(t.toLowerCase())},t}();n.Ignore=r.JsonIgnore,n.Property=r.JsonProperty,n.SAILS_OPTIONS=y,n.SAILS_INTERCEPTORS=E,n.SailsEnvironment=g,n.SailsListener=C,n.Sails=O,n.SailsQuery=P,n.SailsModule=j,n.Method=T,n.SailsRequest=q,n.SailsSubscription=I,n.SailsResponse=s,n.Verb=d,n.SailsEvent=l,n.unserialize=m,n.serialize=function(t){return r.ObjectMapper.serialize(t)},n.SailsRequestOptions=v,n.RequestCriteria=D,n.Endpoint=b,n.SailsInterceptorHandler=i,Object.defineProperty(n,"__esModule",{value:!0})});