ng-http-sw-proxy
Advanced tools
Comparing version 1.2.8 to 2.0.0
(function (global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@angular/http'), require('@angular/core'), require('@angular/common'), require('rxjs'), require('idb')) : | ||
typeof define === 'function' && define.amd ? define(['exports', '@angular/http', '@angular/core', '@angular/common', 'rxjs', 'idb'], factory) : | ||
(factory((global.ng = global.ng || {}, global.ng['http-sw-proxy'] = global.ng['http-sw-proxy'] || {}),global.ng.http,global.ng.core,global.ng.common,global.Rx,global.idb)); | ||
}(this, (function (exports,_angular_http,_angular_core,_angular_common,rxjs,idb) { 'use strict'; | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('idb'), require('@angular/http'), require('@angular/core'), require('rxjs'), require('@angular/common')) : | ||
typeof define === 'function' && define.amd ? define(['exports', 'idb', '@angular/http', '@angular/core', 'rxjs', '@angular/common'], factory) : | ||
(factory((global.ng = global.ng || {}, global.ng['http-sw-proxy'] = global.ng['http-sw-proxy'] || {}),global.idb,global.ng.http,global.ng.core,global.Rx,global.ng.common)); | ||
}(this, (function (exports,idb,_angular_http,_angular_core,rxjs,_angular_common) { 'use strict'; | ||
@@ -43,102 +43,179 @@ var idb__default = idb['default']; | ||
var methods = { | ||
DELETE: "DELETE", | ||
GET: "GET", | ||
HEAD: "HEAD", | ||
OPTIONS: "OPTIONS", | ||
PATCH: "PATCH", | ||
POST: "POST", | ||
PUT: "PUT", | ||
REQUEST: "REQUEST" | ||
}; | ||
var HttpSwProxy = (function () { | ||
function HttpSwProxy(platformId, http, store, appref) { | ||
function HttpSwProxyPlugin() { | ||
return function (worker) { return new HttpSwProxyPluginImpl(worker); }; | ||
} | ||
var HttpSwProxyPluginImpl = (function () { | ||
function HttpSwProxyPluginImpl(worker) { | ||
var _this = this; | ||
this.http = http; | ||
this.store = store; | ||
this.appref = appref; | ||
this.obsRegister = new Map(); | ||
this.isConnected = false; | ||
this.platformId = platformId; //Intellij type checking workaround. | ||
this.hasNetworkConnection().subscribe(function (connected) { return _this.isConnected = connected; }); | ||
this.worker = worker; | ||
this.promiseRegister = new Map(); | ||
this.store = new Store(); | ||
self.addEventListener('sync', function (event) { | ||
event.waitUntil(_this.store.requests('readonly').then(function (requests) { return requests.getAll(); }).then(function (requestes) { | ||
return Promise.all(requestes.map(function (request) { | ||
var promiseResolve; | ||
_this.promiseRegister.set(request.id, new Promise(function (resolve) { return promiseResolve = resolve; })); | ||
return fetch(request.url, { | ||
method: _this.getMethod(request.method), | ||
body: _this.getBody(request._body), | ||
cache: "no-store", | ||
headers: _this.getHeaders(request) | ||
}).then(function (response) { | ||
_this.store.requests('readwrite').then(function (requests) { return requests.delete(request.id); }); | ||
var respToStore = { | ||
type: response.type, | ||
bodyUsed: response.bodyUsed, | ||
body: {}, | ||
headers: new Map(), | ||
ok: response.ok, | ||
status: response.status, | ||
statusText: response.statusText, | ||
url: response.url | ||
}; | ||
var bodyPromise; | ||
if (response.headers.has('content-type')) { | ||
if (response.headers.get('content-type').indexOf('json') > -1) { | ||
bodyPromise = response.json(); | ||
} | ||
else if (response.headers.get('content-type').indexOf('text') > -1) { | ||
bodyPromise = response.text(); | ||
} | ||
else if (response.headers.get('content-type').indexOf('application/x-www-form-urlencoded') > -1) { | ||
bodyPromise = response.formData(); | ||
} | ||
else if (response.headers.get('content-type').indexOf('application/octet-stream') > -1) { | ||
bodyPromise = response.blob(); | ||
} | ||
else { | ||
bodyPromise = response.arrayBuffer(); | ||
} | ||
} | ||
response.headers.forEach(function (key, value) { | ||
respToStore.headers.set(key, value); | ||
}); | ||
return bodyPromise.then(function (body) { | ||
respToStore.body = body; | ||
_this.store.responses('readwrite').then(function (responses) { | ||
responses.put({ id: request.id, response: respToStore }); | ||
promiseResolve(true); | ||
}); | ||
return _this.store.closeTransaction(); | ||
}); | ||
}); | ||
})).catch(function (err) { | ||
console.error(err); | ||
}); | ||
})); | ||
}); | ||
self.addEventListener('message', function (event) { | ||
if (typeof event.data == 'string' && event.data.indexOf("give me response ") > -1) { | ||
var key_1 = event.data.substr(17); | ||
var interval_1; | ||
interval_1 = setInterval(function () { | ||
if (_this.promiseRegister.has(parseInt(key_1))) { | ||
clearInterval(interval_1); | ||
_this.promiseRegister.get(parseInt(key_1)).then(function (value) { | ||
event.ports[0].postMessage("response is waiting: " + key_1); | ||
_this.promiseRegister.delete(parseInt(key_1)); | ||
}); | ||
} | ||
}, 1); | ||
} | ||
}); | ||
} | ||
HttpSwProxy.prototype.get = function (url, options) { | ||
return this.resolveRequest({ method: methods.GET, url: url, options: options }); | ||
HttpSwProxyPluginImpl.prototype.setup = function (ops) { }; | ||
HttpSwProxyPluginImpl.prototype.getMethod = function (id) { | ||
switch (id) { | ||
case 0: return 'GET'; | ||
case 1: return 'POST'; | ||
case 2: return 'PUT'; | ||
case 3: return 'DELETE'; | ||
case 4: return 'OPTIONS'; | ||
case 5: return 'HEAD'; | ||
case 6: return 'PATCH'; | ||
} | ||
}; | ||
HttpSwProxy.prototype.post = function (url, body, options) { | ||
return this.resolveRequest({ method: methods.POST, url: url, options: options, body: body }); | ||
HttpSwProxyPluginImpl.prototype.getContentType = function (body) { | ||
if (body instanceof URLSearchParams) | ||
return 'application/x-www-form-urlencoded'; | ||
else if (body instanceof FormData) | ||
return 'multipart/form-data'; | ||
else if (body instanceof Blob) | ||
return 'application/octet-stream'; | ||
else if (body && typeof body === 'object') | ||
return 'application/json'; | ||
else | ||
return 'text/plain'; | ||
}; | ||
HttpSwProxy.prototype.delete = function (url, options) { | ||
return this.resolveRequest({ method: methods.DELETE, url: url, options: options }); | ||
HttpSwProxyPluginImpl.prototype.getBody = function (body) { | ||
if (body && typeof body === 'object') | ||
return JSON.stringify(body); | ||
return body; | ||
}; | ||
HttpSwProxy.prototype.head = function (url, options) { | ||
return this.resolveRequest({ method: methods.HEAD, url: url, options: options }); | ||
HttpSwProxyPluginImpl.prototype.getHeaders = function (request) { | ||
var headers = new Headers(); | ||
if (request._body != null) | ||
headers.set('Content-Type', this.getContentType(request._body)); | ||
request.headers._headers.forEach(function (value, key) { return headers.set(key, value); }); | ||
return headers; | ||
}; | ||
HttpSwProxy.prototype.options = function (url, options) { | ||
return this.resolveRequest({ method: methods.OPTIONS, url: url, options: options }); | ||
return HttpSwProxyPluginImpl; | ||
}()); | ||
var __extends = (undefined && undefined.__extends) || (function () { | ||
var extendStatics = Object.setPrototypeOf || | ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || | ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; | ||
return function (d, b) { | ||
extendStatics(d, b); | ||
function __() { this.constructor = d; } | ||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
}; | ||
HttpSwProxy.prototype.patch = function (url, body, options) { | ||
return this.resolveRequest({ method: methods.PATCH, url: url, options: options, body: body }); | ||
}; | ||
HttpSwProxy.prototype.put = function (url, body, options) { | ||
return this.resolveRequest({ method: methods.PUT, url: url, options: options, body: body }); | ||
}; | ||
})(); | ||
function mergeOptions(defaultOpts, providedOpts, method, url) { | ||
var newOptions = defaultOpts; | ||
if (providedOpts) { | ||
// Hack so Dart can used named parameters | ||
return newOptions.merge(new _angular_http.RequestOptions({ | ||
method: providedOpts.method || method, | ||
url: providedOpts.url || url, | ||
search: providedOpts.search, | ||
params: providedOpts.params, | ||
headers: providedOpts.headers, | ||
body: providedOpts.body, | ||
withCredentials: providedOpts.withCredentials, | ||
responseType: providedOpts.responseType | ||
})); | ||
} | ||
return newOptions.merge(new _angular_http.RequestOptions({ method: method, url: url })); | ||
} | ||
var HttpSwProxy = (function (_super) { | ||
__extends(HttpSwProxy, _super); | ||
function HttpSwProxy(backend, defaultOptions, appref, store, connectivity) { | ||
var _this = _super.call(this, backend, defaultOptions) || this; | ||
_this.defaultOptions = defaultOptions; | ||
_this.appref = appref; | ||
_this.store = store; | ||
_this.connectivity = connectivity; | ||
_this.obsRegister = new Map(); | ||
_this.isConnected = false; | ||
_this.connectivity.hasNetworkConnection().subscribe(function (connected) { return _this.isConnected = connected; }); | ||
return _this; | ||
} | ||
HttpSwProxy.prototype.request = function (url, options) { | ||
return this.resolveRequest({ method: methods.REQUEST, url: url, options: options }); | ||
if (typeof url == 'string') { | ||
url = new _angular_http.Request(mergeOptions(this.defaultOptions, options, _angular_http.RequestMethod.Get, url)); | ||
} | ||
return this.resolveRequest(url); | ||
}; | ||
HttpSwProxy.prototype.hasNetworkConnection = function () { | ||
if (!_angular_common.isPlatformBrowser(this.platformId)) | ||
return rxjs.Observable.of(true); | ||
return rxjs.Observable.merge(rxjs.Observable.of(navigator.onLine), rxjs.Observable.fromEvent(window, 'online').map(function () { return true; }), rxjs.Observable.fromEvent(window, 'offline').map(function () { return false; })); | ||
}; | ||
HttpSwProxy.prototype.resolveRequest = function (request) { | ||
if (this.isConnected) | ||
return this.resolveNow(request); | ||
return _super.prototype.request.call(this, request); | ||
else | ||
return this.passThroughDB(request); | ||
return this.passToDB(request); | ||
}; | ||
HttpSwProxy.prototype.resolveNow = function (request) { | ||
var obs; | ||
switch (request.method) { | ||
default: { | ||
obs = this.http.get(request.url, request.options); | ||
break; | ||
} | ||
case methods.POST: { | ||
obs = this.http.post(request.url, request.body, request.options); | ||
break; | ||
} | ||
case methods.DELETE: { | ||
obs = this.http.delete(request.url, request.options); | ||
break; | ||
} | ||
case methods.HEAD: { | ||
obs = this.http.head(request.url, request.options); | ||
break; | ||
} | ||
case methods.OPTIONS: { | ||
obs = this.http.options(request.url, request.options); | ||
break; | ||
} | ||
case methods.PATCH: { | ||
obs = this.http.patch(request.url, request.body, request.options); | ||
break; | ||
} | ||
case methods.PUT: { | ||
obs = this.http.put(request.url, request.body, request.options); | ||
break; | ||
} | ||
case methods.REQUEST: { | ||
obs = this.http.request(request.url, request.options); | ||
break; | ||
} | ||
} | ||
return obs; | ||
}; | ||
HttpSwProxy.prototype.passThroughDB = function (request) { | ||
HttpSwProxy.prototype.passToDB = function (request) { | ||
var _this = this; | ||
return rxjs.Observable.create(function (subject) { | ||
_this.store.requests("readwrite").then(function (transaction) { return transaction.put(request); }).then(function (key) { | ||
_this.obsRegister.set(key, subject); | ||
if (process.env.NODE_ENV == 'production' && 'serviceWorker' in navigator) { | ||
@@ -155,2 +232,3 @@ navigator.serviceWorker.ready.then(function (swRegistration) { | ||
else { | ||
_this.obsRegister.set(key, subject); | ||
_this.waitForConnectionAndSend(); | ||
@@ -199,3 +277,3 @@ } | ||
var _this = this; | ||
this.hasNetworkConnection().filter(function (connection) { return connection; }).subscribe(function () { | ||
this.connectivity.hasNetworkConnection().filter(function (connection) { return connection; }).subscribe(function () { | ||
_this.store.requests('readwrite').then(function (transaction) { | ||
@@ -205,4 +283,12 @@ transaction.getAll().then(function (requests) { | ||
transaction.delete(request.id); | ||
if (_this.obsRegister.has(request.id)) | ||
_this.resolveNow(request).subscribe(function (resp) { return _this.obsRegister.get(request.id).next(resp); }); | ||
if (_this.obsRegister.has(request.id)) { | ||
var toSend = new _angular_http.Request({ | ||
method: request.method, | ||
url: request.url, | ||
withCredentials: request.withCredentials, | ||
headers: request.headers, | ||
body: request._body | ||
}); | ||
_super.prototype.request.call(_this, toSend).subscribe(function (resp) { return _this.obsRegister.get(request.id).next(resp); }); | ||
} | ||
}); | ||
@@ -213,15 +299,19 @@ }); | ||
}; | ||
HttpSwProxy.decorators = [ | ||
{ type: _angular_core.Injectable }, | ||
]; | ||
/** @nocollapse */ | ||
HttpSwProxy.ctorParameters = function () { return [ | ||
{ type: Object, decorators: [{ type: _angular_core.Inject, args: [_angular_core.PLATFORM_ID,] },] }, | ||
{ type: _angular_http.Http, }, | ||
{ type: Store, }, | ||
{ type: _angular_core.ApplicationRef, }, | ||
]; }; | ||
return HttpSwProxy; | ||
}(_angular_http.Http)); | ||
var ConnectivityService = (function () { | ||
function ConnectivityService() { | ||
} | ||
ConnectivityService.prototype.hasNetworkConnection = function () { | ||
if (!_angular_common.isPlatformBrowser(_angular_core.PLATFORM_ID)) | ||
return rxjs.Observable.of(true); | ||
return rxjs.Observable.merge(rxjs.Observable.of(navigator.onLine), rxjs.Observable.fromEvent(window, 'online').map(function () { return true; }), rxjs.Observable.fromEvent(window, 'offline').map(function () { return false; })); | ||
}; | ||
return ConnectivityService; | ||
}()); | ||
function httpFactory(backend, defaultOptions, appref, store, connectivity) { | ||
return new HttpSwProxy(backend, defaultOptions, appref, store, connectivity); | ||
} | ||
var HttpSwProxyModule = (function () { | ||
@@ -235,3 +325,11 @@ function HttpSwProxyModule() { | ||
], | ||
providers: [HttpSwProxy, Store] | ||
providers: [ | ||
Store, | ||
ConnectivityService, | ||
{ | ||
provide: _angular_http.Http, | ||
useFactory: httpFactory, | ||
deps: [_angular_http.XHRBackend, _angular_http.RequestOptions, _angular_core.ApplicationRef, Store, ConnectivityService] | ||
} | ||
] | ||
},] }, | ||
@@ -244,109 +342,8 @@ ]; | ||
function HttpSwProxyPlugin() { | ||
return function (worker) { return new HttpSwProxyPluginImpl(worker); }; | ||
} | ||
var HttpSwProxyPluginImpl = (function () { | ||
function HttpSwProxyPluginImpl(worker) { | ||
var _this = this; | ||
this.worker = worker; | ||
this.promiseRegister = new Map(); | ||
this.store = new Store(); | ||
self.addEventListener('sync', function (event) { | ||
event.waitUntil(_this.store.requests('readonly').then(function (requests) { return requests.getAll(); }).then(function (requestes) { | ||
return Promise.all(requestes.map(function (request) { | ||
var promiseResolve; | ||
_this.promiseRegister.set(request.id, new Promise(function (resolve) { return promiseResolve = resolve; })); | ||
return fetch(request.url, { | ||
method: request.method, | ||
body: JSON.stringify(request.body), | ||
cache: "no-store", | ||
headers: _this.getHeaders(request.body) | ||
}).then(function (response) { | ||
_this.store.requests('readwrite').then(function (requests) { return requests.delete(request.id); }); | ||
var respToStore = { | ||
type: response.type, | ||
bodyUsed: response.bodyUsed, | ||
body: {}, | ||
headers: new Map(), | ||
ok: response.ok, | ||
status: response.status, | ||
statusText: response.statusText, | ||
url: response.url | ||
}; | ||
var bodyPromise; | ||
if (response.headers.has('content-type')) { | ||
if (response.headers.get('content-type').indexOf('json') > -1) { | ||
bodyPromise = response.json(); | ||
} | ||
else if (response.headers.get('content-type').indexOf('text') > -1) { | ||
bodyPromise = response.text(); | ||
} | ||
else if (response.headers.get('content-type').indexOf('application/x-www-form-urlencoded') > -1) { | ||
bodyPromise = response.formData(); | ||
} | ||
else if (response.headers.get('content-type').indexOf('application/octet-stream') > -1) { | ||
bodyPromise = response.blob(); | ||
} | ||
else { | ||
bodyPromise = response.arrayBuffer(); | ||
} | ||
} | ||
response.headers.forEach(function (key, value) { | ||
respToStore.headers.set(key, value); | ||
}); | ||
return bodyPromise.then(function (body) { | ||
respToStore.body = body; | ||
_this.store.responses('readwrite').then(function (responses) { | ||
responses.put({ id: request.id, response: respToStore }); | ||
promiseResolve(true); | ||
}); | ||
return _this.store.closeTransaction(); | ||
}); | ||
}); | ||
})); | ||
})); | ||
}); | ||
self.addEventListener('message', function (event) { | ||
if (typeof event.data == 'string' && event.data.indexOf("give me response ") > -1) { | ||
var key_1 = event.data.substr(17); | ||
var interval_1; | ||
interval_1 = setInterval(function () { | ||
if (_this.promiseRegister.has(parseInt(key_1))) { | ||
clearInterval(interval_1); | ||
_this.promiseRegister.get(parseInt(key_1)).then(function (value) { | ||
event.ports[0].postMessage("response is waiting: " + key_1); | ||
_this.promiseRegister.delete(parseInt(key_1)); | ||
}); | ||
} | ||
}, 1); | ||
} | ||
}); | ||
} | ||
HttpSwProxyPluginImpl.prototype.setup = function (ops) { }; | ||
HttpSwProxyPluginImpl.prototype.getContentType = function (body) { | ||
if (body instanceof URLSearchParams) | ||
return 'application/x-www-form-urlencoded'; | ||
else if (body instanceof FormData) | ||
return 'multipart/form-data'; | ||
else if (body instanceof Blob) | ||
return 'application/octet-stream'; | ||
else if (body && typeof body === 'object') | ||
return 'application/json'; | ||
else | ||
return 'text/plain'; | ||
}; | ||
HttpSwProxyPluginImpl.prototype.getHeaders = function (body) { | ||
var headers = new Headers(); | ||
if (body != null) | ||
headers.set('Content-Type', this.getContentType(body)); | ||
return headers; | ||
}; | ||
return HttpSwProxyPluginImpl; | ||
}()); | ||
exports.HttpSwProxyPlugin = HttpSwProxyPlugin; | ||
exports.HttpSwProxyPluginImpl = HttpSwProxyPluginImpl; | ||
exports.HttpSwProxyModule = HttpSwProxyModule; | ||
exports.Store = Store; | ||
exports.HttpSwProxy = HttpSwProxy; | ||
exports.HttpSwProxyPlugin = HttpSwProxyPlugin; | ||
exports.HttpSwProxyPluginImpl = HttpSwProxyPluginImpl; | ||
exports.ConnectivityService = ConnectivityService; | ||
@@ -353,0 +350,0 @@ Object.defineProperty(exports, '__esModule', { value: true }); |
@@ -1,1 +0,1 @@ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@angular/http"),require("@angular/core"),require("@angular/common"),require("rxjs"),require("idb")):"function"==typeof define&&define.amd?define(["exports","@angular/http","@angular/core","@angular/common","rxjs","idb"],t):t((e.ng=e.ng||{},e.ng["http-sw-proxy"]=e.ng["http-sw-proxy"]||{}),e.ng.http,e.ng.core,e.ng.common,e.Rx,e.idb)}(this,function(e,t,n,r,o,s){"use strict";var i=s.default,u=function(){function e(){this.db=null}return e.prototype.init=function(){var e=this;return this.db?Promise.resolve(this.db):i.open("restRequest",1,function(e){e.createObjectStore("requests",{autoIncrement:!0,keyPath:"id"}),e.createObjectStore("responses",{keyPath:"id",autoIncrement:!1})}).then(function(t){return e.db=t})},e.prototype.requests=function(e){var t=this;return this.init().then(function(n){return t.db.transaction("requests",e).objectStore("requests")})},e.prototype.responses=function(e){var t=this;return this.init().then(function(n){return t.db.transaction("responses",e).objectStore("responses")})},e.prototype.closeTransaction=function(){return this.db.transaction.complete},e}(),a={DELETE:"DELETE",GET:"GET",HEAD:"HEAD",OPTIONS:"OPTIONS",PATCH:"PATCH",POST:"POST",PUT:"PUT",REQUEST:"REQUEST"},p=function(){function e(e,t,n,r){var o=this;this.http=t,this.store=n,this.appref=r,this.obsRegister=new Map,this.isConnected=!1,this.platformId=e,this.hasNetworkConnection().subscribe(function(e){return o.isConnected=e})}return e.prototype.get=function(e,t){return this.resolveRequest({method:a.GET,url:e,options:t})},e.prototype.post=function(e,t,n){return this.resolveRequest({method:a.POST,url:e,options:n,body:t})},e.prototype.delete=function(e,t){return this.resolveRequest({method:a.DELETE,url:e,options:t})},e.prototype.head=function(e,t){return this.resolveRequest({method:a.HEAD,url:e,options:t})},e.prototype.options=function(e,t){return this.resolveRequest({method:a.OPTIONS,url:e,options:t})},e.prototype.patch=function(e,t,n){return this.resolveRequest({method:a.PATCH,url:e,options:n,body:t})},e.prototype.put=function(e,t,n){return this.resolveRequest({method:a.PUT,url:e,options:n,body:t})},e.prototype.request=function(e,t){return this.resolveRequest({method:a.REQUEST,url:e,options:t})},e.prototype.hasNetworkConnection=function(){return r.isPlatformBrowser(this.platformId)?o.Observable.merge(o.Observable.of(navigator.onLine),o.Observable.fromEvent(window,"online").map(function(){return!0}),o.Observable.fromEvent(window,"offline").map(function(){return!1})):o.Observable.of(!0)},e.prototype.resolveRequest=function(e){return this.isConnected?this.resolveNow(e):this.passThroughDB(e)},e.prototype.resolveNow=function(e){var t;switch(e.method){default:t=this.http.get(e.url,e.options);break;case a.POST:t=this.http.post(e.url,e.body,e.options);break;case a.DELETE:t=this.http.delete(e.url,e.options);break;case a.HEAD:t=this.http.head(e.url,e.options);break;case a.OPTIONS:t=this.http.options(e.url,e.options);break;case a.PATCH:t=this.http.patch(e.url,e.body,e.options);break;case a.PUT:t=this.http.put(e.url,e.body,e.options);break;case a.REQUEST:t=this.http.request(e.url,e.options)}return t},e.prototype.passThroughDB=function(e){var t=this;return o.Observable.create(function(n){t.store.requests("readwrite").then(function(t){return t.put(e)}).then(function(e){t.obsRegister.set(e,n),"production"==process.env.NODE_ENV&&"serviceWorker"in navigator?navigator.serviceWorker.ready.then(function(r){r.sync.register("request");var o=new MessageChannel;o.port1.onmessage=function(r){t.getResponseFromDB(e).subscribe(function(e){return n.next(e)})},navigator.serviceWorker.controller.postMessage("give me response "+e,[o.port2])}):t.waitForConnectionAndSend()})})},e.prototype.getResponseFromDB=function(e){var n=this;return o.Observable.create(function(r){n.store.responses("readwrite").then(function(o){return o.get(e).then(function(s){o.delete(e);for(var i,u=new t.Headers,a=t.ResponseType.Basic,p=0,c=s.response.headers.entries();p<c.length;p++){var h=c[p];u.set(h[0],h[1])}switch(s.response.type){case"basic":a=t.ResponseType.Basic}var d=new t.ResponseOptions({body:s.response.body,status:s.response.status,headers:u,statusText:s.response.statusText,type:a,url:s.response.url});(i=new t.Response(d)).ok?r.next(i):r.error(i),r.complete,n.appref.tick()})})})},e.prototype.waitForConnectionAndSend=function(){var e=this;this.hasNetworkConnection().filter(function(e){return e}).subscribe(function(){e.store.requests("readwrite").then(function(t){t.getAll().then(function(n){n.forEach(function(n){t.delete(n.id),e.obsRegister.has(n.id)&&e.resolveNow(n).subscribe(function(t){return e.obsRegister.get(n.id).next(t)})})})})})},e.decorators=[{type:n.Injectable}],e.ctorParameters=function(){return[{type:Object,decorators:[{type:n.Inject,args:[n.PLATFORM_ID]}]},{type:t.Http},{type:u},{type:n.ApplicationRef}]},e}(),c=function(){function e(){}return e.decorators=[{type:n.NgModule,args:[{imports:[t.HttpModule],providers:[p,u]}]}],e.ctorParameters=function(){return[]},e}(),h=function(){function e(e){var t=this;this.worker=e,this.promiseRegister=new Map,this.store=new u,self.addEventListener("sync",function(e){e.waitUntil(t.store.requests("readonly").then(function(e){return e.getAll()}).then(function(e){return Promise.all(e.map(function(e){var n;return t.promiseRegister.set(e.id,new Promise(function(e){return n=e})),fetch(e.url,{method:e.method,body:JSON.stringify(e.body),cache:"no-store",headers:t.getHeaders(e.body)}).then(function(r){t.store.requests("readwrite").then(function(t){return t.delete(e.id)});var o,s={type:r.type,bodyUsed:r.bodyUsed,body:{},headers:new Map,ok:r.ok,status:r.status,statusText:r.statusText,url:r.url};return r.headers.has("content-type")&&(o=r.headers.get("content-type").indexOf("json")>-1?r.json():r.headers.get("content-type").indexOf("text")>-1?r.text():r.headers.get("content-type").indexOf("application/x-www-form-urlencoded")>-1?r.formData():r.headers.get("content-type").indexOf("application/octet-stream")>-1?r.blob():r.arrayBuffer()),r.headers.forEach(function(e,t){s.headers.set(e,t)}),o.then(function(r){return s.body=r,t.store.responses("readwrite").then(function(t){t.put({id:e.id,response:s}),n(!0)}),t.store.closeTransaction()})})}))}))}),self.addEventListener("message",function(e){if("string"==typeof e.data&&e.data.indexOf("give me response ")>-1){var n,r=e.data.substr(17);n=setInterval(function(){t.promiseRegister.has(parseInt(r))&&(clearInterval(n),t.promiseRegister.get(parseInt(r)).then(function(n){e.ports[0].postMessage("response is waiting: "+r),t.promiseRegister.delete(parseInt(r))}))},1)}})}return e.prototype.setup=function(e){},e.prototype.getContentType=function(e){return e instanceof URLSearchParams?"application/x-www-form-urlencoded":e instanceof FormData?"multipart/form-data":e instanceof Blob?"application/octet-stream":e&&"object"==typeof e?"application/json":"text/plain"},e.prototype.getHeaders=function(e){var t=new Headers;return null!=e&&t.set("Content-Type",this.getContentType(e)),t},e}();e.HttpSwProxyModule=c,e.Store=u,e.HttpSwProxy=p,e.HttpSwProxyPlugin=function(){return function(e){return new h(e)}},e.HttpSwProxyPluginImpl=h,Object.defineProperty(e,"__esModule",{value:!0})}); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("idb"),require("@angular/http"),require("@angular/core"),require("rxjs"),require("@angular/common")):"function"==typeof define&&define.amd?define(["exports","idb","@angular/http","@angular/core","rxjs","@angular/common"],t):t((e.ng=e.ng||{},e.ng["http-sw-proxy"]=e.ng["http-sw-proxy"]||{}),e.idb,e.ng.http,e.ng.core,e.Rx,e.ng.common)}(this,function(e,t,n,r,o,s){"use strict";function i(e,t,r,o){var s=e;return t?s.merge(new n.RequestOptions({method:t.method||r,url:t.url||o,search:t.search,params:t.params,headers:t.headers,body:t.body,withCredentials:t.withCredentials,responseType:t.responseType})):s.merge(new n.RequestOptions({method:r,url:o}))}function a(e,t,n,r,o){return new f(e,t,n,r,o)}var u=t.default,c=function(){function e(){this.db=null}return e.prototype.init=function(){var e=this;return this.db?Promise.resolve(this.db):u.open("restRequest",1,function(e){e.createObjectStore("requests",{autoIncrement:!0,keyPath:"id"}),e.createObjectStore("responses",{keyPath:"id",autoIncrement:!1})}).then(function(t){return e.db=t})},e.prototype.requests=function(e){var t=this;return this.init().then(function(n){return t.db.transaction("requests",e).objectStore("requests")})},e.prototype.responses=function(e){var t=this;return this.init().then(function(n){return t.db.transaction("responses",e).objectStore("responses")})},e.prototype.closeTransaction=function(){return this.db.transaction.complete},e}(),p=function(){function e(e){var t=this;this.worker=e,this.promiseRegister=new Map,this.store=new c,self.addEventListener("sync",function(e){e.waitUntil(t.store.requests("readonly").then(function(e){return e.getAll()}).then(function(e){return Promise.all(e.map(function(e){var n;return t.promiseRegister.set(e.id,new Promise(function(e){return n=e})),fetch(e.url,{method:t.getMethod(e.method),body:t.getBody(e._body),cache:"no-store",headers:t.getHeaders(e)}).then(function(r){t.store.requests("readwrite").then(function(t){return t.delete(e.id)});var o,s={type:r.type,bodyUsed:r.bodyUsed,body:{},headers:new Map,ok:r.ok,status:r.status,statusText:r.statusText,url:r.url};return r.headers.has("content-type")&&(o=r.headers.get("content-type").indexOf("json")>-1?r.json():r.headers.get("content-type").indexOf("text")>-1?r.text():r.headers.get("content-type").indexOf("application/x-www-form-urlencoded")>-1?r.formData():r.headers.get("content-type").indexOf("application/octet-stream")>-1?r.blob():r.arrayBuffer()),r.headers.forEach(function(e,t){s.headers.set(e,t)}),o.then(function(r){return s.body=r,t.store.responses("readwrite").then(function(t){t.put({id:e.id,response:s}),n(!0)}),t.store.closeTransaction()})})})).catch(function(e){console.error(e)})}))}),self.addEventListener("message",function(e){if("string"==typeof e.data&&e.data.indexOf("give me response ")>-1){var n,r=e.data.substr(17);n=setInterval(function(){t.promiseRegister.has(parseInt(r))&&(clearInterval(n),t.promiseRegister.get(parseInt(r)).then(function(n){e.ports[0].postMessage("response is waiting: "+r),t.promiseRegister.delete(parseInt(r))}))},1)}})}return e.prototype.setup=function(e){},e.prototype.getMethod=function(e){switch(e){case 0:return"GET";case 1:return"POST";case 2:return"PUT";case 3:return"DELETE";case 4:return"OPTIONS";case 5:return"HEAD";case 6:return"PATCH"}},e.prototype.getContentType=function(e){return e instanceof URLSearchParams?"application/x-www-form-urlencoded":e instanceof FormData?"multipart/form-data":e instanceof Blob?"application/octet-stream":e&&"object"==typeof e?"application/json":"text/plain"},e.prototype.getBody=function(e){return e&&"object"==typeof e?JSON.stringify(e):e},e.prototype.getHeaders=function(e){var t=new Headers;return null!=e._body&&t.set("Content-Type",this.getContentType(e._body)),e.headers._headers.forEach(function(e,n){return t.set(n,e)}),t},e}(),d=function(){var e=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var n in t)t.hasOwnProperty(n)&&(e[n]=t[n])};return function(t,n){function r(){this.constructor=t}e(t,n),t.prototype=null===n?Object.create(n):(r.prototype=n.prototype,new r)}}(),f=function(e){function t(t,n,r,o,s){var i=e.call(this,t,n)||this;return i.defaultOptions=n,i.appref=r,i.store=o,i.connectivity=s,i.obsRegister=new Map,i.isConnected=!1,i.connectivity.hasNetworkConnection().subscribe(function(e){return i.isConnected=e}),i}return d(t,e),t.prototype.request=function(e,t){return"string"==typeof e&&(e=new n.Request(i(this.defaultOptions,t,n.RequestMethod.Get,e))),this.resolveRequest(e)},t.prototype.resolveRequest=function(t){return this.isConnected?e.prototype.request.call(this,t):this.passToDB(t)},t.prototype.passToDB=function(e){var t=this;return o.Observable.create(function(n){t.store.requests("readwrite").then(function(t){return t.put(e)}).then(function(e){"production"==process.env.NODE_ENV&&"serviceWorker"in navigator?navigator.serviceWorker.ready.then(function(r){r.sync.register("request");var o=new MessageChannel;o.port1.onmessage=function(r){t.getResponseFromDB(e).subscribe(function(e){return n.next(e)})},navigator.serviceWorker.controller.postMessage("give me response "+e,[o.port2])}):(t.obsRegister.set(e,n),t.waitForConnectionAndSend())})})},t.prototype.getResponseFromDB=function(e){var t=this;return o.Observable.create(function(r){t.store.responses("readwrite").then(function(o){return o.get(e).then(function(s){o.delete(e);for(var i,a=new n.Headers,u=n.ResponseType.Basic,c=0,p=s.response.headers.entries();c<p.length;c++){var d=p[c];a.set(d[0],d[1])}switch(s.response.type){case"basic":u=n.ResponseType.Basic}var f=new n.ResponseOptions({body:s.response.body,status:s.response.status,headers:a,statusText:s.response.statusText,type:u,url:s.response.url});(i=new n.Response(f)).ok?r.next(i):r.error(i),r.complete,t.appref.tick()})})})},t.prototype.waitForConnectionAndSend=function(){var t=this;this.connectivity.hasNetworkConnection().filter(function(e){return e}).subscribe(function(){t.store.requests("readwrite").then(function(r){r.getAll().then(function(o){o.forEach(function(o){if(r.delete(o.id),t.obsRegister.has(o.id)){var s=new n.Request({method:o.method,url:o.url,withCredentials:o.withCredentials,headers:o.headers,body:o._body});e.prototype.request.call(t,s).subscribe(function(e){return t.obsRegister.get(o.id).next(e)})}})})})})},t}(n.Http),h=function(){function e(){}return e.prototype.hasNetworkConnection=function(){return s.isPlatformBrowser(r.PLATFORM_ID)?o.Observable.merge(o.Observable.of(navigator.onLine),o.Observable.fromEvent(window,"online").map(function(){return!0}),o.Observable.fromEvent(window,"offline").map(function(){return!1})):o.Observable.of(!0)},e}(),l=function(){function e(){}return e.decorators=[{type:r.NgModule,args:[{imports:[n.HttpModule],providers:[c,h,{provide:n.Http,useFactory:a,deps:[n.XHRBackend,n.RequestOptions,r.ApplicationRef,c,h]}]}]}],e.ctorParameters=function(){return[]},e}();e.HttpSwProxyPlugin=function(){return function(e){return new p(e)}},e.HttpSwProxyPluginImpl=p,e.HttpSwProxyModule=l,e.Store=c,e.HttpSwProxy=f,e.ConnectivityService=h,Object.defineProperty(e,"__esModule",{value:!0})}); |
@@ -1,6 +0,2 @@ | ||
export { HttpSwProxyModule } from './src/http-sw-proxy.module'; | ||
export { RestRequest } from './src/rest-request'; | ||
export { RestResponse } from './src/rest-response'; | ||
export { Store } from './src/store'; | ||
export { HttpSwProxy } from './src/http-sw-proxy.service'; | ||
export * from './plugin/index'; | ||
export * from './src/index'; |
@@ -1,5 +0,3 @@ | ||
export { HttpSwProxyModule } from './src/http-sw-proxy.module'; | ||
export { Store } from './src/store'; | ||
export { HttpSwProxy } from './src/http-sw-proxy.service'; | ||
export * from './plugin/index'; | ||
export * from './src/index'; | ||
//# sourceMappingURL=index.js.map |
@@ -1,1 +0,1 @@ | ||
[{"__symbolic":"module","version":3,"metadata":{},"exports":[{"from":"./src/http-sw-proxy.module","export":["HttpSwProxyModule"]},{"from":"./src/rest-request","export":["RestRequest"]},{"from":"./src/rest-response","export":["RestResponse"]},{"from":"./src/store","export":["Store"]},{"from":"./src/http-sw-proxy.service","export":["HttpSwProxy"]},{"from":"./plugin/index"}]},{"__symbolic":"module","version":1,"metadata":{},"exports":[{"from":"./src/http-sw-proxy.module","export":["HttpSwProxyModule"]},{"from":"./src/rest-request","export":["RestRequest"]},{"from":"./src/rest-response","export":["RestResponse"]},{"from":"./src/store","export":["Store"]},{"from":"./src/http-sw-proxy.service","export":["HttpSwProxy"]},{"from":"./plugin/index"}]}] | ||
[{"__symbolic":"module","version":3,"metadata":{},"exports":[{"from":"./plugin/index"},{"from":"./src/index"}]},{"__symbolic":"module","version":1,"metadata":{},"exports":[{"from":"./plugin/index"},{"from":"./src/index"}]}] |
@@ -7,5 +7,7 @@ export declare function HttpSwProxyPlugin(): (worker: any) => HttpSwProxyPluginImpl; | ||
private store; | ||
private getMethod(id); | ||
private getContentType(body); | ||
private getHeaders(body); | ||
private getBody(body); | ||
private getHeaders(request); | ||
constructor(worker: any); | ||
} |
@@ -17,6 +17,6 @@ import { Store } from '../src/store'; | ||
return fetch(request.url, { | ||
method: request.method, | ||
body: JSON.stringify(request.body), | ||
method: _this.getMethod(request.method), | ||
body: _this.getBody(request._body), | ||
cache: "no-store", | ||
headers: _this.getHeaders(request.body) | ||
headers: _this.getHeaders(request) | ||
}).then(function (response) { | ||
@@ -64,3 +64,5 @@ _this.store.requests('readwrite').then(function (requests) { return requests.delete(request.id); }); | ||
}); | ||
})); | ||
})).catch(function (err) { | ||
console.error(err); | ||
}); | ||
})); | ||
@@ -85,2 +87,13 @@ }); | ||
HttpSwProxyPluginImpl.prototype.setup = function (ops) { }; | ||
HttpSwProxyPluginImpl.prototype.getMethod = function (id) { | ||
switch (id) { | ||
case 0: return 'GET'; | ||
case 1: return 'POST'; | ||
case 2: return 'PUT'; | ||
case 3: return 'DELETE'; | ||
case 4: return 'OPTIONS'; | ||
case 5: return 'HEAD'; | ||
case 6: return 'PATCH'; | ||
} | ||
}; | ||
HttpSwProxyPluginImpl.prototype.getContentType = function (body) { | ||
@@ -98,6 +111,12 @@ if (body instanceof URLSearchParams) | ||
}; | ||
HttpSwProxyPluginImpl.prototype.getHeaders = function (body) { | ||
HttpSwProxyPluginImpl.prototype.getBody = function (body) { | ||
if (body && typeof body === 'object') | ||
return JSON.stringify(body); | ||
return body; | ||
}; | ||
HttpSwProxyPluginImpl.prototype.getHeaders = function (request) { | ||
var headers = new Headers(); | ||
if (body != null) | ||
headers.set('Content-Type', this.getContentType(body)); | ||
if (request._body != null) | ||
headers.set('Content-Type', this.getContentType(request._body)); | ||
request.headers._headers.forEach(function (value, key) { return headers.set(key, value); }); | ||
return headers; | ||
@@ -104,0 +123,0 @@ }; |
@@ -1,1 +0,1 @@ | ||
[{"__symbolic":"module","version":3,"metadata":{"HttpSwProxyPlugin":{"__symbolic":"function","parameters":[],"value":{"__symbolic":"error","message":"Function call not supported","line":8,"character":11}},"HttpSwProxyPluginImpl":{"__symbolic":"class","members":{"setup":[{"__symbolic":"method"}],"getContentType":[{"__symbolic":"method"}],"getHeaders":[{"__symbolic":"method"}],"__ctor__":[{"__symbolic":"constructor","parameters":[{"__symbolic":"reference","name":"any"}]}]}}}},{"__symbolic":"module","version":1,"metadata":{"HttpSwProxyPlugin":{"__symbolic":"function","parameters":[],"value":{"__symbolic":"error","message":"Function call not supported","line":8,"character":11}},"HttpSwProxyPluginImpl":{"__symbolic":"class","members":{"setup":[{"__symbolic":"method"}],"getContentType":[{"__symbolic":"method"}],"getHeaders":[{"__symbolic":"method"}],"__ctor__":[{"__symbolic":"constructor","parameters":[{"__symbolic":"reference","name":"any"}]}]}}}}] | ||
[{"__symbolic":"module","version":3,"metadata":{"HttpSwProxyPlugin":{"__symbolic":"function","parameters":[],"value":{"__symbolic":"error","message":"Function call not supported","line":6,"character":11}},"HttpSwProxyPluginImpl":{"__symbolic":"class","members":{"setup":[{"__symbolic":"method"}],"getMethod":[{"__symbolic":"method"}],"getContentType":[{"__symbolic":"method"}],"getBody":[{"__symbolic":"method"}],"getHeaders":[{"__symbolic":"method"}],"__ctor__":[{"__symbolic":"constructor","parameters":[{"__symbolic":"reference","name":"any"}]}]}}}},{"__symbolic":"module","version":1,"metadata":{"HttpSwProxyPlugin":{"__symbolic":"function","parameters":[],"value":{"__symbolic":"error","message":"Function call not supported","line":6,"character":11}},"HttpSwProxyPluginImpl":{"__symbolic":"class","members":{"setup":[{"__symbolic":"method"}],"getMethod":[{"__symbolic":"method"}],"getContentType":[{"__symbolic":"method"}],"getBody":[{"__symbolic":"method"}],"getHeaders":[{"__symbolic":"method"}],"__ctor__":[{"__symbolic":"constructor","parameters":[{"__symbolic":"reference","name":"any"}]}]}}}}] |
@@ -0,2 +1,8 @@ | ||
import { RequestOptions, XHRBackend } from '@angular/http'; | ||
import { ApplicationRef } from '@angular/core'; | ||
import { HttpSwProxy } from './http-sw-proxy.service'; | ||
import { Store } from './store'; | ||
import { ConnectivityService } from './connectivity.service'; | ||
export declare function httpFactory(backend: XHRBackend, defaultOptions: RequestOptions, appref: ApplicationRef, store: Store, connectivity: ConnectivityService): HttpSwProxy; | ||
export declare class HttpSwProxyModule { | ||
} |
@@ -1,5 +0,9 @@ | ||
import { HttpModule } from '@angular/http'; | ||
import { NgModule } from '@angular/core'; | ||
import { Http, RequestOptions, XHRBackend, HttpModule } from '@angular/http'; | ||
import { NgModule, ApplicationRef } from '@angular/core'; | ||
import { HttpSwProxy } from './http-sw-proxy.service'; | ||
import { Store } from './store'; | ||
import { ConnectivityService } from './connectivity.service'; | ||
export function httpFactory(backend, defaultOptions, appref, store, connectivity) { | ||
return new HttpSwProxy(backend, defaultOptions, appref, store, connectivity); | ||
} | ||
var HttpSwProxyModule = (function () { | ||
@@ -13,3 +17,11 @@ function HttpSwProxyModule() { | ||
], | ||
providers: [HttpSwProxy, Store] | ||
providers: [ | ||
Store, | ||
ConnectivityService, | ||
{ | ||
provide: Http, | ||
useFactory: httpFactory, | ||
deps: [XHRBackend, RequestOptions, ApplicationRef, Store, ConnectivityService] | ||
} | ||
] | ||
},] }, | ||
@@ -16,0 +28,0 @@ ]; |
@@ -1,1 +0,1 @@ | ||
[{"__symbolic":"module","version":3,"metadata":{"HttpSwProxyModule":{"__symbolic":"class","decorators":[{"__symbolic":"call","expression":{"__symbolic":"reference","module":"@angular/core","name":"NgModule"},"arguments":[{"imports":[{"__symbolic":"reference","module":"@angular/http","name":"HttpModule"}],"providers":[{"__symbolic":"reference","module":"./http-sw-proxy.service","name":"HttpSwProxy"},{"__symbolic":"reference","module":"./store","name":"Store"}]}]}]}}},{"__symbolic":"module","version":1,"metadata":{"HttpSwProxyModule":{"__symbolic":"class","decorators":[{"__symbolic":"call","expression":{"__symbolic":"reference","module":"@angular/core","name":"NgModule"},"arguments":[{"imports":[{"__symbolic":"reference","module":"@angular/http","name":"HttpModule"}],"providers":[{"__symbolic":"reference","module":"./http-sw-proxy.service","name":"HttpSwProxy"},{"__symbolic":"reference","module":"./store","name":"Store"}]}]}]}}}] | ||
[{"__symbolic":"module","version":3,"metadata":{"httpFactory":{"__symbolic":"function","parameters":["backend","defaultOptions","appref","store","connectivity"],"value":{"__symbolic":"new","expression":{"__symbolic":"reference","module":"./http-sw-proxy.service","name":"HttpSwProxy"},"arguments":[{"__symbolic":"reference","name":"backend"},{"__symbolic":"reference","name":"defaultOptions"},{"__symbolic":"reference","name":"appref"},{"__symbolic":"reference","name":"store"},{"__symbolic":"reference","name":"connectivity"}]}},"HttpSwProxyModule":{"__symbolic":"class","decorators":[{"__symbolic":"call","expression":{"__symbolic":"reference","module":"@angular/core","name":"NgModule"},"arguments":[{"imports":[{"__symbolic":"reference","module":"@angular/http","name":"HttpModule"}],"providers":[{"__symbolic":"reference","module":"./store","name":"Store"},{"__symbolic":"reference","module":"./connectivity.service","name":"ConnectivityService"},{"provide":{"__symbolic":"reference","module":"@angular/http","name":"Http"},"useFactory":{"__symbolic":"reference","name":"httpFactory"},"deps":[{"__symbolic":"reference","module":"@angular/http","name":"XHRBackend"},{"__symbolic":"reference","module":"@angular/http","name":"RequestOptions"},{"__symbolic":"reference","module":"@angular/core","name":"ApplicationRef"},{"__symbolic":"reference","module":"./store","name":"Store"},{"__symbolic":"reference","module":"./connectivity.service","name":"ConnectivityService"}]}]}]}]}}},{"__symbolic":"module","version":1,"metadata":{"httpFactory":{"__symbolic":"function","parameters":["backend","defaultOptions","appref","store","connectivity"],"value":{"__symbolic":"new","expression":{"__symbolic":"reference","module":"./http-sw-proxy.service","name":"HttpSwProxy"},"arguments":[{"__symbolic":"reference","name":"backend"},{"__symbolic":"reference","name":"defaultOptions"},{"__symbolic":"reference","name":"appref"},{"__symbolic":"reference","name":"store"},{"__symbolic":"reference","name":"connectivity"}]}},"HttpSwProxyModule":{"__symbolic":"class","decorators":[{"__symbolic":"call","expression":{"__symbolic":"reference","module":"@angular/core","name":"NgModule"},"arguments":[{"imports":[{"__symbolic":"reference","module":"@angular/http","name":"HttpModule"}],"providers":[{"__symbolic":"reference","module":"./store","name":"Store"},{"__symbolic":"reference","module":"./connectivity.service","name":"ConnectivityService"},{"provide":{"__symbolic":"reference","module":"@angular/http","name":"Http"},"useFactory":{"__symbolic":"reference","name":"httpFactory"},"deps":[{"__symbolic":"reference","module":"@angular/http","name":"XHRBackend"},{"__symbolic":"reference","module":"@angular/http","name":"RequestOptions"},{"__symbolic":"reference","module":"@angular/core","name":"ApplicationRef"},{"__symbolic":"reference","module":"./store","name":"Store"},{"__symbolic":"reference","module":"./connectivity.service","name":"ConnectivityService"}]}]}]}]}}}] |
@@ -1,27 +0,19 @@ | ||
import { Http, Response, RequestOptionsArgs } from '@angular/http'; | ||
import { Http, Response, RequestOptionsArgs, ConnectionBackend, RequestOptions, Request } from '@angular/http'; | ||
import { ApplicationRef } from '@angular/core'; | ||
import { Observable } from 'rxjs'; | ||
import { Store } from './store'; | ||
export declare class HttpSwProxy { | ||
private http; | ||
import { ConnectivityService } from './connectivity.service'; | ||
export declare class HttpSwProxy extends Http { | ||
private defaultOptions; | ||
private appref; | ||
private store; | ||
private appref; | ||
private connectivity; | ||
private obsRegister; | ||
private platformId; | ||
private isConnected; | ||
constructor(platformId: Object, http: Http, store: Store, appref: ApplicationRef); | ||
get(url: string, options?: RequestOptionsArgs): Observable<Response>; | ||
post(url: string, body: any, options?: RequestOptionsArgs): Observable<Response>; | ||
delete(url: string, options?: RequestOptionsArgs): Observable<Response>; | ||
head(url: string, options?: RequestOptionsArgs): Observable<Response>; | ||
options(url: string, options?: RequestOptionsArgs): Observable<Response>; | ||
patch(url: string, body?: any, options?: RequestOptionsArgs): Observable<Response>; | ||
put(url: string, body?: any, options?: RequestOptionsArgs): Observable<Response>; | ||
request(url: string, options?: RequestOptionsArgs): Observable<Response>; | ||
hasNetworkConnection(): Observable<boolean>; | ||
constructor(backend: ConnectionBackend, defaultOptions: RequestOptions, appref: ApplicationRef, store: Store, connectivity: ConnectivityService); | ||
request(url: string | Request, options?: RequestOptionsArgs): Observable<Response>; | ||
private resolveRequest(request); | ||
private resolveNow(request); | ||
private passThroughDB(request); | ||
private passToDB(request); | ||
private getResponseFromDB(messageId); | ||
private waitForConnectionAndSend(); | ||
} |
@@ -1,96 +0,59 @@ | ||
import { Http, Response, ResponseOptions, ResponseType, Headers } from '@angular/http'; | ||
import { Injectable, Inject, PLATFORM_ID, ApplicationRef } from '@angular/core'; | ||
import { isPlatformBrowser } from '@angular/common'; | ||
var __extends = (this && this.__extends) || (function () { | ||
var extendStatics = Object.setPrototypeOf || | ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || | ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; | ||
return function (d, b) { | ||
extendStatics(d, b); | ||
function __() { this.constructor = d; } | ||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
}; | ||
})(); | ||
import { Http, Response, ResponseOptions, ResponseType, Headers, RequestOptions, Request, RequestMethod } from '@angular/http'; | ||
import { Observable } from 'rxjs'; | ||
import { Store } from './store'; | ||
import { methods } from './rest-request'; | ||
var HttpSwProxy = (function () { | ||
function HttpSwProxy(platformId, http, store, appref) { | ||
var _this = this; | ||
this.http = http; | ||
this.store = store; | ||
this.appref = appref; | ||
this.obsRegister = new Map(); | ||
this.isConnected = false; | ||
this.platformId = platformId; //Intellij type checking workaround. | ||
this.hasNetworkConnection().subscribe(function (connected) { return _this.isConnected = connected; }); | ||
function mergeOptions(defaultOpts, providedOpts, method, url) { | ||
var newOptions = defaultOpts; | ||
if (providedOpts) { | ||
// Hack so Dart can used named parameters | ||
return newOptions.merge(new RequestOptions({ | ||
method: providedOpts.method || method, | ||
url: providedOpts.url || url, | ||
search: providedOpts.search, | ||
params: providedOpts.params, | ||
headers: providedOpts.headers, | ||
body: providedOpts.body, | ||
withCredentials: providedOpts.withCredentials, | ||
responseType: providedOpts.responseType | ||
})); | ||
} | ||
HttpSwProxy.prototype.get = function (url, options) { | ||
return this.resolveRequest({ method: methods.GET, url: url, options: options }); | ||
}; | ||
HttpSwProxy.prototype.post = function (url, body, options) { | ||
return this.resolveRequest({ method: methods.POST, url: url, options: options, body: body }); | ||
}; | ||
HttpSwProxy.prototype.delete = function (url, options) { | ||
return this.resolveRequest({ method: methods.DELETE, url: url, options: options }); | ||
}; | ||
HttpSwProxy.prototype.head = function (url, options) { | ||
return this.resolveRequest({ method: methods.HEAD, url: url, options: options }); | ||
}; | ||
HttpSwProxy.prototype.options = function (url, options) { | ||
return this.resolveRequest({ method: methods.OPTIONS, url: url, options: options }); | ||
}; | ||
HttpSwProxy.prototype.patch = function (url, body, options) { | ||
return this.resolveRequest({ method: methods.PATCH, url: url, options: options, body: body }); | ||
}; | ||
HttpSwProxy.prototype.put = function (url, body, options) { | ||
return this.resolveRequest({ method: methods.PUT, url: url, options: options, body: body }); | ||
}; | ||
return newOptions.merge(new RequestOptions({ method: method, url: url })); | ||
} | ||
var HttpSwProxy = (function (_super) { | ||
__extends(HttpSwProxy, _super); | ||
function HttpSwProxy(backend, defaultOptions, appref, store, connectivity) { | ||
var _this = _super.call(this, backend, defaultOptions) || this; | ||
_this.defaultOptions = defaultOptions; | ||
_this.appref = appref; | ||
_this.store = store; | ||
_this.connectivity = connectivity; | ||
_this.obsRegister = new Map(); | ||
_this.isConnected = false; | ||
_this.connectivity.hasNetworkConnection().subscribe(function (connected) { return _this.isConnected = connected; }); | ||
return _this; | ||
} | ||
HttpSwProxy.prototype.request = function (url, options) { | ||
return this.resolveRequest({ method: methods.REQUEST, url: url, options: options }); | ||
if (typeof url == 'string') { | ||
url = new Request(mergeOptions(this.defaultOptions, options, RequestMethod.Get, url)); | ||
} | ||
return this.resolveRequest(url); | ||
}; | ||
HttpSwProxy.prototype.hasNetworkConnection = function () { | ||
if (!isPlatformBrowser(this.platformId)) | ||
return Observable.of(true); | ||
return Observable.merge(Observable.of(navigator.onLine), Observable.fromEvent(window, 'online').map(function () { return true; }), Observable.fromEvent(window, 'offline').map(function () { return false; })); | ||
}; | ||
HttpSwProxy.prototype.resolveRequest = function (request) { | ||
if (this.isConnected) | ||
return this.resolveNow(request); | ||
return _super.prototype.request.call(this, request); | ||
else | ||
return this.passThroughDB(request); | ||
return this.passToDB(request); | ||
}; | ||
HttpSwProxy.prototype.resolveNow = function (request) { | ||
var obs; | ||
switch (request.method) { | ||
default: { | ||
obs = this.http.get(request.url, request.options); | ||
break; | ||
} | ||
case methods.POST: { | ||
obs = this.http.post(request.url, request.body, request.options); | ||
break; | ||
} | ||
case methods.DELETE: { | ||
obs = this.http.delete(request.url, request.options); | ||
break; | ||
} | ||
case methods.HEAD: { | ||
obs = this.http.head(request.url, request.options); | ||
break; | ||
} | ||
case methods.OPTIONS: { | ||
obs = this.http.options(request.url, request.options); | ||
break; | ||
} | ||
case methods.PATCH: { | ||
obs = this.http.patch(request.url, request.body, request.options); | ||
break; | ||
} | ||
case methods.PUT: { | ||
obs = this.http.put(request.url, request.body, request.options); | ||
break; | ||
} | ||
case methods.REQUEST: { | ||
obs = this.http.request(request.url, request.options); | ||
break; | ||
} | ||
} | ||
return obs; | ||
}; | ||
HttpSwProxy.prototype.passThroughDB = function (request) { | ||
HttpSwProxy.prototype.passToDB = function (request) { | ||
var _this = this; | ||
return Observable.create(function (subject) { | ||
_this.store.requests("readwrite").then(function (transaction) { return transaction.put(request); }).then(function (key) { | ||
_this.obsRegister.set(key, subject); | ||
if (process.env.NODE_ENV == 'production' && 'serviceWorker' in navigator) { | ||
@@ -107,2 +70,3 @@ navigator.serviceWorker.ready.then(function (swRegistration) { | ||
else { | ||
_this.obsRegister.set(key, subject); | ||
_this.waitForConnectionAndSend(); | ||
@@ -151,3 +115,3 @@ } | ||
var _this = this; | ||
this.hasNetworkConnection().filter(function (connection) { return connection; }).subscribe(function () { | ||
this.connectivity.hasNetworkConnection().filter(function (connection) { return connection; }).subscribe(function () { | ||
_this.store.requests('readwrite').then(function (transaction) { | ||
@@ -157,4 +121,12 @@ transaction.getAll().then(function (requests) { | ||
transaction.delete(request.id); | ||
if (_this.obsRegister.has(request.id)) | ||
_this.resolveNow(request).subscribe(function (resp) { return _this.obsRegister.get(request.id).next(resp); }); | ||
if (_this.obsRegister.has(request.id)) { | ||
var toSend = new Request({ | ||
method: request.method, | ||
url: request.url, | ||
withCredentials: request.withCredentials, | ||
headers: request.headers, | ||
body: request._body | ||
}); | ||
_super.prototype.request.call(_this, toSend).subscribe(function (resp) { return _this.obsRegister.get(request.id).next(resp); }); | ||
} | ||
}); | ||
@@ -165,15 +137,5 @@ }); | ||
}; | ||
HttpSwProxy.decorators = [ | ||
{ type: Injectable }, | ||
]; | ||
/** @nocollapse */ | ||
HttpSwProxy.ctorParameters = function () { return [ | ||
{ type: Object, decorators: [{ type: Inject, args: [PLATFORM_ID,] },] }, | ||
{ type: Http, }, | ||
{ type: Store, }, | ||
{ type: ApplicationRef, }, | ||
]; }; | ||
return HttpSwProxy; | ||
}()); | ||
}(Http)); | ||
export { HttpSwProxy }; | ||
//# sourceMappingURL=http-sw-proxy.service.js.map |
@@ -1,1 +0,1 @@ | ||
[{"__symbolic":"module","version":3,"metadata":{"HttpSwProxy":{"__symbolic":"class","decorators":[{"__symbolic":"call","expression":{"__symbolic":"reference","module":"@angular/core","name":"Injectable"}}],"members":{"__ctor__":[{"__symbolic":"constructor","parameterDecorators":[[{"__symbolic":"call","expression":{"__symbolic":"reference","module":"@angular/core","name":"Inject"},"arguments":[{"__symbolic":"reference","module":"@angular/core","name":"PLATFORM_ID"}]}],null,null,null],"parameters":[{"__symbolic":"reference","name":"Object"},{"__symbolic":"reference","module":"@angular/http","name":"Http"},{"__symbolic":"reference","module":"./store","name":"Store"},{"__symbolic":"reference","module":"@angular/core","name":"ApplicationRef"}]}],"get":[{"__symbolic":"method"}],"post":[{"__symbolic":"method"}],"delete":[{"__symbolic":"method"}],"head":[{"__symbolic":"method"}],"options":[{"__symbolic":"method"}],"patch":[{"__symbolic":"method"}],"put":[{"__symbolic":"method"}],"request":[{"__symbolic":"method"}],"hasNetworkConnection":[{"__symbolic":"method"}],"resolveRequest":[{"__symbolic":"method"}],"resolveNow":[{"__symbolic":"method"}],"passThroughDB":[{"__symbolic":"method"}],"getResponseFromDB":[{"__symbolic":"method"}],"waitForConnectionAndSend":[{"__symbolic":"method"}]}}}},{"__symbolic":"module","version":1,"metadata":{"HttpSwProxy":{"__symbolic":"class","decorators":[{"__symbolic":"call","expression":{"__symbolic":"reference","module":"@angular/core","name":"Injectable"}}],"members":{"__ctor__":[{"__symbolic":"constructor","parameterDecorators":[[{"__symbolic":"call","expression":{"__symbolic":"reference","module":"@angular/core","name":"Inject"},"arguments":[{"__symbolic":"reference","module":"@angular/core","name":"PLATFORM_ID"}]}],null,null,null],"parameters":[{"__symbolic":"reference","name":"Object"},{"__symbolic":"reference","module":"@angular/http","name":"Http"},{"__symbolic":"reference","module":"./store","name":"Store"},{"__symbolic":"reference","module":"@angular/core","name":"ApplicationRef"}]}],"get":[{"__symbolic":"method"}],"post":[{"__symbolic":"method"}],"delete":[{"__symbolic":"method"}],"head":[{"__symbolic":"method"}],"options":[{"__symbolic":"method"}],"patch":[{"__symbolic":"method"}],"put":[{"__symbolic":"method"}],"request":[{"__symbolic":"method"}],"hasNetworkConnection":[{"__symbolic":"method"}],"resolveRequest":[{"__symbolic":"method"}],"resolveNow":[{"__symbolic":"method"}],"passThroughDB":[{"__symbolic":"method"}],"getResponseFromDB":[{"__symbolic":"method"}],"waitForConnectionAndSend":[{"__symbolic":"method"}]}}}}] | ||
[{"__symbolic":"module","version":3,"metadata":{"HttpSwProxy":{"__symbolic":"class","extends":{"__symbolic":"reference","module":"@angular/http","name":"Http"},"members":{"__ctor__":[{"__symbolic":"constructor","parameters":[{"__symbolic":"reference","module":"@angular/http","name":"ConnectionBackend"},{"__symbolic":"reference","module":"@angular/http","name":"RequestOptions"},{"__symbolic":"reference","module":"@angular/core","name":"ApplicationRef"},{"__symbolic":"reference","module":"./store","name":"Store"},{"__symbolic":"reference","module":"./connectivity.service","name":"ConnectivityService"}]}],"request":[{"__symbolic":"method"}],"resolveRequest":[{"__symbolic":"method"}],"passToDB":[{"__symbolic":"method"}],"getResponseFromDB":[{"__symbolic":"method"}],"waitForConnectionAndSend":[{"__symbolic":"method"}]}}}},{"__symbolic":"module","version":1,"metadata":{"HttpSwProxy":{"__symbolic":"class","extends":{"__symbolic":"reference","module":"@angular/http","name":"Http"},"members":{"__ctor__":[{"__symbolic":"constructor","parameters":[{"__symbolic":"reference","module":"@angular/http","name":"ConnectionBackend"},{"__symbolic":"reference","module":"@angular/http","name":"RequestOptions"},{"__symbolic":"reference","module":"@angular/core","name":"ApplicationRef"},{"__symbolic":"reference","module":"./store","name":"Store"},{"__symbolic":"reference","module":"./connectivity.service","name":"ConnectivityService"}]}],"request":[{"__symbolic":"method"}],"resolveRequest":[{"__symbolic":"method"}],"passToDB":[{"__symbolic":"method"}],"getResponseFromDB":[{"__symbolic":"method"}],"waitForConnectionAndSend":[{"__symbolic":"method"}]}}}}] |
{ | ||
"name": "ng-http-sw-proxy", | ||
"version": "1.2.8", | ||
"version": "2.0.0", | ||
"description": "Proxy for angular http service. Schedules request send in service-worker and/or IndexedDB.", | ||
@@ -5,0 +5,0 @@ "main": "dist/bundles/ng-http-sw-proxy.umd.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
44
117808
777
4