better-ship 0.43.0 → 0.44.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,10 +4,12 @@ type EventObject<K extends Stripe.Event['type']> = Extract<Stripe.Event, {
4
4
  type: K;
5
5
  }>['data']['object'];
6
6
  /**
7
- * Convert verified Stripe events with callbacks typed to each event's object.
8
- * Unmapped events return undefined. Applications own validation and payment policy.
7
+ * Convert one verified Stripe event with callbacks typed to that event's object.
8
+ * An entry is one callback or an array of callbacks. Results stay in entry order.
9
+ * A callback returns `undefined` to ignore the event; the result omits it.
10
+ * An unmapped event returns `[]`. Applications own validation and payment policy.
9
11
  */
10
- export declare function stripeEventMap<T>(map: { [K in Stripe.Event['type']]?: (object: EventObject<K>) => T; }): (event: Stripe.Event) => T | undefined;
12
+ export declare function stripeEventMap<T>(map: { [K in Stripe.Event['type']]?: ((object: EventObject<K>) => T | undefined) | readonly ((object: EventObject<K>) => T | undefined)[]; }): (event: Stripe.Event) => T[];
11
13
  /** Identify one Stripe event delivery; different events for one payment retain different IDs. */
12
14
  export declare function stripeEventId(event: Stripe.Event): string;
13
15
  //#endregion
@@ -1 +1 @@
1
- {"version":3,"file":"event-map.d.ts","names":[],"sources":["../../src/infrastructure/stripe/event-map.ts"],"mappings":";;KAEK,YAAY,UAAU,OAAO,iBAAiB,QACjD,OAAO;EACL,MAAM;;;;;;wBAOM,eAAe,GAAG,QAC/B,KAAK,OAAO,kBAAkB,QAAQ,YAAY,OAAO,QACvD,OAAO,OAAO,UAAU;;wBASb,cAAc,OAAO,OAAO"}
1
+ {"version":3,"file":"event-map.d.ts","names":[],"sources":["../../src/infrastructure/stripe/event-map.ts"],"mappings":";;KAEK,YAAY,UAAU,OAAO,iBAAiB,QACjD,OAAO;EACL,MAAM;;;;;;;;wBAWM,eAAe,GAAG,QAC/B,KAAK,OAAO,mBACP,QAAQ,YAAY,OAAO,4BAClB,QAAQ,YAAY,OAAO,uBACvC,OAAO,OAAO,UAAU;;wBAwBb,cAAc,OAAO,OAAO"}
@@ -1,14 +1,24 @@
1
1
  //#region src/infrastructure/stripe/event-map.ts
2
2
  /**
3
- * Convert verified Stripe events with callbacks typed to each event's object.
4
- * Unmapped events return undefined. Applications own validation and payment policy.
3
+ * Convert one verified Stripe event with callbacks typed to that event's object.
4
+ * An entry is one callback or an array of callbacks. Results stay in entry order.
5
+ * A callback returns `undefined` to ignore the event; the result omits it.
6
+ * An unmapped event returns `[]`. Applications own validation and payment policy.
5
7
  */
6
8
  function stripeEventMap(map) {
7
9
  const handlers = map;
8
10
  return (event) => {
9
- return handlers[event.type]?.(event.data.object);
11
+ const entry = handlers[event.type];
12
+ if (entry === void 0) return [];
13
+ return (isCallbackList(entry) ? entry : [entry]).flatMap((callback) => {
14
+ const result = callback(event.data.object);
15
+ return result === void 0 ? [] : [result];
16
+ });
10
17
  };
11
18
  }
19
+ function isCallbackList(entry) {
20
+ return Array.isArray(entry);
21
+ }
12
22
  /** Identify one Stripe event delivery; different events for one payment retain different IDs. */
13
23
  function stripeEventId(event) {
14
24
  return `stripe:event:${event.id}`;
@@ -1 +1 @@
1
- {"version":3,"file":"event-map.js","names":[],"sources":["../../src/infrastructure/stripe/event-map.ts"],"sourcesContent":["import type Stripe from 'stripe'\n\ntype EventObject<K extends Stripe.Event['type']> = Extract<\n Stripe.Event,\n { type: K }\n>['data']['object']\n\n/**\n * Convert verified Stripe events with callbacks typed to each event's object.\n * Unmapped events return undefined. Applications own validation and payment policy.\n */\nexport function stripeEventMap<T>(map: {\n [K in Stripe.Event['type']]?: (object: EventObject<K>) => T\n}): (event: Stripe.Event) => T | undefined {\n const handlers: Partial<Record<Stripe.Event['type'], (object: never) => T>> = map\n return (event) => {\n // SAFETY: The map binds each event type to its object; indexed lookup loses that correlation.\n return handlers[event.type]?.(event.data.object as never)\n }\n}\n\n/** Identify one Stripe event delivery; different events for one payment retain different IDs. */\nexport function stripeEventId(event: Stripe.Event): string {\n return `stripe:event:${event.id}`\n}\n"],"mappings":";;;;;AAWA,SAAgB,eAAkB,KAES;CACzC,MAAM,WAAwE;CAC9E,QAAQ,UAAU;EAEhB,OAAO,SAAS,MAAM,KAAK,GAAG,MAAM,KAAK,MAAe;CAC1D;AACF;;AAGA,SAAgB,cAAc,OAA6B;CACzD,OAAO,gBAAgB,MAAM;AAC/B"}
1
+ {"version":3,"file":"event-map.js","names":[],"sources":["../../src/infrastructure/stripe/event-map.ts"],"sourcesContent":["import type Stripe from 'stripe'\n\ntype EventObject<K extends Stripe.Event['type']> = Extract<\n Stripe.Event,\n { type: K }\n>['data']['object']\n\ntype EventCallback<T> = (object: never) => T | undefined\n\n/**\n * Convert one verified Stripe event with callbacks typed to that event's object.\n * An entry is one callback or an array of callbacks. Results stay in entry order.\n * A callback returns `undefined` to ignore the event; the result omits it.\n * An unmapped event returns `[]`. Applications own validation and payment policy.\n */\nexport function stripeEventMap<T>(map: {\n [K in Stripe.Event['type']]?:\n | ((object: EventObject<K>) => T | undefined)\n | readonly ((object: EventObject<K>) => T | undefined)[]\n}): (event: Stripe.Event) => T[] {\n const handlers: Partial<\n Record<Stripe.Event['type'], EventCallback<T> | readonly EventCallback<T>[]>\n > = map\n return (event) => {\n const entry = handlers[event.type]\n if (entry === undefined) return []\n const callbacks = isCallbackList(entry) ? entry : [entry]\n return callbacks.flatMap((callback) => {\n // SAFETY: The map binds each event type to its object; indexed lookup loses that correlation.\n const result = callback(event.data.object as never)\n return result === undefined ? [] : [result]\n })\n }\n}\n\n// Array.isArray widens to any[], so the predicate restores the callback list type.\nfunction isCallbackList<T>(\n entry: EventCallback<T> | readonly EventCallback<T>[],\n): entry is readonly EventCallback<T>[] {\n return Array.isArray(entry)\n}\n\n/** Identify one Stripe event delivery; different events for one payment retain different IDs. */\nexport function stripeEventId(event: Stripe.Event): string {\n return `stripe:event:${event.id}`\n}\n"],"mappings":";;;;;;;AAeA,SAAgB,eAAkB,KAID;CAC/B,MAAM,WAEF;CACJ,QAAQ,UAAU;EAChB,MAAM,QAAQ,SAAS,MAAM;EAC7B,IAAI,UAAU,QAAW,OAAO,CAAC;EAEjC,QADkB,eAAe,KAAK,IAAI,QAAQ,CAAC,KAAK,EACxC,CAAC,SAAS,aAAa;GAErC,MAAM,SAAS,SAAS,MAAM,KAAK,MAAe;GAClD,OAAO,WAAW,SAAY,CAAC,IAAI,CAAC,MAAM;EAC5C,CAAC;CACH;AACF;AAGA,SAAS,eACP,OACsC;CACtC,OAAO,MAAM,QAAQ,KAAK;AAC5B;;AAGA,SAAgB,cAAc,OAA6B;CACzD,OAAO,gBAAgB,MAAM;AAC/B"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://www.schemastore.org/package.json",
3
3
  "name": "better-ship",
4
- "version": "0.43.0",
4
+ "version": "0.44.0",
5
5
  "description": "Foundation for applications on Cloudflare Workers: logger, errors, message bus, infrastructure adapters, and UI primitives",
6
6
  "license": "Apache-2.0",
7
7
  "repository": {
@@ -5,20 +5,41 @@ type EventObject<K extends Stripe.Event['type']> = Extract<
5
5
  { type: K }
6
6
  >['data']['object']
7
7
 
8
+ type EventCallback<T> = (object: never) => T | undefined
9
+
8
10
  /**
9
- * Convert verified Stripe events with callbacks typed to each event's object.
10
- * Unmapped events return undefined. Applications own validation and payment policy.
11
+ * Convert one verified Stripe event with callbacks typed to that event's object.
12
+ * An entry is one callback or an array of callbacks. Results stay in entry order.
13
+ * A callback returns `undefined` to ignore the event; the result omits it.
14
+ * An unmapped event returns `[]`. Applications own validation and payment policy.
11
15
  */
12
16
  export function stripeEventMap<T>(map: {
13
- [K in Stripe.Event['type']]?: (object: EventObject<K>) => T
14
- }): (event: Stripe.Event) => T | undefined {
15
- const handlers: Partial<Record<Stripe.Event['type'], (object: never) => T>> = map
17
+ [K in Stripe.Event['type']]?:
18
+ | ((object: EventObject<K>) => T | undefined)
19
+ | readonly ((object: EventObject<K>) => T | undefined)[]
20
+ }): (event: Stripe.Event) => T[] {
21
+ const handlers: Partial<
22
+ Record<Stripe.Event['type'], EventCallback<T> | readonly EventCallback<T>[]>
23
+ > = map
16
24
  return (event) => {
17
- // SAFETY: The map binds each event type to its object; indexed lookup loses that correlation.
18
- return handlers[event.type]?.(event.data.object as never)
25
+ const entry = handlers[event.type]
26
+ if (entry === undefined) return []
27
+ const callbacks = isCallbackList(entry) ? entry : [entry]
28
+ return callbacks.flatMap((callback) => {
29
+ // SAFETY: The map binds each event type to its object; indexed lookup loses that correlation.
30
+ const result = callback(event.data.object as never)
31
+ return result === undefined ? [] : [result]
32
+ })
19
33
  }
20
34
  }
21
35
 
36
+ // Array.isArray widens to any[], so the predicate restores the callback list type.
37
+ function isCallbackList<T>(
38
+ entry: EventCallback<T> | readonly EventCallback<T>[],
39
+ ): entry is readonly EventCallback<T>[] {
40
+ return Array.isArray(entry)
41
+ }
42
+
22
43
  /** Identify one Stripe event delivery; different events for one payment retain different IDs. */
23
44
  export function stripeEventId(event: Stripe.Event): string {
24
45
  return `stripe:event:${event.id}`