Skip to content

Commit

Permalink
feat: Date parsing library used instead of Date.parse
Browse files Browse the repository at this point in the history
  • Loading branch information
divanc committed Mar 1, 2023
1 parent 1070e41 commit 3272a99
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 72 deletions.
57 changes: 22 additions & 35 deletions lib/src/internal/coders/AdaptyAccessLevel.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import dayjs from 'dayjs';

import { Log } from '../../sdk/logger';
import type {
AdaptyAccessLevel,
Expand Down Expand Up @@ -60,8 +62,8 @@ export class AdaptyAccessLevelCoder extends Coder<Type> {

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"`,
Expand All @@ -74,7 +76,6 @@ export class AdaptyAccessLevelCoder extends Coder<Type> {
current: activatedAtStr,
});
}
const activatedAt = new Date(activatedAtTs);

const activeIntroductoryOfferType = data[
'active_introductory_offer_type'
Expand Down Expand Up @@ -130,12 +131,11 @@ export class AdaptyAccessLevelCoder extends Coder<Type> {
}

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"`,
Expand All @@ -148,8 +148,6 @@ export class AdaptyAccessLevelCoder extends Coder<Type> {
current: billingIssueDetectedAtStr,
});
}

billingIssueDetectedAt = new Date(billingIssueDetectedAtTs);
}

const cancellationReason = data['cancellation_reason'] as
Expand All @@ -172,10 +170,10 @@ export class AdaptyAccessLevelCoder extends Coder<Type> {
}

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"`,
Expand All @@ -188,8 +186,6 @@ export class AdaptyAccessLevelCoder extends Coder<Type> {
current: expiresAtStr,
});
}

expiresAt = new Date(expiresAtTs);
}

const id = data['id'];
Expand Down Expand Up @@ -277,10 +273,9 @@ export class AdaptyAccessLevelCoder extends Coder<Type> {
}

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"`,
Expand All @@ -293,15 +288,12 @@ export class AdaptyAccessLevelCoder extends Coder<Type> {
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"`,
Expand All @@ -314,8 +306,6 @@ export class AdaptyAccessLevelCoder extends Coder<Type> {
current: startsAtStr,
});
}

startsAt = new Date(startsAtTs);
}

const store = data['store'] as VendorStore | undefined;
Expand Down Expand Up @@ -343,12 +333,11 @@ export class AdaptyAccessLevelCoder extends Coder<Type> {
}

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"`,
Expand All @@ -361,8 +350,6 @@ export class AdaptyAccessLevelCoder extends Coder<Type> {
current: unsubscribedAtStr,
});
}

unsubscribedAt = new Date(unsubscribedAtTs);
}

const vendorProductId = data['vendor_product_id'];
Expand Down
7 changes: 4 additions & 3 deletions lib/src/internal/coders/AdaptyNonSubscription.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import dayjs from 'dayjs';

import { Log } from '../../sdk/logger';
import type { AdaptyNonSubscription, VendorStore } from '../../types';

Expand Down Expand Up @@ -84,15 +86,14 @@ export class AdaptyNonSubscriptionCoder extends Coder<Type> {
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') {
Expand Down
54 changes: 21 additions & 33 deletions lib/src/internal/coders/AdaptySubscription.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import dayjs from 'dayjs';

import { Log } from '../../sdk/logger';
import type { AdaptySubscription } from '../../types';

Expand Down Expand Up @@ -49,15 +51,14 @@ export class AdaptySubscriptionCoder extends Coder<Type> {
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'
Expand Down Expand Up @@ -99,20 +100,17 @@ export class AdaptySubscriptionCoder extends Coder<Type> {
}

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[
Expand All @@ -129,18 +127,15 @@ export class AdaptySubscriptionCoder extends Coder<Type> {
}

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'];
Expand Down Expand Up @@ -191,32 +186,27 @@ export class AdaptySubscriptionCoder extends Coder<Type> {
}

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'];
Expand All @@ -232,19 +222,17 @@ export class AdaptySubscriptionCoder extends Coder<Type> {
}

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[
Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-native-adapty",
"version": "2.3.11",
"version": "2.3.12",
"nativePackage": true,
"description": "Adapty SDK",
"license": "MIT",
Expand Down Expand Up @@ -73,5 +73,8 @@
"tslib": "^2.4.0",
"typedoc": "^0.23.23",
"typescript": "4.8.4"
},
"dependencies": {
"dayjs": "^1.11.7"
}
}

0 comments on commit 3272a99

Please sign in to comment.