Comparing version 0.2.0 to 0.3.0
{ | ||
"name": "Example Project", | ||
"segments": { | ||
@@ -3,0 +4,0 @@ "alpha-users": { |
{ | ||
"name": "@pbs/vanna", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"description": "Feature flagging service client", | ||
"main": "dist/vanna.js", | ||
"scripts": { | ||
"build": "babel src --out-dir dist", | ||
"build:package": "babel src --out-dir dist", | ||
"build:example": "parcel build example/index.html --out-dir example/build", | ||
"build": "npm run build:package && npm run build:example", | ||
"start": "parcel example/index.html --out-dir example/build", | ||
"test": "jest" | ||
@@ -26,4 +29,5 @@ }, | ||
"babel-preset-env": "^1.7.0", | ||
"jest": "^23.2.0" | ||
"jest": "^23.2.0", | ||
"parcel-bundler": "^1.9.4" | ||
} | ||
} |
@@ -29,7 +29,6 @@ # Vanna Javascript (Browser) Client | ||
```js | ||
import vanna from "@pbs/vanna"; | ||
import { VannaClient } from "@pbs/vanna"; | ||
const client = vanna.setup({ | ||
server: "https://vanna.example.com/", | ||
project: "example-project-name", | ||
const client = VannaClient({ | ||
uri: "https://vanna.example.com/project", | ||
userSegment: "beta-tester" | ||
@@ -36,0 +35,0 @@ }); |
import invariant from "invariant"; | ||
function getPayloadUrl(server, project) { | ||
return `${server}/${project}.json`; | ||
export function validateOptions(options) { | ||
const { uri } = options; | ||
invariant(uri, "uri is a required setup parameter"); | ||
return options; | ||
} | ||
function getPayload(server, project) { | ||
const url = getPayloadUrl(server, project); | ||
return fetch(url).then(r => r.json()); | ||
export function getManifest(uri) { | ||
return fetch(uri).then(r => r.json()); | ||
} | ||
export function isFeatureEnabled(feature, { userSegment }) { | ||
export function getFeatureVariation(feature, { userSegment }) { | ||
const segmentMatch = feature.targetSegment.includes(userSegment); | ||
@@ -22,19 +23,8 @@ if (!segmentMatch) { | ||
export class VannaClient { | ||
constructor(dependencies = {}) { | ||
this.options = undefined; | ||
this.payload = undefined; | ||
constructor(options = {}) { | ||
this.options = validateOptions(options); | ||
this.manifest = undefined; | ||
this.setup = this.setup.bind(this); | ||
this.on = this.on.bind(this); | ||
this.variation = this.variation.bind(this); | ||
this.getPayload = dependencies.getPayload || getPayload; | ||
} | ||
setup(options = {}) { | ||
const { server, project, userSegment } = options; | ||
invariant(server, "server is a required setup parameter"); | ||
invariant(project, "project is a required setup parameter"); | ||
this.options = { server, project, userSegment }; | ||
return { on: this.on, variation: this.variation }; | ||
@@ -44,16 +34,19 @@ } | ||
on(eventName, cb) { | ||
invariant( | ||
this.options, | ||
"setup must be called before events can be attached" | ||
); | ||
invariant(eventName === "ready", `${eventName} is not a valid event`); | ||
this.onReady(cb); | ||
} | ||
const { server, project } = this.options; | ||
this.getPayload(server, project) | ||
.then(payload => { | ||
this.payload = payload; | ||
onReady(cb) { | ||
const { uri, _overrides } = this.options; | ||
getManifest = _overrides.getManifest || getManifest; | ||
getManifest(uri) | ||
.then(manifest => { | ||
this.manifest = manifest; | ||
}) | ||
.then(cb) | ||
.catch(() => { | ||
this.payload = null; | ||
// Fetching or parsing the manifest can fail in certain cases. | ||
// In these cases, we'll have to make sure to serve fallback | ||
// values for each variant calls. | ||
this.manifest = null; | ||
cb(); | ||
@@ -64,3 +57,3 @@ }); | ||
variation(featureName, { fallback }) { | ||
if (!this.payload) { | ||
if (!this.manifest) { | ||
return fallback; | ||
@@ -70,9 +63,12 @@ } | ||
const { userSegment } = this.options; | ||
const feature = this.payload.features[featureName]; | ||
const feature = this.manifest.features[featureName]; | ||
invariant(feature, `${featureName} is not a valid feature`); | ||
return isFeatureEnabled(feature, { userSegment }); | ||
const { _overrides } = this.options; | ||
const getFeatureVariation = | ||
_overrides.getFeatureVariation || getFeatureVariation; | ||
return getFeatureVariation(feature, { userSegment }); | ||
} | ||
} | ||
export default new VannaClient(); | ||
export default VannaClient; |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
11644
11
191
4
52