@drawbridge/drawbridge-stripe 0.1.13 → 0.1.14

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.
Files changed (3) hide show
  1. package/dist/index.js +108 -58
  2. package/dist/index.mjs +108 -58
  3. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -18,6 +18,22 @@ var require_billing = __commonJS({
18
18
  "Stripe-Version": "2026-01-28.preview"
19
19
  }
20
20
  };
21
+ const withIdem = (base, suffix) => {
22
+ if (!base) return options;
23
+ return {
24
+ ...options,
25
+ headers: {
26
+ ...options.headers,
27
+ "Idempotency-Key": base + ":" + suffix
28
+ }
29
+ };
30
+ };
31
+ const idem = (base, suffix) => {
32
+ if (!base) return void 0;
33
+ return {
34
+ idempotencyKey: base + ":" + suffix
35
+ };
36
+ };
21
37
  const billableItems = async () => {
22
38
  var _a, _b, _c, _d, _e, _f;
23
39
  try {
@@ -55,6 +71,7 @@ var require_billing = __commonJS({
55
71
  currency,
56
72
  customer,
57
73
  default_payment_method = null,
74
+ idempotencyKey,
58
75
  metadata = {}
59
76
  }) => {
60
77
  var _a, _b, _c, _d;
@@ -94,17 +111,17 @@ var require_billing = __commonJS({
94
111
  }
95
112
  ]
96
113
  },
97
- options
114
+ withIdem(idempotencyKey, "intent.create")
98
115
  );
99
116
  const { data: reserve } = await axios.post(
100
117
  billing + "/intents/" + (intent == null ? void 0 : intent.id) + "/reserve",
101
118
  {},
102
- options
119
+ withIdem(idempotencyKey, "intent.reserve")
103
120
  );
104
121
  const { data: commit } = await axios.post(
105
122
  billing + "/intents/" + (intent == null ? void 0 : intent.id) + "/commit",
106
123
  {},
107
- options
124
+ withIdem(idempotencyKey, "intent.commit")
108
125
  );
109
126
  const { data: actions } = await axios.get(
110
127
  billing + "/intents/" + (intent == null ? void 0 : intent.id) + "/actions",
@@ -124,36 +141,42 @@ var require_billing = __commonJS({
124
141
  type: "customer"
125
142
  }
126
143
  ];
127
- const replenish = await stripe2.billing.alerts.create({
128
- alert_type: "credit_balance_threshold",
129
- credit_balance_threshold: {
130
- filters,
131
- lte: {
132
- balance_type: "monetary",
133
- monetary: {
134
- currency,
135
- value: 200
136
- // cents
144
+ const replenish = await stripe2.billing.alerts.create(
145
+ {
146
+ alert_type: "credit_balance_threshold",
147
+ credit_balance_threshold: {
148
+ filters,
149
+ lte: {
150
+ balance_type: "monetary",
151
+ monetary: {
152
+ currency,
153
+ value: 200
154
+ // cents
155
+ }
137
156
  }
138
- }
157
+ },
158
+ title: customer + ".credit_balance_replenish"
139
159
  },
140
- title: customer + ".credit_balance_replenish"
141
- });
142
- const depleted = await stripe2.billing.alerts.create({
143
- alert_type: "credit_balance_threshold",
144
- credit_balance_threshold: {
145
- filters,
146
- lte: {
147
- balance_type: "monetary",
148
- monetary: {
149
- currency,
150
- value: 1
151
- // cents
160
+ idem(idempotencyKey, "alert.replenish")
161
+ );
162
+ const depleted = await stripe2.billing.alerts.create(
163
+ {
164
+ alert_type: "credit_balance_threshold",
165
+ credit_balance_threshold: {
166
+ filters,
167
+ lte: {
168
+ balance_type: "monetary",
169
+ monetary: {
170
+ currency,
171
+ value: 1
172
+ // cents
173
+ }
152
174
  }
153
- }
175
+ },
176
+ title: customer + ".credit_balance_depleted"
154
177
  },
155
- title: customer + ".credit_balance_depleted"
156
- });
178
+ idem(idempotencyKey, "alert.depleted")
179
+ );
157
180
  const stripePricingPlanSubscriptionId = (_d = (_c = (_b = (_a = actions == null ? void 0 : actions.data) == null ? void 0 : _a[0]) == null ? void 0 : _b.subscribe) == null ? void 0 : _c.pricing_plan_subscription_details) == null ? void 0 : _d.pricing_plan_subscription;
158
181
  if (!stripePricingPlanSubscriptionId) {
159
182
  throw new Error("Stripe billing intent did not return a pricing plan subscription ID for customer: " + customer);
@@ -193,7 +216,8 @@ var require_billing = __commonJS({
193
216
  };
194
217
  const unsubscribe = async ({
195
218
  currency,
196
- id
219
+ id,
220
+ idempotencyKey
197
221
  }) => {
198
222
  try {
199
223
  const { data } = await axios.post(
@@ -212,17 +236,17 @@ var require_billing = __commonJS({
212
236
  }
213
237
  ]
214
238
  },
215
- options
239
+ withIdem(idempotencyKey, "intent.create")
216
240
  );
217
241
  await axios.post(
218
242
  billing + "/intents/" + (data == null ? void 0 : data.id) + "/reserve",
219
243
  {},
220
- options
244
+ withIdem(idempotencyKey, "intent.reserve")
221
245
  );
222
246
  const commit = await axios.post(
223
247
  billing + "/intents/" + (data == null ? void 0 : data.id) + "/commit",
224
248
  {},
225
- options
249
+ withIdem(idempotencyKey, "intent.commit")
226
250
  );
227
251
  return commit;
228
252
  } catch (error) {
@@ -562,6 +586,7 @@ var require_subscription = __commonJS({
562
586
  billingCycleAnchor,
563
587
  customer,
564
588
  discounts = [],
589
+ idempotencyKey,
565
590
  metadata,
566
591
  stripePaymentMethodId,
567
592
  to
@@ -570,6 +595,7 @@ var require_subscription = __commonJS({
570
595
  try {
571
596
  const actions = await createItem({
572
597
  currency: to.currency,
598
+ idempotencyKey,
573
599
  interval: to.interval,
574
600
  limit: to.limits.organization.actions,
575
601
  metadata,
@@ -577,31 +603,37 @@ var require_subscription = __commonJS({
577
603
  overage: to.overages.actions,
578
604
  proration_behavior: "none"
579
605
  });
580
- const subscription = await stripe2.subscriptions.create({
581
- automatic_tax: {
582
- enabled: true
583
- },
584
- customer,
585
- description: "Organization subscription: " + metadata.organization,
586
- default_payment_method: stripePaymentMethodId,
587
- discounts: (discounts == null ? void 0 : discounts.length) > 0 ? discounts : "",
588
- items: [
589
- {
590
- price: actions.price
606
+ const subscription = await stripe2.subscriptions.create(
607
+ {
608
+ automatic_tax: {
609
+ enabled: true
610
+ },
611
+ customer,
612
+ description: "Organization subscription: " + metadata.organization,
613
+ default_payment_method: stripePaymentMethodId,
614
+ discounts: (discounts == null ? void 0 : discounts.length) > 0 ? discounts : "",
615
+ items: [
616
+ {
617
+ price: actions.price
618
+ }
619
+ ],
620
+ metadata,
621
+ ...billingCycleAnchor && {
622
+ billing_cycle_anchor: billingCycleAnchor
591
623
  }
592
- ],
593
- metadata,
594
- ...billingCycleAnchor && {
595
- billing_cycle_anchor: billingCycleAnchor
596
- }
597
- });
624
+ },
625
+ idem(idempotencyKey, "subscription.create")
626
+ );
598
627
  const { product } = await stripe2.prices.retrieve(to == null ? void 0 : to.stripePriceId);
599
- const plan = await stripe2.subscriptionItems.create({
600
- subscription: subscription == null ? void 0 : subscription.id,
601
- price: to == null ? void 0 : to.stripePriceId,
602
- quantity: 1,
603
- proration_behavior: "always_invoice"
604
- });
628
+ const plan = await stripe2.subscriptionItems.create(
629
+ {
630
+ subscription: subscription == null ? void 0 : subscription.id,
631
+ price: to == null ? void 0 : to.stripePriceId,
632
+ quantity: 1,
633
+ proration_behavior: "always_invoice"
634
+ },
635
+ idem(idempotencyKey, "plan.create")
636
+ );
605
637
  return {
606
638
  discounts,
607
639
  items: {
@@ -623,6 +655,7 @@ var require_subscription = __commonJS({
623
655
  }
624
656
  },
625
657
  pause: async ({
658
+ idempotencyKey,
626
659
  stripeSubscriptionId
627
660
  }) => {
628
661
  try {
@@ -634,7 +667,15 @@ var require_subscription = __commonJS({
634
667
  const { data } = await axios.post(
635
668
  subscriptions + "/" + stripeSubscriptionId + "/pause",
636
669
  body,
637
- options
670
+ {
671
+ ...options,
672
+ ...idempotencyKey && {
673
+ headers: {
674
+ ...options.headers,
675
+ "Idempotency-Key": idempotencyKey
676
+ }
677
+ }
678
+ }
638
679
  );
639
680
  return data;
640
681
  } catch (error) {
@@ -642,6 +683,7 @@ var require_subscription = __commonJS({
642
683
  }
643
684
  },
644
685
  resume: async ({
686
+ idempotencyKey,
645
687
  stripeSubscriptionId
646
688
  }) => {
647
689
  try {
@@ -651,7 +693,15 @@ var require_subscription = __commonJS({
651
693
  const { data } = await axios.post(
652
694
  subscriptions + "/" + stripeSubscriptionId + "/resume",
653
695
  body,
654
- options
696
+ {
697
+ ...options,
698
+ ...idempotencyKey && {
699
+ headers: {
700
+ ...options.headers,
701
+ "Idempotency-Key": idempotencyKey
702
+ }
703
+ }
704
+ }
655
705
  );
656
706
  return data;
657
707
  } catch (error) {
package/dist/index.mjs CHANGED
@@ -24,6 +24,22 @@ var require_billing = __commonJS({
24
24
  "Stripe-Version": "2026-01-28.preview"
25
25
  }
26
26
  };
27
+ const withIdem = (base, suffix) => {
28
+ if (!base) return options;
29
+ return {
30
+ ...options,
31
+ headers: {
32
+ ...options.headers,
33
+ "Idempotency-Key": base + ":" + suffix
34
+ }
35
+ };
36
+ };
37
+ const idem = (base, suffix) => {
38
+ if (!base) return void 0;
39
+ return {
40
+ idempotencyKey: base + ":" + suffix
41
+ };
42
+ };
27
43
  const billableItems = async () => {
28
44
  var _a, _b, _c, _d, _e, _f;
29
45
  try {
@@ -61,6 +77,7 @@ var require_billing = __commonJS({
61
77
  currency,
62
78
  customer,
63
79
  default_payment_method = null,
80
+ idempotencyKey,
64
81
  metadata = {}
65
82
  }) => {
66
83
  var _a, _b, _c, _d;
@@ -100,17 +117,17 @@ var require_billing = __commonJS({
100
117
  }
101
118
  ]
102
119
  },
103
- options
120
+ withIdem(idempotencyKey, "intent.create")
104
121
  );
105
122
  const { data: reserve } = await axios.post(
106
123
  billing + "/intents/" + (intent == null ? void 0 : intent.id) + "/reserve",
107
124
  {},
108
- options
125
+ withIdem(idempotencyKey, "intent.reserve")
109
126
  );
110
127
  const { data: commit } = await axios.post(
111
128
  billing + "/intents/" + (intent == null ? void 0 : intent.id) + "/commit",
112
129
  {},
113
- options
130
+ withIdem(idempotencyKey, "intent.commit")
114
131
  );
115
132
  const { data: actions } = await axios.get(
116
133
  billing + "/intents/" + (intent == null ? void 0 : intent.id) + "/actions",
@@ -130,36 +147,42 @@ var require_billing = __commonJS({
130
147
  type: "customer"
131
148
  }
132
149
  ];
133
- const replenish = await stripe.billing.alerts.create({
134
- alert_type: "credit_balance_threshold",
135
- credit_balance_threshold: {
136
- filters,
137
- lte: {
138
- balance_type: "monetary",
139
- monetary: {
140
- currency,
141
- value: 200
142
- // cents
150
+ const replenish = await stripe.billing.alerts.create(
151
+ {
152
+ alert_type: "credit_balance_threshold",
153
+ credit_balance_threshold: {
154
+ filters,
155
+ lte: {
156
+ balance_type: "monetary",
157
+ monetary: {
158
+ currency,
159
+ value: 200
160
+ // cents
161
+ }
143
162
  }
144
- }
163
+ },
164
+ title: customer + ".credit_balance_replenish"
145
165
  },
146
- title: customer + ".credit_balance_replenish"
147
- });
148
- const depleted = await stripe.billing.alerts.create({
149
- alert_type: "credit_balance_threshold",
150
- credit_balance_threshold: {
151
- filters,
152
- lte: {
153
- balance_type: "monetary",
154
- monetary: {
155
- currency,
156
- value: 1
157
- // cents
166
+ idem(idempotencyKey, "alert.replenish")
167
+ );
168
+ const depleted = await stripe.billing.alerts.create(
169
+ {
170
+ alert_type: "credit_balance_threshold",
171
+ credit_balance_threshold: {
172
+ filters,
173
+ lte: {
174
+ balance_type: "monetary",
175
+ monetary: {
176
+ currency,
177
+ value: 1
178
+ // cents
179
+ }
158
180
  }
159
- }
181
+ },
182
+ title: customer + ".credit_balance_depleted"
160
183
  },
161
- title: customer + ".credit_balance_depleted"
162
- });
184
+ idem(idempotencyKey, "alert.depleted")
185
+ );
163
186
  const stripePricingPlanSubscriptionId = (_d = (_c = (_b = (_a = actions == null ? void 0 : actions.data) == null ? void 0 : _a[0]) == null ? void 0 : _b.subscribe) == null ? void 0 : _c.pricing_plan_subscription_details) == null ? void 0 : _d.pricing_plan_subscription;
164
187
  if (!stripePricingPlanSubscriptionId) {
165
188
  throw new Error("Stripe billing intent did not return a pricing plan subscription ID for customer: " + customer);
@@ -199,7 +222,8 @@ var require_billing = __commonJS({
199
222
  };
200
223
  const unsubscribe = async ({
201
224
  currency,
202
- id
225
+ id,
226
+ idempotencyKey
203
227
  }) => {
204
228
  try {
205
229
  const { data } = await axios.post(
@@ -218,17 +242,17 @@ var require_billing = __commonJS({
218
242
  }
219
243
  ]
220
244
  },
221
- options
245
+ withIdem(idempotencyKey, "intent.create")
222
246
  );
223
247
  await axios.post(
224
248
  billing + "/intents/" + (data == null ? void 0 : data.id) + "/reserve",
225
249
  {},
226
- options
250
+ withIdem(idempotencyKey, "intent.reserve")
227
251
  );
228
252
  const commit = await axios.post(
229
253
  billing + "/intents/" + (data == null ? void 0 : data.id) + "/commit",
230
254
  {},
231
- options
255
+ withIdem(idempotencyKey, "intent.commit")
232
256
  );
233
257
  return commit;
234
258
  } catch (error) {
@@ -568,6 +592,7 @@ var require_subscription = __commonJS({
568
592
  billingCycleAnchor,
569
593
  customer,
570
594
  discounts = [],
595
+ idempotencyKey,
571
596
  metadata,
572
597
  stripePaymentMethodId,
573
598
  to
@@ -576,6 +601,7 @@ var require_subscription = __commonJS({
576
601
  try {
577
602
  const actions = await createItem({
578
603
  currency: to.currency,
604
+ idempotencyKey,
579
605
  interval: to.interval,
580
606
  limit: to.limits.organization.actions,
581
607
  metadata,
@@ -583,31 +609,37 @@ var require_subscription = __commonJS({
583
609
  overage: to.overages.actions,
584
610
  proration_behavior: "none"
585
611
  });
586
- const subscription = await stripe.subscriptions.create({
587
- automatic_tax: {
588
- enabled: true
589
- },
590
- customer,
591
- description: "Organization subscription: " + metadata.organization,
592
- default_payment_method: stripePaymentMethodId,
593
- discounts: (discounts == null ? void 0 : discounts.length) > 0 ? discounts : "",
594
- items: [
595
- {
596
- price: actions.price
612
+ const subscription = await stripe.subscriptions.create(
613
+ {
614
+ automatic_tax: {
615
+ enabled: true
616
+ },
617
+ customer,
618
+ description: "Organization subscription: " + metadata.organization,
619
+ default_payment_method: stripePaymentMethodId,
620
+ discounts: (discounts == null ? void 0 : discounts.length) > 0 ? discounts : "",
621
+ items: [
622
+ {
623
+ price: actions.price
624
+ }
625
+ ],
626
+ metadata,
627
+ ...billingCycleAnchor && {
628
+ billing_cycle_anchor: billingCycleAnchor
597
629
  }
598
- ],
599
- metadata,
600
- ...billingCycleAnchor && {
601
- billing_cycle_anchor: billingCycleAnchor
602
- }
603
- });
630
+ },
631
+ idem(idempotencyKey, "subscription.create")
632
+ );
604
633
  const { product } = await stripe.prices.retrieve(to == null ? void 0 : to.stripePriceId);
605
- const plan = await stripe.subscriptionItems.create({
606
- subscription: subscription == null ? void 0 : subscription.id,
607
- price: to == null ? void 0 : to.stripePriceId,
608
- quantity: 1,
609
- proration_behavior: "always_invoice"
610
- });
634
+ const plan = await stripe.subscriptionItems.create(
635
+ {
636
+ subscription: subscription == null ? void 0 : subscription.id,
637
+ price: to == null ? void 0 : to.stripePriceId,
638
+ quantity: 1,
639
+ proration_behavior: "always_invoice"
640
+ },
641
+ idem(idempotencyKey, "plan.create")
642
+ );
611
643
  return {
612
644
  discounts,
613
645
  items: {
@@ -629,6 +661,7 @@ var require_subscription = __commonJS({
629
661
  }
630
662
  },
631
663
  pause: async ({
664
+ idempotencyKey,
632
665
  stripeSubscriptionId
633
666
  }) => {
634
667
  try {
@@ -640,7 +673,15 @@ var require_subscription = __commonJS({
640
673
  const { data } = await axios.post(
641
674
  subscriptions + "/" + stripeSubscriptionId + "/pause",
642
675
  body,
643
- options
676
+ {
677
+ ...options,
678
+ ...idempotencyKey && {
679
+ headers: {
680
+ ...options.headers,
681
+ "Idempotency-Key": idempotencyKey
682
+ }
683
+ }
684
+ }
644
685
  );
645
686
  return data;
646
687
  } catch (error) {
@@ -648,6 +689,7 @@ var require_subscription = __commonJS({
648
689
  }
649
690
  },
650
691
  resume: async ({
692
+ idempotencyKey,
651
693
  stripeSubscriptionId
652
694
  }) => {
653
695
  try {
@@ -657,7 +699,15 @@ var require_subscription = __commonJS({
657
699
  const { data } = await axios.post(
658
700
  subscriptions + "/" + stripeSubscriptionId + "/resume",
659
701
  body,
660
- options
702
+ {
703
+ ...options,
704
+ ...idempotencyKey && {
705
+ headers: {
706
+ ...options.headers,
707
+ "Idempotency-Key": idempotencyKey
708
+ }
709
+ }
710
+ }
661
711
  );
662
712
  return data;
663
713
  } catch (error) {
package/package.json CHANGED
@@ -24,5 +24,5 @@
24
24
  "build": "tsup ./index.js && npm publish"
25
25
  },
26
26
  "types": "dist/index.d.ts",
27
- "version": "0.1.13"
27
+ "version": "0.1.14"
28
28
  }