@obelism/improve-sdk 0.0.3 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/client.cjs CHANGED
@@ -1 +1 @@
1
- Object.defineProperty(exports,"__esModule",{value:!0});var i=require("ua-parser-js"),t=i&&i.__esModule?i:{default:i};const e={device:["wearable","mobile","tablet","console","smarttv","embedded","desktop"],browser:["chrome","safari","firefox","edge","ie","samsung internet","social","other"],os:["mac os","ios","android","windows","unix"]},s=i=>i&&e.device.includes(i)?i:"desktop",r=["tiktok","wechat","weibo","snapchat","klarna","Line","instagram","facebook","alipay","Baidu"],o=(i="")=>{if(!i)return"other";let t=i.toLowerCase();return e.browser.find(i=>t.includes(i))||(r.includes(t)?"social":"other")},n=["wearable","mobile","tablet"],l=i=>n.includes(i)?"coarse":"fine",a=(i="")=>{if(!i)return"unix";let t=i.toLowerCase();return e.os.find(i=>t.includes(i))||"unix"},u=i=>{if(!i||"string"!=typeof i)return null;let e=new t.default(i).getResult(),r=s(e.device.type);return{pointer:l(r),device:r,browser:o(e.browser.name),os:a(e.os.name)}},h=(i,t)=>!i||Object.entries(i).every(([i,e])=>t[i]===e),c=i=>{if(0===i.length)return null;if(1===i.length)return i[0].slug;let t=Math.random()*i.reduce((i,{split:t})=>i+t,0);return(i.find(({split:i})=>(t-=i)<=0)||i[0]).slug},d="https://improve.obelism.studio",f=`${d}/config`,g=`${d}/api/log`,v="ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",p=i=>new Promise(t=>setTimeout(t,i)),m=async(i=1e3,t)=>(await p(i),t?.abort(),null),w=(i=3e3,t,e)=>{let s=new AbortController;return Promise.race([fetch(t,{...e,signal:s.signal}),m(i,s)])};class b{#i;constructor({organizationId:i,environment:t,state:e,config:s,fetchTimeout:r}){this.organizationId="",this.environment="develop",this.#i=null,this.config=null,this.fetchConfig=async()=>{if(this.config)return;if(!this.#i){console.log("No config fetch setup provided");return}let i=await w(this.#i.timeout,this.#i.url);if(!i){console.log("Configuration fetch timed-out");return}this.config=await i.json()},this.generateVisitorId=()=>`visi_${(function(i=5){return i&&"number"==typeof i?Array(i).fill("").reduce(i=>i+=v.charAt(Math.floor(Math.random()*v.length)),""):""})(26).toUpperCase()}`,this.getVisitorCookieName=()=>"visitorId",this.organizationId=i,this.environment=t,s?this.config=s:this.#i={url:[f,i,t,e||"active"].join("/"),timeout:r||3e3}}}const y=i=>{if(!i)return!1;let t=document.cookie.split("; ").find(t=>{let[e]=t.split("=");return i===e});return!!t&&t.split("=")[1]},I=(i,t)=>{let e=new Date;e.setMonth(e.getMonth()+1),document.cookie=`${i}=${t};path=/;expires=${e.toUTCString()}`},V=()=>{let i=window.innerWidth;return i<=768?"small":i<=1024?"medium":i<=1200?"large":"huge"};exports.ImproveClientSDK=class extends b{#t;#e;#s;#r;#o;constructor(...i){super(...i),this.#e=!1,this.#s="",this.#r={},this.#o=g,this.setupVisitor=(i=window.navigator.userAgent)=>{let t=y(this.getVisitorCookieName());this.#e=!!t,this.#s=t||this.generateVisitorId();let e=u(i);return e?(this.#t=e,I(this.getVisitorCookieName(),this.#s),this.#s):null},this.getFlagValue=i=>{if(!this.config)return null;let t=this.config.flags[i];if(!t)return null;if(this.#t||this.setupVisitor(),!this.#s||!this.#t)return t.options[0].slug;if(this.#t?.[i])return this.#t[i];if(!h(this.config.audience[t.audience],this.#t))return t.options[0].slug;let e=y(i)||c(t.options);return e?(this.#t[i]=e,I(i,e),e):null},this.getTestValue=i=>{if(!this.config)return null;let t=this.config.tests[i];if(!t)return null;if(this.#t||this.setupVisitor(),!this.#s||!this.#t)return t.defaultValue;if(this.#t?.[i])return this.#t[i];if(!h(this.config.audience[t.audience],this.#t))return t.defaultValue;if(t.allocation<100&&100*Math.random()>t.allocation)return this.#t[i]=t.defaultValue,this.#t?.[i];let e=y(i)||c(t.options);return e?(this.#t[i]=e,I(i,e),e):null},this.setAnalyticsUrls=i=>{this.#o=i},this.postAnalytic=(i,t,e)=>{if(!this.config)return null;let s=this.config.tests[i];if(this.#t||this.setupVisitor(),!s||!this.#t||this.#r?.[i]?.[t])return null;this.#r[i]=this.#r[i]||{},this.#r[i][t]=!0,this.#t?.[i]||this.getTestValue(i);let r={organizationId:this.organizationId,environment:this.environment,testId:s.id,testValue:this.#t[i],visitorId:this.#s,pointer:this.#t.pointer,device:this.#t.device,screen:V(),browser:this.#t.browser,os:this.#t.os,visitor:this.#e?"recurring":"new",event:t,message:e||""};return fetch(this.#o,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(r)})}}};
1
+ Object.defineProperty(exports,"__esModule",{value:!0});var i=require("ua-parser-js"),t=i&&i.__esModule?i:{default:i};const e={device:["wearable","mobile","tablet","console","smarttv","embedded","desktop"],browser:["chrome","safari","firefox","edge","ie","samsung internet","social","other"],os:["mac os","ios","android","windows","unix"]},s=i=>i&&e.device.includes(i)?i:"desktop",r=["tiktok","wechat","weibo","snapchat","klarna","Line","instagram","facebook","alipay","Baidu"],o=(i="")=>{if(!i)return"other";let t=i.toLowerCase();return e.browser.find(i=>t.includes(i))||(r.includes(t)?"social":"other")},n=["wearable","mobile","tablet"],l=i=>n.includes(i)?"coarse":"fine",a=(i="")=>{if(!i)return"unix";let t=i.toLowerCase();return e.os.find(i=>t.includes(i))||"unix"},u=i=>{if(!i||"string"!=typeof i)return null;let e=new t.default(i).getResult(),r=s(e.device.type);return{pointer:l(r),device:r,browser:o(e.browser.name),os:a(e.os.name)}},h=(i,t)=>!i||Object.entries(i).every(([i,e])=>t[i]===e),c=i=>{if(0===i.length)return null;if(1===i.length)return i[0].slug;let t=Math.random()*i.reduce((i,{split:t})=>i+t,0);return(i.find(({split:i})=>(t-=i)<=0)||i[0]).slug},f="https://improve.obelism.studio",d=`${f}/config`,g=`${f}/api/log`,v="ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",p=i=>new Promise(t=>setTimeout(t,i)),m=async(i=1e3,t)=>(await p(i),t?.abort(),null),w=(i=3e3,t,e)=>{let s=new AbortController;return Promise.race([fetch(t,{...e,signal:s.signal}),m(i,s)])};class b{#i;constructor({organizationId:i,environment:t,state:e,config:s,fetchTimeout:r}){this.organizationId="",this.environment="develop",this.#i=null,this.config=null,this.fetchConfig=async()=>{if(this.config)return;if(!this.#i)throw Error("No config fetch setup provided");let i=await w(this.#i.timeout,this.#i.url);if(!i)throw Error("Configuration fetch timed-out");this.config=await i.json()},this.loadConfig=i=>{this.config=i},this.generateVisitorId=()=>`visi_${(function(i=5){return i&&"number"==typeof i?Array(i).fill("").reduce(i=>i+=v.charAt(Math.floor(Math.random()*v.length)),""):""})(26).toUpperCase()}`,this.getVisitorCookieName=()=>"visitorId",this.validateTestValue=(i,t)=>{if(!this.config)throw Error("Config is required before validating, either use `.fetchConfig()`, .loadConfig(config) or provide it during setup");let e=this.config.tests[i];if(!e)throw Error(`No config found for ${i}`);return!!e.options.find(i=>i.slug===t)},this.organizationId=i,this.environment=t,s?this.config=s:this.#i={url:[d,i,t,e||"active"].join("/"),timeout:r||3e3}}}const y=i=>{if(!i)return!1;let t=document.cookie.split("; ").find(t=>{let[e]=t.split("=");return i===e});return!!t&&t.split("=")[1]},I=(i,t)=>{let e=new Date;e.setMonth(e.getMonth()+1),document.cookie=`${i}=${t};path=/;expires=${e.toUTCString()}`},V=()=>{let i=window.innerWidth;return i<=768?"small":i<=1024?"medium":i<=1200?"large":"huge"};exports.ImproveClientSDK=class extends b{#t;#e;#s;#r;#o;constructor(...i){super(...i),this.#e=!1,this.#s="",this.#r={},this.#o=g,this.setupVisitor=(i=window.navigator.userAgent)=>{let t=y(this.getVisitorCookieName());this.#e=!!t,this.#s=t||this.generateVisitorId();let e=u(i);return e?(this.#t=e,I(this.getVisitorCookieName(),this.#s),this.#s):null},this.getFlagValue=i=>{if(!this.config)return null;let t=this.config.flags[i];if(!t||!t.options[0])return null;if(this.#t||this.setupVisitor(),!this.#s||!this.#t)return t.options[0].slug;if(this.#t?.[i])return this.#t[i];if(!h(this.config.audience[t.audience],this.#t))return t.options[0].slug;let e=y(i)||c(t.options);return e?(this.#t[i]=e,I(i,e),e):null},this.getTestValue=i=>{if(!this.config)return null;let t=this.config.tests[i];if(!t)return null;if(this.#t||this.setupVisitor(),!this.#s||!this.#t)return t.defaultValue;if(this.#t?.[i])return this.#t[i];if(!h(this.config.audience[t.audience],this.#t))return t.defaultValue;if(t.allocation<100&&100*Math.random()>t.allocation)return this.#t[i]=t.defaultValue,this.#t?.[i];let e=y(i)||c(t.options);return e?(this.#t[i]=e,I(i,e),e):null},this.setAnalyticsUrls=i=>{this.#o=i},this.postAnalytic=(i,t,e)=>{if(!this.config)return null;let s=this.config.tests[i];if(this.#t||this.setupVisitor(),!s||!this.#t||this.#r?.[i]?.[t])return null;let r=this.#r[i]||{};r[t]=!0,this.#r[i]=r;let o=this.#t?.[i]||null;if(o||(o=this.getTestValue(i)||null),!o)return;let n={organizationId:this.organizationId,environment:this.environment,testId:s.id,testValue:o,visitorId:this.#s,pointer:this.#t.pointer,device:this.#t.device,screen:V(),browser:this.#t.browser,os:this.#t.os,visitor:this.#e?"recurring":"new",event:t,message:e||""};return fetch(this.#o,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(n)})}}};
package/dist/client.d.ts CHANGED
@@ -1,19 +1,21 @@
1
- import { EnvironmentOption, Configuration, ImproveArgs } from './types.ts';
1
+ import { ImproveEnvironmentOption, ImproveConfiguration, ImproveSetupArgs } from './types.js';
2
2
 
3
3
  declare class BaseImproveSDK {
4
4
  #private;
5
5
  organizationId: string;
6
- environment: EnvironmentOption;
7
- config: Configuration | null;
8
- constructor({ organizationId, environment, state, config, fetchTimeout, }: ImproveArgs);
6
+ environment: ImproveEnvironmentOption;
7
+ config: ImproveConfiguration | null;
8
+ constructor({ organizationId, environment, state, config, fetchTimeout, }: ImproveSetupArgs);
9
9
  fetchConfig: () => Promise<void>;
10
+ loadConfig: (config: ImproveConfiguration) => void;
10
11
  generateVisitorId: () => string;
11
12
  getVisitorCookieName: () => string;
13
+ validateTestValue: (testName: string, testValue: string) => boolean;
12
14
  }
13
15
 
14
16
  type CreateAnalytic = {
15
17
  organizationId: string;
16
- environment: EnvironmentOption;
18
+ environment: ImproveEnvironmentOption;
17
19
  testId: string;
18
20
  testValue: string;
19
21
  visitorId: string;
@@ -28,11 +30,11 @@ type CreateAnalytic = {
28
30
  };
29
31
  declare class ImproveClientSDK extends BaseImproveSDK {
30
32
  #private;
31
- setupVisitor: (userAgent?: string) => string | null;
32
- getFlagValue: (flagSlug: string) => string | null;
33
- getTestValue: (testSlug: string) => string | null;
33
+ setupVisitor: (userAgent?: string) => string;
34
+ getFlagValue: (flagSlug: string) => string;
35
+ getTestValue: (testSlug: string) => string;
34
36
  setAnalyticsUrls: (url: string) => void;
35
- postAnalytic: (testSlug: string, event: string, message?: string) => Promise<Response> | null;
37
+ postAnalytic: (testSlug: string, event: string, message?: string) => Promise<Response>;
36
38
  }
37
39
 
38
40
  export { type CreateAnalytic, ImproveClientSDK };
@@ -0,0 +1 @@
1
+ import i from"ua-parser-js";let t={device:["wearable","mobile","tablet","console","smarttv","embedded","desktop"],browser:["chrome","safari","firefox","edge","ie","samsung internet","social","other"],os:["mac os","ios","android","windows","unix"]},e=i=>i&&t.device.includes(i)?i:"desktop",s=["tiktok","wechat","weibo","snapchat","klarna","Line","instagram","facebook","alipay","Baidu"],r=(i="")=>{if(!i)return"other";let e=i.toLowerCase();return t.browser.find(i=>e.includes(i))||(s.includes(e)?"social":"other")},o=["wearable","mobile","tablet"],n=i=>o.includes(i)?"coarse":"fine",l=(i="")=>{if(!i)return"unix";let e=i.toLowerCase();return t.os.find(i=>e.includes(i))||"unix"},a=t=>{if(!t||"string"!=typeof t)return null;let s=new i(t).getResult(),o=e(s.device.type);return{pointer:n(o),device:o,browser:r(s.browser.name),os:l(s.os.name)}},h=(i,t)=>!i||Object.entries(i).every(([i,e])=>t[i]===e),u=i=>{if(0===i.length)return null;if(1===i.length)return i[0].slug;let t=Math.random()*i.reduce((i,{split:t})=>i+t,0);return(i.find(({split:i})=>(t-=i)<=0)||i[0]).slug},c="https://improve.obelism.studio",f=`${c}/config`,d=`${c}/api/log`,g="ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",v=i=>new Promise(t=>setTimeout(t,i)),p=async(i=1e3,t)=>(await v(i),t?.abort(),null),m=(i=3e3,t,e)=>{let s=new AbortController;return Promise.race([fetch(t,{...e,signal:s.signal}),p(i,s)])};class w{#i;constructor({organizationId:i,environment:t,state:e,config:s,fetchTimeout:r}){this.organizationId="",this.environment="develop",this.#i=null,this.config=null,this.fetchConfig=async()=>{if(this.config)return;if(!this.#i)throw Error("No config fetch setup provided");let i=await m(this.#i.timeout,this.#i.url);if(!i)throw Error("Configuration fetch timed-out");this.config=await i.json()},this.loadConfig=i=>{this.config=i},this.generateVisitorId=()=>`visi_${(function(i=5){return i&&"number"==typeof i?Array(i).fill("").reduce(i=>i+=g.charAt(Math.floor(Math.random()*g.length)),""):""})(26).toUpperCase()}`,this.getVisitorCookieName=()=>"visitorId",this.validateTestValue=(i,t)=>{if(!this.config)throw Error("Config is required before validating, either use `.fetchConfig()`, .loadConfig(config) or provide it during setup");let e=this.config.tests[i];if(!e)throw Error(`No config found for ${i}`);return!!e.options.find(i=>i.slug===t)},this.organizationId=i,this.environment=t,s?this.config=s:this.#i={url:[f,i,t,e||"active"].join("/"),timeout:r||3e3}}}let b=i=>{if(!i)return!1;let t=document.cookie.split("; ").find(t=>{let[e]=t.split("=");return i===e});return!!t&&t.split("=")[1]},y=(i,t)=>{let e=new Date;e.setMonth(e.getMonth()+1),document.cookie=`${i}=${t};path=/;expires=${e.toUTCString()}`},I=()=>{let i=window.innerWidth;return i<=768?"small":i<=1024?"medium":i<=1200?"large":"huge"};class V extends w{#t;#e;#s;#r;#o;constructor(...i){super(...i),this.#e=!1,this.#s="",this.#r={},this.#o=d,this.setupVisitor=(i=window.navigator.userAgent)=>{let t=b(this.getVisitorCookieName());this.#e=!!t,this.#s=t||this.generateVisitorId();let e=a(i);return e?(this.#t=e,y(this.getVisitorCookieName(),this.#s),this.#s):null},this.getFlagValue=i=>{if(!this.config)return null;let t=this.config.flags[i];if(!t||!t.options[0])return null;if(this.#t||this.setupVisitor(),!this.#s||!this.#t)return t.options[0].slug;if(this.#t?.[i])return this.#t[i];if(!h(this.config.audience[t.audience],this.#t))return t.options[0].slug;let e=b(i)||u(t.options);return e?(this.#t[i]=e,y(i,e),e):null},this.getTestValue=i=>{if(!this.config)return null;let t=this.config.tests[i];if(!t)return null;if(this.#t||this.setupVisitor(),!this.#s||!this.#t)return t.defaultValue;if(this.#t?.[i])return this.#t[i];if(!h(this.config.audience[t.audience],this.#t))return t.defaultValue;if(t.allocation<100&&100*Math.random()>t.allocation)return this.#t[i]=t.defaultValue,this.#t?.[i];let e=b(i)||u(t.options);return e?(this.#t[i]=e,y(i,e),e):null},this.setAnalyticsUrls=i=>{this.#o=i},this.postAnalytic=(i,t,e)=>{if(!this.config)return null;let s=this.config.tests[i];if(this.#t||this.setupVisitor(),!s||!this.#t||this.#r?.[i]?.[t])return null;let r=this.#r[i]||{};r[t]=!0,this.#r[i]=r;let o=this.#t?.[i]||null;if(o||(o=this.getTestValue(i)||null),!o)return;let n={organizationId:this.organizationId,environment:this.environment,testId:s.id,testValue:o,visitorId:this.#s,pointer:this.#t.pointer,device:this.#t.device,screen:I(),browser:this.#t.browser,os:this.#t.os,visitor:this.#e?"recurring":"new",event:t,message:e||""};return fetch(this.#o,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(n)})}}}export{V as ImproveClientSDK};
package/dist/server.cjs CHANGED
@@ -1 +1 @@
1
- Object.defineProperty(exports,"__esModule",{value:!0});var t=require("ua-parser-js"),i=t&&t.__esModule?t:{default:t};const e={device:["wearable","mobile","tablet","console","smarttv","embedded","desktop"],browser:["chrome","safari","firefox","edge","ie","samsung internet","social","other"],os:["mac os","ios","android","windows","unix"]},s=t=>t&&e.device.includes(t)?t:"desktop",o=["tiktok","wechat","weibo","snapchat","klarna","Line","instagram","facebook","alipay","Baidu"],r=(t="")=>{if(!t)return"other";let i=t.toLowerCase();return e.browser.find(t=>i.includes(t))||(o.includes(i)?"social":"other")},n=["wearable","mobile","tablet"],l=t=>n.includes(t)?"coarse":"fine",a=(t="")=>{if(!t)return"unix";let i=t.toLowerCase();return e.os.find(t=>i.includes(t))||"unix"},u=t=>{if(!t||"string"!=typeof t)return null;let e=new i.default(t).getResult(),o=s(e.device.type);return{pointer:l(o),device:o,browser:r(e.browser.name),os:a(e.os.name)}},h=(t,i)=>!t||Object.entries(t).every(([t,e])=>i[t]===e),c=t=>{if(0===t.length)return null;if(1===t.length)return t[0].slug;let i=Math.random()*t.reduce((t,{split:i})=>t+i,0);return(t.find(({split:t})=>(i-=t)<=0)||t[0]).slug},f="ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",g=t=>new Promise(i=>setTimeout(i,t)),d=async(t=1e3,i)=>(await g(t),i?.abort(),null),v=(t=3e3,i,e)=>{let s=new AbortController;return Promise.race([fetch(i,{...e,signal:s.signal}),d(t,s)])};class m{#t;constructor({organizationId:t,environment:i,state:e,config:s,fetchTimeout:o}){this.organizationId="",this.environment="develop",this.#t=null,this.config=null,this.fetchConfig=async()=>{if(this.config)return;if(!this.#t){console.log("No config fetch setup provided");return}let t=await v(this.#t.timeout,this.#t.url);if(!t){console.log("Configuration fetch timed-out");return}this.config=await t.json()},this.generateVisitorId=()=>`visi_${(function(t=5){return t&&"number"==typeof t?Array(t).fill("").reduce(t=>t+=f.charAt(Math.floor(Math.random()*f.length)),""):""})(26).toUpperCase()}`,this.getVisitorCookieName=()=>"visitorId",this.organizationId=t,this.environment=i,s?this.config=s:this.#t={url:["https://improve.obelism.studio/config",t,i,e||"active"].join("/"),timeout:o||3e3}}}exports.ImproveServerSDK=class extends m{#i;constructor(...t){super(...t),this.#i={},this.getFlagConfig=t=>this.config?.flags?.[t],this.getTestConfig=t=>this.config?.tests?.[t],this.getFlagValue=(t,i,e)=>{let s=this.getFlagConfig(t);if(!s||!this.config)return null;if(!i)return s.options[0].slug;if(this.#i?.[i]?.[e]?.[t])return this.#i[i][e][t];if(this.#i[i]=this.#i[i]||{},this.#i[i][e]=this.#i[i][e]||u(e),!h(this.config.audience[s.audience],this.#i[i][e]))return s.options[0].slug;let o=c(s.options);return o?(this.#i[i][e][t]=o,o):null},this.getTestValue=(t,i,e)=>{let s=this.getTestConfig(t);if(!s||!this.config)return null;if(!i||!e)return s.defaultValue;if(this.#i?.[i]?.[e]?.[t])return this.#i[i][e][t];if(this.#i[i]=this.#i[i]||{},this.#i[i][e]=this.#i[i][e]||u(e),!h(this.config.audience[s.audience],this.#i[i][e]))return s.defaultValue;if(s.allocation<100&&100*Math.random()>s.allocation)return this.#i[i][e][t]=s.defaultValue,this.#i[i][e][t];let o=c(s.options);return o?(this.#i[i][e][t]=o,o):null}}};
1
+ Object.defineProperty(exports,"__esModule",{value:!0});var i=require("ua-parser-js"),t=i&&i.__esModule?i:{default:i};const e={device:["wearable","mobile","tablet","console","smarttv","embedded","desktop"],browser:["chrome","safari","firefox","edge","ie","samsung internet","social","other"],os:["mac os","ios","android","windows","unix"]},s=i=>i&&e.device.includes(i)?i:"desktop",o=["tiktok","wechat","weibo","snapchat","klarna","Line","instagram","facebook","alipay","Baidu"],r=(i="")=>{if(!i)return"other";let t=i.toLowerCase();return e.browser.find(i=>t.includes(i))||(o.includes(t)?"social":"other")},n=["wearable","mobile","tablet"],a=i=>n.includes(i)?"coarse":"fine",l=(i="")=>{if(!i)return"unix";let t=i.toLowerCase();return e.os.find(i=>t.includes(i))||"unix"},u=i=>{if(!i||"string"!=typeof i)return null;let e=new t.default(i).getResult(),o=s(e.device.type);return{pointer:a(o),device:o,browser:r(e.browser.name),os:l(e.os.name)}},h=(i,t)=>!i||Object.entries(i).every(([i,e])=>t[i]===e),f=i=>{if(0===i.length)return null;if(1===i.length)return i[0].slug;let t=Math.random()*i.reduce((i,{split:t})=>i+t,0);return(i.find(({split:i})=>(t-=i)<=0)||i[0]).slug},c="ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",g=i=>new Promise(t=>setTimeout(t,i)),d=async(i=1e3,t)=>(await g(i),t?.abort(),null),v=(i=3e3,t,e)=>{let s=new AbortController;return Promise.race([fetch(t,{...e,signal:s.signal}),d(i,s)])};class p{#i;constructor({organizationId:i,environment:t,state:e,config:s,fetchTimeout:o}){this.organizationId="",this.environment="develop",this.#i=null,this.config=null,this.fetchConfig=async()=>{if(this.config)return;if(!this.#i)throw Error("No config fetch setup provided");let i=await v(this.#i.timeout,this.#i.url);if(!i)throw Error("Configuration fetch timed-out");this.config=await i.json()},this.loadConfig=i=>{this.config=i},this.generateVisitorId=()=>`visi_${(function(i=5){return i&&"number"==typeof i?Array(i).fill("").reduce(i=>i+=c.charAt(Math.floor(Math.random()*c.length)),""):""})(26).toUpperCase()}`,this.getVisitorCookieName=()=>"visitorId",this.validateTestValue=(i,t)=>{if(!this.config)throw Error("Config is required before validating, either use `.fetchConfig()`, .loadConfig(config) or provide it during setup");let e=this.config.tests[i];if(!e)throw Error(`No config found for ${i}`);return!!e.options.find(i=>i.slug===t)},this.organizationId=i,this.environment=t,s?this.config=s:this.#i={url:["https://improve.obelism.studio/config",i,t,e||"active"].join("/"),timeout:o||3e3}}}exports.ImproveServerSDK=class extends p{#t;constructor(...i){super(...i),this.#t={},this.getFlagConfig=i=>this.config?.flags?.[i],this.getTestConfig=i=>this.config?.tests?.[i],this.getFlagValue=(i,t,e)=>{let s=this.getFlagConfig(i);if(!s||!this.config)return null;if(!t)return s.options[0].slug;if(this.#t?.[t]?.[e]?.[i])return this.#t[t][e][i];if(this.#t[t]=this.#t[t]||{},this.#t[t][e]=this.#t[t][e]||u(e),!h(this.config.audience[s.audience],this.#t[t][e]))return s.options[0].slug;let o=f(s.options);return o?(this.#t[t][e][i]=o,o):null},this.getTestValue=(i,t,e)=>{let s=this.getTestConfig(i);if(!s||!this.config)return null;if(!t||!e)return s.defaultValue;if(this.#t?.[t]?.[e]?.[i])return this.#t[t][e][i];if(this.#t[t]=this.#t[t]||{},this.#t[t][e]=this.#t[t][e]||u(e),!h(this.config.audience[s.audience],this.#t[t][e]))return s.defaultValue;if(s.allocation<100&&100*Math.random()>s.allocation)return this.#t[t][e][i]=s.defaultValue,this.#t[t][e][i];let o=f(s.options);return o?(this.#t[t][e][i]=o,o):null}}};
package/dist/server.d.ts CHANGED
@@ -1,23 +1,25 @@
1
- import * as __types from './types.ts';
2
- import { EnvironmentOption, Configuration, ImproveArgs } from './types.ts';
1
+ import * as __types from './types.js';
2
+ import { ImproveEnvironmentOption, ImproveConfiguration, ImproveSetupArgs } from './types.js';
3
3
 
4
4
  declare class BaseImproveSDK {
5
5
  #private;
6
6
  organizationId: string;
7
- environment: EnvironmentOption;
8
- config: Configuration | null;
9
- constructor({ organizationId, environment, state, config, fetchTimeout, }: ImproveArgs);
7
+ environment: ImproveEnvironmentOption;
8
+ config: ImproveConfiguration | null;
9
+ constructor({ organizationId, environment, state, config, fetchTimeout, }: ImproveSetupArgs);
10
10
  fetchConfig: () => Promise<void>;
11
+ loadConfig: (config: ImproveConfiguration) => void;
11
12
  generateVisitorId: () => string;
12
13
  getVisitorCookieName: () => string;
14
+ validateTestValue: (testName: string, testValue: string) => boolean;
13
15
  }
14
16
 
15
17
  declare class ImproveServerSDK extends BaseImproveSDK {
16
18
  #private;
17
- getFlagConfig: (flagSlug: string) => __types.Flag | undefined;
18
- getTestConfig: (testSlug: string) => __types.Test | undefined;
19
- getFlagValue: (flagSlug: string, visitorId: string, userAgent: string) => string | null;
20
- getTestValue: (testSlug: string, visitorId: string, userAgent: string) => string | null;
19
+ getFlagConfig: (flagSlug: string) => __types.ImproveFlag;
20
+ getTestConfig: (testSlug: string) => __types.ImproveTest;
21
+ getFlagValue: (flagSlug: string, visitorId: string, userAgent: string) => string;
22
+ getTestValue: (testSlug: string, visitorId: string, userAgent: string) => string;
21
23
  }
22
24
 
23
25
  export { ImproveServerSDK };
@@ -0,0 +1 @@
1
+ import i from"ua-parser-js";let t={device:["wearable","mobile","tablet","console","smarttv","embedded","desktop"],browser:["chrome","safari","firefox","edge","ie","samsung internet","social","other"],os:["mac os","ios","android","windows","unix"]},e=i=>i&&t.device.includes(i)?i:"desktop",s=["tiktok","wechat","weibo","snapchat","klarna","Line","instagram","facebook","alipay","Baidu"],o=(i="")=>{if(!i)return"other";let e=i.toLowerCase();return t.browser.find(i=>e.includes(i))||(s.includes(e)?"social":"other")},r=["wearable","mobile","tablet"],n=i=>r.includes(i)?"coarse":"fine",a=(i="")=>{if(!i)return"unix";let e=i.toLowerCase();return t.os.find(i=>e.includes(i))||"unix"},l=t=>{if(!t||"string"!=typeof t)return null;let s=new i(t).getResult(),r=e(s.device.type);return{pointer:n(r),device:r,browser:o(s.browser.name),os:a(s.os.name)}},u=(i,t)=>!i||Object.entries(i).every(([i,e])=>t[i]===e),h=i=>{if(0===i.length)return null;if(1===i.length)return i[0].slug;let t=Math.random()*i.reduce((i,{split:t})=>i+t,0);return(i.find(({split:i})=>(t-=i)<=0)||i[0]).slug},f="ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",c=i=>new Promise(t=>setTimeout(t,i)),g=async(i=1e3,t)=>(await c(i),t?.abort(),null),d=(i=3e3,t,e)=>{let s=new AbortController;return Promise.race([fetch(t,{...e,signal:s.signal}),g(i,s)])};class v{#i;constructor({organizationId:i,environment:t,state:e,config:s,fetchTimeout:o}){this.organizationId="",this.environment="develop",this.#i=null,this.config=null,this.fetchConfig=async()=>{if(this.config)return;if(!this.#i)throw Error("No config fetch setup provided");let i=await d(this.#i.timeout,this.#i.url);if(!i)throw Error("Configuration fetch timed-out");this.config=await i.json()},this.loadConfig=i=>{this.config=i},this.generateVisitorId=()=>`visi_${(function(i=5){return i&&"number"==typeof i?Array(i).fill("").reduce(i=>i+=f.charAt(Math.floor(Math.random()*f.length)),""):""})(26).toUpperCase()}`,this.getVisitorCookieName=()=>"visitorId",this.validateTestValue=(i,t)=>{if(!this.config)throw Error("Config is required before validating, either use `.fetchConfig()`, .loadConfig(config) or provide it during setup");let e=this.config.tests[i];if(!e)throw Error(`No config found for ${i}`);return!!e.options.find(i=>i.slug===t)},this.organizationId=i,this.environment=t,s?this.config=s:this.#i={url:["https://improve.obelism.studio/config",i,t,e||"active"].join("/"),timeout:o||3e3}}}class m extends v{#t;constructor(...i){super(...i),this.#t={},this.getFlagConfig=i=>this.config?.flags?.[i],this.getTestConfig=i=>this.config?.tests?.[i],this.getFlagValue=(i,t,e)=>{let s=this.getFlagConfig(i);if(!s||!this.config)return null;if(!t)return s.options[0].slug;if(this.#t?.[t]?.[e]?.[i])return this.#t[t][e][i];if(this.#t[t]=this.#t[t]||{},this.#t[t][e]=this.#t[t][e]||l(e),!u(this.config.audience[s.audience],this.#t[t][e]))return s.options[0].slug;let o=h(s.options);return o?(this.#t[t][e][i]=o,o):null},this.getTestValue=(i,t,e)=>{let s=this.getTestConfig(i);if(!s||!this.config)return null;if(!t||!e)return s.defaultValue;if(this.#t?.[t]?.[e]?.[i])return this.#t[t][e][i];if(this.#t[t]=this.#t[t]||{},this.#t[t][e]=this.#t[t][e]||l(e),!u(this.config.audience[s.audience],this.#t[t][e]))return s.defaultValue;if(s.allocation<100&&100*Math.random()>s.allocation)return this.#t[t][e][i]=s.defaultValue,this.#t[t][e][i];let o=h(s.options);return o?(this.#t[t][e][i]=o,o):null}}}export{m as ImproveServerSDK};
package/dist/types.d.ts CHANGED
@@ -10,42 +10,42 @@ declare const AUDIENCE_PARAMS: {
10
10
  };
11
11
  type AudienceParamKey = keyof typeof AUDIENCE_PARAMS;
12
12
 
13
- type EnvironmentOption = 'develop' | 'staging' | 'production';
14
- type ImproveArgs = {
13
+ type ImproveEnvironmentOption = 'develop' | 'staging' | 'production';
14
+ type ImproveTestState = 'draft' | 'active' | 'finished' | 'archived';
15
+ type ImproveSetupArgs = {
15
16
  organizationId: string;
16
- environment: EnvironmentOption;
17
- state?: TestState;
18
- config?: Configuration;
17
+ environment: ImproveEnvironmentOption;
18
+ state?: ImproveTestState;
19
+ config?: ImproveConfiguration;
19
20
  fetchTimeout?: number;
20
21
  };
21
- type TestState = 'draft' | 'active' | 'finished' | 'archived';
22
- type FlagOption = {
22
+ type ImproveFlagOption = {
23
23
  name: string;
24
24
  slug: string;
25
25
  value: string | undefined;
26
26
  split: number;
27
27
  };
28
- type TestOption = {
28
+ type ImproveTestOption = {
29
29
  name: string;
30
30
  slug: string;
31
31
  value: string | undefined;
32
32
  split: number;
33
33
  };
34
- type Flag = {
34
+ type ImproveFlag = {
35
35
  id: string;
36
36
  name: string;
37
37
  audience: string;
38
- options: FlagOption[];
38
+ options: ImproveFlagOption[];
39
39
  };
40
- type Flags = {
41
- [flagSlug in string]: Flag;
40
+ type ImproveFlags = {
41
+ [flagSlug in string]: ImproveFlag;
42
42
  };
43
- type Events = {
43
+ type ImproveEvents = {
44
44
  start: string;
45
45
  metrics: string[];
46
46
  conversion: string;
47
47
  };
48
- type Result = {
48
+ type ImproveResult = {
49
49
  result: {
50
50
  [variant: string]: {
51
51
  [metric: string]: number;
@@ -58,30 +58,30 @@ type Result = {
58
58
  }[];
59
59
  }[];
60
60
  };
61
- type Test = {
61
+ type ImproveTest = {
62
62
  id: string;
63
63
  name: string;
64
64
  defaultValue: string;
65
65
  audience: string;
66
66
  allocation: number;
67
- options: TestOption[];
68
- events: Events;
67
+ options: ImproveTestOption[];
68
+ events: ImproveEvents;
69
69
  };
70
- type Tests = {
71
- [testSlug in string]: Test;
70
+ type ImproveTests = {
71
+ [testSlug in string]: ImproveTest;
72
72
  };
73
- type AudienceValue = {
73
+ type ImproveAudienceValue = {
74
74
  [Key in AudienceParamKey]: string;
75
75
  };
76
- type Audience = {
77
- [audienceSlug in string]: AudienceValue;
76
+ type ImproveAudience = {
77
+ [audienceSlug in string]: ImproveAudienceValue;
78
78
  };
79
- type Configuration = {
79
+ type ImproveConfiguration = {
80
80
  name: string;
81
81
  version: number;
82
- flags: Flags;
83
- tests: Tests;
84
- audience: Audience;
82
+ flags: ImproveFlags;
83
+ tests: ImproveTests;
84
+ audience: ImproveAudience;
85
85
  };
86
86
 
87
- export type { Audience, AudienceValue, Configuration, EnvironmentOption, Events, Flag, FlagOption, Flags, ImproveArgs, Result, Test, TestOption, TestState, Tests };
87
+ export type { ImproveAudience, ImproveAudienceValue, ImproveConfiguration, ImproveEnvironmentOption, ImproveEvents, ImproveFlag, ImproveFlagOption, ImproveFlags, ImproveResult, ImproveSetupArgs, ImproveTest, ImproveTestOption, ImproveTestState, ImproveTests };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@obelism/improve-sdk",
3
3
  "description": "Obelism Improve SDK",
4
- "version": "0.0.3",
4
+ "version": "0.1.0",
5
5
  "keywords": [
6
6
  "ab-tests",
7
7
  "feature-flags",
@@ -21,52 +21,58 @@
21
21
  "exports": {
22
22
  "./server": {
23
23
  "types": "./dist/server.d.ts",
24
- "import": "./dist/server.js",
24
+ "import": "./dist/server.mjs",
25
25
  "require": "./dist/server.cjs"
26
26
  },
27
27
  "./client": {
28
28
  "types": "./dist/client.d.ts",
29
- "import": "./dist/client.js",
29
+ "import": "./dist/client.mjs",
30
30
  "require": "./dist/client.cjs"
31
31
  },
32
32
  "./types": {
33
33
  "types": "./dist/types.d.ts"
34
34
  }
35
35
  },
36
+ "typesVersions": {
37
+ "*": {
38
+ "server": [
39
+ "./dist/server.d.ts"
40
+ ],
41
+ "client": [
42
+ "./dist/client.d.ts"
43
+ ],
44
+ "types": [
45
+ "./dist/types.d.ts"
46
+ ]
47
+ }
48
+ },
36
49
  "scripts": {
37
50
  "build": "bunchee -m",
38
- "dev": "bunchee --watch",
51
+ "dev": "bunchee --watch --no-clean",
39
52
  "test": "vitest",
40
53
  "test:coverage": "vitest run --coverage",
41
54
  "test:ci": "vitest run",
42
- "lint": "eslint",
55
+ "lint": "npm-run-all --parallel lint:*",
43
56
  "lint:fix": "eslint --fix",
44
57
  "lint:types": "tsc --noEmit",
45
- "lint-staged": "lint-staged",
46
- "prettier": "prettier --write --ignore-unknown .",
47
- "prettier:check": "prettier --check --ignore-unknown .",
48
- "clean": "rm -rf node_modules && rm -rf .next"
49
- },
50
- "git": {
51
- "pre-commit": "lint-staged"
52
- },
53
- "lint-staged": {
54
- "*": "prettier --write --ignore-unknown"
58
+ "clean": "rm -rf ./node_modules && rm -rf ./dist && rm -rf ./.turbo"
55
59
  },
56
60
  "peerDependencies": {
57
- "ua-parser-js": "^1.0.37"
61
+ "ua-parser-js": "^1.0.38"
58
62
  },
59
63
  "devDependencies": {
64
+ "@obelism/improve-sdk-eslint-config": "^0.0.0",
65
+ "@obelism/improve-sdk-typescript-config": "^0.0.0",
66
+ "@types/ua-parser-js": "^0.7.39",
60
67
  "@typescript-eslint/eslint-plugin": "^6.15.0",
61
- "@vercel/git-hooks": "^1.0.0",
62
- "bunchee": "^4.2.0",
68
+ "bunchee": "^5.3.1",
63
69
  "eslint": "^8.56.0",
64
70
  "eslint-config-prettier": "^9.1.0",
65
71
  "eslint-plugin-no-relative-import-paths": "^1.5.3",
66
- "eslint-plugin-simple-import-sort": "^10.0.0",
72
+ "eslint-plugin-simple-import-sort": "^12.1.1",
67
73
  "lint-staged": "^15.2.0",
68
- "prettier": "^3.1.1",
74
+ "npm-run-all": "^4.1.5",
69
75
  "typescript": "^5.3.3",
70
- "vitest": "^1.1.0"
76
+ "vitest": "^2.0.5"
71
77
  }
72
78
  }
package/LICENSE DELETED
@@ -1,21 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2023 Obelism.
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
package/README.md DELETED
@@ -1 +0,0 @@
1
- # Obelism Improve SDK
package/dist/client.d.cts DELETED
@@ -1,38 +0,0 @@
1
- import { EnvironmentOption, Configuration, ImproveArgs } from './types.ts';
2
-
3
- declare class BaseImproveSDK {
4
- #private;
5
- organizationId: string;
6
- environment: EnvironmentOption;
7
- config: Configuration | null;
8
- constructor({ organizationId, environment, state, config, fetchTimeout, }: ImproveArgs);
9
- fetchConfig: () => Promise<void>;
10
- generateVisitorId: () => string;
11
- getVisitorCookieName: () => string;
12
- }
13
-
14
- type CreateAnalytic = {
15
- organizationId: string;
16
- environment: EnvironmentOption;
17
- testId: string;
18
- testValue: string;
19
- visitorId: string;
20
- pointer: string;
21
- device: string;
22
- screen: string;
23
- browser: string;
24
- os: string;
25
- visitor: string;
26
- event: string;
27
- message: string;
28
- };
29
- declare class ImproveClientSDK extends BaseImproveSDK {
30
- #private;
31
- setupVisitor: (userAgent?: string) => string | null;
32
- getFlagValue: (flagSlug: string) => string | null;
33
- getTestValue: (testSlug: string) => string | null;
34
- setAnalyticsUrls: (url: string) => void;
35
- postAnalytic: (testSlug: string, event: string, message?: string) => Promise<Response> | null;
36
- }
37
-
38
- export { type CreateAnalytic, ImproveClientSDK };
package/dist/client.js DELETED
@@ -1 +0,0 @@
1
- import i from"ua-parser-js";let t={device:["wearable","mobile","tablet","console","smarttv","embedded","desktop"],browser:["chrome","safari","firefox","edge","ie","samsung internet","social","other"],os:["mac os","ios","android","windows","unix"]},e=i=>i&&t.device.includes(i)?i:"desktop",s=["tiktok","wechat","weibo","snapchat","klarna","Line","instagram","facebook","alipay","Baidu"],r=(i="")=>{if(!i)return"other";let e=i.toLowerCase();return t.browser.find(i=>e.includes(i))||(s.includes(e)?"social":"other")},o=["wearable","mobile","tablet"],n=i=>o.includes(i)?"coarse":"fine",l=(i="")=>{if(!i)return"unix";let e=i.toLowerCase();return t.os.find(i=>e.includes(i))||"unix"},a=t=>{if(!t||"string"!=typeof t)return null;let s=new i(t).getResult(),o=e(s.device.type);return{pointer:n(o),device:o,browser:r(s.browser.name),os:l(s.os.name)}},h=(i,t)=>!i||Object.entries(i).every(([i,e])=>t[i]===e),u=i=>{if(0===i.length)return null;if(1===i.length)return i[0].slug;let t=Math.random()*i.reduce((i,{split:t})=>i+t,0);return(i.find(({split:i})=>(t-=i)<=0)||i[0]).slug},c="https://improve.obelism.studio",d=`${c}/config`,f=`${c}/api/log`,g="ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",v=i=>new Promise(t=>setTimeout(t,i)),p=async(i=1e3,t)=>(await v(i),t?.abort(),null),m=(i=3e3,t,e)=>{let s=new AbortController;return Promise.race([fetch(t,{...e,signal:s.signal}),p(i,s)])};class w{#i;constructor({organizationId:i,environment:t,state:e,config:s,fetchTimeout:r}){this.organizationId="",this.environment="develop",this.#i=null,this.config=null,this.fetchConfig=async()=>{if(this.config)return;if(!this.#i){console.log("No config fetch setup provided");return}let i=await m(this.#i.timeout,this.#i.url);if(!i){console.log("Configuration fetch timed-out");return}this.config=await i.json()},this.generateVisitorId=()=>`visi_${(function(i=5){return i&&"number"==typeof i?Array(i).fill("").reduce(i=>i+=g.charAt(Math.floor(Math.random()*g.length)),""):""})(26).toUpperCase()}`,this.getVisitorCookieName=()=>"visitorId",this.organizationId=i,this.environment=t,s?this.config=s:this.#i={url:[d,i,t,e||"active"].join("/"),timeout:r||3e3}}}let b=i=>{if(!i)return!1;let t=document.cookie.split("; ").find(t=>{let[e]=t.split("=");return i===e});return!!t&&t.split("=")[1]},y=(i,t)=>{let e=new Date;e.setMonth(e.getMonth()+1),document.cookie=`${i}=${t};path=/;expires=${e.toUTCString()}`},I=()=>{let i=window.innerWidth;return i<=768?"small":i<=1024?"medium":i<=1200?"large":"huge"};class V extends w{#t;#e;#s;#r;#o;constructor(...i){super(...i),this.#e=!1,this.#s="",this.#r={},this.#o=f,this.setupVisitor=(i=window.navigator.userAgent)=>{let t=b(this.getVisitorCookieName());this.#e=!!t,this.#s=t||this.generateVisitorId();let e=a(i);return e?(this.#t=e,y(this.getVisitorCookieName(),this.#s),this.#s):null},this.getFlagValue=i=>{if(!this.config)return null;let t=this.config.flags[i];if(!t)return null;if(this.#t||this.setupVisitor(),!this.#s||!this.#t)return t.options[0].slug;if(this.#t?.[i])return this.#t[i];if(!h(this.config.audience[t.audience],this.#t))return t.options[0].slug;let e=b(i)||u(t.options);return e?(this.#t[i]=e,y(i,e),e):null},this.getTestValue=i=>{if(!this.config)return null;let t=this.config.tests[i];if(!t)return null;if(this.#t||this.setupVisitor(),!this.#s||!this.#t)return t.defaultValue;if(this.#t?.[i])return this.#t[i];if(!h(this.config.audience[t.audience],this.#t))return t.defaultValue;if(t.allocation<100&&100*Math.random()>t.allocation)return this.#t[i]=t.defaultValue,this.#t?.[i];let e=b(i)||u(t.options);return e?(this.#t[i]=e,y(i,e),e):null},this.setAnalyticsUrls=i=>{this.#o=i},this.postAnalytic=(i,t,e)=>{if(!this.config)return null;let s=this.config.tests[i];if(this.#t||this.setupVisitor(),!s||!this.#t||this.#r?.[i]?.[t])return null;this.#r[i]=this.#r[i]||{},this.#r[i][t]=!0,this.#t?.[i]||this.getTestValue(i);let r={organizationId:this.organizationId,environment:this.environment,testId:s.id,testValue:this.#t[i],visitorId:this.#s,pointer:this.#t.pointer,device:this.#t.device,screen:I(),browser:this.#t.browser,os:this.#t.os,visitor:this.#e?"recurring":"new",event:t,message:e||""};return fetch(this.#o,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(r)})}}}export{V as ImproveClientSDK};
package/dist/index.d.ts DELETED
@@ -1,87 +0,0 @@
1
- /**
2
- * @constant AUDIENCE_PARAMS
3
- * @description All posibile audience tracking options
4
- */
5
- declare const AUDIENCE_PARAMS: {
6
- readonly pointer: readonly ["coarse", "fine"];
7
- readonly device: readonly ["wearable", "mobile", "tablet", "console", "smarttv", "embedded", "desktop"];
8
- readonly browser: readonly ["chrome", "safari", "firefox", "edge", "ie", "samsung internet", "social", "other"];
9
- readonly os: readonly ["mac os", "ios", "android", "windows", "unix"];
10
- };
11
- type AudienceParamKey = keyof typeof AUDIENCE_PARAMS;
12
-
13
- type EnvironmentOption = 'develop' | 'staging' | 'production';
14
- type ImproveArgs = {
15
- organizationId: string;
16
- environment: EnvironmentOption;
17
- state?: TestState;
18
- config?: Configuration;
19
- fetchTimeout?: number;
20
- };
21
- type TestState = 'draft' | 'active' | 'finished' | 'archived';
22
- type FlagOption = {
23
- name: string;
24
- slug: string;
25
- value: string | undefined;
26
- split: number;
27
- };
28
- type TestOption = {
29
- name: string;
30
- slug: string;
31
- value: string | undefined;
32
- split: number;
33
- };
34
- type Flag = {
35
- id: string;
36
- name: string;
37
- audience: string;
38
- options: FlagOption[];
39
- };
40
- type Flags = {
41
- [flagSlug in string]: Flag;
42
- };
43
- type Events = {
44
- start: string;
45
- metrics: string[];
46
- conversion: string;
47
- };
48
- type Result = {
49
- result: {
50
- [variant: string]: {
51
- [metric: string]: number;
52
- };
53
- };
54
- resultsByDay: {
55
- label: string;
56
- values: {
57
- [x in string]: string | number;
58
- }[];
59
- }[];
60
- };
61
- type Test = {
62
- id: string;
63
- name: string;
64
- defaultValue: string;
65
- audience: string;
66
- allocation: number;
67
- options: TestOption[];
68
- events: Events;
69
- };
70
- type Tests = {
71
- [testSlug in string]: Test;
72
- };
73
- type AudienceValue = {
74
- [Key in AudienceParamKey]: string;
75
- };
76
- type Audience = {
77
- [audienceSlug in string]: AudienceValue;
78
- };
79
- type Configuration = {
80
- name: string;
81
- version: number;
82
- flags: Flags;
83
- tests: Tests;
84
- audience: Audience;
85
- };
86
-
87
- export type { Audience, AudienceValue, Configuration, EnvironmentOption, Events, Flag, FlagOption, Flags, ImproveArgs, Result, Test, TestOption, TestState, Tests };
package/dist/index.js DELETED
@@ -1 +0,0 @@
1
-
package/dist/server.d.cts DELETED
@@ -1,23 +0,0 @@
1
- import * as __types from './types.ts';
2
- import { EnvironmentOption, Configuration, ImproveArgs } from './types.ts';
3
-
4
- declare class BaseImproveSDK {
5
- #private;
6
- organizationId: string;
7
- environment: EnvironmentOption;
8
- config: Configuration | null;
9
- constructor({ organizationId, environment, state, config, fetchTimeout, }: ImproveArgs);
10
- fetchConfig: () => Promise<void>;
11
- generateVisitorId: () => string;
12
- getVisitorCookieName: () => string;
13
- }
14
-
15
- declare class ImproveServerSDK extends BaseImproveSDK {
16
- #private;
17
- getFlagConfig: (flagSlug: string) => __types.Flag | undefined;
18
- getTestConfig: (testSlug: string) => __types.Test | undefined;
19
- getFlagValue: (flagSlug: string, visitorId: string, userAgent: string) => string | null;
20
- getTestValue: (testSlug: string, visitorId: string, userAgent: string) => string | null;
21
- }
22
-
23
- export { ImproveServerSDK };
package/dist/server.js DELETED
@@ -1 +0,0 @@
1
- import i from"ua-parser-js";let t={device:["wearable","mobile","tablet","console","smarttv","embedded","desktop"],browser:["chrome","safari","firefox","edge","ie","samsung internet","social","other"],os:["mac os","ios","android","windows","unix"]},e=i=>i&&t.device.includes(i)?i:"desktop",s=["tiktok","wechat","weibo","snapchat","klarna","Line","instagram","facebook","alipay","Baidu"],o=(i="")=>{if(!i)return"other";let e=i.toLowerCase();return t.browser.find(i=>e.includes(i))||(s.includes(e)?"social":"other")},r=["wearable","mobile","tablet"],n=i=>r.includes(i)?"coarse":"fine",l=(i="")=>{if(!i)return"unix";let e=i.toLowerCase();return t.os.find(i=>e.includes(i))||"unix"},a=t=>{if(!t||"string"!=typeof t)return null;let s=new i(t).getResult(),r=e(s.device.type);return{pointer:n(r),device:r,browser:o(s.browser.name),os:l(s.os.name)}},u=(i,t)=>!i||Object.entries(i).every(([i,e])=>t[i]===e),h=i=>{if(0===i.length)return null;if(1===i.length)return i[0].slug;let t=Math.random()*i.reduce((i,{split:t})=>i+t,0);return(i.find(({split:i})=>(t-=i)<=0)||i[0]).slug},c="ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",f=i=>new Promise(t=>setTimeout(t,i)),g=async(i=1e3,t)=>(await f(i),t?.abort(),null),d=(i=3e3,t,e)=>{let s=new AbortController;return Promise.race([fetch(t,{...e,signal:s.signal}),g(i,s)])};class v{#i;constructor({organizationId:i,environment:t,state:e,config:s,fetchTimeout:o}){this.organizationId="",this.environment="develop",this.#i=null,this.config=null,this.fetchConfig=async()=>{if(this.config)return;if(!this.#i){console.log("No config fetch setup provided");return}let i=await d(this.#i.timeout,this.#i.url);if(!i){console.log("Configuration fetch timed-out");return}this.config=await i.json()},this.generateVisitorId=()=>`visi_${(function(i=5){return i&&"number"==typeof i?Array(i).fill("").reduce(i=>i+=c.charAt(Math.floor(Math.random()*c.length)),""):""})(26).toUpperCase()}`,this.getVisitorCookieName=()=>"visitorId",this.organizationId=i,this.environment=t,s?this.config=s:this.#i={url:["https://improve.obelism.studio/config",i,t,e||"active"].join("/"),timeout:o||3e3}}}class m extends v{#t;constructor(...i){super(...i),this.#t={},this.getFlagConfig=i=>this.config?.flags?.[i],this.getTestConfig=i=>this.config?.tests?.[i],this.getFlagValue=(i,t,e)=>{let s=this.getFlagConfig(i);if(!s||!this.config)return null;if(!t)return s.options[0].slug;if(this.#t?.[t]?.[e]?.[i])return this.#t[t][e][i];if(this.#t[t]=this.#t[t]||{},this.#t[t][e]=this.#t[t][e]||a(e),!u(this.config.audience[s.audience],this.#t[t][e]))return s.options[0].slug;let o=h(s.options);return o?(this.#t[t][e][i]=o,o):null},this.getTestValue=(i,t,e)=>{let s=this.getTestConfig(i);if(!s||!this.config)return null;if(!t||!e)return s.defaultValue;if(this.#t?.[t]?.[e]?.[i])return this.#t[t][e][i];if(this.#t[t]=this.#t[t]||{},this.#t[t][e]=this.#t[t][e]||a(e),!u(this.config.audience[s.audience],this.#t[t][e]))return s.defaultValue;if(s.allocation<100&&100*Math.random()>s.allocation)return this.#t[t][e][i]=s.defaultValue,this.#t[t][e][i];let o=h(s.options);return o?(this.#t[t][e][i]=o,o):null}}}export{m as ImproveServerSDK};