{"version":3,"file":"handler.mjs","names":[],"sources":["../../src/app-store/handler.ts"],"sourcesContent":["import type {\n  JWSRenewalInfoDecodedPayload,\n  JWSTransactionDecodedPayload,\n  NotificationTypeV2,\n  ResponseBodyV2DecodedPayload,\n} from '@apple/app-store-server-library';\n\nexport interface AppStoreNotificationEvent {\n  payload: ResponseBodyV2DecodedPayload;\n  transaction: JWSTransactionDecodedPayload | null;\n  renewalInfo: JWSRenewalInfoDecodedPayload | null;\n}\n\ntype NotificationHandlerFn = (event: AppStoreNotificationEvent) => Promise<void>;\n\n/**\n * App Store Server Notification dispatcher keyed by notificationType, the app-store counterpart\n * of StripeEventHandler. The caller verifies and decodes the notification (payload, transaction,\n * renewalInfo) first and passes the decoded event in. Subtype branching stays inside each\n * handler: Apple subtypes share the handler's derived context and are usually a few lines each.\n */\nexport class AppStoreNotificationHandler {\n  private readonly handlers: Map<NotificationTypeV2, NotificationHandlerFn[]>;\n\n  private constructor() {\n    this.handlers = new Map();\n  }\n\n  static create() {\n    return new AppStoreNotificationHandler();\n  }\n\n  on = (type: NotificationTypeV2, handler: NotificationHandlerFn) => {\n    if (!this.handlers.has(type)) {\n      this.handlers.set(type, []);\n    }\n\n    this.handlers.get(type)?.push(handler);\n    return this;\n  };\n\n  process = async (event: AppStoreNotificationEvent): Promise<void> => {\n    const type = event.payload.notificationType as NotificationTypeV2 | undefined;\n    const handlers = type ? this.handlers.get(type) : undefined;\n    if (!handlers) {\n      console.error(`No app store notification handler found for type: ${type}`);\n      throw new Error(`No app store notification handler found for type: ${type}`);\n    }\n    await Promise.all(handlers.map((handler) => handler(event)));\n  };\n}\n"],"mappings":";;;;;;;AAqBA,IAAa,8BAAb,MAAa,4BAA4B;CACvC;CAEA,cAAsB;EACpB,KAAK,2BAAW,IAAI,IAAI;CAC1B;CAEA,OAAO,SAAS;EACd,OAAO,IAAI,4BAA4B;CACzC;CAEA,MAAM,MAA0B,YAAmC;EACjE,IAAI,CAAC,KAAK,SAAS,IAAI,IAAI,GACzB,KAAK,SAAS,IAAI,MAAM,CAAC,CAAC;EAG5B,KAAK,SAAS,IAAI,IAAI,CAAC,EAAE,KAAK,OAAO;EACrC,OAAO;CACT;CAEA,UAAU,OAAO,UAAoD;EACnE,MAAM,OAAO,MAAM,QAAQ;EAC3B,MAAM,WAAW,OAAO,KAAK,SAAS,IAAI,IAAI,IAAI,KAAA;EAClD,IAAI,CAAC,UAAU;GACb,QAAQ,MAAM,qDAAqD,MAAM;GACzE,MAAM,IAAI,MAAM,qDAAqD,MAAM;EAC7E;EACA,MAAM,QAAQ,IAAI,SAAS,KAAK,YAAY,QAAQ,KAAK,CAAC,CAAC;CAC7D;AACF"}