{"version":3,"file":"handler.cjs","names":[],"sources":["../../src/stripe/handler.ts"],"sourcesContent":["import { type Stripe } from 'stripe';\n\ntype StripeEventObject<T extends Stripe.Event.Type> = Extract<\n  Stripe.Event,\n  { type: T }\n>['data']['object'];\n\ntype EventHandlerFn<T extends Stripe.Event.Type = Stripe.Event.Type> = (\n  object: StripeEventObject<T>\n) => Promise<void>;\n\nexport class StripeEventHandler {\n  // eslint-disable-next-line @typescript-eslint/no-explicit-any\n  private readonly handlers: Map<Stripe.Event.Type, Array<EventHandlerFn<any>>>;\n\n  private constructor() {\n    this.handlers = new Map();\n  }\n\n  static create() {\n    return new StripeEventHandler();\n  }\n\n  on = <T extends Stripe.Event.Type>(type: T, handler: EventHandlerFn<T>) => {\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: Stripe.Event): Promise<void> => {\n    const eventHandlers = this.handlers.get(event.type);\n    if (eventHandlers) {\n      await Promise.all(eventHandlers.map((handler) => handler(event.data.object)));\n    } else {\n      console.error(`No stripe event handler found for event type: ${event.type}`);\n      throw new Error(`No stripe event handler found for event type: ${event.type}`);\n    }\n  };\n}\n"],"mappings":";;AAWA,IAAa,qBAAb,MAAa,mBAAmB;CAE9B;CAEA,cAAsB;EACpB,KAAK,2BAAW,IAAI,IAAI;CAC1B;CAEA,OAAO,SAAS;EACd,OAAO,IAAI,mBAAmB;CAChC;CAEA,MAAmC,MAAS,YAA+B;EACzE,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,UAAuC;EACtD,MAAM,gBAAgB,KAAK,SAAS,IAAI,MAAM,IAAI;EAClD,IAAI,eACF,MAAM,QAAQ,IAAI,cAAc,KAAK,YAAY,QAAQ,MAAM,KAAK,MAAM,CAAC,CAAC;OACvE;GACL,QAAQ,MAAM,iDAAiD,MAAM,MAAM;GAC3E,MAAM,IAAI,MAAM,iDAAiD,MAAM,MAAM;EAC/E;CACF;AACF"}