@harnessio/ff-javascript-client-sdk
Advanced tools
Comparing version 1.2.2 to 1.3.0
@@ -1,3 +0,4 @@ | ||
import { Options, Target, StreamEvent, Event, EventCallback, Result, Evaluation, VariationValue } from './types'; | ||
import type { Options, Target, StreamEvent, EventCallback, Result, Evaluation, VariationValue } from './types'; | ||
import { Event } from './types'; | ||
declare const initialize: (apiKey: string, target: Target, options: Options) => Result; | ||
export { initialize, Options, Target, StreamEvent, Event, EventCallback, Result, Evaluation, VariationValue }; |
@@ -1,2 +0,2 @@ | ||
var z=Object.create,T=Object.defineProperty,B=Object.getPrototypeOf,K=Object.prototype.hasOwnProperty,G=Object.getOwnPropertyNames,Y=Object.getOwnPropertyDescriptor;var D=a=>T(a,"__esModule",{value:!0});var q=(a,r)=>{for(var d in r)T(a,d,{get:r[d],enumerable:!0})},Q=(a,r,d)=>{if(r&&typeof r=="object"||typeof r=="function")for(let g of G(r))!K.call(a,g)&&g!=="default"&&T(a,g,{get:()=>r[g],enumerable:!(d=Y(r,g))||d.enumerable});return a},k=a=>Q(D(T(a!=null?z(B(a)):{},"default",a&&a.__esModule&&"default"in a?{get:()=>a.default,enumerable:!0}:{value:a,enumerable:!0})),a);D(exports);q(exports,{Event:()=>l,initialize:()=>X});var x=k(require("jwt-decode")),P=k(require("mitt")),j=k(require("event-source-polyfill"));var l;(function(a){a.READY="ready",a.CONNECTED="connected",a.DISCONNECTED="disconnected",a.RECONNECTED="reconnected",a.CHANGED="changed",a.ERROR="error"})(l||(l={}));var $={debug:!1,baseUrl:"https://config.feature-flags.uat.harness.io/api/1.0",eventUrl:"https://event.feature-flags.uat.harness.io/api/1.0",streamEnabled:!0,allAttributesPrivate:!1,privateAttributeNames:[]},h=(a,...r)=>console.error(`[FF-SDK] ${a}`,...r),R=30*1e3;var N=globalThis.fetch,W=j.EventSourcePolyfill,S=!!globalThis.Proxy,C=a=>{let{value:r}=a;try{switch(a.kind.toLowerCase()){case"int":case"number":r=Number(r);break;case"boolean":r=r.toLocaleString()==="true";break;case"json":r=JSON.parse(r);break}}catch(d){h(d)}return r},X=(a,r,d)=>{let g,v,E,y,i=[],s=(0,P.default)(),b={...$,...d},c=(t,...e)=>{b.debug&&console.debug(`[FF-SDK] ${t}`,...e)};globalThis.onbeforeunload=()=>{i.length&&globalThis.localStorage&&(globalThis.localStorage.HARNESS_FF_METRICS=JSON.stringify(i))};let M=async(t,e)=>(await(await N(`${e.baseUrl}/client/auth`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({apiKey:t,target:r})})).json()).authToken,O=()=>{if(i.length){c("Sending metrics...",i);let t={metricsData:i.map(e=>({timestamp:Date.now(),count:e.count,metricsType:"FFMETRICS",attributes:[{key:"featureIdentifier",value:e.featureIdentifier},{key:"featureName",value:e.featureIdentifier},{key:"featureValue",value:String(e.featureValue)},{key:"target",value:r.identifier},{key:"SDK_NAME",value:"JavaScript"},{key:"SDK_TYPE",value:"client"}]}))};N(`${d.eventUrl}/metrics/${g}`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${E}`},body:JSON.stringify(t)}).then(()=>{i=[]}).catch(e=>{h(e)}).finally(()=>{y=setTimeout(O,R)})}else y=setTimeout(O,R)},I=function(){return S?new Proxy({},{get(t,e){if(t.hasOwnProperty(e)){let n=t[e],u=i.find(o=>o.featureIdentifier===e&&o.featureValue===n);u?u.count++:i.push({featureIdentifier:e,featureValue:n,count:1}),c("Metrics event: Flag",e,"has been read with value",n)}return t[e]}}):{}},f=I();M(a,b).then(t=>{E=t;let e=(0,x.default)(t);if(c("Authenticated",e),globalThis.localStorage&&globalThis.localStorage.HARNESS_FF_METRICS)try{i=JSON.parse(globalThis.localStorage.HARNESS_FF_METRICS),delete globalThis.localStorage.HARNESS_FF_METRICS,c("Picking up metrics from previous session")}catch(n){}y=setTimeout(O,R),g=e.environment,U().then(()=>{c("Fetch all flags ok",f)}).then(()=>{_()}).then(()=>{c("Event stream ready",{storage:f}),s.emit(l.READY,f),S||Object.keys(f).forEach(n=>{i.push({featureIdentifier:n,featureValue:f[n],count:1})})}).catch(n=>{s.emit(l.ERROR,n)})}).catch(t=>{h("Authentication error: ",t),s.emit(l.ERROR,t)});let U=async()=>{try{(await(await N(`${b.baseUrl}/client/env/${g}/target/${r.identifier}/evaluations`,{headers:{Authorization:`Bearer ${E}`}})).json()).forEach(n=>{f[n.flag]=C(n)})}catch(t){return h("Features fetch operation error: ",t),s.emit(l.ERROR,t),t}},V=async t=>{try{let e=await N(`${b.baseUrl}/client/env/${g}/target/${r.identifier}/evaluations/${t}`,{headers:{Authorization:`Bearer ${E}`}});if(e.ok){let n=await e.json();if(f[t]=C(n),s.emit(l.CHANGED,S?new Proxy(n,{get(u,o){if(u.hasOwnProperty(o)&&o==="value"){let m=u.flag,p=n.value,F=i.find(w=>w.featureIdentifier===m&&w.featureValue===p);F?F.count++:i.push({featureIdentifier:o,featureValue:String(p),count:1}),c("Metrics event: Flag",o,"has been read with value via stream update",p)}return o==="value"?C(n):n[o]}}):{deleted:n.deleted,flag:n.flag,value:C(n)}),!S){let u=n.flag,o=i.find(m=>m.featureIdentifier===u&&m.featureValue===n.value);o?o.count++:i.push({featureIdentifier:u,featureValue:String(n.value),count:1})}}else s.emit(l.ERROR,e)}catch(e){h("Feature fetch operation error: ",e),s.emit(l.ERROR,e)}},_=()=>{if(!b.streamEnabled){c("Stream is disabled by configuration. Note: Polling is not yet supported");return}v=new W(`${b.baseUrl}/stream`,{headers:{Authorization:`Bearer ${E}`,"API-Key":a}}),v.onopen=t=>{c("Stream connected",t),s.emit(l.CONNECTED)},v.onclose=t=>{c("Stream disconnected"),s.emit(l.DISCONNECTED)},v.onerror=t=>{h("Stream has issue",t),s.emit("error",t)},v.addEventListener("*",t=>{let e=JSON.parse(t.data);switch(c("Received event from stream: ",e),e.event){case"create":setTimeout(()=>V(e.identifier),1e3);break;case"patch":V(e.identifier);break;case"delete":delete f[e.identifier],s.emit(l.CHANGED,{flag:e.identifier,value:void 0,deleted:!0}),c("Evaluation deleted",{message:e,storage:f});break}})},H=(t,e)=>s.on(t,e),J=(t,e)=>{t?s.off(t,e):A()},L=(t,e)=>{let n=f[t];if(!S&&n!==void 0){let u=n,o=t,m=i.find(p=>p.featureIdentifier===o&&p.featureValue===u);m?m.count++:i.push({featureIdentifier:o,featureValue:u,count:1})}return n!==void 0?n:e},A=()=>{c("Closing event stream"),f=I(),clearTimeout(y),s.all.clear(),v.close()};return{on:H,off:J,variation:L,close:A}}; | ||
var Q=Object.create,C=Object.defineProperty,W=Object.getPrototypeOf,X=Object.prototype.hasOwnProperty,Z=Object.getOwnPropertyNames,ee=Object.getOwnPropertyDescriptor;var U=a=>C(a,"__esModule",{value:!0});var te=(a,n)=>{for(var v in n)C(a,v,{get:n[v],enumerable:!0})},ae=(a,n,v)=>{if(n&&typeof n=="object"||typeof n=="function")for(let m of Z(n))!X.call(a,m)&&m!=="default"&&C(a,m,{get:()=>n[m],enumerable:!(v=ee(n,m))||v.enumerable});return a},D=a=>ae(U(C(a!=null?Q(W(a)):{},"default",a&&a.__esModule&&"default"in a?{get:()=>a.default,enumerable:!0}:{value:a,enumerable:!0})),a);U(exports);te(exports,{Event:()=>u,initialize:()=>oe});var L=D(require("jwt-decode")),H=D(require("mitt")),K=D(require("event-source-polyfill"));var u;(function(a){a.READY="ready",a.CONNECTED="connected",a.DISCONNECTED="disconnected",a.RECONNECTED="reconnected",a.CHANGED="changed",a.ERROR="error"})(u||(u={}));var _={debug:!1,baseUrl:"https://config.feature-flags.uat.harness.io/api/1.0",eventUrl:"https://event.feature-flags.uat.harness.io/api/1.0",streamEnabled:!0,allAttributesPrivate:!1,privateAttributeNames:[]},E=(a,...n)=>console.error(`[FF-SDK] ${a}`,...n),w=30*1e3;var ie="1.3.0",ne=500,A=globalThis.fetch,re=K.EventSourcePolyfill,R=!!globalThis.Proxy,N=a=>{let{value:n}=a;try{switch(a.kind.toLowerCase()){case"int":case"number":n=Number(n);break;case"boolean":n=n.toString().toLowerCase()==="true";break;case"json":n=JSON.parse(n);break}}catch(v){E(v)}return n},oe=(a,n,v)=>{let m,S,I,T,b=!0,F=()=>{b=!1},M=()=>{b=!0},s=[],l=(0,H.default)(),p={..._,...v},f=(t,...e)=>{p.debug&&console.debug(`[FF-SDK] ${t}`,...e)},y=t=>{if(b){let e=Date.now();e-t.lastAccessed>ne&&(t.count++,t.lastAccessed=e)}};globalThis.onbeforeunload=()=>{s.length&&globalThis.localStorage&&(F(),globalThis.localStorage.HARNESS_FF_METRICS=JSON.stringify(s),M())};let J=async(t,e)=>(await(await A(`${e.baseUrl}/client/auth`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({apiKey:t,target:n})})).json()).authToken,O=()=>{if(s.length){f("Sending metrics...",{metrics:s,evaluations:c});let t={metricsData:s.map(e=>({timestamp:Date.now(),count:e.count,metricsType:"FFMETRICS",attributes:[{key:"featureIdentifier",value:e.featureIdentifier},{key:"featureName",value:e.featureIdentifier},{key:"variationIdentifier",value:e.variationIdentifier},{key:"target",value:n.identifier},{key:"SDK_NAME",value:"JavaScript"},{key:"SDK_TYPE",value:"client"},{key:"SDK_VERSION",value:ie}]}))};A(`${v.eventUrl}/metrics/${m}`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${I}`},body:JSON.stringify(t)}).then(()=>{s=[]}).catch(e=>{E(e)}).finally(()=>{T=window.setTimeout(O,w)})}else T=window.setTimeout(O,w)},c={},x=function(){return R?new Proxy({},{get(t,e){let i=t[e];if(t.hasOwnProperty(e)){let d=t[e],r=s.find(o=>o.featureIdentifier===e&&d===o.featureValue);r?(r.variationIdentifier=c[e]?.identifier||"",y(r)):s.push({featureIdentifier:e,featureValue:d,variationIdentifier:c[e]?.identifier||"",count:b?1:0,lastAccessed:Date.now()}),f("Metrics event: Flag:",e,"has been read with value:",d,"variationIdentifier:",c[e]?.identifier)}return i}}):{}},g=x();J(a,p).then(t=>{I=t;let e=(0,L.default)(t);if(f("Authenticated",e),globalThis.localStorage&&globalThis.localStorage.HARNESS_FF_METRICS)try{delete globalThis.localStorage.HARNESS_FF_METRICS,f("Picking up metrics from previous session")}catch(i){}T=window.setTimeout(O,w),m=e.environment,z().then(()=>{f("Fetch all flags ok",g)}).then(()=>{B()}).then(()=>{f("Event stream ready",{storage:g}),l.emit(u.READY,g),R||Object.keys(g).forEach(i=>{s.push({featureIdentifier:i,featureValue:g[i],variationIdentifier:c[i]?.identifier||"",count:b?1:0,lastAccessed:Date.now()})})}).catch(i=>{l.emit(u.ERROR,i)})}).catch(t=>{E("Authentication error: ",t),l.emit(u.ERROR,t)});let z=async()=>{try{(await(await A(`${p.baseUrl}/client/env/${m}/target/${n.identifier}/evaluations`,{headers:{Authorization:`Bearer ${I}`}})).json()).forEach(i=>{let d=N(i);g[i.flag]=d,c[i.flag]={...i,value:d}})}catch(t){return E("Features fetch operation error: ",t),l.emit(u.ERROR,t),t}},$=async t=>{try{let e=await A(`${p.baseUrl}/client/env/${m}/target/${n.identifier}/evaluations/${t}`,{headers:{Authorization:`Bearer ${I}`}});if(e.ok){let i=await e.json(),d=N(i);if(F(),g[t]=d,c[t]={...i,value:d},M(),l.emit(u.CHANGED,R?new Proxy(i,{get(r,o){if(r.hasOwnProperty(o)&&o==="value"){let h=r.flag,V=i.value,k=s.find(j=>j.featureIdentifier===h&&j.featureValue===V);k?(y(k),k.variationIdentifier=c[h]?.identifier||""):s.push({featureIdentifier:h,featureValue:String(V),variationIdentifier:c[h].identifier||"",count:b?1:0,lastAccessed:Date.now()}),f("Metrics event: Flag",o,"has been read with value via stream update",V)}return o==="value"?N(i):i[o]}}):{deleted:i.deleted,flag:i.flag,value:N(i)}),!R){let r=i.flag,o=s.find(h=>h.featureIdentifier===r&&h.featureValue===i.value);o?(y(o),o.variationIdentifier=c[r]?.identifier||""):s.push({featureIdentifier:r,featureValue:String(i.value),variationIdentifier:c[r].identifier||"",count:b?1:0,lastAccessed:Date.now()})}}else l.emit(u.ERROR,e)}catch(e){E("Feature fetch operation error: ",e),l.emit(u.ERROR,e)}},B=()=>{if(!p.streamEnabled){f("Stream is disabled by configuration. Note: Polling is not yet supported");return}S=new re(`${p.baseUrl}/stream`,{headers:{Authorization:`Bearer ${I}`,"API-Key":a}}),S.onopen=t=>{f("Stream connected",t),l.emit(u.CONNECTED)},S.onclose=t=>{f("Stream disconnected"),l.emit(u.DISCONNECTED)},S.onerror=t=>{E("Stream has issue",t),l.emit("error",t)},S.addEventListener("*",t=>{let e=JSON.parse(t.data);switch(f("Received event from stream: ",e),e.event){case"create":setTimeout(()=>$(e.identifier),1e3);break;case"patch":$(e.identifier);break;case"delete":delete g[e.identifier],l.emit(u.CHANGED,{flag:e.identifier,value:void 0,deleted:!0}),f("Evaluation deleted",{message:e,storage:g});break}})},G=(t,e)=>l.on(t,e),Y=(t,e)=>{t?l.off(t,e):P()},q=(t,e)=>{let i=g[t];if(!R&&i!==void 0){let d=i,r=t,o=s.find(h=>h.featureIdentifier===r&&h.featureValue===d);o?(y(o),o.variationIdentifier=c[r]?.identifier||""):s.push({featureIdentifier:r,featureValue:d,count:b?1:0,variationIdentifier:c[r].identifier||"",lastAccessed:Date.now()})}return i!==void 0?i:e},P=()=>{f("Closing event stream"),g=x(),c={},clearTimeout(T),l.all.clear(),S.close()};return{on:G,off:Y,variation:q,close:P}}; | ||
//# sourceMappingURL=sdk.cjs.js.map |
@@ -1,2 +0,2 @@ | ||
import M from"jwt-decode";import U from"mitt";import{EventSourcePolyfill as _}from"event-source-polyfill";var l;(function(n){n.READY="ready",n.CONNECTED="connected",n.DISCONNECTED="disconnected",n.RECONNECTED="reconnected",n.CHANGED="changed",n.ERROR="error"})(l||(l={}));var F={debug:!1,baseUrl:"https://config.feature-flags.uat.harness.io/api/1.0",eventUrl:"https://event.feature-flags.uat.harness.io/api/1.0",streamEnabled:!0,allAttributesPrivate:!1,privateAttributeNames:[]},g=(n,...o)=>console.error(`[FF-SDK] ${n}`,...o),T=30*1e3;var R=globalThis.fetch,H=_,p=!!globalThis.Proxy,N=n=>{let{value:o}=n;try{switch(n.kind.toLowerCase()){case"int":case"number":o=Number(o);break;case"boolean":o=o.toLocaleString()==="true";break;case"json":o=JSON.parse(o);break}}catch(E){g(E)}return o},J=(n,o,E)=>{let S,m,b,y,r=[],s=U(),h={...F,...E},c=(t,...e)=>{h.debug&&console.debug(`[FF-SDK] ${t}`,...e)};globalThis.onbeforeunload=()=>{r.length&&globalThis.localStorage&&(globalThis.localStorage.HARNESS_FF_METRICS=JSON.stringify(r))};let w=async(t,e)=>(await(await R(`${e.baseUrl}/client/auth`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({apiKey:t,target:o})})).json()).authToken,C=()=>{if(r.length){c("Sending metrics...",r);let t={metricsData:r.map(e=>({timestamp:Date.now(),count:e.count,metricsType:"FFMETRICS",attributes:[{key:"featureIdentifier",value:e.featureIdentifier},{key:"featureName",value:e.featureIdentifier},{key:"featureValue",value:String(e.featureValue)},{key:"target",value:o.identifier},{key:"SDK_NAME",value:"JavaScript"},{key:"SDK_TYPE",value:"client"}]}))};R(`${E.eventUrl}/metrics/${S}`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${b}`},body:JSON.stringify(t)}).then(()=>{r=[]}).catch(e=>{g(e)}).finally(()=>{y=setTimeout(C,T)})}else y=setTimeout(C,T)},O=function(){return p?new Proxy({},{get(t,e){if(t.hasOwnProperty(e)){let a=t[e],u=r.find(i=>i.featureIdentifier===e&&i.featureValue===a);u?u.count++:r.push({featureIdentifier:e,featureValue:a,count:1}),c("Metrics event: Flag",e,"has been read with value",a)}return t[e]}}):{}},f=O();w(n,h).then(t=>{b=t;let e=M(t);if(c("Authenticated",e),globalThis.localStorage&&globalThis.localStorage.HARNESS_FF_METRICS)try{r=JSON.parse(globalThis.localStorage.HARNESS_FF_METRICS),delete globalThis.localStorage.HARNESS_FF_METRICS,c("Picking up metrics from previous session")}catch(a){}y=setTimeout(C,T),S=e.environment,D().then(()=>{c("Fetch all flags ok",f)}).then(()=>{$()}).then(()=>{c("Event stream ready",{storage:f}),s.emit(l.READY,f),p||Object.keys(f).forEach(a=>{r.push({featureIdentifier:a,featureValue:f[a],count:1})})}).catch(a=>{s.emit(l.ERROR,a)})}).catch(t=>{g("Authentication error: ",t),s.emit(l.ERROR,t)});let D=async()=>{try{(await(await R(`${h.baseUrl}/client/env/${S}/target/${o.identifier}/evaluations`,{headers:{Authorization:`Bearer ${b}`}})).json()).forEach(a=>{f[a.flag]=N(a)})}catch(t){return g("Features fetch operation error: ",t),s.emit(l.ERROR,t),t}},k=async t=>{try{let e=await R(`${h.baseUrl}/client/env/${S}/target/${o.identifier}/evaluations/${t}`,{headers:{Authorization:`Bearer ${b}`}});if(e.ok){let a=await e.json();if(f[t]=N(a),s.emit(l.CHANGED,p?new Proxy(a,{get(u,i){if(u.hasOwnProperty(i)&&i==="value"){let d=u.flag,v=a.value,V=r.find(A=>A.featureIdentifier===d&&A.featureValue===v);V?V.count++:r.push({featureIdentifier:i,featureValue:String(v),count:1}),c("Metrics event: Flag",i,"has been read with value via stream update",v)}return i==="value"?N(a):a[i]}}):{deleted:a.deleted,flag:a.flag,value:N(a)}),!p){let u=a.flag,i=r.find(d=>d.featureIdentifier===u&&d.featureValue===a.value);i?i.count++:r.push({featureIdentifier:u,featureValue:String(a.value),count:1})}}else s.emit(l.ERROR,e)}catch(e){g("Feature fetch operation error: ",e),s.emit(l.ERROR,e)}},$=()=>{if(!h.streamEnabled){c("Stream is disabled by configuration. Note: Polling is not yet supported");return}m=new H(`${h.baseUrl}/stream`,{headers:{Authorization:`Bearer ${b}`,"API-Key":n}}),m.onopen=t=>{c("Stream connected",t),s.emit(l.CONNECTED)},m.onclose=t=>{c("Stream disconnected"),s.emit(l.DISCONNECTED)},m.onerror=t=>{g("Stream has issue",t),s.emit("error",t)},m.addEventListener("*",t=>{let e=JSON.parse(t.data);switch(c("Received event from stream: ",e),e.event){case"create":setTimeout(()=>k(e.identifier),1e3);break;case"patch":k(e.identifier);break;case"delete":delete f[e.identifier],s.emit(l.CHANGED,{flag:e.identifier,value:void 0,deleted:!0}),c("Evaluation deleted",{message:e,storage:f});break}})},x=(t,e)=>s.on(t,e),P=(t,e)=>{t?s.off(t,e):I()},j=(t,e)=>{let a=f[t];if(!p&&a!==void 0){let u=a,i=t,d=r.find(v=>v.featureIdentifier===i&&v.featureValue===u);d?d.count++:r.push({featureIdentifier:i,featureValue:u,count:1})}return a!==void 0?a:e},I=()=>{c("Closing event stream"),f=O(),clearTimeout(y),s.all.clear(),m.close()};return{on:x,off:P,variation:j,close:I}};export{l as Event,J as initialize}; | ||
import J from"jwt-decode";import z from"mitt";import{EventSourcePolyfill as B}from"event-source-polyfill";var u;(function(i){i.READY="ready",i.CONNECTED="connected",i.DISCONNECTED="disconnected",i.RECONNECTED="reconnected",i.CHANGED="changed",i.ERROR="error"})(u||(u={}));var P={debug:!1,baseUrl:"https://config.feature-flags.uat.harness.io/api/1.0",eventUrl:"https://event.feature-flags.uat.harness.io/api/1.0",streamEnabled:!0,allAttributesPrivate:!1,privateAttributeNames:[]},h=(i,...s)=>console.error(`[FF-SDK] ${i}`,...s),C=30*1e3;var G="1.3.0",Y=500,w=globalThis.fetch,q=B,p=!!globalThis.Proxy,A=i=>{let{value:s}=i;try{switch(i.kind.toLowerCase()){case"int":case"number":s=Number(s);break;case"boolean":s=s.toString().toLowerCase()==="true";break;case"json":s=JSON.parse(s);break}}catch(I){h(I)}return s},Q=(i,s,I)=>{let R,b,S,T,m=!0,k=()=>{m=!1},D=()=>{m=!0},o=[],l=z(),E={...P,...I},f=(t,...e)=>{E.debug&&console.debug(`[FF-SDK] ${t}`,...e)},y=t=>{if(m){let e=Date.now();e-t.lastAccessed>Y&&(t.count++,t.lastAccessed=e)}};globalThis.onbeforeunload=()=>{o.length&&globalThis.localStorage&&(k(),globalThis.localStorage.HARNESS_FF_METRICS=JSON.stringify(o),D())};let j=async(t,e)=>(await(await w(`${e.baseUrl}/client/auth`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({apiKey:t,target:s})})).json()).authToken,N=()=>{if(o.length){f("Sending metrics...",{metrics:o,evaluations:c});let t={metricsData:o.map(e=>({timestamp:Date.now(),count:e.count,metricsType:"FFMETRICS",attributes:[{key:"featureIdentifier",value:e.featureIdentifier},{key:"featureName",value:e.featureIdentifier},{key:"variationIdentifier",value:e.variationIdentifier},{key:"target",value:s.identifier},{key:"SDK_NAME",value:"JavaScript"},{key:"SDK_TYPE",value:"client"},{key:"SDK_VERSION",value:G}]}))};w(`${I.eventUrl}/metrics/${R}`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${S}`},body:JSON.stringify(t)}).then(()=>{o=[]}).catch(e=>{h(e)}).finally(()=>{T=window.setTimeout(N,C)})}else T=window.setTimeout(N,C)},c={},F=function(){return p?new Proxy({},{get(t,e){let a=t[e];if(t.hasOwnProperty(e)){let d=t[e],n=o.find(r=>r.featureIdentifier===e&&d===r.featureValue);n?(n.variationIdentifier=c[e]?.identifier||"",y(n)):o.push({featureIdentifier:e,featureValue:d,variationIdentifier:c[e]?.identifier||"",count:m?1:0,lastAccessed:Date.now()}),f("Metrics event: Flag:",e,"has been read with value:",d,"variationIdentifier:",c[e]?.identifier)}return a}}):{}},g=F();j(i,E).then(t=>{S=t;let e=J(t);if(f("Authenticated",e),globalThis.localStorage&&globalThis.localStorage.HARNESS_FF_METRICS)try{delete globalThis.localStorage.HARNESS_FF_METRICS,f("Picking up metrics from previous session")}catch(a){}T=window.setTimeout(N,C),R=e.environment,U().then(()=>{f("Fetch all flags ok",g)}).then(()=>{_()}).then(()=>{f("Event stream ready",{storage:g}),l.emit(u.READY,g),p||Object.keys(g).forEach(a=>{o.push({featureIdentifier:a,featureValue:g[a],variationIdentifier:c[a]?.identifier||"",count:m?1:0,lastAccessed:Date.now()})})}).catch(a=>{l.emit(u.ERROR,a)})}).catch(t=>{h("Authentication error: ",t),l.emit(u.ERROR,t)});let U=async()=>{try{(await(await w(`${E.baseUrl}/client/env/${R}/target/${s.identifier}/evaluations`,{headers:{Authorization:`Bearer ${S}`}})).json()).forEach(a=>{let d=A(a);g[a.flag]=d,c[a.flag]={...a,value:d}})}catch(t){return h("Features fetch operation error: ",t),l.emit(u.ERROR,t),t}},M=async t=>{try{let e=await w(`${E.baseUrl}/client/env/${R}/target/${s.identifier}/evaluations/${t}`,{headers:{Authorization:`Bearer ${S}`}});if(e.ok){let a=await e.json(),d=A(a);if(k(),g[t]=d,c[t]={...a,value:d},D(),l.emit(u.CHANGED,p?new Proxy(a,{get(n,r){if(n.hasOwnProperty(r)&&r==="value"){let v=n.flag,O=a.value,V=o.find($=>$.featureIdentifier===v&&$.featureValue===O);V?(y(V),V.variationIdentifier=c[v]?.identifier||""):o.push({featureIdentifier:v,featureValue:String(O),variationIdentifier:c[v].identifier||"",count:m?1:0,lastAccessed:Date.now()}),f("Metrics event: Flag",r,"has been read with value via stream update",O)}return r==="value"?A(a):a[r]}}):{deleted:a.deleted,flag:a.flag,value:A(a)}),!p){let n=a.flag,r=o.find(v=>v.featureIdentifier===n&&v.featureValue===a.value);r?(y(r),r.variationIdentifier=c[n]?.identifier||""):o.push({featureIdentifier:n,featureValue:String(a.value),variationIdentifier:c[n].identifier||"",count:m?1:0,lastAccessed:Date.now()})}}else l.emit(u.ERROR,e)}catch(e){h("Feature fetch operation error: ",e),l.emit(u.ERROR,e)}},_=()=>{if(!E.streamEnabled){f("Stream is disabled by configuration. Note: Polling is not yet supported");return}b=new q(`${E.baseUrl}/stream`,{headers:{Authorization:`Bearer ${S}`,"API-Key":i}}),b.onopen=t=>{f("Stream connected",t),l.emit(u.CONNECTED)},b.onclose=t=>{f("Stream disconnected"),l.emit(u.DISCONNECTED)},b.onerror=t=>{h("Stream has issue",t),l.emit("error",t)},b.addEventListener("*",t=>{let e=JSON.parse(t.data);switch(f("Received event from stream: ",e),e.event){case"create":setTimeout(()=>M(e.identifier),1e3);break;case"patch":M(e.identifier);break;case"delete":delete g[e.identifier],l.emit(u.CHANGED,{flag:e.identifier,value:void 0,deleted:!0}),f("Evaluation deleted",{message:e,storage:g});break}})},L=(t,e)=>l.on(t,e),H=(t,e)=>{t?l.off(t,e):x()},K=(t,e)=>{let a=g[t];if(!p&&a!==void 0){let d=a,n=t,r=o.find(v=>v.featureIdentifier===n&&v.featureValue===d);r?(y(r),r.variationIdentifier=c[n]?.identifier||""):o.push({featureIdentifier:n,featureValue:d,count:m?1:0,variationIdentifier:c[n].identifier||"",lastAccessed:Date.now()})}return a!==void 0?a:e},x=()=>{f("Closing event stream"),g=F(),c={},clearTimeout(T),l.all.clear(),b.close()};return{on:L,off:H,variation:K,close:x}};export{u as Event,Q as initialize}; | ||
//# sourceMappingURL=sdk.esm.js.map |
@@ -24,2 +24,3 @@ export interface Target { | ||
flag: string; | ||
identifier: string; | ||
value: VariationValue; | ||
@@ -44,1 +45,8 @@ kind: string; | ||
} | ||
export interface MetricsInfo { | ||
featureIdentifier: string; | ||
featureValue: any; | ||
variationIdentifier: string; | ||
count: number; | ||
lastAccessed: number; | ||
} |
{ | ||
"name": "@harnessio/ff-javascript-client-sdk", | ||
"version": "1.2.2", | ||
"version": "1.3.0", | ||
"author": "Harness", | ||
@@ -18,3 +18,3 @@ "license": "Apache-2.0", | ||
"lint": "tslint -p tsconfig.json", | ||
"type": "tsc ./src/*.ts --declaration --emitDeclarationOnly --outDir dist --lib ES2015", | ||
"type": "tsc ./src/*.ts --declaration --emitDeclarationOnly --outDir dist --lib ES2015,DOM", | ||
"clean": "rm -rf ./dist" | ||
@@ -21,0 +21,0 @@ }, |
Sorry, the diff of this file is not supported yet
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
74812
107