@revenuecat/purchases-js 0.0.6 → 0.0.8
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/Purchases.es.d.ts +2 -3
- package/dist/Purchases.es.js +116 -84
- package/dist/Purchases.umd.js +1 -1
- package/package.json +2 -1
package/dist/Purchases.es.d.ts
CHANGED
|
@@ -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,11 @@ 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>;
|
|
53
52
|
}
|
|
54
53
|
|
|
55
54
|
declare interface SubscribeResponse {
|
package/dist/Purchases.es.js
CHANGED
|
@@ -1,84 +1,116 @@
|
|
|
1
|
-
var
|
|
2
|
-
var L = (E,
|
|
3
|
-
var A = (E,
|
|
4
|
-
const
|
|
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) => ({
|
|
5
5
|
amount: E.amount,
|
|
6
6
|
currency: E.currency
|
|
7
|
-
}),
|
|
8
|
-
id: E.
|
|
7
|
+
}), l = (E) => ({
|
|
8
|
+
id: E.identifier,
|
|
9
9
|
identifier: E.identifier,
|
|
10
|
-
displayName: E.
|
|
11
|
-
currentPrice: E.current_price ?
|
|
10
|
+
displayName: E.title,
|
|
11
|
+
currentPrice: E.current_price ? D(E.current_price) : null,
|
|
12
12
|
normalPeriodDuration: E.normal_period_duration
|
|
13
|
-
}),
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
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
|
+
}, o = (E, n) => ({
|
|
21
|
+
id: E.identifier,
|
|
20
22
|
identifier: E.identifier,
|
|
21
|
-
displayName: E.
|
|
22
|
-
packages: E.packages.map(
|
|
23
|
+
displayName: E.description,
|
|
24
|
+
packages: E.packages.map(
|
|
25
|
+
(r) => M(r, n)
|
|
26
|
+
)
|
|
23
27
|
}), F = (E) => {
|
|
24
|
-
var
|
|
28
|
+
var n;
|
|
25
29
|
return {
|
|
26
30
|
nextAction: E.next_action,
|
|
27
31
|
data: {
|
|
28
|
-
clientSecret: ((
|
|
32
|
+
clientSecret: ((n = E.data) == null ? void 0 : n.client_secret) ?? void 0
|
|
29
33
|
}
|
|
30
34
|
};
|
|
31
35
|
};
|
|
32
|
-
var
|
|
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
|
-
})(
|
|
39
|
+
})(I || (I = {}));
|
|
36
40
|
class c extends Error {
|
|
37
|
-
constructor(
|
|
38
|
-
super(
|
|
41
|
+
constructor(n, r) {
|
|
42
|
+
super(r), this.statusCode = n;
|
|
39
43
|
}
|
|
40
44
|
}
|
|
41
|
-
class
|
|
45
|
+
class h extends c {
|
|
42
46
|
constructor() {
|
|
43
|
-
super(
|
|
47
|
+
super(I.INTERNAL_SERVER_ERROR, "An unknown error occurred.");
|
|
44
48
|
}
|
|
45
49
|
}
|
|
46
|
-
class
|
|
50
|
+
class Y extends c {
|
|
47
51
|
}
|
|
48
|
-
class
|
|
52
|
+
class w extends c {
|
|
49
53
|
}
|
|
50
|
-
class
|
|
54
|
+
class B extends c {
|
|
51
55
|
}
|
|
52
|
-
class
|
|
56
|
+
class g extends c {
|
|
53
57
|
}
|
|
54
|
-
const
|
|
55
|
-
constructor(
|
|
58
|
+
const P = "0.0.8", O = class O {
|
|
59
|
+
constructor(n) {
|
|
56
60
|
A(this, "_API_KEY", null);
|
|
57
61
|
A(this, "_APP_USER_ID", null);
|
|
58
|
-
A(this, "toOfferingsPage", (
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
62
|
+
A(this, "toOfferingsPage", (n, r) => {
|
|
63
|
+
const _ = n.offerings.filter(
|
|
64
|
+
(T) => T.identifier === n.current_offering_id
|
|
65
|
+
), i = {};
|
|
66
|
+
r.product_details.forEach((T) => {
|
|
67
|
+
i[T.identifier] = T;
|
|
68
|
+
});
|
|
69
|
+
const R = {};
|
|
70
|
+
return _[0].packages.forEach(
|
|
71
|
+
(T) => R[T.identifier] = D(
|
|
72
|
+
i[T.platform_product_identifier].current_price
|
|
73
|
+
)
|
|
74
|
+
), {
|
|
75
|
+
offerings: _.map(
|
|
76
|
+
(T) => o(T, i)
|
|
77
|
+
),
|
|
78
|
+
priceByPackageId: R
|
|
79
|
+
};
|
|
80
|
+
});
|
|
81
|
+
this._API_KEY = n, O._RC_ENDPOINT === void 0 && console.error(
|
|
63
82
|
"Project was build without some of the environment variables set"
|
|
64
83
|
);
|
|
65
84
|
}
|
|
66
|
-
async listOfferings() {
|
|
85
|
+
async listOfferings(n) {
|
|
67
86
|
const _ = await (await fetch(
|
|
68
|
-
`${
|
|
87
|
+
`${O._RC_ENDPOINT}/v1/subscribers/${n}/offerings`,
|
|
69
88
|
{
|
|
70
89
|
headers: {
|
|
71
90
|
Authorization: `Bearer ${this._API_KEY}`,
|
|
72
91
|
"Content-Type": "application/json",
|
|
73
|
-
Accept: "application/json"
|
|
92
|
+
Accept: "application/json",
|
|
93
|
+
"X-Platform": "web",
|
|
94
|
+
"X-Version": P
|
|
95
|
+
}
|
|
96
|
+
}
|
|
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=")}`,
|
|
99
|
+
{
|
|
100
|
+
headers: {
|
|
101
|
+
Authorization: `Bearer ${this._API_KEY}`,
|
|
102
|
+
"Content-Type": "application/json",
|
|
103
|
+
Accept: "application/json",
|
|
104
|
+
"X-Platform": "web",
|
|
105
|
+
"X-Version": P
|
|
74
106
|
}
|
|
75
107
|
}
|
|
76
108
|
)).json();
|
|
77
|
-
return this.toOfferingsPage(_);
|
|
109
|
+
return this.toOfferingsPage(_, T);
|
|
78
110
|
}
|
|
79
|
-
async isEntitledTo(
|
|
80
|
-
const
|
|
81
|
-
`${
|
|
111
|
+
async isEntitledTo(n, r) {
|
|
112
|
+
const _ = await fetch(
|
|
113
|
+
`${O._RC_ENDPOINT}/${O._BASE_PATH}/entitlements/${n}`,
|
|
82
114
|
{
|
|
83
115
|
headers: {
|
|
84
116
|
Authorization: `Bearer ${this._API_KEY}`,
|
|
@@ -87,28 +119,28 @@ const r = class r {
|
|
|
87
119
|
}
|
|
88
120
|
}
|
|
89
121
|
);
|
|
90
|
-
return
|
|
91
|
-
(
|
|
92
|
-
).includes(
|
|
122
|
+
return _.status === 404 ? !1 : (await _.json()).entitlements.map(
|
|
123
|
+
(N) => N.lookup_key
|
|
124
|
+
).includes(r);
|
|
93
125
|
}
|
|
94
|
-
waitForEntitlement(
|
|
95
|
-
return new Promise((
|
|
96
|
-
const
|
|
97
|
-
if (
|
|
98
|
-
return
|
|
99
|
-
if (
|
|
100
|
-
return
|
|
126
|
+
waitForEntitlement(n, r, _ = 10) {
|
|
127
|
+
return new Promise((R, T) => {
|
|
128
|
+
const N = (e = 1) => this.isEntitledTo(n, r).then((f) => {
|
|
129
|
+
if (e > _)
|
|
130
|
+
return R(!1);
|
|
131
|
+
if (f)
|
|
132
|
+
return R(!0);
|
|
101
133
|
setTimeout(
|
|
102
|
-
() =>
|
|
134
|
+
() => N(e + 1),
|
|
103
135
|
1e3
|
|
104
136
|
);
|
|
105
|
-
}).catch(
|
|
106
|
-
|
|
137
|
+
}).catch(T);
|
|
138
|
+
N();
|
|
107
139
|
});
|
|
108
140
|
}
|
|
109
|
-
async subscribe(
|
|
110
|
-
const
|
|
111
|
-
`${
|
|
141
|
+
async subscribe(n, r, _, i = "production") {
|
|
142
|
+
const R = i === "sandbox", T = await fetch(
|
|
143
|
+
`${O._RC_ENDPOINT}/${O._BASE_PATH}/subscribe`,
|
|
112
144
|
{
|
|
113
145
|
method: "POST",
|
|
114
146
|
headers: {
|
|
@@ -117,40 +149,40 @@ const r = class r {
|
|
|
117
149
|
Accept: "application/json"
|
|
118
150
|
},
|
|
119
151
|
body: JSON.stringify({
|
|
120
|
-
app_user_id:
|
|
121
|
-
product_id:
|
|
122
|
-
is_sandbox:
|
|
123
|
-
email:
|
|
152
|
+
app_user_id: n,
|
|
153
|
+
product_id: r,
|
|
154
|
+
is_sandbox: R,
|
|
155
|
+
email: _
|
|
124
156
|
})
|
|
125
157
|
}
|
|
126
158
|
);
|
|
127
|
-
if (
|
|
128
|
-
throw new
|
|
129
|
-
if (
|
|
130
|
-
throw new
|
|
131
|
-
if (
|
|
132
|
-
throw new
|
|
133
|
-
if (
|
|
134
|
-
throw new
|
|
135
|
-
if (
|
|
136
|
-
const
|
|
137
|
-
return F(
|
|
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);
|
|
138
170
|
}
|
|
139
|
-
throw new
|
|
171
|
+
throw new h();
|
|
140
172
|
}
|
|
141
|
-
async getPackage(
|
|
142
|
-
const _ = await this.listOfferings(),
|
|
173
|
+
async getPackage(n, r) {
|
|
174
|
+
const _ = await this.listOfferings(n), i = [];
|
|
143
175
|
_.offerings.forEach(
|
|
144
|
-
(
|
|
176
|
+
(T) => i.push(...T.packages)
|
|
145
177
|
);
|
|
146
|
-
const
|
|
147
|
-
(
|
|
178
|
+
const R = i.filter(
|
|
179
|
+
(T) => T.identifier === r
|
|
148
180
|
);
|
|
149
|
-
return
|
|
181
|
+
return R.length === 0 ? null : R[0];
|
|
150
182
|
}
|
|
151
183
|
};
|
|
152
|
-
A(
|
|
153
|
-
let
|
|
184
|
+
A(O, "_RC_ENDPOINT", "https://api.revenuecat.com"), A(O, "_BASE_PATH", "rcbilling/v1");
|
|
185
|
+
let U = O;
|
|
154
186
|
export {
|
|
155
|
-
|
|
187
|
+
U as Purchases
|
|
156
188
|
};
|
package/dist/Purchases.umd.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(
|
|
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"})});
|
package/package.json
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@revenuecat/purchases-js",
|
|
3
3
|
"private": false,
|
|
4
|
-
"version": "0.0.
|
|
4
|
+
"version": "0.0.8",
|
|
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",
|