UNPKG

@adapty/capacitor

Version:
54 lines 2.23 kB
import { SimpleCoder } from './coder'; import { AdaptyProfileCoder } from './adapty-profile'; import { getPlatform } from '../utils/platform'; export class AdaptyPurchaseResultCoder extends SimpleCoder { constructor() { super(...arguments); this.properties = { type: { key: 'type', required: true, type: 'string', }, }; } decode(data) { const baseResult = super.decode(data); if (baseResult.type === 'success') { if (!data.profile) { throw new Error('Profile is required for success type of purchase result'); } const platform = getPlatform(); const anyData = data; return Object.assign(Object.assign(Object.assign(Object.assign({}, baseResult), { profile: new AdaptyProfileCoder().decode(data.profile) }), (platform === 'ios' && anyData.jws_transaction ? { ios: { jwsTransaction: anyData.jws_transaction } } : {})), (platform === 'android' && anyData.google_purchase_token ? { android: { purchaseToken: anyData.google_purchase_token } } : {})); } return baseResult; } encode(data) { var _a, _b; const { type } = data; if (type === 'success') { if (!('profile' in data)) { throw new Error('Profile is required for success type of purchase result'); } const platform = getPlatform(); const result = { type: 'success', profile: new AdaptyProfileCoder().encode(data.profile), }; if (platform === 'ios' && ((_a = data.ios) === null || _a === void 0 ? void 0 : _a.jwsTransaction)) { result.jws_transaction = data.ios.jwsTransaction; } if (platform === 'android' && ((_b = data.android) === null || _b === void 0 ? void 0 : _b.purchaseToken)) { result.google_purchase_token = data.android.purchaseToken; } return result; } return super.encode({ type }); } } //# sourceMappingURL=adapty-purchase-result.js.map