UNPKG

@kirz/react-native-toolkit

Version:

Toolkit to speed up React Native development

90 lines (89 loc) 4.6 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 { adapty } from 'react-native-adapty'; import { Plugin } from '../Plugin'; export class AdaptyPlugin extends Plugin { get remoteValues() { return this._remoteConfig; } constructor(options) { super(); this.options = options; _defineProperty(this, "name", 'AdaptyPlugin'); _defineProperty(this, "features", ['IAPReceiptValidator', 'RemoteConfig']); _defineProperty(this, "initializationTimeout", 15000); _defineProperty(this, "premiumAccessLevelKey", void 0); _defineProperty(this, "paywall", void 0); _defineProperty(this, "products", void 0); _defineProperty(this, "_remoteConfig", void 0); this._remoteConfig = options.remoteConfig ?? {}; this.premiumAccessLevelKey = (options === null || options === void 0 ? void 0 : options.premiumAccessLevelKey) ?? 'premium'; } async initialize() { await adapty.activate(this.options.apiKey, this.options); this.paywall = await adapty.getPaywall(this.options.paywallId ?? 'placement-1'); this.products = await adapty.getPaywallProducts(this.paywall); // @ts-ignore this._remoteConfig = this.paywall.remoteConfig; } async isTrialAvailable(subscriptionId) { var _this$products; const product = (_this$products = this.products) === null || _this$products === void 0 ? void 0 : _this$products.find(x => x.vendorProductId === subscriptionId); if (!product) { throw new Error(`Product ${subscriptionId} not found`); } const { [subscriptionId]: result } = await adapty.getProductsIntroductoryOfferEligibility([product]); return result === 'eligible'; } async hasPremiumAccess() { var _profile$accessLevels; const profile = await adapty.getProfile(); const premiumAccessLevel = (_profile$accessLevels = profile.accessLevels) === null || _profile$accessLevels === void 0 ? void 0 : _profile$accessLevels[this.premiumAccessLevelKey]; return !!(premiumAccessLevel !== null && premiumAccessLevel !== void 0 && premiumAccessLevel.isActive); } async hasActiveSubscription() { return this.hasPremiumAccess(); } async getPurchasedSubscriptions() { const profile = await adapty.getProfile(); return Object.values(profile.subscriptions ?? {}).map(value => { var _value$expiresAt, _value$startsAt, _value$unsubscribedAt; return { productId: value.vendorProductId, expiresAt: (_value$expiresAt = value.expiresAt) === null || _value$expiresAt === void 0 ? void 0 : _value$expiresAt.toISOString(), isActive: value.isActive, isAutoRenewEnabled: value.willRenew, isSandbox: value.isSandbox, startedAt: (_value$startsAt = value.startsAt) === null || _value$startsAt === void 0 ? void 0 : _value$startsAt.toISOString(), cancelledAt: (_value$unsubscribedAt = value.unsubscribedAt) === null || _value$unsubscribedAt === void 0 ? void 0 : _value$unsubscribedAt.toISOString() }; }); } async getActiveSubscription() { const subscriptions = await this.getPurchasedSubscriptions(); return subscriptions.find(x => x.isActive) ?? null; } async getPurchasedProducts() { const profile = await adapty.getProfile(); return Object.values(profile.nonSubscriptions ?? {}).flatMap(x => x).map(x => ({ productId: x.vendorProductId, purchasedAt: x.purchasedAt.toISOString(), canceledAt: x.isRefund ? x.purchasedAt.toISOString() : undefined })); } async isProductPurchased(productId) { var _profile$nonSubscript; const profile = await adapty.getProfile(); return !!((_profile$nonSubscript = profile.nonSubscriptions) !== null && _profile$nonSubscript !== void 0 && _profile$nonSubscript[productId]); } async restorePurchases() { await adapty.restorePurchases(); } async handlePurchase() { // no-op } } //# sourceMappingURL=AdaptyPlugin.js.map