diff --git a/lib/src/internal/coders/AdaptyAccessLevel.ts b/lib/src/internal/coders/AdaptyAccessLevel.ts index d212609..d33d5d8 100644 --- a/lib/src/internal/coders/AdaptyAccessLevel.ts +++ b/lib/src/internal/coders/AdaptyAccessLevel.ts @@ -1,3 +1,5 @@ +import dayjs from 'dayjs'; + import { Log } from '../../sdk/logger'; import type { AdaptyAccessLevel, @@ -60,8 +62,8 @@ export class AdaptyAccessLevelCoder extends Coder { throw this.errRequired('activated_at'); } - const activatedAtTs = Date.parse(activatedAtStr); - if (isNaN(activatedAtTs)) { + const activatedAt = dayjs(activatedAtStr).toDate(); + if (isNaN(activatedAt.getTime())) { Log.verbose( `${this.prototype.constructor.name}.tryDecode`, `Failed to decode: "activated_at" is not a valid Date"`, @@ -74,7 +76,6 @@ export class AdaptyAccessLevelCoder extends Coder { current: activatedAtStr, }); } - const activatedAt = new Date(activatedAtTs); const activeIntroductoryOfferType = data[ 'active_introductory_offer_type' @@ -130,12 +131,11 @@ export class AdaptyAccessLevelCoder extends Coder { } const billingIssueDetectedAtStr = data['billing_issue_detected_at']; - const billingIssueDetectedAtTs = billingIssueDetectedAtStr - ? Date.parse(billingIssueDetectedAtStr) + const billingIssueDetectedAt = billingIssueDetectedAtStr + ? dayjs(billingIssueDetectedAtStr).toDate() : undefined; - let billingIssueDetectedAt: Date | undefined; - if (billingIssueDetectedAtTs) { - if (isNaN(billingIssueDetectedAtTs)) { + if (billingIssueDetectedAt) { + if (isNaN(billingIssueDetectedAt.getTime())) { Log.verbose( `${this.prototype.constructor.name}.tryDecode`, `Failed to decode: "billing_issue_detected_at" is not a valid Date"`, @@ -148,8 +148,6 @@ export class AdaptyAccessLevelCoder extends Coder { current: billingIssueDetectedAtStr, }); } - - billingIssueDetectedAt = new Date(billingIssueDetectedAtTs); } const cancellationReason = data['cancellation_reason'] as @@ -172,10 +170,10 @@ export class AdaptyAccessLevelCoder extends Coder { } const expiresAtStr = data['expires_at']; - const expiresAtTs = expiresAtStr ? Date.parse(expiresAtStr) : undefined; - let expiresAt: Date | undefined; - if (expiresAtTs) { - if (isNaN(expiresAtTs)) { + + const expiresAt = expiresAtStr ? dayjs(expiresAtStr).toDate() : undefined; + if (expiresAt) { + if (isNaN(expiresAt.getTime())) { Log.verbose( `${this.prototype.constructor.name}.tryDecode`, `Failed to decode: "expires_at" is not a valid Date"`, @@ -188,8 +186,6 @@ export class AdaptyAccessLevelCoder extends Coder { current: expiresAtStr, }); } - - expiresAt = new Date(expiresAtTs); } const id = data['id']; @@ -277,10 +273,9 @@ export class AdaptyAccessLevelCoder extends Coder { } const renewedAtStr = data['renewed_at']; - const renewedAtTs = renewedAtStr ? Date.parse(renewedAtStr) : undefined; - let renewedAt: Date | undefined; - if (renewedAtTs) { - if (isNaN(renewedAtTs)) { + const renewedAt = renewedAtStr ? dayjs(renewedAtStr).toDate() : undefined; + if (renewedAt) { + if (isNaN(renewedAt.getTime())) { Log.verbose( `${this.prototype.constructor.name}.tryDecode`, `Failed to decode: "renewed_at" is not a valid Date"`, @@ -293,15 +288,12 @@ export class AdaptyAccessLevelCoder extends Coder { current: renewedAtStr, }); } - - renewedAt = new Date(renewedAtTs); } const startsAtStr = data['starts_at']; - const startsAtTs = startsAtStr ? Date.parse(startsAtStr) : undefined; - let startsAt: Date | undefined; - if (startsAtTs) { - if (isNaN(startsAtTs)) { + const startsAt = startsAtStr ? dayjs(startsAtStr).toDate() : undefined; + if (startsAt) { + if (isNaN(startsAt.getTime())) { Log.verbose( `${this.prototype.constructor.name}.tryDecode`, `Failed to decode: "starts_at" is not a valid Date"`, @@ -314,8 +306,6 @@ export class AdaptyAccessLevelCoder extends Coder { current: startsAtStr, }); } - - startsAt = new Date(startsAtTs); } const store = data['store'] as VendorStore | undefined; @@ -343,12 +333,11 @@ export class AdaptyAccessLevelCoder extends Coder { } const unsubscribedAtStr = data['unsubscribed_at']; - const unsubscribedAtTs = unsubscribedAtStr - ? Date.parse(unsubscribedAtStr) + const unsubscribedAt = unsubscribedAtStr + ? dayjs(unsubscribedAtStr).toDate() : undefined; - let unsubscribedAt: Date | undefined; - if (unsubscribedAtTs) { - if (isNaN(unsubscribedAtTs)) { + if (unsubscribedAt) { + if (isNaN(unsubscribedAt.getTime())) { Log.verbose( `${this.prototype.constructor.name}.tryDecode`, `Failed to decode: "unsubscribed_at" is not a valid Date"`, @@ -361,8 +350,6 @@ export class AdaptyAccessLevelCoder extends Coder { current: unsubscribedAtStr, }); } - - unsubscribedAt = new Date(unsubscribedAtTs); } const vendorProductId = data['vendor_product_id']; diff --git a/lib/src/internal/coders/AdaptyNonSubscription.ts b/lib/src/internal/coders/AdaptyNonSubscription.ts index 6e98d9e..d131fd0 100644 --- a/lib/src/internal/coders/AdaptyNonSubscription.ts +++ b/lib/src/internal/coders/AdaptyNonSubscription.ts @@ -1,3 +1,5 @@ +import dayjs from 'dayjs'; + import { Log } from '../../sdk/logger'; import type { AdaptyNonSubscription, VendorStore } from '../../types'; @@ -84,15 +86,14 @@ export class AdaptyNonSubscriptionCoder extends Coder { if (!purchasedAtStr) { throw this.errRequired('purchased_at'); } - const purchasedAtTs = Date.parse(purchasedAtStr); - if (isNaN(purchasedAtTs)) { + const purchasedAt = dayjs(purchasedAtStr).toDate(); + if (isNaN(purchasedAt.getTime())) { throw this.errType({ name: 'purchasedAt', expected: 'Date', current: purchasedAtStr, }); } - const purchasedAt = new Date(purchasedAtTs); const store = data['store'] as VendorStore | undefined; if (typeof store !== 'string') { diff --git a/lib/src/internal/coders/AdaptySubscription.ts b/lib/src/internal/coders/AdaptySubscription.ts index f124343..d4b735a 100644 --- a/lib/src/internal/coders/AdaptySubscription.ts +++ b/lib/src/internal/coders/AdaptySubscription.ts @@ -1,3 +1,5 @@ +import dayjs from 'dayjs'; + import { Log } from '../../sdk/logger'; import type { AdaptySubscription } from '../../types'; @@ -49,15 +51,14 @@ export class AdaptySubscriptionCoder extends Coder { if (!activatedAtStr) { throw this.errRequired('activated_at'); } - const activatedAtTs = Date.parse(activatedAtStr); - if (isNaN(activatedAtTs)) { + const activatedAt = dayjs(activatedAtStr).toDate(); + if (isNaN(activatedAt.getTime())) { throw this.errType({ name: 'activatedAt', expected: 'Date', current: activatedAtStr, }); } - const activatedAt = new Date(activatedAtTs); const activeIntroductoryOfferType = data[ 'active_introductory_offer_type' @@ -99,20 +100,17 @@ export class AdaptySubscriptionCoder extends Coder { } const billingIssueDetectedAtStr = data['billing_issue_detected_at']; - const billingIssueDetectedAtTs = billingIssueDetectedAtStr - ? Date.parse(billingIssueDetectedAtStr) + const billingIssueDetectedAt = billingIssueDetectedAtStr + ? dayjs(billingIssueDetectedAtStr).toDate() : undefined; - let billingIssueDetectedAt: Date | undefined; - if (billingIssueDetectedAtTs) { - if (isNaN(billingIssueDetectedAtTs)) { + if (billingIssueDetectedAt) { + if (isNaN(billingIssueDetectedAt.getTime())) { throw this.errType({ name: 'billingIssueDetectedAt', expected: 'Date', current: billingIssueDetectedAtStr, }); } - - billingIssueDetectedAt = new Date(billingIssueDetectedAtTs); } const cancellationReason = data[ @@ -129,18 +127,15 @@ export class AdaptySubscriptionCoder extends Coder { } const expiresAtStr = data['expires_at']; - const expiresAtTs = expiresAtStr ? Date.parse(expiresAtStr) : undefined; - let expiresAt: Date | undefined; - if (expiresAtTs) { - if (isNaN(expiresAtTs)) { + const expiresAt = expiresAtStr ? dayjs(expiresAtStr).toDate() : undefined; + if (expiresAt) { + if (isNaN(expiresAt.getTime())) { throw this.errType({ name: 'expiresAt', expected: 'Date', current: expiresAtStr, }); } - - expiresAt = new Date(expiresAtTs); } const isActive = data['is_active'] as Type['isActive']; @@ -191,32 +186,27 @@ export class AdaptySubscriptionCoder extends Coder { } const renewedAtStr = data['renewed_at']; - const renewedAtTs = renewedAtStr ? Date.parse(renewedAtStr) : undefined; - let renewedAt: Date | undefined; - if (renewedAtTs) { - if (isNaN(renewedAtTs)) { + const renewedAt = renewedAtStr ? dayjs(renewedAtStr).toDate() : undefined; + if (renewedAt) { + if (isNaN(renewedAt.getTime())) { throw this.errType({ name: 'renewedAt', expected: 'Date', current: renewedAtStr, }); } - renewedAt = new Date(renewedAtTs); } const startsAtStr = data['starts_at']; - const startsAtTs = startsAtStr ? Date.parse(startsAtStr) : undefined; - let startsAt: Date | undefined; - if (startsAtTs) { - if (isNaN(startsAtTs)) { + const startsAt = startsAtStr ? dayjs(startsAtStr).toDate() : undefined; + if (startsAt) { + if (isNaN(startsAt.getTime())) { throw this.errType({ name: 'startsAt', expected: 'Date', current: startsAtStr, }); } - - startsAt = new Date(startsAtTs); } const store = data['store'] as Type['store']; @@ -232,19 +222,17 @@ export class AdaptySubscriptionCoder extends Coder { } const unsubscribedAtStr = data['unsubscribed_at']; - const unsubscribedAtTs = unsubscribedAtStr - ? Date.parse(unsubscribedAtStr) + const unsubscribedAt = unsubscribedAtStr + ? dayjs(unsubscribedAtStr).toDate() : undefined; - let unsubscribedAt: Date | undefined; - if (unsubscribedAtTs) { - if (isNaN(unsubscribedAtTs)) { + if (unsubscribedAt) { + if (isNaN(unsubscribedAt.getTime())) { throw this.errType({ name: 'unsubscribedAt', expected: 'Date', current: unsubscribedAtStr, }); } - unsubscribedAt = new Date(unsubscribedAtTs); } const vendorOriginalTransactionId = data[ diff --git a/package.json b/package.json index 6b16c79..0a5d407 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-native-adapty", - "version": "2.3.11", + "version": "2.3.12", "nativePackage": true, "description": "Adapty SDK", "license": "MIT", @@ -73,5 +73,8 @@ "tslib": "^2.4.0", "typedoc": "^0.23.23", "typescript": "4.8.4" + }, + "dependencies": { + "dayjs": "^1.11.7" } } \ No newline at end of file