@revenuecat/purchases-js 0.0.14 → 0.0.16

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 CHANGED
@@ -55,10 +55,12 @@ By downloading the current Offerings you can easily build a Paywall page using t
55
55
  associated `rcBillingProduct` and price.
56
56
 
57
57
  ```typescript
58
- const appUserId = "the unique id of the user in your systems";
59
- const purchases = new Purchases("your RC_PUBLISHABLE_API_KEY");
58
+ const purchases = Purchases.configure(
59
+ "your RC_PUBLISHABLE_API_KEY",
60
+ "the unique id of the user in your systems",
61
+ );
60
62
 
61
- purchases.getOfferings(appUserId).then((offerings) => {
63
+ purchases.getOfferings().then((offerings) => {
62
64
  // Get current offering
63
65
  console.log(offerings.current);
64
66
  // Or a dictionary of all offerings
@@ -77,12 +79,15 @@ You can check the entitlements granted to your users throughout all the platform
77
79
  also on your website!
78
80
 
79
81
  ```typescript
80
- const appUserId = "the unique id of the user in your systems";
81
82
  const entitlementId = "the entitlementId you set up in RC";
82
83
 
83
- const purchases = new Purchases("your RC_PUBLISHABLE_API_KEY");
84
+ const purchases = Purchases.configure(
85
+ "your RC_PUBLISHABLE_API_KEY",
86
+ "the unique id of the user in your systems",
87
+ );
88
+ const appUserID = purchases.getAppUserId();
84
89
 
85
- purchases.isEntitledTo(appUserId, entitlementId).then((isEntitled) => {
90
+ purchases.isEntitledTo(entitlementId).then((isEntitled) => {
86
91
  if (isEntitled == true) {
87
92
  console.log(`User ${appUserID} is entitled to ${entitlementId}`);
88
93
  } else {
@@ -94,15 +99,21 @@ purchases.isEntitledTo(appUserId, entitlementId).then((isEntitled) => {
94
99
  As example, you can build a cool React component with it:
95
100
 
96
101
  ```tsx
97
- const WithEntitlement = ({ appUserId, entitlementId, children }) => {
102
+ Purchases.configure(
103
+ "your RC_PUBLISHABLE_API_KEY",
104
+ "the unique id of the user in your systems",
105
+ );
106
+
107
+ const WithEntitlement = ({ entitlementId, children }) => {
98
108
  const [isEntitled, setIsEntitled] = useState<boolean | null>(null);
99
109
 
100
110
  useEffect(() => {
101
- const purchases = new Purchases("your RC_PUBLISHABLE_API_KEY");
102
- purchases.isEntitledTo(appUserId, entitlementId).then((isEntitled) => {
103
- setIsEntitled(isEntitled);
104
- });
105
- }, [appUserId, entitlementId]);
111
+ Purchases.getSharedInstance()
112
+ .isEntitledTo(entitlementId)
113
+ .then((isEntitled) => {
114
+ setIsEntitled(isEntitled);
115
+ });
116
+ }, [entitlementId]);
106
117
 
107
118
  if (isEntitled === null) {
108
119
  return <>"loading..."</>;
@@ -121,7 +132,7 @@ And then use it in your app:
121
132
  ```tsx
122
133
  const App = () => (
123
134
  <>
124
- <WithEntitlement appUserId={"user12345"} entitlementId={"functionality5"}>
135
+ <WithEntitlement entitlementId={"functionality5"}>
125
136
  <Functionality5 />
126
137
  </WithEntitlement>
127
138
  </>
@@ -131,7 +142,7 @@ const App = () => (
131
142
  If you need further information about the user's entitlements, you can use the `getCustomerInfo` method:
132
143
 
133
144
  ```ts
134
- const customerInfo = await purchases.getCustomerInfo(appUserId);
145
+ const customerInfo = await purchases.getCustomerInfo();
135
146
  ```
136
147
 
137
148
  ### Important note
@@ -149,15 +160,17 @@ In this example we will show Stripe, more will be supported soon!
149
160
  You built your paywall, and your user just clicked on the offer they want to subscribe to.
150
161
 
151
162
  ```tsx
152
- const purchases = new Purchases("your RC_PUBLISHABLE_API_KEY");
163
+ const purchases = Purchases.configure(
164
+ "your RC_PUBLISHABLE_API_KEY",
165
+ "the unique id of the user in your systems",
166
+ );
153
167
  // You can retrieve the package from the offerings through `getOfferings`:
154
168
  const rcBillingPackage = offerings.current.availablePackages[0];
155
- const appUserId =
156
- "the unique id of the user that wants to subscribe to your product";
169
+ const appUserId = purchases.getAppUserId();
157
170
  const entitlementIdToCheck =
158
171
  "the entitlementId you set up in RC for your product"; // TODO: remove once this is not needed
159
172
 
160
- purchase.purchasePackage(appUserId, rcBillingPackage).then((response) => {
173
+ purchase.purchasePackage(rcBillingPackage).then((response) => {
161
174
  const isEntitled =
162
175
  entitlementIdToCheck in response.customerInfo.entitlements.active;
163
176
  if (isEntitled == true) {
@@ -368,92 +368,123 @@ export declare interface Product {
368
368
  */
369
369
  export declare class Purchases {
370
370
  /**
371
- * Constructor for Purchases.
372
- * @param apiKey - RevenueCat API Key. Can be obtained from the RevenueCat dashboard.
373
- */
374
- constructor(apiKey: string);
375
- /**
376
- * Fetch the configured offerings for this user. You can configure these
377
- * in the RevenueCat dashboard.
378
- * @param appUserId - Your app's user id in your system.
379
- */
380
- getOfferings(appUserId: string): Promise<Offerings>;
381
- /**
382
- * Convenience method to check whether a user is entitled to a specific
383
- * entitlement. This will use {@link Purchases.getCustomerInfo} under the hood.
384
- * @param appUserId - Your app's user id in your system.
385
- * @param entitlementIdentifier - The entitlement identifier you want to check.
386
- * @returns Whether the user is entitled to the specified entitlement
387
- * @throws {@link PurchasesError} if there is an error while fetching the customer info.
388
- * @see {@link Purchases.getCustomerInfo}
371
+ * Get the singleton instance of Purchases. It's preferred to use the instance
372
+ * obtained from the {@link Purchases.configure} method when possible.
373
+ * @throws {@link UninitializedPurchasesError} if the instance has not been initialized yet.
389
374
  */
390
- isEntitledTo(appUserId: string, entitlementIdentifier: string): Promise<boolean>;
375
+ static getSharedInstance(): Purchases;
376
+ /**
377
+ * Returns whether the Purchases SDK is configured or not.
378
+ */
379
+ static isConfigured(): boolean;
380
+ /**
381
+ * Configures the Purchases SDK. This should be called as soon as your app
382
+ * has a unique user id for your user. You should only call this once, and
383
+ * keep the returned instance around for use throughout your application.
384
+ * @param apiKey - RevenueCat API Key. Can be obtained from the RevenueCat dashboard.
385
+ * @param appUserId - Your unique id for identifying the user.
386
+ */
387
+ static configure(apiKey: string, appUserId: string): Purchases;
391
388
  /**
392
- * Method to perform a purchase for a given package. You can obtain the
393
- * package from {@link Purchases.getOfferings}. This method will present the purchase
394
- * form on your site, using the given HTML element as the mount point, if
395
- * provided, or as a modal if not.
396
- * @param appUserId - Your app's user id in your system.
397
- * @param rcPackage - The package you want to purchase. Obtained from {@link Purchases.getOfferings}.
398
- * @param customerEmail - The email of the user. If null, RevenueCat will ask the customer for their email.
399
- * @param htmlTarget - The HTML element where the billing view should be added. If null, a new div will be created at the root of the page and appended to the body.
400
- * @returns The customer info after the purchase is completed successfuly.
401
- * @throws {@link PurchasesError} if there is an error while performing the purchase. If the {@link PurchasesError.errorCode} is {@link ErrorCode.UserCancelledError}, the user cancelled the purchase.
389
+ * Fetch the configured offerings for this user. You can configure these
390
+ * in the RevenueCat dashboard.
391
+ */
392
+ getOfferings(): Promise<Offerings>;
393
+ /**
394
+ * Convenience method to check whether a user is entitled to a specific
395
+ * entitlement. This will use {@link Purchases.getCustomerInfo} under the hood.
396
+ * @param entitlementIdentifier - The entitlement identifier you want to check.
397
+ * @returns Whether the user is entitled to the specified entitlement
398
+ * @throws {@link PurchasesError} if there is an error while fetching the customer info.
399
+ * @see {@link Purchases.getCustomerInfo}
402
400
  */
403
- purchasePackage(appUserId: string, rcPackage: Package, customerEmail?: string, htmlTarget?: HTMLElement): Promise<{
404
- customerInfo: CustomerInfo;
405
- }>;
401
+ isEntitledTo(entitlementIdentifier: string): Promise<boolean>;
406
402
  /**
407
- * Gets latest available {@link CustomerInfo}.
408
- * @param appUserId - Your app's user id in your system.
409
- * @returns The latest {@link CustomerInfo}.
410
- * @throws {@link PurchasesError} if there is an error while fetching the customer info.
403
+ * Method to perform a purchase for a given package. You can obtain the
404
+ * package from {@link Purchases.getOfferings}. This method will present the purchase
405
+ * form on your site, using the given HTML element as the mount point, if
406
+ * provided, or as a modal if not.
407
+ * @param rcPackage - The package you want to purchase. Obtained from {@link Purchases.getOfferings}.
408
+ * @param customerEmail - The email of the user. If null, RevenueCat will ask the customer for their email.
409
+ * @param htmlTarget - The HTML element where the billing view should be added. If null, a new div will be created at the root of the page and appended to the body.
410
+ * @returns The customer info after the purchase is completed successfuly.
411
+ * @throws {@link PurchasesError} if there is an error while performing the purchase. If the {@link PurchasesError.errorCode} is {@link ErrorCode.UserCancelledError}, the user cancelled the purchase.
411
412
  */
412
- getCustomerInfo(appUserId: string): Promise<CustomerInfo>;
413
+ purchasePackage(rcPackage: Package, customerEmail?: string, htmlTarget?: HTMLElement): Promise<{
414
+ customerInfo: CustomerInfo;
415
+ }>;
416
+ /**
417
+ * Gets latest available {@link CustomerInfo}.
418
+ * @returns The latest {@link CustomerInfo}.
419
+ * @throws {@link PurchasesError} if there is an error while fetching the customer info.
420
+ */
421
+ getCustomerInfo(): Promise<CustomerInfo>;
422
+ /**
423
+ * Gets the current app user id.
424
+ */
425
+ getAppUserId(): string;
426
+ /**
427
+ * Change the current app user id. Returns the customer info for the new
428
+ * user id.
429
+ * @param newAppUserId - The user id to change to.
430
+ */
431
+ changeUser(newAppUserId: string): Promise<CustomerInfo>;
432
+ /**
433
+ * @returns Whether the SDK is using a sandbox API Key.
434
+ */
435
+ isSandbox(): boolean;
436
+ /**
437
+ * Closes the Purchases instance. You should never have to do this normally.
438
+ */
439
+ close(): void;
440
+ }
441
+
413
442
  /**
414
- * @returns Whether the SDK is using a sandbox API Key.
443
+ * Error class for Purchases SDK. You should handle these errors and react
444
+ * accordingly in your app.
445
+ * @public
415
446
  */
416
- isSandbox(): boolean;
417
- }
447
+ export declare class PurchasesError extends Error {
448
+ /**
449
+ * Error code for the error. This is useful to appropriately react to
450
+ * different error situations.
451
+ */
452
+ readonly errorCode: ErrorCode;
453
+ /**
454
+ * Underlying error message. This provides more details on the error and
455
+ * can be useful for debugging and logging.
456
+ */
457
+ readonly underlyingErrorMessage?: string | null | undefined;
458
+ constructor(
459
+ /**
460
+ * Error code for the error. This is useful to appropriately react to
461
+ * different error situations.
462
+ */
463
+ errorCode: ErrorCode,
464
+ /**
465
+ * Message for the error. This is useful for debugging and logging.
466
+ */
467
+ message?: string,
468
+ /**
469
+ * Underlying error message. This provides more details on the error and
470
+ * can be useful for debugging and logging.
471
+ */
472
+ underlyingErrorMessage?: string | null | undefined);
473
+ toString: () => string;
474
+ }
418
475
 
419
- /**
420
- * Error class for Purchases SDK. You should handle these errors and react
421
- * accordingly in your app.
422
- * @public
423
- */
424
- export declare class PurchasesError extends Error {
425
- /**
426
- * Error code for the error. This is useful to appropriately react to
427
- * different error situations.
428
- */
429
- readonly errorCode: ErrorCode;
430
- /**
431
- * Underlying error message. This provides more details on the error and
432
- * can be useful for debugging and logging.
433
- */
434
- readonly underlyingErrorMessage?: string | null | undefined;
435
- constructor(
436
- /**
437
- * Error code for the error. This is useful to appropriately react to
438
- * different error situations.
439
- */
440
- errorCode: ErrorCode,
441
- /**
442
- * Message for the error. This is useful for debugging and logging.
443
- */
444
- message?: string,
445
- /**
446
- * Underlying error message. This provides more details on the error and
447
- * can be useful for debugging and logging.
448
- */
449
- underlyingErrorMessage?: string | null | undefined);
450
- toString: () => string;
451
- }
476
+ /**
477
+ * The store where the user originally subscribed.
478
+ * @public
479
+ */
480
+ export declare type Store = "app_store" | "mac_app_store" | "play_store" | "amazon" | "stripe" | "rc_billing" | "promotional" | "unknown";
452
481
 
453
- /**
454
- * The store where the user originally subscribed.
455
- * @public
456
- */
457
- export declare type Store = "app_store" | "mac_app_store" | "play_store" | "amazon" | "stripe" | "rc_billing" | "promotional" | "unknown";
482
+ /**
483
+ * Error indicating that the SDK was accessed before it was initialized.
484
+ * @public
485
+ */
486
+ export declare class UninitializedPurchasesError extends Error {
487
+ constructor();
488
+ }
458
489
 
459
- export { }
490
+ export { }