UNPKG

@kirz/react-native-toolkit

Version:

Toolkit to speed up React Native development

89 lines 3.56 kB
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); } function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); } import ApphudSdk from '@kirz/react-native-apphud'; import { Platform } from 'react-native'; import { PromiseUtils } from '../../index'; import { Plugin } from '../Plugin'; export class ApphudPlugin extends Plugin { constructor(options) { super(); this.options = options; _defineProperty(this, "name", 'ApphudPlugin'); _defineProperty(this, "features", ['IAPReceiptValidator']); _defineProperty(this, "initializationTimeout", 15000); } async initialize() { await ApphudSdk.start({ ...this.options, observerMode: true }); } async isTrialAvailable(subscriptionId) { try { return await PromiseUtils.timeout(ApphudSdk.checkEligibilitiesForIntroductoryOffer(subscriptionId), 5000); } catch { return false; } } async hasPremiumAccess() { return await ApphudSdk.hasPremiumAccess(); } async hasActiveSubscription() { return await ApphudSdk.hasActiveSubscription(); } async getPurchasedSubscriptions() { const subscriptions = await ApphudSdk.subscriptions(); return subscriptions.map(x => { if (Platform.OS === 'ios') { const subscription = x; return { productId: subscription.productId, expiresAt: new Date(subscription.expiresDate * 1000).toISOString(), startedAt: new Date(subscription.startedAt * 1000).toISOString(), cancelledAt: subscription.canceledAt ? new Date(subscription.canceledAt * 1000).toISOString() : undefined, isAutoRenewEnabled: subscription.isAutorenewEnabled, isActive: subscription.isActive, isSandbox: subscription.isSandbox }; } return { productId: x.productId, expiresAt: new Date(parseInt(x.expiresAt, 10)).toISOString(), // @ts-ignore startedAt: new Date(parseInt(x.statedAt, 10)).toISOString(), cancelledAt: x.cancelledAt, isAutoRenewEnabled: x.isInRetryBilling, isActive: x.isActive, isSandbox: false }; }); } async getActiveSubscription() { const subscriptions = await this.getPurchasedSubscriptions(); return subscriptions.find(x => x.isActive) ?? null; } async getPurchasedProducts() { const purchases = await ApphudSdk.nonRenewingPurchases(); return purchases.map(x => ({ productId: x.productId, purchasedAt: x.purchasedAt, canceledAt: x.canceledAt })); } isProductPurchased(productId) { return ApphudSdk.isNonRenewingPurchaseActive(productId); } async restorePurchases() { if (Platform.OS === 'android') { await ApphudSdk.syncPurchases(); } await ApphudSdk.restorePurchases(); } async handlePurchase() { if (Platform.OS === 'android') { await ApphudSdk.syncPurchases(); } } } //# sourceMappingURL=ApphudPlugin.js.map