@revenuecat/purchases-js 0.0.8 → 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;
@@ -49,6 +49,7 @@ export declare class Purchases {
49
49
  waitForEntitlement(appUserId: string, entitlementIdentifier: string, maxAttempts?: number): Promise<boolean>;
50
50
  subscribe(appUserId: string, productId: string, email: string, environment?: "sandbox" | "production"): Promise<SubscribeResponse>;
51
51
  getPackage(appUserId: string, packageIdentifier: string): Promise<Package | null>;
52
+ private logMissingProductIds;
52
53
  }
53
54
 
54
55
  declare interface SubscribeResponse {
@@ -1,30 +1,30 @@
1
- var p = Object.defineProperty;
2
- var L = (E, n, r) => n in E ? p(E, n, { enumerable: !0, configurable: !0, writable: !0, value: r }) : E[n] = r;
3
- var A = (E, n, r) => (L(E, typeof n != "symbol" ? n + "" : n, r), r);
4
- const D = (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
- }), l = (E) => ({
7
+ }), L = (E) => ({
8
8
  id: E.identifier,
9
9
  identifier: E.identifier,
10
10
  displayName: E.title,
11
- currentPrice: E.current_price ? D(E.current_price) : null,
11
+ currentPrice: E.current_price ? p(E.current_price) : null,
12
12
  normalPeriodDuration: E.normal_period_duration
13
13
  }), M = (E, n) => {
14
14
  const r = n[E.platform_product_identifier];
15
15
  return {
16
16
  id: E.identifier,
17
17
  identifier: E.identifier,
18
- rcBillingProduct: r ? l(r) : null
18
+ rcBillingProduct: r ? L(r) : null
19
19
  };
20
- }, o = (E, n) => ({
20
+ }, P = (E, n) => ({
21
21
  id: E.identifier,
22
22
  identifier: E.identifier,
23
23
  displayName: E.description,
24
24
  packages: E.packages.map(
25
25
  (r) => M(r, n)
26
26
  )
27
- }), F = (E) => {
27
+ }), t = (E) => {
28
28
  var n;
29
29
  return {
30
30
  nextAction: E.next_action,
@@ -47,70 +47,66 @@ class h extends c {
47
47
  super(I.INTERNAL_SERVER_ERROR, "An unknown error occurred.");
48
48
  }
49
49
  }
50
- class Y extends c {
50
+ class g extends c {
51
51
  }
52
- class w extends c {
52
+ class F extends c {
53
53
  }
54
- class B extends c {
54
+ class Y extends c {
55
55
  }
56
- class g extends c {
56
+ class w extends c {
57
57
  }
58
- const P = "0.0.8", O = class O {
58
+ const l = "0.0.8", N = class N {
59
59
  constructor(n) {
60
60
  A(this, "_API_KEY", null);
61
61
  A(this, "_APP_USER_ID", null);
62
62
  A(this, "toOfferingsPage", (n, r) => {
63
- const _ = n.offerings.filter(
63
+ const _ = n.offerings.find(
64
64
  (T) => T.identifier === n.current_offering_id
65
- ), i = {};
65
+ ) ?? null, R = {};
66
66
  r.product_details.forEach((T) => {
67
- i[T.identifier] = T;
67
+ R[T.identifier] = T;
68
68
  });
69
- const R = {};
70
- return _[0].packages.forEach(
71
- (T) => R[T.identifier] = D(
72
- i[T.platform_product_identifier].current_price
73
- )
69
+ const O = _ == null ? null : P(_, R), i = {};
70
+ return n.offerings.forEach(
71
+ (T) => i[T.identifier] = P(T, R)
74
72
  ), {
75
- offerings: _.map(
76
- (T) => o(T, i)
77
- ),
78
- priceByPackageId: R
73
+ all: i,
74
+ current: O
79
75
  };
80
76
  });
81
- this._API_KEY = n, O._RC_ENDPOINT === void 0 && console.error(
77
+ this._API_KEY = n, N._RC_ENDPOINT === void 0 && console.error(
82
78
  "Project was build without some of the environment variables set"
83
79
  );
84
80
  }
85
81
  async listOfferings(n) {
86
82
  const _ = await (await fetch(
87
- `${O._RC_ENDPOINT}/v1/subscribers/${n}/offerings`,
83
+ `${N._RC_ENDPOINT}/v1/subscribers/${n}/offerings`,
88
84
  {
89
85
  headers: {
90
86
  Authorization: `Bearer ${this._API_KEY}`,
91
87
  "Content-Type": "application/json",
92
88
  Accept: "application/json",
93
89
  "X-Platform": "web",
94
- "X-Version": P
90
+ "X-Version": l
95
91
  }
96
92
  }
97
- )).json(), i = _.offerings.flatMap((N) => N.packages).map((N) => N.platform_product_identifier), T = await (await fetch(
98
- `${O._RC_ENDPOINT}/${O._BASE_PATH}/subscribers/${n}/products?id=${i.join("&id=")}`,
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=")}`,
99
95
  {
100
96
  headers: {
101
97
  Authorization: `Bearer ${this._API_KEY}`,
102
98
  "Content-Type": "application/json",
103
99
  Accept: "application/json",
104
100
  "X-Platform": "web",
105
- "X-Version": P
101
+ "X-Version": l
106
102
  }
107
103
  }
108
104
  )).json();
109
- return this.toOfferingsPage(_, T);
105
+ return this.logMissingProductIds(R, i.product_details), this.toOfferingsPage(_, i);
110
106
  }
111
107
  async isEntitledTo(n, r) {
112
108
  const _ = await fetch(
113
- `${O._RC_ENDPOINT}/${O._BASE_PATH}/entitlements/${n}`,
109
+ `${N._RC_ENDPOINT}/${N._BASE_PATH}/entitlements/${n}`,
114
110
  {
115
111
  headers: {
116
112
  Authorization: `Bearer ${this._API_KEY}`,
@@ -120,27 +116,27 @@ const P = "0.0.8", O = class O {
120
116
  }
121
117
  );
122
118
  return _.status === 404 ? !1 : (await _.json()).entitlements.map(
123
- (N) => N.lookup_key
119
+ (T) => T.lookup_key
124
120
  ).includes(r);
125
121
  }
126
122
  waitForEntitlement(n, r, _ = 10) {
127
- return new Promise((R, T) => {
128
- const N = (e = 1) => this.isEntitledTo(n, r).then((f) => {
123
+ return new Promise((O, i) => {
124
+ const T = (e = 1) => this.isEntitledTo(n, r).then((f) => {
129
125
  if (e > _)
130
- return R(!1);
126
+ return O(!1);
131
127
  if (f)
132
- return R(!0);
128
+ return O(!0);
133
129
  setTimeout(
134
- () => N(e + 1),
130
+ () => T(e + 1),
135
131
  1e3
136
132
  );
137
- }).catch(T);
138
- N();
133
+ }).catch(i);
134
+ T();
139
135
  });
140
136
  }
141
- async subscribe(n, r, _, i = "production") {
142
- const R = i === "sandbox", T = await fetch(
143
- `${O._RC_ENDPOINT}/${O._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`,
144
140
  {
145
141
  method: "POST",
146
142
  headers: {
@@ -151,38 +147,52 @@ const P = "0.0.8", O = class O {
151
147
  body: JSON.stringify({
152
148
  app_user_id: n,
153
149
  product_id: r,
154
- is_sandbox: R,
150
+ is_sandbox: O,
155
151
  email: _
156
152
  })
157
153
  }
158
154
  );
159
- if (T.status === I.BAD_REQUEST)
160
- throw new Y(T.status);
161
- if (T.status === I.TOO_MANY_REQUESTS)
162
- throw new g(T.status);
163
- if (T.status === I.CONFLICT)
164
- throw new w(T.status);
165
- if (T.status === I.INTERNAL_SERVER_ERROR)
166
- throw new B(T.status);
167
- if (T.status === I.OK || T.status === I.CREATED) {
168
- const N = await T.json();
169
- return F(N);
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);
170
166
  }
171
167
  throw new h();
172
168
  }
173
169
  async getPackage(n, r) {
174
- const _ = await this.listOfferings(n), i = [];
175
- _.offerings.forEach(
176
- (T) => i.push(...T.packages)
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];
178
+ }
179
+ logMissingProductIds(n, r) {
180
+ const _ = {};
181
+ r.forEach(
182
+ (O) => _[O.identifier] = O
177
183
  );
178
- const R = i.filter(
179
- (T) => T.identifier === r
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."
180
191
  );
181
- return R.length === 0 ? null : R[0];
182
192
  }
183
193
  };
184
- A(O, "_RC_ENDPOINT", "https://api.revenuecat.com"), A(O, "_BASE_PATH", "rcbilling/v1");
185
- let U = O;
194
+ A(N, "_RC_ENDPOINT", "https://api.revenuecat.com"), A(N, "_BASE_PATH", "rcbilling/v1");
195
+ let U = N;
186
196
  export {
187
197
  U as Purchases
188
198
  };
@@ -1 +1 @@
1
- (function(R,i){typeof exports=="object"&&typeof module<"u"?i(exports):typeof define=="function"&&define.amd?define(["exports"],i):(R=typeof globalThis<"u"?globalThis:R||self,i(R.Purchases={}))})(this,function(R){"use strict";var g=Object.defineProperty;var w=(R,i,c)=>i in R?g(R,i,{enumerable:!0,configurable:!0,writable:!0,value:c}):R[i]=c;var P=(R,i,c)=>(w(R,typeof i!="symbol"?i+"":i,c),c);const i=E=>({amount:E.amount,currency:E.currency}),c=E=>({id:E.identifier,identifier:E.identifier,displayName:E.title,currentPrice:E.current_price?i(E.current_price):null,normalPeriodDuration:E.normal_period_duration}),l=(E,r)=>{const T=r[E.platform_product_identifier];return{id:E.identifier,identifier:E.identifier,rcBillingProduct:T?c(T):null}},L=(E,r)=>({id:E.identifier,identifier:E.identifier,displayName:E.description,packages:E.packages.map(T=>l(T,r))}),o=E=>{var r;return{nextAction:E.next_action,data:{clientSecret:((r=E.data)==null?void 0:r.client_secret)??void 0}}};var A;(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"})(A||(A={}));class f extends Error{constructor(r,T){super(T),this.statusCode=r}}class M extends f{constructor(){super(A.INTERNAL_SERVER_ERROR,"An unknown error occurred.")}}class t extends f{}class h extends f{}class F extends f{}class m extends f{}const D="0.0.8",O=class O{constructor(r){P(this,"_API_KEY",null);P(this,"_APP_USER_ID",null);P(this,"toOfferingsPage",(r,T)=>{const _=r.offerings.filter(n=>n.identifier===r.current_offering_id),N={};T.product_details.forEach(n=>{N[n.identifier]=n});const e={};return _[0].packages.forEach(n=>e[n.identifier]=i(N[n.platform_product_identifier].current_price)),{offerings:_.map(n=>L(n,N)),priceByPackageId:e}});this._API_KEY=r,O._RC_ENDPOINT===void 0&&console.error("Project was build without some of the environment variables set")}async listOfferings(r){const _=await(await fetch(`${O._RC_ENDPOINT}/v1/subscribers/${r}/offerings`,{headers:{Authorization:`Bearer ${this._API_KEY}`,"Content-Type":"application/json",Accept:"application/json","X-Platform":"web","X-Version":D}})).json(),N=_.offerings.flatMap(I=>I.packages).map(I=>I.platform_product_identifier),n=await(await fetch(`${O._RC_ENDPOINT}/${O._BASE_PATH}/subscribers/${r}/products?id=${N.join("&id=")}`,{headers:{Authorization:`Bearer ${this._API_KEY}`,"Content-Type":"application/json",Accept:"application/json","X-Platform":"web","X-Version":D}})).json();return this.toOfferingsPage(_,n)}async isEntitledTo(r,T){const _=await fetch(`${O._RC_ENDPOINT}/${O._BASE_PATH}/entitlements/${r}`,{headers:{Authorization:`Bearer ${this._API_KEY}`,"Content-Type":"application/json",Accept:"application/json"}});return _.status===404?!1:(await _.json()).entitlements.map(I=>I.lookup_key).includes(T)}waitForEntitlement(r,T,_=10){return new Promise((e,n)=>{const I=(p=1)=>this.isEntitledTo(r,T).then(Y=>{if(p>_)return e(!1);if(Y)return e(!0);setTimeout(()=>I(p+1),1e3)}).catch(n);I()})}async subscribe(r,T,_,N="production"){const e=N==="sandbox",n=await fetch(`${O._RC_ENDPOINT}/${O._BASE_PATH}/subscribe`,{method:"POST",headers:{Authorization:`Bearer ${this._API_KEY}`,"Content-Type":"application/json",Accept:"application/json"},body:JSON.stringify({app_user_id:r,product_id:T,is_sandbox:e,email:_})});if(n.status===A.BAD_REQUEST)throw new t(n.status);if(n.status===A.TOO_MANY_REQUESTS)throw new m(n.status);if(n.status===A.CONFLICT)throw new h(n.status);if(n.status===A.INTERNAL_SERVER_ERROR)throw new F(n.status);if(n.status===A.OK||n.status===A.CREATED){const I=await n.json();return o(I)}throw new M}async getPackage(r,T){const _=await this.listOfferings(r),N=[];_.offerings.forEach(n=>N.push(...n.packages));const e=N.filter(n=>n.identifier===T);return e.length===0?null:e[0]}};P(O,"_RC_ENDPOINT","https://api.revenuecat.com"),P(O,"_BASE_PATH","rcbilling/v1");let U=O;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,7 +1,7 @@
1
1
  {
2
2
  "name": "@revenuecat/purchases-js",
3
3
  "private": false,
4
- "version": "0.0.8",
4
+ "version": "0.0.9",
5
5
  "type": "module",
6
6
  "files": [
7
7
  "dist"