unleash-proxy-client
Advanced tools
Comparing version 0.0.11 to 0.1.0
"use strict"; | ||
var __extends = (this && this.__extends) || (function () { | ||
var extendStatics = function (d, b) { | ||
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 extendStatics(d, b); | ||
}; | ||
return function (d, b) { | ||
extendStatics(d, b); | ||
function __() { this.constructor = d; } | ||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
}; | ||
})(); | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
@@ -39,11 +51,18 @@ }); | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var tiny_emitter_1 = require("tiny-emitter"); | ||
var storage_provider_local_1 = require("./storage-provider-local"); | ||
exports.EVENTS = { | ||
READY: 'ready', | ||
UPDATE: 'update', | ||
}; | ||
var defaultVariant = { name: 'disabled' }; | ||
var storeKey = 'repo'; | ||
var UnleashClient = /** @class */ (function () { | ||
var UnleashClient = /** @class */ (function (_super) { | ||
__extends(UnleashClient, _super); | ||
function UnleashClient(config, context) { | ||
this.toggles = []; | ||
this.etag = ''; | ||
this.storage = config.storageProvider || new storage_provider_local_1.default(); | ||
this.refreshInterval = (config.refreshInterval || 30) * 1000; | ||
var _this = _super.call(this) || this; | ||
_this.toggles = []; | ||
_this.etag = ''; | ||
_this.storage = config.storageProvider || new storage_provider_local_1.default(); | ||
_this.refreshInterval = (config.refreshInterval || 30) * 1000; | ||
// Validations | ||
@@ -56,6 +75,7 @@ if (!config.url) { | ||
} | ||
this.url = new URL(config.url + "/proxy"); | ||
this.clientKey = config.clientKey; | ||
this.context = context || {}; | ||
this.toggles = this.storage.get(storeKey) || []; | ||
_this.url = new URL(config.url + "/proxy"); | ||
_this.clientKey = config.clientKey; | ||
_this.context = context || {}; | ||
_this.toggles = _this.storage.get(storeKey) || []; | ||
return _this; | ||
} | ||
@@ -72,2 +92,3 @@ UnleashClient.prototype.isEnabled = function (toggleName) { | ||
this.context = context; | ||
this.fetchToggles(); | ||
}; | ||
@@ -87,2 +108,3 @@ UnleashClient.prototype.start = function () { | ||
_a.sent(); | ||
this.emit(exports.EVENTS.READY); | ||
this.timerRef = setInterval(function () { return _this.fetchToggles(); }, interval); | ||
@@ -106,2 +128,3 @@ return [3 /*break*/, 3]; | ||
this.toggles = toggles; | ||
this.emit(exports.EVENTS.UPDATE); | ||
this.storage.save(storeKey, toggles); | ||
@@ -152,4 +175,4 @@ }; | ||
return UnleashClient; | ||
}()); | ||
}(tiny_emitter_1.TinyEmitter)); | ||
exports.UnleashClient = UnleashClient; | ||
//# sourceMappingURL=index.js.map |
@@ -1,1 +0,1 @@ | ||
var unleash=function(a){'use strict';function b(a,b,c,d){return new(c||(c=Promise))(function(e,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?e(a.value):new c(function(b){b(a.value)}).then(g,h)}i((d=d.apply(a,b||[])).next())})}function c(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}var d=function(){function a(){this.prefix="unleash:repository"}return a.prototype.save=function(a,b){try{var c=JSON.stringify(b),d=this.prefix+":"+a;window.localStorage.setItem(d,c)}catch(a){console.error(a)}},a.prototype.get=function(a){try{var b=this.prefix+":"+a,c=window.localStorage.getItem(b);return c?JSON.parse(c):void 0}catch(a){console.error(a)}},a}(),e={name:"disabled"},f="repo",g=function(){function a(a,b){if(this.toggles=[],this.etag="",this.storage=a.storageProvider||new d,this.refreshInterval=1e3*(a.refreshInterval||30),!a.url)throw new Error("You have to specify the url!");if(!a.clientKey)throw new Error("You have to specify the clientKey!");this.url=new URL(a.url+"/proxy"),this.clientKey=a.clientKey,this.context=b||{},this.toggles=this.storage.get(f)||[]}return a.prototype.isEnabled=function(a){var b=this.toggles.find(function(b){return b.name===a});return!!b&&b.enabled},a.prototype.getVariant=function(a){var b=this.toggles.find(function(b){return b.name===a});return b?b.variant:e},a.prototype.updateContext=function(a){this.context=a},a.prototype.start=function(){return b(this,void 0,void 0,function(){var a,b=this;return c(this,function(c){switch(c.label){case 0:return fetch?(this.stop(),a=this.refreshInterval,[4,this.fetchToggles()]):[3,2];case 1:return c.sent(),this.timerRef=setInterval(function(){return b.fetchToggles()},a),[3,3];case 2:console.error("Unleash: Client does not support fetch."),c.label=3;case 3:return[2];}})})},a.prototype.stop=function(){this.timerRef&&clearInterval(this.timerRef)},a.prototype.storeToggles=function(a){this.toggles=a,this.storage.save(f,a)},a.prototype.fetchToggles=function(){return b(this,void 0,void 0,function(){var a,b,d,e,f;return c(this,function(c){switch(c.label){case 0:if(!fetch)return[3,6];c.label=1;case 1:return c.trys.push([1,5,,6]),a=this.context,b=new URL(this.url.toString()),Object.keys(a).forEach(function(c){return b.searchParams.append(c,a[c])}),[4,fetch(b.toString(),{cache:"no-cache",headers:{Authorization:this.clientKey,Accept:"application/json","Content-Type":"application/json","If-None-Match":this.etag}})];case 2:return(d=c.sent(),!(d.ok&&304!==d.status))?[3,4]:(this.etag=d.headers.get("ETag")||"",[4,d.json()]);case 3:e=c.sent(),this.storeToggles(e.toggles),c.label=4;case 4:return[3,6];case 5:return f=c.sent(),console.error("Unleash: unable to fetch feature toggles",f),[3,6];case 6:return[2];}})})},a}();return a.UnleashClient=g,a}({}); | ||
var unleash=function(a){'use strict';function b(a,c){function b(){this.constructor=a}f(a,c),a.prototype=null===c?Object.create(c):(b.prototype=c.prototype,new b)}function c(a,b,c,d){return new(c||(c=Promise))(function(e,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?e(a.value):new c(function(b){b(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(){}var f=function(a,c){return f=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(a,c){a.__proto__=c}||function(a,c){for(var b in c)c.hasOwnProperty(b)&&(a[b]=c[b])},f(a,c)};e.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 g=e;e.TinyEmitter=g;var h=function(){function a(){this.prefix="unleash:repository"}return a.prototype.save=function(a,b){try{var c=JSON.stringify(b),d=this.prefix+":"+a;window.localStorage.setItem(d,c)}catch(a){console.error(a)}},a.prototype.get=function(a){try{var b=this.prefix+":"+a,c=window.localStorage.getItem(b);return c?JSON.parse(c):void 0}catch(a){console.error(a)}},a}(),i={READY:"ready",UPDATE:"update"},j={name:"disabled"},k="repo",l=function(a){function e(b,c){var d=a.call(this)||this;if(d.toggles=[],d.etag="",d.storage=b.storageProvider||new h,d.refreshInterval=1e3*(b.refreshInterval||30),!b.url)throw new Error("You have to specify the url!");if(!b.clientKey)throw new Error("You have to specify the clientKey!");return d.url=new URL(b.url+"/proxy"),d.clientKey=b.clientKey,d.context=c||{},d.toggles=d.storage.get(k)||[],d}return b(e,a),e.prototype.isEnabled=function(a){var b=this.toggles.find(function(b){return b.name===a});return!!b&&b.enabled},e.prototype.getVariant=function(a){var b=this.toggles.find(function(b){return b.name===a});return b?b.variant:j},e.prototype.updateContext=function(a){this.context=a,this.fetchToggles()},e.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 fetch?(this.stop(),a=this.refreshInterval,[4,this.fetchToggles()]):[3,2];case 1:return c.sent(),this.emit(i.READY),this.timerRef=setInterval(function(){return b.fetchToggles()},a),[3,3];case 2:console.error("Unleash: Client does not support fetch."),c.label=3;case 3:return[2];}})})},e.prototype.stop=function(){this.timerRef&&clearInterval(this.timerRef)},e.prototype.storeToggles=function(a){this.toggles=a,this.emit(i.UPDATE),this.storage.save(k,a)},e.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(!fetch)return[3,6];d.label=1;case 1:return d.trys.push([1,5,,6]),a=this.context,b=new URL(this.url.toString()),Object.keys(a).forEach(function(c){return b.searchParams.append(c,a[c])}),[4,fetch(b.toString(),{cache:"no-cache",headers:{Authorization:this.clientKey,Accept:"application/json","Content-Type":"application/json","If-None-Match":this.etag}})];case 2:return(c=d.sent(),!(c.ok&&304!==c.status))?[3,4]:(this.etag=c.headers.get("ETag")||"",[4,c.json()]);case 3:e=d.sent(),this.storeToggles(e.toggles),d.label=4;case 4:return[3,6];case 5:return f=d.sent(),console.error("Unleash: unable to fetch feature toggles",f),[3,6];case 6:return[2];}})})},e}(g);return a.EVENTS=i,a.UnleashClient=l,a}({}); |
@@ -9,3 +9,3 @@ { | ||
"react-scripts": "3.1.1", | ||
"unleash-proxy-client": "^0.0.10" | ||
"unleash-proxy-client": "^0.0.11" | ||
}, | ||
@@ -12,0 +12,0 @@ "scripts": { |
{ | ||
"name": "unleash-proxy-client", | ||
"version": "0.0.11", | ||
"version": "0.1.0", | ||
"description": "A browser client that can be used together with the unleash-proxy.", | ||
@@ -34,2 +34,4 @@ "main": "./build/index.js", | ||
"rollup-plugin-babel-minify": "^9.0.0", | ||
"rollup-plugin-commonjs": "^10.1.0", | ||
"rollup-plugin-node-resolve": "^5.2.0", | ||
"rollup-plugin-typescript2": "^0.24.0", | ||
@@ -42,3 +44,5 @@ "rollup-plugin-uglify": "^6.0.2", | ||
}, | ||
"dependencies": {} | ||
"dependencies": { | ||
"tiny-emitter": "^2.1.0" | ||
} | ||
} |
# Unleash Proxy Client for the browser (js) | ||
This is a super tiny (80Byte gzipped) Unleash Client SDK you can use together with the | ||
This is a tiny Unleash Client SDK you can use together with the | ||
[Unleash Hosted Proxy](https://www.unleash-hosted.com/articles/the-unleash-proxy). | ||
@@ -50,2 +50,13 @@ This makes it super simple to use Unleash-hosted from any single page app. | ||
**Listen for updates via the EventEmitter** | ||
The client is also an event emitter. This means that your code can subscribe to updates from the client. This is a neat way to update a single page app when toggle state updates. | ||
```js | ||
unleash.subscribe('update', () => { | ||
const myToggle = unleash.isEnabled('proxy.demo'); | ||
//do something useful | ||
}); | ||
``` | ||
## How to use the client via CDN. | ||
@@ -52,0 +63,0 @@ |
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
490605
536
80
1
13
+ Addedtiny-emitter@^2.1.0
+ Addedtiny-emitter@2.1.0(transitive)