unleash-proxy-client
Advanced tools
Comparing version 2.2.0 to 2.2.1
@@ -403,5 +403,8 @@ "use strict"; | ||
case 5: | ||
if (!response.ok) { | ||
if (!response.ok && response.status !== 304) { | ||
console.error('Unleash: Fetching feature toggles did not have an ok response'); | ||
this.emit(exports.EVENTS.ERROR, { type: 'HttpError', code: response.status }); | ||
this.emit(exports.EVENTS.ERROR, { | ||
type: 'HttpError', | ||
code: response.status, | ||
}); | ||
} | ||
@@ -408,0 +411,0 @@ _a.label = 6; |
@@ -1,1 +0,1 @@ | ||
(function(a,b){"object"==typeof exports&&"undefined"!=typeof module?b(exports):"function"==typeof define&&define.amd?define(["exports"],b):(a=a||self,b(a.unleash={}))})(this,function(a){"use strict";function b(a,c){function b(){this.constructor=a}if("function"!=typeof c&&null!==c)throw new TypeError("Class extends value "+(c+"")+" is not a constructor or null");k(a,c),a.prototype=null===c?Object.create(c):(b.prototype=c.prototype,new b)}function c(a,b,c,d){function e(a){return a instanceof c?a:new c(function(b){b(a)})}return new(c||(c=Promise))(function(c,f){function g(a){try{i(d.next(a))}catch(a){f(a)}}function h(a){try{i(d["throw"](a))}catch(a){f(a)}}function i(a){a.done?c(a.value):e(a.value).then(g,h)}i((d=d.apply(a,b||[])).next())})}function d(a,b){function c(a){return function(b){return d([a,b])}}function d(c){if(e)throw new TypeError("Generator is already executing.");for(;k;)try{if(e=1,h&&(i=2&c[0]?h["return"]:c[0]?h["throw"]||((i=h["return"])&&i.call(h),0):h.next)&&!(i=i.call(h,c[1])).done)return i;switch((h=0,i)&&(c=[2&c[0],i.value]),c[0]){case 0:case 1:i=c;break;case 4:return k.label++,{value:c[1],done:!1};case 5:k.label++,h=c[1],c=[0];continue;case 7:c=k.ops.pop(),k.trys.pop();continue;default:if((i=k.trys,!(i=0<i.length&&i[i.length-1]))&&(6===c[0]||2===c[0])){k=0;continue}if(3===c[0]&&(!i||c[1]>i[0]&&c[1]<i[3])){k.label=c[1];break}if(6===c[0]&&k.label<i[1]){k.label=i[1],i=c;break}if(i&&k.label<i[2]){k.label=i[2],k.ops.push(c);break}i[2]&&k.ops.pop(),k.trys.pop();continue;}c=b.call(a,k)}catch(a){c=[6,a],h=0}finally{e=i=0}if(5&c[0])throw c[1];return{value:c[0]?c[1]:void 0,done:!0}}var e,h,i,j,k={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]};return j={next:c(0),throw:c(1),return:c(2)},"function"==typeof Symbol&&(j[Symbol.iterator]=function(){return this}),j}function e(a,b,c){if(c||2===arguments.length)for(var d,e=0,f=b.length;e<f;e++)(d||!(e in b))&&(d||(d=Array.prototype.slice.call(b,0,e)),d[e]=b[e]);return a.concat(d||Array.prototype.slice.call(b))}function f(){}function g(){return s>r.length-16&&(q.randomFillSync(r),s=0),r.slice(s,s+=16)}function h(a){return"string"==typeof a&&t.test(a)}function i(a,b=0){const c=(u[a[b+0]]+u[a[b+1]]+u[a[b+2]]+u[a[b+3]]+"-"+u[a[b+4]]+u[a[b+5]]+"-"+u[a[b+6]]+u[a[b+7]]+"-"+u[a[b+8]]+u[a[b+9]]+"-"+u[a[b+10]]+u[a[b+11]]+u[a[b+12]]+u[a[b+13]]+u[a[b+14]]+u[a[b+15]]).toLowerCase();if(!h(c))throw TypeError("Stringified UUID is invalid");return c}function j(a,b,c){a=a||{};const d=a.random||(a.rng||g)();if(d[6]=64|15&d[6],d[8]=128|63&d[8],b){c=c||0;for(let a=0;16>a;++a)b[c+a]=d[a];return b}return i(d)}var k=function(a,c){return k=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(a,c){a.__proto__=c}||function(a,c){for(var b in c)Object.prototype.hasOwnProperty.call(c,b)&&(a[b]=c[b])},k(a,c)},l=function(){return l=Object.assign||function(a){for(var b,c=1,d=arguments.length;c<d;c++)for(var e in b=arguments[c],b)Object.prototype.hasOwnProperty.call(b,e)&&(a[e]=b[e]);return a},l.apply(this,arguments)};f.prototype={on:function(a,b,c){var d=this.e||(this.e={});return(d[a]||(d[a]=[])).push({fn:b,ctx:c}),this},once:function(a,b,c){function d(){e.off(a,d),b.apply(c,arguments)}var e=this;return d._=b,this.on(a,d,c)},emit:function(a){var b=[].slice.call(arguments,1),c=((this.e||(this.e={}))[a]||[]).slice(),d=0,e=c.length;for(d;d<e;d++)c[d].fn.apply(c[d].ctx,b);return this},off:function(a,b){var c=this.e||(this.e={}),d=c[a],e=[];if(d&&b)for(var f=0,g=d.length;f<g;f++)d[f].fn!==b&&d[f].fn._!==b&&e.push(d[f]);return e.length?c[a]=e:delete c[a],this}};var m=f;f.TinyEmitter=m;var n=function(){function a(a){var b=a.onError,c=a.appName,d=a.metricsInterval,e=a.disableMetrics,f=a.url,g=a.clientKey,h=a.fetch,i=a.headerName;this.onError=b,this.disabled=void 0!==e&&e,this.metricsInterval=1e3*d,this.appName=c,this.url=f instanceof URL?f:new URL(f),this.clientKey=g,this.bucket=this.createEmptyBucket(),this.fetch=h,this.headerName=i}return a.prototype.start=function(){var a=this;return!this.disabled&&void("number"==typeof this.metricsInterval&&0<this.metricsInterval&&setTimeout(function(){a.startTimer(),a.sendMetrics()},2e3))},a.prototype.stop=function(){this.timer&&(clearTimeout(this.timer),delete this.timer)},a.prototype.createEmptyBucket=function(){return{start:new Date,stop:null,toggles:{}}},a.prototype.sendMetrics=function(){return c(this,void 0,void 0,function(){var a,b,c,e;return d(this,function(d){switch(d.label){case 0:if(a="".concat(this.url,"/client/metrics"),b=this.getPayload(),this.bucketIsEmpty(b))return[2];d.label=1;case 1:return d.trys.push([1,3,,4]),[4,this.fetch(a,{cache:"no-cache",method:"POST",headers:(e={},e[this.headerName]=this.clientKey,e.Accept="application/json",e["Content-Type"]="application/json",e),body:JSON.stringify(b)})];case 2:return d.sent(),[3,4];case 3:return c=d.sent(),console.error("Unleash: unable to send feature metrics",c),this.onError(c),[3,4];case 4:return[2];}})})},a.prototype.count=function(a,b){return!this.disabled&&this.bucket&&(this.assertBucket(a),this.bucket.toggles[a][b?"yes":"no"]++,!0)},a.prototype.assertBucket=function(a){return!this.disabled&&this.bucket&&void(!this.bucket.toggles[a]&&(this.bucket.toggles[a]={yes:0,no:0}))},a.prototype.startTimer=function(){var a=this;this.timer=setInterval(function(){a.sendMetrics()},this.metricsInterval)},a.prototype.bucketIsEmpty=function(a){return 0===Object.keys(a.bucket.toggles).length},a.prototype.getPayload=function(){var a=l(l({},this.bucket),{stop:new Date});return this.bucket=this.createEmptyBucket(),{bucket:a,appName:this.appName,instanceId:"browser"}},a}(),o=function(){function a(){this.store=new Map}return a.prototype.save=function(a,b){return c(this,void 0,void 0,function(){return d(this,function(){return this.store.set(a,b),[2]})})},a.prototype.get=function(a){return c(this,void 0,void 0,function(){return d(this,function(){return[2,this.store.get(a)]})})},a}(),p=function(){function a(){this.prefix="unleash:repository"}return a.prototype.save=function(a,b){return c(this,void 0,void 0,function(){var c,e;return d(this,function(){c=JSON.stringify(b),e="".concat(this.prefix,":").concat(a);try{window.localStorage.setItem(e,c)}catch(a){console.error(a)}return[2]})})},a.prototype.get=function(a){try{var b="".concat(this.prefix,":").concat(a),c=window.localStorage.getItem(b);return c?JSON.parse(c):void 0}catch(a){console.error(a)}},a}(),q={};const r=new Uint8Array(256);let s=r.length;var t=/^(?:[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}|00000000-0000-0000-0000-000000000000)$/i;const u=[];for(let b=0;256>b;++b)u.push((b+256).toString(16).substr(1));var v=function(){function a(){}return a.prototype.generateEventId=function(){return j()},a.prototype.createImpressionEvent=function(a,b,c,d,e){var f=this.createBaseEvent(a,b,c,d);return e?l(l({},f),{variant:e}):f},a.prototype.createBaseEvent=function(a,b,c,d){return{eventType:d,eventId:this.generateEventId(),context:a,enabled:b,featureName:c}},a}(),w=function(a){var b=a[1];return b!==void 0&&null!==b},x=["userId","sessionId","remoteAddress"],y={INIT:"initialized",ERROR:"error",READY:"ready",UPDATE:"update",IMPRESSION:"impression"},z={IS_ENABLED:"isEnabled",GET_VARIANT:"getVariant"},A={name:"disabled",enabled:!1},B="repo",C=function(){try{if("fetch"in window)return fetch.bind(window);if("fetch"in globalThis)return fetch.bind(globalThis)}catch(a){console.error("Unleash failed to resolve \"fetch\"",a)}},D=function(a){function f(b){var c=b.storageProvider,d=b.url,e=b.clientKey,f=b.disableRefresh,g=b.refreshInterval,h=void 0===g?30:g,i=b.metricsInterval,j=void 0===i?30:i,k=b.disableMetrics,m=b.appName,o=b.environment,q=void 0===o?"default":o,r=b.context,s=b.fetch,t=void 0===s?C():s,u=b.bootstrap,w=b.bootstrapOverride,x=b.headerName,z=void 0===x?"Authorization":x,A=b.customHeaders,B=void 0===A?{}:A,D=a.call(this)||this;if(D.toggles=[],D.etag="",D.readyEventEmitted=!1,!d)throw new Error("url is required");if(!e)throw new Error("clientKey is required");if(!m)throw new Error("appName is required.");return D.eventsHandler=new v,D.toggles=u&&0<u.length?u:[],D.url=d instanceof URL?d:new URL(d),D.clientKey=e,D.headerName=z,D.customHeaders=B,D.storage=c||new p,D.refreshInterval=void 0!==f&&f?0:1e3*h,D.context=l({appName:m,environment:q},r),D.ready=new Promise(function(a){D.init().then(a).catch(function(b){console.error(b),D.emit(y.ERROR,b),a()})}),t||console.error("Unleash: You must either provide your own \"fetch\" implementation or run in an environment where \"fetch\" is available."),D.fetch=t,D.bootstrap=u&&0<u.length?u:void 0,D.bootstrapOverride=void 0===w||w,D.metrics=new n({onError:D.emit.bind(D,y.ERROR),appName:m,metricsInterval:j,disableMetrics:void 0!==k&&k,url:D.url,clientKey:e,fetch:t,headerName:z}),D}return b(f,a),f.prototype.getAllToggles=function(){return e([],this.toggles,!0)},f.prototype.isEnabled=function(a){var b=this.toggles.find(function(b){return b.name===a}),c=!!b&&b.enabled;if(this.metrics.count(a,c),null===b||void 0===b?void 0:b.impressionData){var d=this.eventsHandler.createImpressionEvent(this.context,c,a,z.IS_ENABLED);this.emit(y.IMPRESSION,d)}return c},f.prototype.getVariant=function(a){var b=this.toggles.find(function(b){return b.name===a});if(b){if(this.metrics.count(a,!0),b.impressionData){var c=this.eventsHandler.createImpressionEvent(this.context,b.enabled,a,z.GET_VARIANT,b.variant.name);this.emit(y.IMPRESSION,c)}return b.variant}return this.metrics.count(a,!1),A},f.prototype.updateContext=function(a){return c(this,void 0,void 0,function(){var b;return d(this,function(c){switch(c.label){case 0:return(a.appName||a.environment)&&console.warn("appName and environment are static. They can't be updated with updateContext."),b={environment:this.context.environment,appName:this.context.appName},this.context=l(l({},b),a),this.timerRef?[4,this.fetchToggles()]:[3,2];case 1:c.sent(),c.label=2;case 2:return[2];}})})},f.prototype.getContext=function(){return l({},this.context)},f.prototype.setContextField=function(a,b){var c,d;if(x.includes(a))this.context=l(l({},this.context),(c={},c[a]=b,c));else{var e=l(l({},this.context.properties),(d={},d[a]=b,d));this.context=l(l({},this.context),{properties:e})}this.timerRef&&this.fetchToggles()},f.prototype.init=function(){return c(this,void 0,void 0,function(){var a,b;return d(this,function(c){switch(c.label){case 0:return[4,this.resolveSessionId()];case 1:return a=c.sent(),this.context=l({sessionId:a},this.context),b=this,[4,this.storage.get(B)];case 2:return b.toggles=c.sent()||[],this.bootstrap&&(this.bootstrapOverride||0===this.toggles.length)?[4,this.storage.save(B,this.bootstrap)]:[3,4];case 3:c.sent(),this.toggles=this.bootstrap,this.emit(y.READY),c.label=4;case 4:return this.emit(y.INIT),[2];}})})},f.prototype.start=function(){return c(this,void 0,void 0,function(){var a,b=this;return d(this,function(c){switch(c.label){case 0:return this.timerRef?(console.error("Unleash SDK has already started, if you want to restart the SDK you should call client.stop() before starting again."),[2]):[4,this.ready];case 1:return c.sent(),this.metrics.start(),a=this.refreshInterval,[4,this.fetchToggles()];case 2:return c.sent(),0<a&&(this.timerRef=setInterval(function(){return b.fetchToggles()},a)),[2];}})})},f.prototype.stop=function(){this.timerRef&&(clearInterval(this.timerRef),this.timerRef=void 0),this.metrics.stop()},f.prototype.resolveSessionId=function(){return c(this,void 0,void 0,function(){var a;return d(this,function(b){var c=Math.floor;switch(b.label){case 0:return this.context.sessionId?[2,this.context.sessionId]:[3,1];case 1:return[4,this.storage.get("sessionId")];case 2:return(a=b.sent(),!!a)?[3,4]:(a=c(1e9*Math.random()),[4,this.storage.save("sessionId",a)]);case 3:b.sent(),b.label=4;case 4:return[2,a];}})})},f.prototype.getHeaders=function(){var a,b=(a={},a[this.headerName]=this.clientKey,a.Accept="application/json",a["Content-Type"]="application/json",a["If-None-Match"]=this.etag,a);return Object.entries(this.customHeaders).filter(w).forEach(function(a){var c=a[0],d=a[1];return b[c]=d}),b},f.prototype.storeToggles=function(a){return c(this,void 0,void 0,function(){return d(this,function(b){switch(b.label){case 0:return this.toggles=a,this.emit(y.UPDATE),[4,this.storage.save(B,a)];case 1:return b.sent(),[2];}})})},f.prototype.fetchToggles=function(){return c(this,void 0,void 0,function(){var a,b,c,e,f;return d(this,function(d){switch(d.label){case 0:if(!this.fetch)return[3,8];d.label=1;case 1:return d.trys.push([1,7,,8]),a=this.context,b=new URL(this.url.toString()),Object.entries(a).filter(w).forEach(function(a){var c=a[0],d=a[1];"properties"===c&&d?Object.entries(d).filter(w).forEach(function(a){var c=a[0],d=a[1];return b.searchParams.append("properties[".concat(c,"]"),d)}):b.searchParams.append(c,d)}),[4,this.fetch(b.toString(),{cache:"no-cache",headers:this.getHeaders()})];case 2:return(c=d.sent(),!(c.ok&&304!==c.status))?[3,5]:(this.etag=c.headers.get("ETag")||"",[4,c.json()]);case 3:return e=d.sent(),[4,this.storeToggles(e.toggles)];case 4:return d.sent(),this.bootstrap||this.readyEventEmitted||(this.emit(y.READY),this.readyEventEmitted=!0),[3,6];case 5:c.ok||(console.error("Unleash: Fetching feature toggles did not have an ok response"),this.emit(y.ERROR,{type:"HttpError",code:c.status})),d.label=6;case 6:return[3,8];case 7:return f=d.sent(),console.error("Unleash: unable to fetch feature toggles",f),this.emit(y.ERROR,f),[3,8];case 8:return[2];}})})},f}(m);a.EVENTS=y,a.InMemoryStorageProvider=o,a.LocalStorageProvider=p,a.UnleashClient=D,a.resolveFetch=C,Object.defineProperty(a,"__esModule",{value:!0})}); | ||
(function(a,b){"object"==typeof exports&&"undefined"!=typeof module?b(exports):"function"==typeof define&&define.amd?define(["exports"],b):(a=a||self,b(a.unleash={}))})(this,function(a){"use strict";function b(a,c){function b(){this.constructor=a}if("function"!=typeof c&&null!==c)throw new TypeError("Class extends value "+(c+"")+" is not a constructor or null");k(a,c),a.prototype=null===c?Object.create(c):(b.prototype=c.prototype,new b)}function c(a,b,c,d){function e(a){return a instanceof c?a:new c(function(b){b(a)})}return new(c||(c=Promise))(function(c,f){function g(a){try{i(d.next(a))}catch(a){f(a)}}function h(a){try{i(d["throw"](a))}catch(a){f(a)}}function i(a){a.done?c(a.value):e(a.value).then(g,h)}i((d=d.apply(a,b||[])).next())})}function d(a,b){function c(a){return function(b){return d([a,b])}}function d(c){if(e)throw new TypeError("Generator is already executing.");for(;k;)try{if(e=1,h&&(i=2&c[0]?h["return"]:c[0]?h["throw"]||((i=h["return"])&&i.call(h),0):h.next)&&!(i=i.call(h,c[1])).done)return i;switch((h=0,i)&&(c=[2&c[0],i.value]),c[0]){case 0:case 1:i=c;break;case 4:return k.label++,{value:c[1],done:!1};case 5:k.label++,h=c[1],c=[0];continue;case 7:c=k.ops.pop(),k.trys.pop();continue;default:if((i=k.trys,!(i=0<i.length&&i[i.length-1]))&&(6===c[0]||2===c[0])){k=0;continue}if(3===c[0]&&(!i||c[1]>i[0]&&c[1]<i[3])){k.label=c[1];break}if(6===c[0]&&k.label<i[1]){k.label=i[1],i=c;break}if(i&&k.label<i[2]){k.label=i[2],k.ops.push(c);break}i[2]&&k.ops.pop(),k.trys.pop();continue;}c=b.call(a,k)}catch(a){c=[6,a],h=0}finally{e=i=0}if(5&c[0])throw c[1];return{value:c[0]?c[1]:void 0,done:!0}}var e,h,i,j,k={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]};return j={next:c(0),throw:c(1),return:c(2)},"function"==typeof Symbol&&(j[Symbol.iterator]=function(){return this}),j}function e(a,b,c){if(c||2===arguments.length)for(var d,e=0,f=b.length;e<f;e++)(d||!(e in b))&&(d||(d=Array.prototype.slice.call(b,0,e)),d[e]=b[e]);return a.concat(d||Array.prototype.slice.call(b))}function f(){}function g(){return s>r.length-16&&(q.randomFillSync(r),s=0),r.slice(s,s+=16)}function h(a){return"string"==typeof a&&t.test(a)}function i(a,b=0){const c=(u[a[b+0]]+u[a[b+1]]+u[a[b+2]]+u[a[b+3]]+"-"+u[a[b+4]]+u[a[b+5]]+"-"+u[a[b+6]]+u[a[b+7]]+"-"+u[a[b+8]]+u[a[b+9]]+"-"+u[a[b+10]]+u[a[b+11]]+u[a[b+12]]+u[a[b+13]]+u[a[b+14]]+u[a[b+15]]).toLowerCase();if(!h(c))throw TypeError("Stringified UUID is invalid");return c}function j(a,b,c){a=a||{};const d=a.random||(a.rng||g)();if(d[6]=64|15&d[6],d[8]=128|63&d[8],b){c=c||0;for(let a=0;16>a;++a)b[c+a]=d[a];return b}return i(d)}var k=function(a,c){return k=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(a,c){a.__proto__=c}||function(a,c){for(var b in c)Object.prototype.hasOwnProperty.call(c,b)&&(a[b]=c[b])},k(a,c)},l=function(){return l=Object.assign||function(a){for(var b,c=1,d=arguments.length;c<d;c++)for(var e in b=arguments[c],b)Object.prototype.hasOwnProperty.call(b,e)&&(a[e]=b[e]);return a},l.apply(this,arguments)};f.prototype={on:function(a,b,c){var d=this.e||(this.e={});return(d[a]||(d[a]=[])).push({fn:b,ctx:c}),this},once:function(a,b,c){function d(){e.off(a,d),b.apply(c,arguments)}var e=this;return d._=b,this.on(a,d,c)},emit:function(a){var b=[].slice.call(arguments,1),c=((this.e||(this.e={}))[a]||[]).slice(),d=0,e=c.length;for(d;d<e;d++)c[d].fn.apply(c[d].ctx,b);return this},off:function(a,b){var c=this.e||(this.e={}),d=c[a],e=[];if(d&&b)for(var f=0,g=d.length;f<g;f++)d[f].fn!==b&&d[f].fn._!==b&&e.push(d[f]);return e.length?c[a]=e:delete c[a],this}};var m=f;f.TinyEmitter=m;var n=function(){function a(a){var b=a.onError,c=a.appName,d=a.metricsInterval,e=a.disableMetrics,f=a.url,g=a.clientKey,h=a.fetch,i=a.headerName;this.onError=b,this.disabled=void 0!==e&&e,this.metricsInterval=1e3*d,this.appName=c,this.url=f instanceof URL?f:new URL(f),this.clientKey=g,this.bucket=this.createEmptyBucket(),this.fetch=h,this.headerName=i}return a.prototype.start=function(){var a=this;return!this.disabled&&void("number"==typeof this.metricsInterval&&0<this.metricsInterval&&setTimeout(function(){a.startTimer(),a.sendMetrics()},2e3))},a.prototype.stop=function(){this.timer&&(clearTimeout(this.timer),delete this.timer)},a.prototype.createEmptyBucket=function(){return{start:new Date,stop:null,toggles:{}}},a.prototype.sendMetrics=function(){return c(this,void 0,void 0,function(){var a,b,c,e;return d(this,function(d){switch(d.label){case 0:if(a="".concat(this.url,"/client/metrics"),b=this.getPayload(),this.bucketIsEmpty(b))return[2];d.label=1;case 1:return d.trys.push([1,3,,4]),[4,this.fetch(a,{cache:"no-cache",method:"POST",headers:(e={},e[this.headerName]=this.clientKey,e.Accept="application/json",e["Content-Type"]="application/json",e),body:JSON.stringify(b)})];case 2:return d.sent(),[3,4];case 3:return c=d.sent(),console.error("Unleash: unable to send feature metrics",c),this.onError(c),[3,4];case 4:return[2];}})})},a.prototype.count=function(a,b){return!this.disabled&&this.bucket&&(this.assertBucket(a),this.bucket.toggles[a][b?"yes":"no"]++,!0)},a.prototype.assertBucket=function(a){return!this.disabled&&this.bucket&&void(!this.bucket.toggles[a]&&(this.bucket.toggles[a]={yes:0,no:0}))},a.prototype.startTimer=function(){var a=this;this.timer=setInterval(function(){a.sendMetrics()},this.metricsInterval)},a.prototype.bucketIsEmpty=function(a){return 0===Object.keys(a.bucket.toggles).length},a.prototype.getPayload=function(){var a=l(l({},this.bucket),{stop:new Date});return this.bucket=this.createEmptyBucket(),{bucket:a,appName:this.appName,instanceId:"browser"}},a}(),o=function(){function a(){this.store=new Map}return a.prototype.save=function(a,b){return c(this,void 0,void 0,function(){return d(this,function(){return this.store.set(a,b),[2]})})},a.prototype.get=function(a){return c(this,void 0,void 0,function(){return d(this,function(){return[2,this.store.get(a)]})})},a}(),p=function(){function a(){this.prefix="unleash:repository"}return a.prototype.save=function(a,b){return c(this,void 0,void 0,function(){var c,e;return d(this,function(){c=JSON.stringify(b),e="".concat(this.prefix,":").concat(a);try{window.localStorage.setItem(e,c)}catch(a){console.error(a)}return[2]})})},a.prototype.get=function(a){try{var b="".concat(this.prefix,":").concat(a),c=window.localStorage.getItem(b);return c?JSON.parse(c):void 0}catch(a){console.error(a)}},a}(),q={};const r=new Uint8Array(256);let s=r.length;var t=/^(?:[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}|00000000-0000-0000-0000-000000000000)$/i;const u=[];for(let b=0;256>b;++b)u.push((b+256).toString(16).substr(1));var v=function(){function a(){}return a.prototype.generateEventId=function(){return j()},a.prototype.createImpressionEvent=function(a,b,c,d,e){var f=this.createBaseEvent(a,b,c,d);return e?l(l({},f),{variant:e}):f},a.prototype.createBaseEvent=function(a,b,c,d){return{eventType:d,eventId:this.generateEventId(),context:a,enabled:b,featureName:c}},a}(),w=function(a){var b=a[1];return b!==void 0&&null!==b},x=["userId","sessionId","remoteAddress"],y={INIT:"initialized",ERROR:"error",READY:"ready",UPDATE:"update",IMPRESSION:"impression"},z={IS_ENABLED:"isEnabled",GET_VARIANT:"getVariant"},A={name:"disabled",enabled:!1},B="repo",C=function(){try{if("fetch"in window)return fetch.bind(window);if("fetch"in globalThis)return fetch.bind(globalThis)}catch(a){console.error("Unleash failed to resolve \"fetch\"",a)}},D=function(a){function f(b){var c=b.storageProvider,d=b.url,e=b.clientKey,f=b.disableRefresh,g=b.refreshInterval,h=void 0===g?30:g,i=b.metricsInterval,j=void 0===i?30:i,k=b.disableMetrics,m=b.appName,o=b.environment,q=void 0===o?"default":o,r=b.context,s=b.fetch,t=void 0===s?C():s,u=b.bootstrap,w=b.bootstrapOverride,x=b.headerName,z=void 0===x?"Authorization":x,A=b.customHeaders,B=void 0===A?{}:A,D=a.call(this)||this;if(D.toggles=[],D.etag="",D.readyEventEmitted=!1,!d)throw new Error("url is required");if(!e)throw new Error("clientKey is required");if(!m)throw new Error("appName is required.");return D.eventsHandler=new v,D.toggles=u&&0<u.length?u:[],D.url=d instanceof URL?d:new URL(d),D.clientKey=e,D.headerName=z,D.customHeaders=B,D.storage=c||new p,D.refreshInterval=void 0!==f&&f?0:1e3*h,D.context=l({appName:m,environment:q},r),D.ready=new Promise(function(a){D.init().then(a).catch(function(b){console.error(b),D.emit(y.ERROR,b),a()})}),t||console.error("Unleash: You must either provide your own \"fetch\" implementation or run in an environment where \"fetch\" is available."),D.fetch=t,D.bootstrap=u&&0<u.length?u:void 0,D.bootstrapOverride=void 0===w||w,D.metrics=new n({onError:D.emit.bind(D,y.ERROR),appName:m,metricsInterval:j,disableMetrics:void 0!==k&&k,url:D.url,clientKey:e,fetch:t,headerName:z}),D}return b(f,a),f.prototype.getAllToggles=function(){return e([],this.toggles,!0)},f.prototype.isEnabled=function(a){var b=this.toggles.find(function(b){return b.name===a}),c=!!b&&b.enabled;if(this.metrics.count(a,c),null===b||void 0===b?void 0:b.impressionData){var d=this.eventsHandler.createImpressionEvent(this.context,c,a,z.IS_ENABLED);this.emit(y.IMPRESSION,d)}return c},f.prototype.getVariant=function(a){var b=this.toggles.find(function(b){return b.name===a});if(b){if(this.metrics.count(a,!0),b.impressionData){var c=this.eventsHandler.createImpressionEvent(this.context,b.enabled,a,z.GET_VARIANT,b.variant.name);this.emit(y.IMPRESSION,c)}return b.variant}return this.metrics.count(a,!1),A},f.prototype.updateContext=function(a){return c(this,void 0,void 0,function(){var b;return d(this,function(c){switch(c.label){case 0:return(a.appName||a.environment)&&console.warn("appName and environment are static. They can't be updated with updateContext."),b={environment:this.context.environment,appName:this.context.appName},this.context=l(l({},b),a),this.timerRef?[4,this.fetchToggles()]:[3,2];case 1:c.sent(),c.label=2;case 2:return[2];}})})},f.prototype.getContext=function(){return l({},this.context)},f.prototype.setContextField=function(a,b){var c,d;if(x.includes(a))this.context=l(l({},this.context),(c={},c[a]=b,c));else{var e=l(l({},this.context.properties),(d={},d[a]=b,d));this.context=l(l({},this.context),{properties:e})}this.timerRef&&this.fetchToggles()},f.prototype.init=function(){return c(this,void 0,void 0,function(){var a,b;return d(this,function(c){switch(c.label){case 0:return[4,this.resolveSessionId()];case 1:return a=c.sent(),this.context=l({sessionId:a},this.context),b=this,[4,this.storage.get(B)];case 2:return b.toggles=c.sent()||[],this.bootstrap&&(this.bootstrapOverride||0===this.toggles.length)?[4,this.storage.save(B,this.bootstrap)]:[3,4];case 3:c.sent(),this.toggles=this.bootstrap,this.emit(y.READY),c.label=4;case 4:return this.emit(y.INIT),[2];}})})},f.prototype.start=function(){return c(this,void 0,void 0,function(){var a,b=this;return d(this,function(c){switch(c.label){case 0:return this.timerRef?(console.error("Unleash SDK has already started, if you want to restart the SDK you should call client.stop() before starting again."),[2]):[4,this.ready];case 1:return c.sent(),this.metrics.start(),a=this.refreshInterval,[4,this.fetchToggles()];case 2:return c.sent(),0<a&&(this.timerRef=setInterval(function(){return b.fetchToggles()},a)),[2];}})})},f.prototype.stop=function(){this.timerRef&&(clearInterval(this.timerRef),this.timerRef=void 0),this.metrics.stop()},f.prototype.resolveSessionId=function(){return c(this,void 0,void 0,function(){var a;return d(this,function(b){var c=Math.floor;switch(b.label){case 0:return this.context.sessionId?[2,this.context.sessionId]:[3,1];case 1:return[4,this.storage.get("sessionId")];case 2:return(a=b.sent(),!!a)?[3,4]:(a=c(1e9*Math.random()),[4,this.storage.save("sessionId",a)]);case 3:b.sent(),b.label=4;case 4:return[2,a];}})})},f.prototype.getHeaders=function(){var a,b=(a={},a[this.headerName]=this.clientKey,a.Accept="application/json",a["Content-Type"]="application/json",a["If-None-Match"]=this.etag,a);return Object.entries(this.customHeaders).filter(w).forEach(function(a){var c=a[0],d=a[1];return b[c]=d}),b},f.prototype.storeToggles=function(a){return c(this,void 0,void 0,function(){return d(this,function(b){switch(b.label){case 0:return this.toggles=a,this.emit(y.UPDATE),[4,this.storage.save(B,a)];case 1:return b.sent(),[2];}})})},f.prototype.fetchToggles=function(){return c(this,void 0,void 0,function(){var a,b,c,e,f;return d(this,function(d){switch(d.label){case 0:if(!this.fetch)return[3,8];d.label=1;case 1:return d.trys.push([1,7,,8]),a=this.context,b=new URL(this.url.toString()),Object.entries(a).filter(w).forEach(function(a){var c=a[0],d=a[1];"properties"===c&&d?Object.entries(d).filter(w).forEach(function(a){var c=a[0],d=a[1];return b.searchParams.append("properties[".concat(c,"]"),d)}):b.searchParams.append(c,d)}),[4,this.fetch(b.toString(),{cache:"no-cache",headers:this.getHeaders()})];case 2:return(c=d.sent(),!(c.ok&&304!==c.status))?[3,5]:(this.etag=c.headers.get("ETag")||"",[4,c.json()]);case 3:return e=d.sent(),[4,this.storeToggles(e.toggles)];case 4:return d.sent(),this.bootstrap||this.readyEventEmitted||(this.emit(y.READY),this.readyEventEmitted=!0),[3,6];case 5:c.ok||304===c.status||(console.error("Unleash: Fetching feature toggles did not have an ok response"),this.emit(y.ERROR,{type:"HttpError",code:c.status})),d.label=6;case 6:return[3,8];case 7:return f=d.sent(),console.error("Unleash: unable to fetch feature toggles",f),this.emit(y.ERROR,f),[3,8];case 8:return[2];}})})},f}(m);a.EVENTS=y,a.InMemoryStorageProvider=o,a.LocalStorageProvider=p,a.UnleashClient=D,a.resolveFetch=C,Object.defineProperty(a,"__esModule",{value:!0})}); |
{ | ||
"name": "unleash-proxy-client", | ||
"version": "2.2.0", | ||
"version": "2.2.1", | ||
"description": "A browser client that can be used together with the unleash-proxy.", | ||
"main": "./build/index.js", | ||
"types": "./build/index.d.ts", | ||
"browser": "./build/main.min.js", | ||
"files": [ | ||
@@ -9,0 +8,0 @@ "build", |
@@ -107,3 +107,3 @@ # Unleash Proxy Client for the browser (JS) | ||
| bootstrap | no | `[]` | Allows you to bootstrap the cached feature toggle configuration. | | ||
| bootstrapOverride | no| `true` | Should the boostrap automatically override cached data in the local-storage. Will only be used if boostrap is not an empty array. | | ||
| bootstrapOverride | no| `true` | Should the bootstrap automatically override cached data in the local-storage. Will only be used if bootstrap is not an empty array. | | ||
| headerName | no| `Authorization` | Provides possiblity to specify custom header that is passed to Unleash / Unleash Proxy with the `clientKey` | | ||
@@ -110,0 +110,0 @@ | customHeaders | no| `{}` | Additional headers to use when making HTTP requests to the Unleash proxy. In case of name collisions with the default headers, the `customHeaders` value will be used. | |
@@ -371,5 +371,10 @@ import { TinyEmitter } from 'tiny-emitter'; | ||
} | ||
} else if (!response.ok) { | ||
console.error('Unleash: Fetching feature toggles did not have an ok response'); | ||
this.emit(EVENTS.ERROR, { type: 'HttpError', code: response.status }) | ||
} else if (!response.ok && response.status !== 304) { | ||
console.error( | ||
'Unleash: Fetching feature toggles did not have an ok response' | ||
); | ||
this.emit(EVENTS.ERROR, { | ||
type: 'HttpError', | ||
code: response.status, | ||
}); | ||
} | ||
@@ -376,0 +381,0 @@ } catch (e) { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
165138
2861