@revenuecat/purchases-js 0.0.5 → 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/README.md +21 -2
- package/dist/Purchases.es.d.ts +61 -0
- package/dist/Purchases.es.js +188 -0
- package/dist/Purchases.umd.js +1 -0
- package/package.json +16 -3
- package/.eslintrc.cjs +0 -30
- package/.prettierignore +0 -2
- package/.prettierrc +0 -6
- package/CHANGELOG +0 -23
- package/dist/entities/offerings.d.ts +0 -34
- package/dist/entities/subscribe-response.d.ts +0 -8
- package/dist/entities/types.d.ts +0 -1
- package/dist/main.d.ts +0 -18
- package/dist/purchases-js.js +0 -144
- package/global.d.ts +0 -3
- package/revenuecat-purchases-js-0.0.5.tgz +0 -0
- package/test.html +0 -38
- package/tsconfig.json +0 -23
package/README.md
CHANGED
|
@@ -195,8 +195,27 @@ const PaymentForm = ({ clientSecret }) => {
|
|
|
195
195
|
|
|
196
196
|
### 3. Wait for the entitlement to be granted
|
|
197
197
|
|
|
198
|
-
You can
|
|
199
|
-
|
|
198
|
+
You can use the `.waitForEntitlement` method.
|
|
199
|
+
|
|
200
|
+
```tsx
|
|
201
|
+
const appUserId = "the unique id of the user in your systems";
|
|
202
|
+
const entitlementId = "the entitlementId you set up in RC";
|
|
203
|
+
|
|
204
|
+
const purchases = new Purchases("your RC_PUBLISHABLE_API_KEY");
|
|
205
|
+
const numberOfAttempts = 10;
|
|
206
|
+
|
|
207
|
+
purchases
|
|
208
|
+
.waitForEntitlement(appUserId, entitlementId, numberOfAttempts)
|
|
209
|
+
.then((isEntitled) => {
|
|
210
|
+
if (isEntitled == true) {
|
|
211
|
+
console.log(`User ${appUserID} is entitled to ${entitlementId}`);
|
|
212
|
+
} else {
|
|
213
|
+
console.log(
|
|
214
|
+
`User ${appUserID} is not entitled to ${entitlementId}, even after ${numberOfAttempts} attempts`,
|
|
215
|
+
);
|
|
216
|
+
}
|
|
217
|
+
});
|
|
218
|
+
```
|
|
200
219
|
|
|
201
220
|
# Development
|
|
202
221
|
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
export declare type Offering = Offering_2;
|
|
2
|
+
|
|
3
|
+
declare interface Offering_2 {
|
|
4
|
+
id: string;
|
|
5
|
+
identifier: string;
|
|
6
|
+
displayName: string;
|
|
7
|
+
packages: Package_2[];
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export declare type OfferingsPage = OfferingsPage_2;
|
|
11
|
+
|
|
12
|
+
declare interface OfferingsPage_2 {
|
|
13
|
+
offerings: Offering_2[];
|
|
14
|
+
priceByPackageId: {
|
|
15
|
+
[packageId: string]: number;
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export declare type Package = Package_2;
|
|
20
|
+
|
|
21
|
+
declare interface Package_2 {
|
|
22
|
+
id: string;
|
|
23
|
+
identifier: string;
|
|
24
|
+
rcBillingProduct: Product | null;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
declare interface Price {
|
|
28
|
+
amount: number;
|
|
29
|
+
currency: string;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
declare interface Product {
|
|
33
|
+
id: string;
|
|
34
|
+
displayName: string;
|
|
35
|
+
identifier: string;
|
|
36
|
+
currentPrice: Price | null;
|
|
37
|
+
normalPeriodDuration: string | null;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export declare class Purchases {
|
|
41
|
+
_API_KEY: string | null;
|
|
42
|
+
_APP_USER_ID: string | null;
|
|
43
|
+
private static readonly _RC_ENDPOINT;
|
|
44
|
+
private static readonly _BASE_PATH;
|
|
45
|
+
constructor(apiKey: string);
|
|
46
|
+
private toOfferingsPage;
|
|
47
|
+
listOfferings(appUserId: string): Promise<OfferingsPage>;
|
|
48
|
+
isEntitledTo(appUserId: string, entitlementIdentifier: string): Promise<boolean>;
|
|
49
|
+
waitForEntitlement(appUserId: string, entitlementIdentifier: string, maxAttempts?: number): Promise<boolean>;
|
|
50
|
+
subscribe(appUserId: string, productId: string, email: string, environment?: "sandbox" | "production"): Promise<SubscribeResponse>;
|
|
51
|
+
getPackage(appUserId: string, packageIdentifier: string): Promise<Package | null>;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
declare interface SubscribeResponse {
|
|
55
|
+
nextAction: string;
|
|
56
|
+
data: {
|
|
57
|
+
clientSecret?: string;
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export { }
|
|
@@ -0,0 +1,188 @@
|
|
|
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
|
+
amount: E.amount,
|
|
6
|
+
currency: E.currency
|
|
7
|
+
}), l = (E) => ({
|
|
8
|
+
id: E.identifier,
|
|
9
|
+
identifier: E.identifier,
|
|
10
|
+
displayName: E.title,
|
|
11
|
+
currentPrice: E.current_price ? D(E.current_price) : null,
|
|
12
|
+
normalPeriodDuration: E.normal_period_duration
|
|
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,
|
|
22
|
+
identifier: E.identifier,
|
|
23
|
+
displayName: E.description,
|
|
24
|
+
packages: E.packages.map(
|
|
25
|
+
(r) => M(r, n)
|
|
26
|
+
)
|
|
27
|
+
}), F = (E) => {
|
|
28
|
+
var n;
|
|
29
|
+
return {
|
|
30
|
+
nextAction: E.next_action,
|
|
31
|
+
data: {
|
|
32
|
+
clientSecret: ((n = E.data) == null ? void 0 : n.client_secret) ?? void 0
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
};
|
|
36
|
+
var I;
|
|
37
|
+
(function(E) {
|
|
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";
|
|
39
|
+
})(I || (I = {}));
|
|
40
|
+
class c extends Error {
|
|
41
|
+
constructor(n, r) {
|
|
42
|
+
super(r), this.statusCode = n;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
class h extends c {
|
|
46
|
+
constructor() {
|
|
47
|
+
super(I.INTERNAL_SERVER_ERROR, "An unknown error occurred.");
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
class Y extends c {
|
|
51
|
+
}
|
|
52
|
+
class w extends c {
|
|
53
|
+
}
|
|
54
|
+
class B extends c {
|
|
55
|
+
}
|
|
56
|
+
class g extends c {
|
|
57
|
+
}
|
|
58
|
+
const P = "0.0.8", O = class O {
|
|
59
|
+
constructor(n) {
|
|
60
|
+
A(this, "_API_KEY", null);
|
|
61
|
+
A(this, "_APP_USER_ID", null);
|
|
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(
|
|
82
|
+
"Project was build without some of the environment variables set"
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
async listOfferings(n) {
|
|
86
|
+
const _ = await (await fetch(
|
|
87
|
+
`${O._RC_ENDPOINT}/v1/subscribers/${n}/offerings`,
|
|
88
|
+
{
|
|
89
|
+
headers: {
|
|
90
|
+
Authorization: `Bearer ${this._API_KEY}`,
|
|
91
|
+
"Content-Type": "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
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
)).json();
|
|
109
|
+
return this.toOfferingsPage(_, T);
|
|
110
|
+
}
|
|
111
|
+
async isEntitledTo(n, r) {
|
|
112
|
+
const _ = await fetch(
|
|
113
|
+
`${O._RC_ENDPOINT}/${O._BASE_PATH}/entitlements/${n}`,
|
|
114
|
+
{
|
|
115
|
+
headers: {
|
|
116
|
+
Authorization: `Bearer ${this._API_KEY}`,
|
|
117
|
+
"Content-Type": "application/json",
|
|
118
|
+
Accept: "application/json"
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
);
|
|
122
|
+
return _.status === 404 ? !1 : (await _.json()).entitlements.map(
|
|
123
|
+
(N) => N.lookup_key
|
|
124
|
+
).includes(r);
|
|
125
|
+
}
|
|
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);
|
|
133
|
+
setTimeout(
|
|
134
|
+
() => N(e + 1),
|
|
135
|
+
1e3
|
|
136
|
+
);
|
|
137
|
+
}).catch(T);
|
|
138
|
+
N();
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
async subscribe(n, r, _, i = "production") {
|
|
142
|
+
const R = i === "sandbox", T = await fetch(
|
|
143
|
+
`${O._RC_ENDPOINT}/${O._BASE_PATH}/subscribe`,
|
|
144
|
+
{
|
|
145
|
+
method: "POST",
|
|
146
|
+
headers: {
|
|
147
|
+
Authorization: `Bearer ${this._API_KEY}`,
|
|
148
|
+
"Content-Type": "application/json",
|
|
149
|
+
Accept: "application/json"
|
|
150
|
+
},
|
|
151
|
+
body: JSON.stringify({
|
|
152
|
+
app_user_id: n,
|
|
153
|
+
product_id: r,
|
|
154
|
+
is_sandbox: R,
|
|
155
|
+
email: _
|
|
156
|
+
})
|
|
157
|
+
}
|
|
158
|
+
);
|
|
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);
|
|
170
|
+
}
|
|
171
|
+
throw new h();
|
|
172
|
+
}
|
|
173
|
+
async getPackage(n, r) {
|
|
174
|
+
const _ = await this.listOfferings(n), i = [];
|
|
175
|
+
_.offerings.forEach(
|
|
176
|
+
(T) => i.push(...T.packages)
|
|
177
|
+
);
|
|
178
|
+
const R = i.filter(
|
|
179
|
+
(T) => T.identifier === r
|
|
180
|
+
);
|
|
181
|
+
return R.length === 0 ? null : R[0];
|
|
182
|
+
}
|
|
183
|
+
};
|
|
184
|
+
A(O, "_RC_ENDPOINT", "https://api.revenuecat.com"), A(O, "_BASE_PATH", "rcbilling/v1");
|
|
185
|
+
let U = O;
|
|
186
|
+
export {
|
|
187
|
+
U as Purchases
|
|
188
|
+
};
|
|
@@ -0,0 +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"})});
|
package/package.json
CHANGED
|
@@ -1,10 +1,20 @@
|
|
|
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
|
-
"
|
|
7
|
-
|
|
6
|
+
"files": [
|
|
7
|
+
"dist"
|
|
8
|
+
],
|
|
9
|
+
"main": "./dist/Purchases.umd.js",
|
|
10
|
+
"module": "./dist/Purchases.es.js",
|
|
11
|
+
"types": "./dist/Purchases.es.d.ts",
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"import": "./dist/Purchases.es.js",
|
|
15
|
+
"require": "./dist/Purchases.umd.js"
|
|
16
|
+
}
|
|
17
|
+
},
|
|
8
18
|
"license": "MIT",
|
|
9
19
|
"scripts": {
|
|
10
20
|
"dev": "vite",
|
|
@@ -33,5 +43,8 @@
|
|
|
33
43
|
"vite": "^4.4.5",
|
|
34
44
|
"vite-plugin-dts": "^3.6.3",
|
|
35
45
|
"vitest": "^0.34.6"
|
|
46
|
+
},
|
|
47
|
+
"dependencies": {
|
|
48
|
+
"http-status-codes": "^2.3.0"
|
|
36
49
|
}
|
|
37
50
|
}
|
package/.eslintrc.cjs
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
module.exports = {
|
|
2
|
-
plugins: ["prettier"],
|
|
3
|
-
env: {
|
|
4
|
-
browser: true,
|
|
5
|
-
es2021: true,
|
|
6
|
-
},
|
|
7
|
-
extends: [
|
|
8
|
-
"plugin:@typescript-eslint/recommended",
|
|
9
|
-
"plugin:prettier/recommended",
|
|
10
|
-
],
|
|
11
|
-
overrides: [
|
|
12
|
-
{
|
|
13
|
-
env: {
|
|
14
|
-
node: true,
|
|
15
|
-
},
|
|
16
|
-
files: [".eslintrc.{js,cjs}"],
|
|
17
|
-
parserOptions: {
|
|
18
|
-
sourceType: "script",
|
|
19
|
-
},
|
|
20
|
-
},
|
|
21
|
-
],
|
|
22
|
-
parserOptions: {
|
|
23
|
-
ecmaVersion: "latest",
|
|
24
|
-
sourceType: "module",
|
|
25
|
-
},
|
|
26
|
-
rules: {
|
|
27
|
-
"prettier/prettier": "error",
|
|
28
|
-
},
|
|
29
|
-
ignorePatterns: ["vite.config.js", "vitest.config.js", "dist/"],
|
|
30
|
-
};
|
package/.prettierignore
DELETED
package/.prettierrc
DELETED
package/CHANGELOG
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
# 0.0.3
|
|
2
|
-
|
|
3
|
-
- Add prettier by @francocorreasosa in #13
|
|
4
|
-
- Bug: fix some types by @alfondotnet in #14
|
|
5
|
-
|
|
6
|
-
# 0.0.2
|
|
7
|
-
|
|
8
|
-
- Fix publishing to NPM by @alfondotnet in # 10
|
|
9
|
-
- BIL-40: Add subscribe method by @francocorreasosa in #11
|
|
10
|
-
- Only build ES modules by @alfondotnet in #12
|
|
11
|
-
|
|
12
|
-
# 0.0.1
|
|
13
|
-
|
|
14
|
-
Initial release 🐱🚀
|
|
15
|
-
|
|
16
|
-
- Add Vite by @alfondotnet in #1
|
|
17
|
-
- add README by @alfondotnet in #2
|
|
18
|
-
- Fon/add entrypoint by @alfondotnet in #3
|
|
19
|
-
- Simplify existing methods + MSW by @alfondotnet in #5
|
|
20
|
-
- Added the listOfferings method by @nicfix in #4
|
|
21
|
-
- Migrated to the new price naming by @nicfix in #6
|
|
22
|
-
- BIL-15: isEntitledTo method! by @nicfix in #7
|
|
23
|
-
- Rename / Clean up package prior to publishing to NPM by @alfondotnet in #9
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import { ServerResponse } from "./types";
|
|
2
|
-
export interface Price {
|
|
3
|
-
amount: number;
|
|
4
|
-
currency: string;
|
|
5
|
-
}
|
|
6
|
-
export interface Product {
|
|
7
|
-
id: string;
|
|
8
|
-
displayName: string;
|
|
9
|
-
identifier: string;
|
|
10
|
-
currentPrice: Price | null;
|
|
11
|
-
normalPeriodDuration: string | null;
|
|
12
|
-
}
|
|
13
|
-
export interface Package {
|
|
14
|
-
id: string;
|
|
15
|
-
identifier: string;
|
|
16
|
-
displayName: string;
|
|
17
|
-
rcBillingProduct: Product | null;
|
|
18
|
-
}
|
|
19
|
-
export interface Offering {
|
|
20
|
-
id: string;
|
|
21
|
-
identifier: string;
|
|
22
|
-
displayName: string;
|
|
23
|
-
packages: Package[];
|
|
24
|
-
}
|
|
25
|
-
export interface OfferingsPage {
|
|
26
|
-
offerings: Offering[];
|
|
27
|
-
priceByPackageId: {
|
|
28
|
-
[packageId: string]: number;
|
|
29
|
-
};
|
|
30
|
-
}
|
|
31
|
-
export declare const toPrice: (data: ServerResponse) => Price;
|
|
32
|
-
export declare const toProduct: (data: ServerResponse) => Product;
|
|
33
|
-
export declare const toPackage: (data: ServerResponse) => Package;
|
|
34
|
-
export declare const toOffering: (data: ServerResponse) => Offering;
|
package/dist/entities/types.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export type ServerResponse = any;
|
package/dist/main.d.ts
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { Offering as InnerOffering, OfferingsPage as InnerOfferingsPage, Package as InnerPackage } from "./entities/offerings";
|
|
2
|
-
import { SubscribeResponse } from "./entities/subscribe-response";
|
|
3
|
-
export type OfferingsPage = InnerOfferingsPage;
|
|
4
|
-
export type Offering = InnerOffering;
|
|
5
|
-
export type Package = InnerPackage;
|
|
6
|
-
export declare class Purchases {
|
|
7
|
-
_API_KEY: string | null;
|
|
8
|
-
_APP_USER_ID: string | null;
|
|
9
|
-
private static readonly _RC_ENDPOINT;
|
|
10
|
-
private static readonly _BASE_PATH;
|
|
11
|
-
constructor(apiKey: string);
|
|
12
|
-
private toOfferingsPage;
|
|
13
|
-
listOfferings(): Promise<OfferingsPage>;
|
|
14
|
-
isEntitledTo(appUserId: string, entitlementIdentifier: string): Promise<boolean>;
|
|
15
|
-
waitForEntitlement(appUserId: string, entitlementIdentifier: string, maxAttempts?: number): Promise<boolean>;
|
|
16
|
-
subscribe(appUserId: string, productId: string, environment?: "sandbox" | "production"): Promise<SubscribeResponse>;
|
|
17
|
-
getPackage(packageIdentifier: string): Promise<Package | null>;
|
|
18
|
-
}
|
package/dist/purchases-js.js
DELETED
|
@@ -1,144 +0,0 @@
|
|
|
1
|
-
var d = Object.defineProperty;
|
|
2
|
-
var g = (e, t, n) => t in e ? d(e, t, { enumerable: !0, configurable: !0, writable: !0, value: n }) : e[t] = n;
|
|
3
|
-
var l = (e, t, n) => (g(e, typeof t != "symbol" ? t + "" : t, n), n);
|
|
4
|
-
var _ = (e, t, n) => new Promise((i, c) => {
|
|
5
|
-
var s = (o) => {
|
|
6
|
-
try {
|
|
7
|
-
a(n.next(o));
|
|
8
|
-
} catch (u) {
|
|
9
|
-
c(u);
|
|
10
|
-
}
|
|
11
|
-
}, p = (o) => {
|
|
12
|
-
try {
|
|
13
|
-
a(n.throw(o));
|
|
14
|
-
} catch (u) {
|
|
15
|
-
c(u);
|
|
16
|
-
}
|
|
17
|
-
}, a = (o) => o.done ? i(o.value) : Promise.resolve(o.value).then(s, p);
|
|
18
|
-
a((n = n.apply(e, t)).next());
|
|
19
|
-
});
|
|
20
|
-
const P = (e) => ({
|
|
21
|
-
amount: e.amount,
|
|
22
|
-
currency: e.currency
|
|
23
|
-
}), m = (e) => ({
|
|
24
|
-
id: e.id,
|
|
25
|
-
identifier: e.identifier,
|
|
26
|
-
displayName: e.display_name,
|
|
27
|
-
currentPrice: e.current_price ? P(e.current_price) : null,
|
|
28
|
-
normalPeriodDuration: e.normal_period_duration
|
|
29
|
-
}), A = (e) => ({
|
|
30
|
-
id: e.id,
|
|
31
|
-
identifier: e.identifier,
|
|
32
|
-
displayName: e.display_name,
|
|
33
|
-
rcBillingProduct: e.rc_billing_product ? m(e.rc_billing_product) : null
|
|
34
|
-
}), h = (e) => ({
|
|
35
|
-
id: e.id,
|
|
36
|
-
identifier: e.identifier,
|
|
37
|
-
displayName: e.display_name,
|
|
38
|
-
packages: e.packages.map(A)
|
|
39
|
-
}), y = (e) => {
|
|
40
|
-
var t, n;
|
|
41
|
-
return {
|
|
42
|
-
nextAction: e.next_action,
|
|
43
|
-
data: {
|
|
44
|
-
clientSecret: (n = (t = e.data) == null ? void 0 : t.client_secret) != null ? n : void 0
|
|
45
|
-
}
|
|
46
|
-
};
|
|
47
|
-
}, r = class r {
|
|
48
|
-
constructor(t) {
|
|
49
|
-
l(this, "_API_KEY", null);
|
|
50
|
-
l(this, "_APP_USER_ID", null);
|
|
51
|
-
l(this, "toOfferingsPage", (t) => ({
|
|
52
|
-
offerings: t.offerings.map(h),
|
|
53
|
-
priceByPackageId: t.prices_by_package_id
|
|
54
|
-
}));
|
|
55
|
-
this._API_KEY = t, r._RC_ENDPOINT === void 0 && console.error(
|
|
56
|
-
"Project was build without some of the environment variables set"
|
|
57
|
-
);
|
|
58
|
-
}
|
|
59
|
-
listOfferings() {
|
|
60
|
-
return _(this, null, function* () {
|
|
61
|
-
const n = yield (yield fetch(
|
|
62
|
-
`${r._RC_ENDPOINT}/${r._BASE_PATH}/offerings`,
|
|
63
|
-
{
|
|
64
|
-
headers: {
|
|
65
|
-
Authorization: `Bearer ${this._API_KEY}`,
|
|
66
|
-
"Content-Type": "application/json",
|
|
67
|
-
Accept: "application/json"
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
)).json();
|
|
71
|
-
return this.toOfferingsPage(n);
|
|
72
|
-
});
|
|
73
|
-
}
|
|
74
|
-
isEntitledTo(t, n) {
|
|
75
|
-
return _(this, null, function* () {
|
|
76
|
-
const i = yield fetch(
|
|
77
|
-
`${r._RC_ENDPOINT}/${r._BASE_PATH}/entitlements/${t}`,
|
|
78
|
-
{
|
|
79
|
-
headers: {
|
|
80
|
-
Authorization: `Bearer ${this._API_KEY}`,
|
|
81
|
-
"Content-Type": "application/json",
|
|
82
|
-
Accept: "application/json"
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
);
|
|
86
|
-
return i.status === 404 ? !1 : (yield i.json()).entitlements.map(
|
|
87
|
-
(a) => a.lookup_key
|
|
88
|
-
).includes(n);
|
|
89
|
-
});
|
|
90
|
-
}
|
|
91
|
-
waitForEntitlement(t, n, i = 10) {
|
|
92
|
-
return new Promise((s, p) => {
|
|
93
|
-
const a = (o = 1) => this.isEntitledTo(t, n).then((u) => {
|
|
94
|
-
if (o > i)
|
|
95
|
-
return s(!1);
|
|
96
|
-
if (u)
|
|
97
|
-
return s(!0);
|
|
98
|
-
setTimeout(
|
|
99
|
-
() => a(o + 1),
|
|
100
|
-
1e3
|
|
101
|
-
);
|
|
102
|
-
}).catch(p);
|
|
103
|
-
a();
|
|
104
|
-
});
|
|
105
|
-
}
|
|
106
|
-
subscribe(t, n, i = "production") {
|
|
107
|
-
return _(this, null, function* () {
|
|
108
|
-
const c = i === "sandbox", p = yield (yield fetch(
|
|
109
|
-
`${r._RC_ENDPOINT}/${r._BASE_PATH}/subscribe`,
|
|
110
|
-
{
|
|
111
|
-
method: "POST",
|
|
112
|
-
headers: {
|
|
113
|
-
Authorization: `Bearer ${this._API_KEY}`,
|
|
114
|
-
"Content-Type": "application/json",
|
|
115
|
-
Accept: "application/json"
|
|
116
|
-
},
|
|
117
|
-
body: JSON.stringify({
|
|
118
|
-
app_user_id: t,
|
|
119
|
-
product_id: n,
|
|
120
|
-
is_sandbox: c
|
|
121
|
-
})
|
|
122
|
-
}
|
|
123
|
-
)).json();
|
|
124
|
-
return y(p);
|
|
125
|
-
});
|
|
126
|
-
}
|
|
127
|
-
getPackage(t) {
|
|
128
|
-
return _(this, null, function* () {
|
|
129
|
-
const n = yield this.listOfferings(), i = [];
|
|
130
|
-
n.offerings.forEach(
|
|
131
|
-
(s) => i.push(...s.packages)
|
|
132
|
-
);
|
|
133
|
-
const c = i.filter(
|
|
134
|
-
(s) => s.identifier === t
|
|
135
|
-
);
|
|
136
|
-
return c.length === 0 ? null : c[0];
|
|
137
|
-
});
|
|
138
|
-
}
|
|
139
|
-
};
|
|
140
|
-
l(r, "_RC_ENDPOINT", "https://api.revenuecat.com"), l(r, "_BASE_PATH", "rcbilling/v1");
|
|
141
|
-
let f = r;
|
|
142
|
-
export {
|
|
143
|
-
f as Purchases
|
|
144
|
-
};
|
package/global.d.ts
DELETED
|
Binary file
|
package/test.html
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
<!doctype html>
|
|
2
|
-
<html lang="en">
|
|
3
|
-
<head>
|
|
4
|
-
<meta charset="UTF-8" />
|
|
5
|
-
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
6
|
-
<title>RCBilling Example</title>
|
|
7
|
-
<script src="./dist/rcbilling-js.iife.js"></script>
|
|
8
|
-
<style>
|
|
9
|
-
/* Some basic styling for our Stripe form */
|
|
10
|
-
#payment-form {
|
|
11
|
-
width: 300px;
|
|
12
|
-
margin: 50px auto;
|
|
13
|
-
padding: 20px;
|
|
14
|
-
border: 1px solid #ddd;
|
|
15
|
-
border-radius: 5px;
|
|
16
|
-
}
|
|
17
|
-
</style>
|
|
18
|
-
</head>
|
|
19
|
-
<body>
|
|
20
|
-
<div id="payment-form"></div>
|
|
21
|
-
|
|
22
|
-
<script>
|
|
23
|
-
document.addEventListener("DOMContentLoaded", function () {
|
|
24
|
-
var billing = new RCBilling(
|
|
25
|
-
"web_tFpmIVwEGKNprbMOyiRRzZAbzIHX",
|
|
26
|
-
"test_rcbilling",
|
|
27
|
-
);
|
|
28
|
-
|
|
29
|
-
setTimeout(() => {
|
|
30
|
-
billing.renderForm(
|
|
31
|
-
document.getElementById("payment-form"),
|
|
32
|
-
"monthly.product_1234",
|
|
33
|
-
);
|
|
34
|
-
}, 3000);
|
|
35
|
-
});
|
|
36
|
-
</script>
|
|
37
|
-
</body>
|
|
38
|
-
</html>
|
package/tsconfig.json
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"target": "ES2020",
|
|
4
|
-
"types": ["vite/client"],
|
|
5
|
-
"useDefineForClassFields": true,
|
|
6
|
-
"module": "ESNext",
|
|
7
|
-
"lib": ["ES2020", "DOM", "DOM.Iterable"],
|
|
8
|
-
// /* Bundler mode */
|
|
9
|
-
"moduleResolution": "node",
|
|
10
|
-
"resolveJsonModule": true,
|
|
11
|
-
"isolatedModules": true,
|
|
12
|
-
"noEmit": true,
|
|
13
|
-
|
|
14
|
-
/* Linting */
|
|
15
|
-
"strict": true,
|
|
16
|
-
"noUnusedLocals": true,
|
|
17
|
-
"noUnusedParameters": true,
|
|
18
|
-
"noFallthroughCasesInSwitch": true,
|
|
19
|
-
"baseUrl": "."
|
|
20
|
-
},
|
|
21
|
-
"include": ["./src/**/*.ts", "global.d.ts"],
|
|
22
|
-
"exclude": ["node_modules", "*.test.ts"]
|
|
23
|
-
}
|