agisboye / app-store-server-api

A Node.js client for the App Store Server API
MIT License
210 stars 32 forks source link

Make "data" and "summary" mutually exclusive in "DecodedNotificationPayload" type #30

Closed fariassdev closed 1 year ago

fariassdev commented 1 year ago

This pull request addresses issue #24.

Problem

During testing, we encountered an issue where the DecodedNotificationPayload type cannot be used effectively to generate notification mocks for testing purposes. This limitation hinders our ability to create accurate test scenarios for handling Apple notifications. Screenshot 2023-08-01 at 11 45 32

To solve that, we have to do some typing trick like this:

export const appleTestSubscribedNotification: DecodedNotificationPayload = {
  data: {
    status: 1,
    bundleId: 'com.example.example',
    appAppleId: '9999999999',
    environment: Environment.Sandbox,
    bundleVersion: 594,
    signedRenewalInfo: 'signedRenewalInfo...',
    signedTransactionInfo: 'signedTransactionInfo...',
  },
  summary: {} as NotificationSummary, // <- This feels a bit hacky
  subtype: NotificationSubtype.Resubscribe,
  version: '2.0',
  signedDate: 1690819907859,
  notificationType: NotificationType.Subscribed,
  notificationUUID: '3e6e95be-b09d-4afb-ac10-e4d2c594a9e3',
};

Proposed Solution

I propose a solution to improve the DecodedNotificationPayload type, enabling seamless utilization of the new summary field and facilitating the convenient generation of notification mocks for testing purposes.

Changes Made

  1. Updated Typed Payload: I have improved the DecodedNotificationPayload type to better represent the Apple notification data payload according to the official documentation.

  2. Type Guard Function: I have introduced a type guard function called isDecodedNotificationDataPayload. This function can be used to mitigate potential breaking changes and allow users to safely access the data (isDecodedNotificationDataPayload) and summary (!isDecodedNotificationDataPayload) properties.

Additional Notes

I understand that there may be concerns about the placement of the type guard function in Models.ts. If you believe that a different file would be more suitable, please feel free to move it accordingly or revert the commit if it is deemed unnecessary.

Thank you for your time, we find the library very useful ❤️. Looking forward to your review and feedback on this pull request.

agisboye commented 1 year ago

Hi Fernando,

Thanks for a great PR!

Apple's decision to effectively make data optional is inherently a breaking change, so I think it's reasonable to allow breaking changes to this library as well. The solution you've come up with looks good to me.

Could you add another type guard to check if the payload is a summary notification (i.e. isDecodedNotificationSummaryPayload) and format the files (there's a prettier config in package.json)?

fariassdev commented 1 year ago

Hey August!

Thanks for the great feedback! I've made the changes as you suggested.

I totally agree that Apple's decision to make data optional is a breaking change. So, updating the types to match the state of the Apple API is definitely the way to go.

Take a look at the PR whenever you get a chance. If everything looks good to you, feel free to merge it.

Thanks again for your input!

agisboye commented 1 year ago

Released in v0.10.0. Thank you for your contribution! 🎊