unleash-client
Advanced tools
Comparing version 1.0.0-beta.1 to 1.0.0-beta.2
@@ -15,3 +15,4 @@ const { Strategy, initialize, isEnabled } = require('../lib'); | ||
const client = initialize({ | ||
url: 'http://unleash.herokuapp.com/features', | ||
appName: 'my-application', | ||
url: 'http://unleash.herokuapp.com/', | ||
refreshInterval: 10000, | ||
@@ -21,2 +22,5 @@ strategies: [new ActiveForUserWithEmailStrategy()], | ||
client.on('error', console.error); | ||
client.on('warn', console.log); | ||
console.log('Fetching toggles from: http://unleash.herokuapp.com', client); | ||
@@ -23,0 +27,0 @@ |
const { initialize, isEnabled } = require('../lib'); | ||
initialize({ | ||
url: 'http://unleash.herokuapp.com/features', | ||
const client = initialize({ | ||
appName: 'my-application', | ||
url: 'http://unleash.herokuapp.com/', | ||
}); | ||
client.on('error', console.error); | ||
client.on('warn', console.log); | ||
console.log('Fetching toggles from: http://unleash.herokuapp.com'); | ||
@@ -8,0 +12,0 @@ |
@@ -13,23 +13,24 @@ 'use strict'; | ||
const unleashInstance = new Unleash({ | ||
const client = new Unleash({ | ||
appName: 'super-app', | ||
backupPath: __dirname, | ||
url: 'http://localhost:1234', | ||
strategies, | ||
errorHandler: (err: Error) => console.error(err) | ||
}); | ||
console.log('featureX', unleashInstance.isEnabled('feature-1', {})) | ||
client.on('error', console.error); | ||
client.on('warn', console.log); | ||
unleashInstance.on('ready', () => { | ||
client.on('ready', () => { | ||
setTimeout(() => { | ||
console.log('featureX', unleashInstance.isEnabled('featureX', {})); | ||
console.log('featureY', unleashInstance.isEnabled('featureY', {})); | ||
console.log('featureX', client.isEnabled('featureX', {})); | ||
console.log('featureY', client.isEnabled('featureY', {})); | ||
}, 100) | ||
}); | ||
console.log('featureX', client.isEnabled('featureX', {})) | ||
setInterval(() => { | ||
console.log('featureX', unleashInstance.isEnabled('featureX', {})); | ||
console.log('featureY', unleashInstance.isEnabled('featureY', {})); | ||
console.log('featureX', client.isEnabled('featureX', {})); | ||
console.log('featureY', client.isEnabled('featureY', {})); | ||
}, 5000); |
@@ -0,10 +1,11 @@ | ||
/// <reference types="node" /> | ||
import { EventEmitter } from 'events'; | ||
import { Strategy } from './strategy'; | ||
import Repository from './repository'; | ||
export default class UnleashClient { | ||
export default class UnleashClient extends EventEmitter { | ||
private repository; | ||
private strategies; | ||
private errorHandler; | ||
constructor(repository: Repository, strategies: Strategy[], errorHandler: any); | ||
constructor(repository: Repository, strategies: Strategy[]); | ||
private getStrategy(name); | ||
isEnabled(name: string, context: any, fallbackValue?: boolean): boolean; | ||
} |
'use strict'; | ||
var UnleashClient = (function () { | ||
function UnleashClient(repository, strategies, errorHandler) { | ||
var __extends = (this && this.__extends) || function (d, b) { | ||
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; | ||
function __() { this.constructor = d; } | ||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
}; | ||
var events_1 = require('events'); | ||
var UnleashClient = (function (_super) { | ||
__extends(UnleashClient, _super); | ||
function UnleashClient(repository, strategies) { | ||
_super.call(this); | ||
this.repository = repository; | ||
this.errorHandler = errorHandler || function () { }; | ||
this.strategies = strategies || []; | ||
@@ -38,3 +45,3 @@ strategies.forEach(function (strategy) { | ||
if (!Array.isArray(feature.strategies)) { | ||
this.errorHandler(new Error("Malformed feature, strategies not an array, is a " + typeof feature.strategies)); | ||
this.emit('error', new Error("Malformed feature, strategies not an array, is a " + typeof feature.strategies)); | ||
return false; | ||
@@ -49,3 +56,3 @@ } | ||
if (!strategy) { | ||
_this.errorHandler(new Error("Missing strategy " + strategySelector.name)); | ||
_this.emit('warn', "Missing strategy " + strategySelector.name); | ||
return false; | ||
@@ -57,5 +64,5 @@ } | ||
return UnleashClient; | ||
}()); | ||
}(events_1.EventEmitter)); | ||
exports.__esModule = true; | ||
exports["default"] = UnleashClient; | ||
//# sourceMappingURL=client.js.map |
@@ -49,3 +49,3 @@ "use strict"; | ||
} | ||
var url = url_1.resolve(this.url, '/client/register'); | ||
var url = url_1.resolve(this.url, './client/register'); | ||
var payload = this.getClientData(); | ||
@@ -78,3 +78,3 @@ request_1.post({ | ||
} | ||
var url = url_1.resolve(this.url, '/client/metrics'); | ||
var url = url_1.resolve(this.url, './client/metrics'); | ||
var payload = this.getPayload(); | ||
@@ -81,0 +81,0 @@ request_1.post({ |
/// <reference types="node" /> | ||
import { EventEmitter } from 'events'; | ||
import { Storage } from './storage'; | ||
import { FeatureInterface } from './feature'; | ||
export interface StorageImpl { | ||
new (Storage: any): any; | ||
} | ||
export interface RepositoryOptions { | ||
backupPath: string; | ||
url: string; | ||
appName: string; | ||
instanceId: string; | ||
refreshInterval?: number; | ||
StorageImpl?: StorageImpl; | ||
} | ||
export default class Repository extends EventEmitter implements EventEmitter { | ||
@@ -10,5 +20,6 @@ private timer; | ||
private etag; | ||
private requestId; | ||
private appName; | ||
private instanceId; | ||
private refreshInterval?; | ||
constructor(backupPath: string, url: string, requestId: string, refreshInterval?: number, StorageImpl?: typeof Storage); | ||
constructor({backupPath, url, appName, instanceId, refreshInterval, StorageImpl}: RepositoryOptions); | ||
timedFetch(): void; | ||
@@ -15,0 +26,0 @@ validateFeature(feature: FeatureInterface): void; |
@@ -12,11 +12,13 @@ 'use strict'; | ||
var request_1 = require('./request'); | ||
; | ||
var Repository = (function (_super) { | ||
__extends(Repository, _super); | ||
function Repository(backupPath, url, requestId, refreshInterval, StorageImpl) { | ||
function Repository(_a) { | ||
var _this = this; | ||
if (StorageImpl === void 0) { StorageImpl = storage_1.Storage; } | ||
var backupPath = _a.backupPath, url = _a.url, appName = _a.appName, instanceId = _a.instanceId, refreshInterval = _a.refreshInterval, _b = _a.StorageImpl, StorageImpl = _b === void 0 ? storage_1.Storage : _b; | ||
_super.call(this); | ||
this.url = url; | ||
this.refreshInterval = refreshInterval; | ||
this.requestId = requestId; | ||
this.instanceId = instanceId; | ||
this.appName = appName; | ||
this.storage = new StorageImpl(backupPath); | ||
@@ -49,7 +51,8 @@ this.storage.on('error', function (err) { return _this.emit('error', err); }); | ||
var _this = this; | ||
var url = url_1.resolve(this.url, '/features'); | ||
var url = url_1.resolve(this.url, './features'); | ||
request_1.get({ | ||
url: url, | ||
etag: this.etag, | ||
requestId: this.requestId | ||
appName: this.appName, | ||
instanceId: this.instanceId | ||
}, function (error, res, body) { | ||
@@ -56,0 +59,0 @@ // start timer for next fetch |
@@ -8,3 +8,4 @@ /// <reference types="request" /> | ||
etag?: string; | ||
requestId?: string; | ||
appName?: string; | ||
instanceId?: string; | ||
} | ||
@@ -18,2 +19,2 @@ export interface Data { | ||
export declare const post: (options: PostRequestOptions, cb: any) => request.Request; | ||
export declare const get: ({url, etag, requestId}: GetRequestOptions, cb: any) => request.Request; | ||
export declare const get: ({url, etag, appName, instanceId}: GetRequestOptions, cb: any) => request.Request; |
@@ -7,8 +7,8 @@ "use strict"; | ||
exports.get = function (_a, cb) { | ||
var url = _a.url, etag = _a.etag, requestId = _a.requestId; | ||
var url = _a.url, etag = _a.etag, appName = _a.appName, instanceId = _a.instanceId; | ||
var options = { | ||
url: url, | ||
requestId: requestId, | ||
headers: { | ||
'UNLEASH-ID': requestId | ||
'UNLEASH-APPNAME': appName, | ||
'UNLEASH-INSTANCEID': instanceId | ||
} | ||
@@ -15,0 +15,0 @@ }; |
@@ -14,3 +14,2 @@ /// <reference types="node" /> | ||
strategies: Strategy[]; | ||
errorHandler?: (err: any) => any; | ||
} | ||
@@ -21,5 +20,5 @@ export declare class Unleash extends EventEmitter { | ||
private metrics; | ||
constructor({appName, instanceId, url, refreshInterval, metricsInterval, disableMetrics, backupPath, strategies, errorHandler}: UnleashConfig); | ||
constructor({appName, instanceId, url, refreshInterval, metricsInterval, disableMetrics, backupPath, strategies}: UnleashConfig); | ||
destroy(): void; | ||
isEnabled(name: string, context: any, fallbackValue?: boolean): boolean; | ||
} |
@@ -21,3 +21,3 @@ 'use strict'; | ||
var _this = this; | ||
var appName = _a.appName, instanceId = _a.instanceId, url = _a.url, _b = _a.refreshInterval, refreshInterval = _b === void 0 ? 15 * 1000 : _b, _c = _a.metricsInterval, metricsInterval = _c === void 0 ? 60 * 1000 : _c, _d = _a.disableMetrics, disableMetrics = _d === void 0 ? false : _d, _e = _a.backupPath, backupPath = _e === void 0 ? BACKUP_PATH : _e, _f = _a.strategies, strategies = _f === void 0 ? [] : _f, _g = _a.errorHandler, errorHandler = _g === void 0 ? function () { } : _g; | ||
var appName = _a.appName, instanceId = _a.instanceId, url = _a.url, _b = _a.refreshInterval, refreshInterval = _b === void 0 ? 15 * 1000 : _b, _c = _a.metricsInterval, metricsInterval = _c === void 0 ? 60 * 1000 : _c, _d = _a.disableMetrics, disableMetrics = _d === void 0 ? false : _d, _e = _a.backupPath, backupPath = _e === void 0 ? BACKUP_PATH : _e, _f = _a.strategies, strategies = _f === void 0 ? [] : _f; | ||
_super.call(this); | ||
@@ -33,7 +33,14 @@ if (!url) { | ||
} | ||
var requestId = appName + ":" + instanceId; | ||
this.repository = new repository_1["default"](backupPath, url, requestId, refreshInterval); | ||
this.repository = new repository_1["default"]({ | ||
backupPath: backupPath, | ||
url: url, | ||
appName: appName, | ||
instanceId: instanceId, | ||
refreshInterval: refreshInterval | ||
}); | ||
strategies = [new strategy_1.Strategy('default', true)].concat(strategies); | ||
this.repository.on('ready', function () { | ||
_this.client = new client_1["default"](_this.repository, strategies, errorHandler); | ||
_this.client = new client_1["default"](_this.repository, strategies); | ||
_this.client.on('error', function (err) { return _this.emit('error', err); }); | ||
_this.client.on('warn', function (msg) { return _this.emit('warn', msg); }); | ||
_this.emit('ready'); | ||
@@ -40,0 +47,0 @@ }); |
{ | ||
"name": "unleash-client", | ||
"version": "1.0.0-beta.1", | ||
"version": "1.0.0-beta.2", | ||
"description": "Unleash Client for Node", | ||
@@ -5,0 +5,0 @@ "main": "./lib/index.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
Sorry, the diff of this file is not supported yet
51371
37
799