@revenuecat/purchases-js 0.0.6 → 0.0.9

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.
@@ -10,10 +10,10 @@ declare interface Offering_2 {
10
10
  export declare type OfferingsPage = OfferingsPage_2;
11
11
 
12
12
  declare interface OfferingsPage_2 {
13
- offerings: Offering_2[];
14
- priceByPackageId: {
15
- [packageId: string]: number;
13
+ all: {
14
+ [offeringId: string]: Offering_2;
16
15
  };
16
+ current: Offering_2 | null;
17
17
  }
18
18
 
19
19
  export declare type Package = Package_2;
@@ -21,7 +21,6 @@ export declare type Package = Package_2;
21
21
  declare interface Package_2 {
22
22
  id: string;
23
23
  identifier: string;
24
- displayName: string;
25
24
  rcBillingProduct: Product | null;
26
25
  }
27
26
 
@@ -45,11 +44,12 @@ export declare class Purchases {
45
44
  private static readonly _BASE_PATH;
46
45
  constructor(apiKey: string);
47
46
  private toOfferingsPage;
48
- listOfferings(): Promise<OfferingsPage>;
47
+ listOfferings(appUserId: string): Promise<OfferingsPage>;
49
48
  isEntitledTo(appUserId: string, entitlementIdentifier: string): Promise<boolean>;
50
49
  waitForEntitlement(appUserId: string, entitlementIdentifier: string, maxAttempts?: number): Promise<boolean>;
51
50
  subscribe(appUserId: string, productId: string, email: string, environment?: "sandbox" | "production"): Promise<SubscribeResponse>;
52
- getPackage(packageIdentifier: string): Promise<Package | null>;
51
+ getPackage(appUserId: string, packageIdentifier: string): Promise<Package | null>;
52
+ private logMissingProductIds;
53
53
  }
54
54
 
55
55
  declare interface SubscribeResponse {
@@ -1,84 +1,112 @@
1
- var U = Object.defineProperty;
2
- var L = (E, T, _) => T in E ? U(E, T, { enumerable: !0, configurable: !0, writable: !0, value: _ }) : E[T] = _;
3
- var A = (E, T, _) => (L(E, typeof T != "symbol" ? T + "" : T, _), _);
4
- const l = (E) => ({
1
+ var D = Object.defineProperty;
2
+ var o = (E, n, r) => n in E ? D(E, n, { enumerable: !0, configurable: !0, writable: !0, value: r }) : E[n] = r;
3
+ var A = (E, n, r) => (o(E, typeof n != "symbol" ? n + "" : n, r), r);
4
+ const p = (E) => ({
5
5
  amount: E.amount,
6
6
  currency: E.currency
7
- }), p = (E) => ({
8
- id: E.id,
7
+ }), L = (E) => ({
8
+ id: E.identifier,
9
9
  identifier: E.identifier,
10
- displayName: E.display_name,
11
- currentPrice: E.current_price ? l(E.current_price) : null,
10
+ displayName: E.title,
11
+ currentPrice: E.current_price ? p(E.current_price) : null,
12
12
  normalPeriodDuration: E.normal_period_duration
13
- }), f = (E) => ({
14
- id: E.id,
15
- identifier: E.identifier,
16
- displayName: E.display_name,
17
- rcBillingProduct: E.rc_billing_product ? p(E.rc_billing_product) : null
18
- }), M = (E) => ({
19
- id: E.id,
13
+ }), M = (E, n) => {
14
+ const r = n[E.platform_product_identifier];
15
+ return {
16
+ id: E.identifier,
17
+ identifier: E.identifier,
18
+ rcBillingProduct: r ? L(r) : null
19
+ };
20
+ }, P = (E, n) => ({
21
+ id: E.identifier,
20
22
  identifier: E.identifier,
21
- displayName: E.display_name,
22
- packages: E.packages.map(f)
23
- }), F = (E) => {
24
- var T;
23
+ displayName: E.description,
24
+ packages: E.packages.map(
25
+ (r) => M(r, n)
26
+ )
27
+ }), t = (E) => {
28
+ var n;
25
29
  return {
26
30
  nextAction: E.next_action,
27
31
  data: {
28
- clientSecret: ((T = E.data) == null ? void 0 : T.client_secret) ?? void 0
32
+ clientSecret: ((n = E.data) == null ? void 0 : n.client_secret) ?? void 0
29
33
  }
30
34
  };
31
35
  };
32
- var i;
36
+ var I;
33
37
  (function(E) {
34
38
  E[E.CONTINUE = 100] = "CONTINUE", E[E.SWITCHING_PROTOCOLS = 101] = "SWITCHING_PROTOCOLS", E[E.PROCESSING = 102] = "PROCESSING", E[E.EARLY_HINTS = 103] = "EARLY_HINTS", E[E.OK = 200] = "OK", E[E.CREATED = 201] = "CREATED", E[E.ACCEPTED = 202] = "ACCEPTED", E[E.NON_AUTHORITATIVE_INFORMATION = 203] = "NON_AUTHORITATIVE_INFORMATION", E[E.NO_CONTENT = 204] = "NO_CONTENT", E[E.RESET_CONTENT = 205] = "RESET_CONTENT", E[E.PARTIAL_CONTENT = 206] = "PARTIAL_CONTENT", E[E.MULTI_STATUS = 207] = "MULTI_STATUS", E[E.MULTIPLE_CHOICES = 300] = "MULTIPLE_CHOICES", E[E.MOVED_PERMANENTLY = 301] = "MOVED_PERMANENTLY", E[E.MOVED_TEMPORARILY = 302] = "MOVED_TEMPORARILY", E[E.SEE_OTHER = 303] = "SEE_OTHER", E[E.NOT_MODIFIED = 304] = "NOT_MODIFIED", E[E.USE_PROXY = 305] = "USE_PROXY", E[E.TEMPORARY_REDIRECT = 307] = "TEMPORARY_REDIRECT", E[E.PERMANENT_REDIRECT = 308] = "PERMANENT_REDIRECT", E[E.BAD_REQUEST = 400] = "BAD_REQUEST", E[E.UNAUTHORIZED = 401] = "UNAUTHORIZED", E[E.PAYMENT_REQUIRED = 402] = "PAYMENT_REQUIRED", E[E.FORBIDDEN = 403] = "FORBIDDEN", E[E.NOT_FOUND = 404] = "NOT_FOUND", E[E.METHOD_NOT_ALLOWED = 405] = "METHOD_NOT_ALLOWED", E[E.NOT_ACCEPTABLE = 406] = "NOT_ACCEPTABLE", E[E.PROXY_AUTHENTICATION_REQUIRED = 407] = "PROXY_AUTHENTICATION_REQUIRED", E[E.REQUEST_TIMEOUT = 408] = "REQUEST_TIMEOUT", E[E.CONFLICT = 409] = "CONFLICT", E[E.GONE = 410] = "GONE", E[E.LENGTH_REQUIRED = 411] = "LENGTH_REQUIRED", E[E.PRECONDITION_FAILED = 412] = "PRECONDITION_FAILED", E[E.REQUEST_TOO_LONG = 413] = "REQUEST_TOO_LONG", E[E.REQUEST_URI_TOO_LONG = 414] = "REQUEST_URI_TOO_LONG", E[E.UNSUPPORTED_MEDIA_TYPE = 415] = "UNSUPPORTED_MEDIA_TYPE", E[E.REQUESTED_RANGE_NOT_SATISFIABLE = 416] = "REQUESTED_RANGE_NOT_SATISFIABLE", E[E.EXPECTATION_FAILED = 417] = "EXPECTATION_FAILED", E[E.IM_A_TEAPOT = 418] = "IM_A_TEAPOT", E[E.INSUFFICIENT_SPACE_ON_RESOURCE = 419] = "INSUFFICIENT_SPACE_ON_RESOURCE", E[E.METHOD_FAILURE = 420] = "METHOD_FAILURE", E[E.MISDIRECTED_REQUEST = 421] = "MISDIRECTED_REQUEST", E[E.UNPROCESSABLE_ENTITY = 422] = "UNPROCESSABLE_ENTITY", E[E.LOCKED = 423] = "LOCKED", E[E.FAILED_DEPENDENCY = 424] = "FAILED_DEPENDENCY", E[E.UPGRADE_REQUIRED = 426] = "UPGRADE_REQUIRED", E[E.PRECONDITION_REQUIRED = 428] = "PRECONDITION_REQUIRED", E[E.TOO_MANY_REQUESTS = 429] = "TOO_MANY_REQUESTS", E[E.REQUEST_HEADER_FIELDS_TOO_LARGE = 431] = "REQUEST_HEADER_FIELDS_TOO_LARGE", E[E.UNAVAILABLE_FOR_LEGAL_REASONS = 451] = "UNAVAILABLE_FOR_LEGAL_REASONS", E[E.INTERNAL_SERVER_ERROR = 500] = "INTERNAL_SERVER_ERROR", E[E.NOT_IMPLEMENTED = 501] = "NOT_IMPLEMENTED", E[E.BAD_GATEWAY = 502] = "BAD_GATEWAY", E[E.SERVICE_UNAVAILABLE = 503] = "SERVICE_UNAVAILABLE", E[E.GATEWAY_TIMEOUT = 504] = "GATEWAY_TIMEOUT", E[E.HTTP_VERSION_NOT_SUPPORTED = 505] = "HTTP_VERSION_NOT_SUPPORTED", E[E.INSUFFICIENT_STORAGE = 507] = "INSUFFICIENT_STORAGE", E[E.NETWORK_AUTHENTICATION_REQUIRED = 511] = "NETWORK_AUTHENTICATION_REQUIRED";
35
- })(i || (i = {}));
39
+ })(I || (I = {}));
36
40
  class c extends Error {
37
- constructor(T, _) {
38
- super(_), this.statusCode = T;
41
+ constructor(n, r) {
42
+ super(r), this.statusCode = n;
39
43
  }
40
44
  }
41
- class Y extends c {
45
+ class h extends c {
42
46
  constructor() {
43
- super(i.INTERNAL_SERVER_ERROR, "An unknown error occurred.");
47
+ super(I.INTERNAL_SERVER_ERROR, "An unknown error occurred.");
44
48
  }
45
49
  }
46
- class H extends c {
50
+ class g extends c {
47
51
  }
48
- class Q extends c {
52
+ class F extends c {
49
53
  }
50
- class g extends c {
54
+ class Y extends c {
51
55
  }
52
- class h extends c {
56
+ class w extends c {
53
57
  }
54
- const r = class r {
55
- constructor(T) {
58
+ const l = "0.0.8", N = class N {
59
+ constructor(n) {
56
60
  A(this, "_API_KEY", null);
57
61
  A(this, "_APP_USER_ID", null);
58
- A(this, "toOfferingsPage", (T) => ({
59
- offerings: T.offerings.map(M),
60
- priceByPackageId: T.prices_by_package_id
61
- }));
62
- this._API_KEY = T, r._RC_ENDPOINT === void 0 && console.error(
62
+ A(this, "toOfferingsPage", (n, r) => {
63
+ const _ = n.offerings.find(
64
+ (T) => T.identifier === n.current_offering_id
65
+ ) ?? null, R = {};
66
+ r.product_details.forEach((T) => {
67
+ R[T.identifier] = T;
68
+ });
69
+ const O = _ == null ? null : P(_, R), i = {};
70
+ return n.offerings.forEach(
71
+ (T) => i[T.identifier] = P(T, R)
72
+ ), {
73
+ all: i,
74
+ current: O
75
+ };
76
+ });
77
+ this._API_KEY = n, N._RC_ENDPOINT === void 0 && console.error(
63
78
  "Project was build without some of the environment variables set"
64
79
  );
65
80
  }
66
- async listOfferings() {
81
+ async listOfferings(n) {
67
82
  const _ = await (await fetch(
68
- `${r._RC_ENDPOINT}/${r._BASE_PATH}/offerings`,
83
+ `${N._RC_ENDPOINT}/v1/subscribers/${n}/offerings`,
69
84
  {
70
85
  headers: {
71
86
  Authorization: `Bearer ${this._API_KEY}`,
72
87
  "Content-Type": "application/json",
73
- Accept: "application/json"
88
+ Accept: "application/json",
89
+ "X-Platform": "web",
90
+ "X-Version": l
91
+ }
92
+ }
93
+ )).json(), R = _.offerings.flatMap((T) => T.packages).map((T) => T.platform_product_identifier), i = await (await fetch(
94
+ `${N._RC_ENDPOINT}/${N._BASE_PATH}/subscribers/${n}/products?id=${R.join("&id=")}`,
95
+ {
96
+ headers: {
97
+ Authorization: `Bearer ${this._API_KEY}`,
98
+ "Content-Type": "application/json",
99
+ Accept: "application/json",
100
+ "X-Platform": "web",
101
+ "X-Version": l
74
102
  }
75
103
  }
76
104
  )).json();
77
- return this.toOfferingsPage(_);
105
+ return this.logMissingProductIds(R, i.product_details), this.toOfferingsPage(_, i);
78
106
  }
79
- async isEntitledTo(T, _) {
80
- const R = await fetch(
81
- `${r._RC_ENDPOINT}/${r._BASE_PATH}/entitlements/${T}`,
107
+ async isEntitledTo(n, r) {
108
+ const _ = await fetch(
109
+ `${N._RC_ENDPOINT}/${N._BASE_PATH}/entitlements/${n}`,
82
110
  {
83
111
  headers: {
84
112
  Authorization: `Bearer ${this._API_KEY}`,
@@ -87,28 +115,28 @@ const r = class r {
87
115
  }
88
116
  }
89
117
  );
90
- return R.status === 404 ? !1 : (await R.json()).entitlements.map(
91
- (I) => I.lookup_key
92
- ).includes(_);
118
+ return _.status === 404 ? !1 : (await _.json()).entitlements.map(
119
+ (T) => T.lookup_key
120
+ ).includes(r);
93
121
  }
94
- waitForEntitlement(T, _, R = 10) {
95
- return new Promise((O, n) => {
96
- const I = (D = 1) => this.isEntitledTo(T, _).then((P) => {
97
- if (D > R)
122
+ waitForEntitlement(n, r, _ = 10) {
123
+ return new Promise((O, i) => {
124
+ const T = (e = 1) => this.isEntitledTo(n, r).then((f) => {
125
+ if (e > _)
98
126
  return O(!1);
99
- if (P)
127
+ if (f)
100
128
  return O(!0);
101
129
  setTimeout(
102
- () => I(D + 1),
130
+ () => T(e + 1),
103
131
  1e3
104
132
  );
105
- }).catch(n);
106
- I();
133
+ }).catch(i);
134
+ T();
107
135
  });
108
136
  }
109
- async subscribe(T, _, R, N = "production") {
110
- const O = N === "sandbox", n = await fetch(
111
- `${r._RC_ENDPOINT}/${r._BASE_PATH}/subscribe`,
137
+ async subscribe(n, r, _, R = "production") {
138
+ const O = R === "sandbox", i = await fetch(
139
+ `${N._RC_ENDPOINT}/${N._BASE_PATH}/subscribe`,
112
140
  {
113
141
  method: "POST",
114
142
  headers: {
@@ -117,40 +145,54 @@ const r = class r {
117
145
  Accept: "application/json"
118
146
  },
119
147
  body: JSON.stringify({
120
- app_user_id: T,
121
- product_id: _,
148
+ app_user_id: n,
149
+ product_id: r,
122
150
  is_sandbox: O,
123
- email: R
151
+ email: _
124
152
  })
125
153
  }
126
154
  );
127
- if (n.status === i.BAD_REQUEST)
128
- throw new H(n.status);
129
- if (n.status === i.TOO_MANY_REQUESTS)
130
- throw new h(n.status);
131
- if (n.status === i.CONFLICT)
132
- throw new Q(n.status);
133
- if (n.status === i.INTERNAL_SERVER_ERROR)
134
- throw new g(n.status);
135
- if (n.status === i.OK || n.status === i.CREATED) {
136
- const I = await n.json();
137
- return F(I);
155
+ if (i.status === I.BAD_REQUEST)
156
+ throw new g(i.status);
157
+ if (i.status === I.TOO_MANY_REQUESTS)
158
+ throw new w(i.status);
159
+ if (i.status === I.CONFLICT)
160
+ throw new F(i.status);
161
+ if (i.status === I.INTERNAL_SERVER_ERROR)
162
+ throw new Y(i.status);
163
+ if (i.status === I.OK || i.status === I.CREATED) {
164
+ const T = await i.json();
165
+ return t(T);
138
166
  }
139
- throw new Y();
167
+ throw new h();
168
+ }
169
+ async getPackage(n, r) {
170
+ const _ = await this.listOfferings(n), R = [];
171
+ Object.values(_.all).forEach(
172
+ (i) => R.push(...i.packages)
173
+ );
174
+ const O = R.filter(
175
+ (i) => i.identifier === r
176
+ );
177
+ return O.length === 0 ? null : O[0];
140
178
  }
141
- async getPackage(T) {
142
- const _ = await this.listOfferings(), R = [];
143
- _.offerings.forEach(
144
- (O) => R.push(...O.packages)
179
+ logMissingProductIds(n, r) {
180
+ const _ = {};
181
+ r.forEach(
182
+ (O) => _[O.identifier] = O
145
183
  );
146
- const N = R.filter(
147
- (O) => O.identifier === T
184
+ const R = [];
185
+ n.forEach((O) => {
186
+ _[O] === void 0 && R.push(O);
187
+ }), R.length > 0 && console.log(
188
+ "Could not find product data for product ids: ",
189
+ R,
190
+ ". Please check that your product configuration is correct."
148
191
  );
149
- return N.length === 0 ? null : N[0];
150
192
  }
151
193
  };
152
- A(r, "_RC_ENDPOINT", "https://api.revenuecat.com"), A(r, "_BASE_PATH", "rcbilling/v1");
153
- let e = r;
194
+ A(N, "_RC_ENDPOINT", "https://api.revenuecat.com"), A(N, "_BASE_PATH", "rcbilling/v1");
195
+ let U = N;
154
196
  export {
155
- e as Purchases
197
+ U as Purchases
156
198
  };
@@ -1 +1 @@
1
- (function(r,n){typeof exports=="object"&&typeof module<"u"?n(exports):typeof define=="function"&&define.amd?define(["exports"],n):(r=typeof globalThis<"u"?globalThis:r||self,n(r.Purchases={}))})(this,function(r){"use strict";var H=Object.defineProperty;var Q=(r,n,A)=>n in r?H(r,n,{enumerable:!0,configurable:!0,writable:!0,value:A}):r[n]=A;var D=(r,n,A)=>(Q(r,typeof n!="symbol"?n+"":n,A),A);const n=E=>({amount:E.amount,currency:E.currency}),A=E=>({id:E.id,identifier:E.identifier,displayName:E.display_name,currentPrice:E.current_price?n(E.current_price):null,normalPeriodDuration:E.normal_period_duration}),L=E=>({id:E.id,identifier:E.identifier,displayName:E.display_name,rcBillingProduct:E.rc_billing_product?A(E.rc_billing_product):null}),p=E=>({id:E.id,identifier:E.identifier,displayName:E.display_name,packages:E.packages.map(L)}),f=E=>{var T;return{nextAction:E.next_action,data:{clientSecret:((T=E.data)==null?void 0:T.client_secret)??void 0}}};var O;(function(E){E[E.CONTINUE=100]="CONTINUE",E[E.SWITCHING_PROTOCOLS=101]="SWITCHING_PROTOCOLS",E[E.PROCESSING=102]="PROCESSING",E[E.EARLY_HINTS=103]="EARLY_HINTS",E[E.OK=200]="OK",E[E.CREATED=201]="CREATED",E[E.ACCEPTED=202]="ACCEPTED",E[E.NON_AUTHORITATIVE_INFORMATION=203]="NON_AUTHORITATIVE_INFORMATION",E[E.NO_CONTENT=204]="NO_CONTENT",E[E.RESET_CONTENT=205]="RESET_CONTENT",E[E.PARTIAL_CONTENT=206]="PARTIAL_CONTENT",E[E.MULTI_STATUS=207]="MULTI_STATUS",E[E.MULTIPLE_CHOICES=300]="MULTIPLE_CHOICES",E[E.MOVED_PERMANENTLY=301]="MOVED_PERMANENTLY",E[E.MOVED_TEMPORARILY=302]="MOVED_TEMPORARILY",E[E.SEE_OTHER=303]="SEE_OTHER",E[E.NOT_MODIFIED=304]="NOT_MODIFIED",E[E.USE_PROXY=305]="USE_PROXY",E[E.TEMPORARY_REDIRECT=307]="TEMPORARY_REDIRECT",E[E.PERMANENT_REDIRECT=308]="PERMANENT_REDIRECT",E[E.BAD_REQUEST=400]="BAD_REQUEST",E[E.UNAUTHORIZED=401]="UNAUTHORIZED",E[E.PAYMENT_REQUIRED=402]="PAYMENT_REQUIRED",E[E.FORBIDDEN=403]="FORBIDDEN",E[E.NOT_FOUND=404]="NOT_FOUND",E[E.METHOD_NOT_ALLOWED=405]="METHOD_NOT_ALLOWED",E[E.NOT_ACCEPTABLE=406]="NOT_ACCEPTABLE",E[E.PROXY_AUTHENTICATION_REQUIRED=407]="PROXY_AUTHENTICATION_REQUIRED",E[E.REQUEST_TIMEOUT=408]="REQUEST_TIMEOUT",E[E.CONFLICT=409]="CONFLICT",E[E.GONE=410]="GONE",E[E.LENGTH_REQUIRED=411]="LENGTH_REQUIRED",E[E.PRECONDITION_FAILED=412]="PRECONDITION_FAILED",E[E.REQUEST_TOO_LONG=413]="REQUEST_TOO_LONG",E[E.REQUEST_URI_TOO_LONG=414]="REQUEST_URI_TOO_LONG",E[E.UNSUPPORTED_MEDIA_TYPE=415]="UNSUPPORTED_MEDIA_TYPE",E[E.REQUESTED_RANGE_NOT_SATISFIABLE=416]="REQUESTED_RANGE_NOT_SATISFIABLE",E[E.EXPECTATION_FAILED=417]="EXPECTATION_FAILED",E[E.IM_A_TEAPOT=418]="IM_A_TEAPOT",E[E.INSUFFICIENT_SPACE_ON_RESOURCE=419]="INSUFFICIENT_SPACE_ON_RESOURCE",E[E.METHOD_FAILURE=420]="METHOD_FAILURE",E[E.MISDIRECTED_REQUEST=421]="MISDIRECTED_REQUEST",E[E.UNPROCESSABLE_ENTITY=422]="UNPROCESSABLE_ENTITY",E[E.LOCKED=423]="LOCKED",E[E.FAILED_DEPENDENCY=424]="FAILED_DEPENDENCY",E[E.UPGRADE_REQUIRED=426]="UPGRADE_REQUIRED",E[E.PRECONDITION_REQUIRED=428]="PRECONDITION_REQUIRED",E[E.TOO_MANY_REQUESTS=429]="TOO_MANY_REQUESTS",E[E.REQUEST_HEADER_FIELDS_TOO_LARGE=431]="REQUEST_HEADER_FIELDS_TOO_LARGE",E[E.UNAVAILABLE_FOR_LEGAL_REASONS=451]="UNAVAILABLE_FOR_LEGAL_REASONS",E[E.INTERNAL_SERVER_ERROR=500]="INTERNAL_SERVER_ERROR",E[E.NOT_IMPLEMENTED=501]="NOT_IMPLEMENTED",E[E.BAD_GATEWAY=502]="BAD_GATEWAY",E[E.SERVICE_UNAVAILABLE=503]="SERVICE_UNAVAILABLE",E[E.GATEWAY_TIMEOUT=504]="GATEWAY_TIMEOUT",E[E.HTTP_VERSION_NOT_SUPPORTED=505]="HTTP_VERSION_NOT_SUPPORTED",E[E.INSUFFICIENT_STORAGE=507]="INSUFFICIENT_STORAGE",E[E.NETWORK_AUTHENTICATION_REQUIRED=511]="NETWORK_AUTHENTICATION_REQUIRED"})(O||(O={}));class P extends Error{constructor(T,R){super(R),this.statusCode=T}}class M extends P{constructor(){super(O.INTERNAL_SERVER_ERROR,"An unknown error occurred.")}}class F extends P{}class h extends P{}class Y extends P{}class g extends P{}const i=class i{constructor(T){D(this,"_API_KEY",null);D(this,"_APP_USER_ID",null);D(this,"toOfferingsPage",T=>({offerings:T.offerings.map(p),priceByPackageId:T.prices_by_package_id}));this._API_KEY=T,i._RC_ENDPOINT===void 0&&console.error("Project was build without some of the environment variables set")}async listOfferings(){const R=await(await fetch(`${i._RC_ENDPOINT}/${i._BASE_PATH}/offerings`,{headers:{Authorization:`Bearer ${this._API_KEY}`,"Content-Type":"application/json",Accept:"application/json"}})).json();return this.toOfferingsPage(R)}async isEntitledTo(T,R){const N=await fetch(`${i._RC_ENDPOINT}/${i._BASE_PATH}/entitlements/${T}`,{headers:{Authorization:`Bearer ${this._API_KEY}`,"Content-Type":"application/json",Accept:"application/json"}});return N.status===404?!1:(await N.json()).entitlements.map(c=>c.lookup_key).includes(R)}waitForEntitlement(T,R,N=10){return new Promise((I,_)=>{const c=(l=1)=>this.isEntitledTo(T,R).then(m=>{if(l>N)return I(!1);if(m)return I(!0);setTimeout(()=>c(l+1),1e3)}).catch(_);c()})}async subscribe(T,R,N,e="production"){const I=e==="sandbox",_=await fetch(`${i._RC_ENDPOINT}/${i._BASE_PATH}/subscribe`,{method:"POST",headers:{Authorization:`Bearer ${this._API_KEY}`,"Content-Type":"application/json",Accept:"application/json"},body:JSON.stringify({app_user_id:T,product_id:R,is_sandbox:I,email:N})});if(_.status===O.BAD_REQUEST)throw new F(_.status);if(_.status===O.TOO_MANY_REQUESTS)throw new g(_.status);if(_.status===O.CONFLICT)throw new h(_.status);if(_.status===O.INTERNAL_SERVER_ERROR)throw new Y(_.status);if(_.status===O.OK||_.status===O.CREATED){const c=await _.json();return f(c)}throw new M}async getPackage(T){const R=await this.listOfferings(),N=[];R.offerings.forEach(I=>N.push(...I.packages));const e=N.filter(I=>I.identifier===T);return e.length===0?null:e[0]}};D(i,"_RC_ENDPOINT","https://api.revenuecat.com"),D(i,"_BASE_PATH","rcbilling/v1");let U=i;r.Purchases=U,Object.defineProperty(r,Symbol.toStringTag,{value:"Module"})});
1
+ (function(N,e){typeof exports=="object"&&typeof module<"u"?e(exports):typeof define=="function"&&define.amd?define(["exports"],e):(N=typeof globalThis<"u"?globalThis:N||self,e(N.Purchases={}))})(this,function(N){"use strict";var m=Object.defineProperty;var w=(N,e,A)=>e in N?m(N,e,{enumerable:!0,configurable:!0,writable:!0,value:A}):N[e]=A;var P=(N,e,A)=>(w(N,typeof e!="symbol"?e+"":e,A),A);const e=E=>({amount:E.amount,currency:E.currency}),A=E=>({id:E.identifier,identifier:E.identifier,displayName:E.title,currentPrice:E.current_price?e(E.current_price):null,normalPeriodDuration:E.normal_period_duration}),p=(E,n)=>{const r=n[E.platform_product_identifier];return{id:E.identifier,identifier:E.identifier,rcBillingProduct:r?A(r):null}},U=(E,n)=>({id:E.identifier,identifier:E.identifier,displayName:E.description,packages:E.packages.map(r=>p(r,n))}),L=E=>{var n;return{nextAction:E.next_action,data:{clientSecret:((n=E.data)==null?void 0:n.client_secret)??void 0}}};var I;(function(E){E[E.CONTINUE=100]="CONTINUE",E[E.SWITCHING_PROTOCOLS=101]="SWITCHING_PROTOCOLS",E[E.PROCESSING=102]="PROCESSING",E[E.EARLY_HINTS=103]="EARLY_HINTS",E[E.OK=200]="OK",E[E.CREATED=201]="CREATED",E[E.ACCEPTED=202]="ACCEPTED",E[E.NON_AUTHORITATIVE_INFORMATION=203]="NON_AUTHORITATIVE_INFORMATION",E[E.NO_CONTENT=204]="NO_CONTENT",E[E.RESET_CONTENT=205]="RESET_CONTENT",E[E.PARTIAL_CONTENT=206]="PARTIAL_CONTENT",E[E.MULTI_STATUS=207]="MULTI_STATUS",E[E.MULTIPLE_CHOICES=300]="MULTIPLE_CHOICES",E[E.MOVED_PERMANENTLY=301]="MOVED_PERMANENTLY",E[E.MOVED_TEMPORARILY=302]="MOVED_TEMPORARILY",E[E.SEE_OTHER=303]="SEE_OTHER",E[E.NOT_MODIFIED=304]="NOT_MODIFIED",E[E.USE_PROXY=305]="USE_PROXY",E[E.TEMPORARY_REDIRECT=307]="TEMPORARY_REDIRECT",E[E.PERMANENT_REDIRECT=308]="PERMANENT_REDIRECT",E[E.BAD_REQUEST=400]="BAD_REQUEST",E[E.UNAUTHORIZED=401]="UNAUTHORIZED",E[E.PAYMENT_REQUIRED=402]="PAYMENT_REQUIRED",E[E.FORBIDDEN=403]="FORBIDDEN",E[E.NOT_FOUND=404]="NOT_FOUND",E[E.METHOD_NOT_ALLOWED=405]="METHOD_NOT_ALLOWED",E[E.NOT_ACCEPTABLE=406]="NOT_ACCEPTABLE",E[E.PROXY_AUTHENTICATION_REQUIRED=407]="PROXY_AUTHENTICATION_REQUIRED",E[E.REQUEST_TIMEOUT=408]="REQUEST_TIMEOUT",E[E.CONFLICT=409]="CONFLICT",E[E.GONE=410]="GONE",E[E.LENGTH_REQUIRED=411]="LENGTH_REQUIRED",E[E.PRECONDITION_FAILED=412]="PRECONDITION_FAILED",E[E.REQUEST_TOO_LONG=413]="REQUEST_TOO_LONG",E[E.REQUEST_URI_TOO_LONG=414]="REQUEST_URI_TOO_LONG",E[E.UNSUPPORTED_MEDIA_TYPE=415]="UNSUPPORTED_MEDIA_TYPE",E[E.REQUESTED_RANGE_NOT_SATISFIABLE=416]="REQUESTED_RANGE_NOT_SATISFIABLE",E[E.EXPECTATION_FAILED=417]="EXPECTATION_FAILED",E[E.IM_A_TEAPOT=418]="IM_A_TEAPOT",E[E.INSUFFICIENT_SPACE_ON_RESOURCE=419]="INSUFFICIENT_SPACE_ON_RESOURCE",E[E.METHOD_FAILURE=420]="METHOD_FAILURE",E[E.MISDIRECTED_REQUEST=421]="MISDIRECTED_REQUEST",E[E.UNPROCESSABLE_ENTITY=422]="UNPROCESSABLE_ENTITY",E[E.LOCKED=423]="LOCKED",E[E.FAILED_DEPENDENCY=424]="FAILED_DEPENDENCY",E[E.UPGRADE_REQUIRED=426]="UPGRADE_REQUIRED",E[E.PRECONDITION_REQUIRED=428]="PRECONDITION_REQUIRED",E[E.TOO_MANY_REQUESTS=429]="TOO_MANY_REQUESTS",E[E.REQUEST_HEADER_FIELDS_TOO_LARGE=431]="REQUEST_HEADER_FIELDS_TOO_LARGE",E[E.UNAVAILABLE_FOR_LEGAL_REASONS=451]="UNAVAILABLE_FOR_LEGAL_REASONS",E[E.INTERNAL_SERVER_ERROR=500]="INTERNAL_SERVER_ERROR",E[E.NOT_IMPLEMENTED=501]="NOT_IMPLEMENTED",E[E.BAD_GATEWAY=502]="BAD_GATEWAY",E[E.SERVICE_UNAVAILABLE=503]="SERVICE_UNAVAILABLE",E[E.GATEWAY_TIMEOUT=504]="GATEWAY_TIMEOUT",E[E.HTTP_VERSION_NOT_SUPPORTED=505]="HTTP_VERSION_NOT_SUPPORTED",E[E.INSUFFICIENT_STORAGE=507]="INSUFFICIENT_STORAGE",E[E.NETWORK_AUTHENTICATION_REQUIRED=511]="NETWORK_AUTHENTICATION_REQUIRED"})(I||(I={}));class f extends Error{constructor(n,r){super(r),this.statusCode=n}}class t extends f{constructor(){super(I.INTERNAL_SERVER_ERROR,"An unknown error occurred.")}}class M extends f{}class h extends f{}class g extends f{}class F extends f{}const o="0.0.8",c=class c{constructor(n){P(this,"_API_KEY",null);P(this,"_APP_USER_ID",null);P(this,"toOfferingsPage",(n,r)=>{const _=n.offerings.find(T=>T.identifier===n.current_offering_id)??null,R={};r.product_details.forEach(T=>{R[T.identifier]=T});const O=_==null?null:U(_,R),i={};return n.offerings.forEach(T=>i[T.identifier]=U(T,R)),{all:i,current:O}});this._API_KEY=n,c._RC_ENDPOINT===void 0&&console.error("Project was build without some of the environment variables set")}async listOfferings(n){const _=await(await fetch(`${c._RC_ENDPOINT}/v1/subscribers/${n}/offerings`,{headers:{Authorization:`Bearer ${this._API_KEY}`,"Content-Type":"application/json",Accept:"application/json","X-Platform":"web","X-Version":o}})).json(),R=_.offerings.flatMap(T=>T.packages).map(T=>T.platform_product_identifier),i=await(await fetch(`${c._RC_ENDPOINT}/${c._BASE_PATH}/subscribers/${n}/products?id=${R.join("&id=")}`,{headers:{Authorization:`Bearer ${this._API_KEY}`,"Content-Type":"application/json",Accept:"application/json","X-Platform":"web","X-Version":o}})).json();return this.logMissingProductIds(R,i.product_details),this.toOfferingsPage(_,i)}async isEntitledTo(n,r){const _=await fetch(`${c._RC_ENDPOINT}/${c._BASE_PATH}/entitlements/${n}`,{headers:{Authorization:`Bearer ${this._API_KEY}`,"Content-Type":"application/json",Accept:"application/json"}});return _.status===404?!1:(await _.json()).entitlements.map(T=>T.lookup_key).includes(r)}waitForEntitlement(n,r,_=10){return new Promise((O,i)=>{const T=(D=1)=>this.isEntitledTo(n,r).then(Y=>{if(D>_)return O(!1);if(Y)return O(!0);setTimeout(()=>T(D+1),1e3)}).catch(i);T()})}async subscribe(n,r,_,R="production"){const O=R==="sandbox",i=await fetch(`${c._RC_ENDPOINT}/${c._BASE_PATH}/subscribe`,{method:"POST",headers:{Authorization:`Bearer ${this._API_KEY}`,"Content-Type":"application/json",Accept:"application/json"},body:JSON.stringify({app_user_id:n,product_id:r,is_sandbox:O,email:_})});if(i.status===I.BAD_REQUEST)throw new M(i.status);if(i.status===I.TOO_MANY_REQUESTS)throw new F(i.status);if(i.status===I.CONFLICT)throw new h(i.status);if(i.status===I.INTERNAL_SERVER_ERROR)throw new g(i.status);if(i.status===I.OK||i.status===I.CREATED){const T=await i.json();return L(T)}throw new t}async getPackage(n,r){const _=await this.listOfferings(n),R=[];Object.values(_.all).forEach(i=>R.push(...i.packages));const O=R.filter(i=>i.identifier===r);return O.length===0?null:O[0]}logMissingProductIds(n,r){const _={};r.forEach(O=>_[O.identifier]=O);const R=[];n.forEach(O=>{_[O]===void 0&&R.push(O)}),R.length>0&&console.log("Could not find product data for product ids: ",R,". Please check that your product configuration is correct.")}};P(c,"_RC_ENDPOINT","https://api.revenuecat.com"),P(c,"_BASE_PATH","rcbilling/v1");let l=c;N.Purchases=l,Object.defineProperty(N,Symbol.toStringTag,{value:"Module"})});
package/package.json CHANGED
@@ -1,13 +1,14 @@
1
1
  {
2
2
  "name": "@revenuecat/purchases-js",
3
3
  "private": false,
4
- "version": "0.0.6",
4
+ "version": "0.0.9",
5
5
  "type": "module",
6
6
  "files": [
7
7
  "dist"
8
8
  ],
9
9
  "main": "./dist/Purchases.umd.js",
10
10
  "module": "./dist/Purchases.es.js",
11
+ "types": "./dist/Purchases.es.d.ts",
11
12
  "exports": {
12
13
  ".": {
13
14
  "import": "./dist/Purchases.es.js",