@entitlehub/sdk 0.2.0 → 0.2.1
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/index.cjs +1 -1
- package/dist/index.d.cts +5 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var d=Object.defineProperty;var
|
|
1
|
+
"use strict";var d=Object.defineProperty;var _=Object.getOwnPropertyDescriptor;var E=Object.getOwnPropertyNames;var x=Object.prototype.hasOwnProperty;var S=(s,t)=>{for(var e in t)d(s,e,{get:t[e],enumerable:!0})},k=(s,t,e,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let r of E(t))!x.call(s,r)&&r!==e&&d(s,r,{get:()=>t[r],enumerable:!(n=_(t,r))||n.enumerable});return s};var P=s=>k(d({},"__esModule",{value:!0}),s);var U={};S(U,{CustomerInfo:()=>o,EntitleHub:()=>l,EntitleHubError:()=>i,EntitleHubServer:()=>f});module.exports=P(U);var o=class{constructor(t){this.appUserId=t.app_user_id,this.entitlements=t.active_entitlements??[];let e={};for(let n of this.entitlements)e[n.entitlement]=n;this.active=e}isActive(t){return!!this.active[t]}get activeEntitlementIds(){return Object.keys(this.active)}expirationDate(t){let e=this.active[t];if(e)return e.expires_at?new Date(e.expires_at):null}};var C="0.2.1";var i=class extends Error{constructor(t,e,n="error"){super(t),this.name="EntitleHubError",this.status=e,this.code=n}},T=s=>new Promise(t=>setTimeout(t,s));async function c(s,t,e,n){let r=s.fetchImpl??globalThis.fetch;if(!r)throw new i("No fetch implementation available. On Node <18, pass fetchImpl.",0,"no_fetch");let h=`${s.baseUrl.replace(/\/$/,"")}${e}`,b=t==="GET"?s.retries??2:0,m;for(let u=0;u<=b;u++){let y=new AbortController,g=s.timeoutMs?setTimeout(()=>y.abort(),s.timeoutMs):void 0;try{let a=await r(h,{method:t,headers:{Authorization:`Bearer ${s.apiKey}`,"X-EntitleHub-Client":s.client??`${"js"}/${C}`,...n?{"Content-Type":"application/json"}:{}},body:n?JSON.stringify(n):void 0,signal:y.signal}),I=await a.text(),p=I?v(I):{};if(!a.ok){let w=`HTTP ${a.status}`;throw p&&typeof p=="object"&&"error"in p&&(w=String(p.error)),new i(w,a.status,a.status===401||a.status===403?"auth":"http")}return p}catch(a){if(m=a,a instanceof i)throw a;u<b&&await T(250*(u+1))}finally{g&&clearTimeout(g)}}throw new i(`Network request failed: ${String(m?.message??m)}`,0,"network")}function v(s){try{return JSON.parse(s)}catch{return{}}}var l=class{constructor(t){this.listeners=new Set;if(!t.apiKey)throw new i("apiKey is required.",0,"config");if(!t.appUserId)throw new i("appUserId is required.",0,"config");if(t.apiKey.startsWith("sk_"))throw new i("Never use a secret (sk_) key in the client SDK \u2014 use your publishable (pk_) key.",0,"config");this.appUserId=t.appUserId,this.cacheTtlMs=t.cacheTtlMs??5*6e4,this.http={baseUrl:t.baseUrl??"https://entitlehub.com/v1",apiKey:t.apiKey,fetchImpl:t.fetchImpl,timeoutMs:15e3,client:t.client}}get currentAppUserId(){return this.appUserId}async logIn(t){return this.appUserId=t,this.cached=void 0,this.getCustomerInfo({fetchPolicy:"network-only"})}logOut(){this.cached=void 0}async getCustomerInfo(t={}){if((t.fetchPolicy??"cache-first")==="cache-first"&&this.cached&&Date.now()-this.cached.at<this.cacheTtlMs)return this.cached.info;let n=await c(this.http,"GET",`/subscribers/${encodeURIComponent(this.appUserId)}`),r=new o(n);this.cached={info:r,at:Date.now()};for(let h of this.listeners)try{h(r)}catch{}return r}async isEntitled(t){return(await this.getCustomerInfo()).isActive(t)}async checkEntitlement(t,e={}){return c(this.http,"POST","/check",{app_user_id:this.appUserId,entitlement:t,sandbox:!!e.sandbox})}async getOfferings(){return c(this.http,"GET","/offerings")}async reportPurchase(t){let e=await c(this.http,"POST",`/subscribers/${encodeURIComponent(this.appUserId)}/purchases`,{store:t.store??"",store_product_id:t.storeProductId??"",is_sandbox:!!t.isSandbox,purchase_token:t.purchaseToken??"",is_subscription:!!t.isSubscription,signed_transaction:t.signedTransaction??"",stripe_subscription_id:t.stripeSubscriptionId??""}),n=new o(e);this.cached={info:n,at:Date.now()};for(let r of this.listeners)try{r(n)}catch{}return n}addCustomerInfoUpdateListener(t){return this.listeners.add(t),()=>this.listeners.delete(t)}};var f=class{constructor(t){if(!t.apiKey)throw new i("apiKey is required.",0,"config");if(!t.apiKey.startsWith("sk_"))throw new i("EntitleHubServer needs a secret (sk_) key. For client reads, use the EntitleHub class with a pk_ key.",0,"config");this.http={baseUrl:t.baseUrl??"https://entitlehub.com/v1",apiKey:t.apiKey,fetchImpl:t.fetchImpl,timeoutMs:15e3}}async reportPurchase(t,e){let n=await c(this.http,"POST",`/subscribers/${encodeURIComponent(t)}/purchases`,{store:e.store,store_product_id:e.storeProductId,is_sandbox:!!e.isSandbox,transaction_id:e.transactionId??"",purchase_token:e.purchaseToken??"",is_subscription:!!e.isSubscription,signed_transaction:e.signedTransaction??"",stripe_subscription_id:e.stripeSubscriptionId??""});return new o(n)}async grantEntitlement(t,e,n={}){let r=await c(this.http,"POST",`/subscribers/${encodeURIComponent(t)}/grant`,{entitlement:e,duration_days:n.durationDays??0,is_sandbox:!!n.isSandbox});return new o(r)}async getCustomerInfo(t){let e=await c(this.http,"GET",`/subscribers/${encodeURIComponent(t)}`);return new o(e)}async check(t,e,n={}){return c(this.http,"POST","/check",{app_user_id:t,entitlement:e,sandbox:!!n.sandbox})}};0&&(module.exports={CustomerInfo,EntitleHub,EntitleHubError,EntitleHubServer});
|
package/dist/index.d.cts
CHANGED
|
@@ -112,6 +112,11 @@ interface EntitleHubOptions {
|
|
|
112
112
|
fetchImpl?: typeof fetch;
|
|
113
113
|
/** How long a cached CustomerInfo is considered fresh, ms (default 5 min). */
|
|
114
114
|
cacheTtlMs?: number;
|
|
115
|
+
/**
|
|
116
|
+
* Identifies the calling SDK as `name/version`, e.g. "react-native/0.1.5". Wrapper SDKs set this
|
|
117
|
+
* so the dashboard can flag an out-of-date SDK. Defaults to this package's own name/version.
|
|
118
|
+
*/
|
|
119
|
+
client?: string;
|
|
115
120
|
}
|
|
116
121
|
type CustomerInfoListener = (info: CustomerInfo) => void;
|
|
117
122
|
type FetchPolicy = "cache-first" | "network-only";
|
package/dist/index.d.ts
CHANGED
|
@@ -112,6 +112,11 @@ interface EntitleHubOptions {
|
|
|
112
112
|
fetchImpl?: typeof fetch;
|
|
113
113
|
/** How long a cached CustomerInfo is considered fresh, ms (default 5 min). */
|
|
114
114
|
cacheTtlMs?: number;
|
|
115
|
+
/**
|
|
116
|
+
* Identifies the calling SDK as `name/version`, e.g. "react-native/0.1.5". Wrapper SDKs set this
|
|
117
|
+
* so the dashboard can flag an out-of-date SDK. Defaults to this package's own name/version.
|
|
118
|
+
*/
|
|
119
|
+
client?: string;
|
|
115
120
|
}
|
|
116
121
|
type CustomerInfoListener = (info: CustomerInfo) => void;
|
|
117
122
|
type FetchPolicy = "cache-first" | "network-only";
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var a=class{constructor(t){this.appUserId=t.app_user_id,this.entitlements=t.active_entitlements??[];let e={};for(let
|
|
1
|
+
var a=class{constructor(t){this.appUserId=t.app_user_id,this.entitlements=t.active_entitlements??[];let e={};for(let n of this.entitlements)e[n.entitlement]=n;this.active=e}isActive(t){return!!this.active[t]}get activeEntitlementIds(){return Object.keys(this.active)}expirationDate(t){let e=this.active[t];if(e)return e.expires_at?new Date(e.expires_at):null}};var w="0.2.1";var r=class extends Error{constructor(t,e,n="error"){super(t),this.name="EntitleHubError",this.status=e,this.code=n}},_=s=>new Promise(t=>setTimeout(t,s));async function c(s,t,e,n){let i=s.fetchImpl??globalThis.fetch;if(!i)throw new r("No fetch implementation available. On Node <18, pass fetchImpl.",0,"no_fetch");let l=`${s.baseUrl.replace(/\/$/,"")}${e}`,d=t==="GET"?s.retries??2:0,f;for(let u=0;u<=d;u++){let b=new AbortController,y=s.timeoutMs?setTimeout(()=>b.abort(),s.timeoutMs):void 0;try{let o=await i(l,{method:t,headers:{Authorization:`Bearer ${s.apiKey}`,"X-EntitleHub-Client":s.client??`${"js"}/${w}`,...n?{"Content-Type":"application/json"}:{}},body:n?JSON.stringify(n):void 0,signal:b.signal}),g=await o.text(),p=g?E(g):{};if(!o.ok){let I=`HTTP ${o.status}`;throw p&&typeof p=="object"&&"error"in p&&(I=String(p.error)),new r(I,o.status,o.status===401||o.status===403?"auth":"http")}return p}catch(o){if(f=o,o instanceof r)throw o;u<d&&await _(250*(u+1))}finally{y&&clearTimeout(y)}}throw new r(`Network request failed: ${String(f?.message??f)}`,0,"network")}function E(s){try{return JSON.parse(s)}catch{return{}}}var h=class{constructor(t){this.listeners=new Set;if(!t.apiKey)throw new r("apiKey is required.",0,"config");if(!t.appUserId)throw new r("appUserId is required.",0,"config");if(t.apiKey.startsWith("sk_"))throw new r("Never use a secret (sk_) key in the client SDK \u2014 use your publishable (pk_) key.",0,"config");this.appUserId=t.appUserId,this.cacheTtlMs=t.cacheTtlMs??5*6e4,this.http={baseUrl:t.baseUrl??"https://entitlehub.com/v1",apiKey:t.apiKey,fetchImpl:t.fetchImpl,timeoutMs:15e3,client:t.client}}get currentAppUserId(){return this.appUserId}async logIn(t){return this.appUserId=t,this.cached=void 0,this.getCustomerInfo({fetchPolicy:"network-only"})}logOut(){this.cached=void 0}async getCustomerInfo(t={}){if((t.fetchPolicy??"cache-first")==="cache-first"&&this.cached&&Date.now()-this.cached.at<this.cacheTtlMs)return this.cached.info;let n=await c(this.http,"GET",`/subscribers/${encodeURIComponent(this.appUserId)}`),i=new a(n);this.cached={info:i,at:Date.now()};for(let l of this.listeners)try{l(i)}catch{}return i}async isEntitled(t){return(await this.getCustomerInfo()).isActive(t)}async checkEntitlement(t,e={}){return c(this.http,"POST","/check",{app_user_id:this.appUserId,entitlement:t,sandbox:!!e.sandbox})}async getOfferings(){return c(this.http,"GET","/offerings")}async reportPurchase(t){let e=await c(this.http,"POST",`/subscribers/${encodeURIComponent(this.appUserId)}/purchases`,{store:t.store??"",store_product_id:t.storeProductId??"",is_sandbox:!!t.isSandbox,purchase_token:t.purchaseToken??"",is_subscription:!!t.isSubscription,signed_transaction:t.signedTransaction??"",stripe_subscription_id:t.stripeSubscriptionId??""}),n=new a(e);this.cached={info:n,at:Date.now()};for(let i of this.listeners)try{i(n)}catch{}return n}addCustomerInfoUpdateListener(t){return this.listeners.add(t),()=>this.listeners.delete(t)}};var m=class{constructor(t){if(!t.apiKey)throw new r("apiKey is required.",0,"config");if(!t.apiKey.startsWith("sk_"))throw new r("EntitleHubServer needs a secret (sk_) key. For client reads, use the EntitleHub class with a pk_ key.",0,"config");this.http={baseUrl:t.baseUrl??"https://entitlehub.com/v1",apiKey:t.apiKey,fetchImpl:t.fetchImpl,timeoutMs:15e3}}async reportPurchase(t,e){let n=await c(this.http,"POST",`/subscribers/${encodeURIComponent(t)}/purchases`,{store:e.store,store_product_id:e.storeProductId,is_sandbox:!!e.isSandbox,transaction_id:e.transactionId??"",purchase_token:e.purchaseToken??"",is_subscription:!!e.isSubscription,signed_transaction:e.signedTransaction??"",stripe_subscription_id:e.stripeSubscriptionId??""});return new a(n)}async grantEntitlement(t,e,n={}){let i=await c(this.http,"POST",`/subscribers/${encodeURIComponent(t)}/grant`,{entitlement:e,duration_days:n.durationDays??0,is_sandbox:!!n.isSandbox});return new a(i)}async getCustomerInfo(t){let e=await c(this.http,"GET",`/subscribers/${encodeURIComponent(t)}`);return new a(e)}async check(t,e,n={}){return c(this.http,"POST","/check",{app_user_id:t,entitlement:e,sandbox:!!n.sandbox})}};export{a as CustomerInfo,h as EntitleHub,r as EntitleHubError,m as EntitleHubServer};
|
package/package.json
CHANGED