alchemy 0.24.0 → 0.25.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.
Files changed (184) hide show
  1. package/lib/aws/control/client.js +1 -1
  2. package/lib/aws/control/client.js.map +1 -1
  3. package/lib/aws/control/resource.d.ts.map +1 -1
  4. package/lib/aws/control/resource.js +2 -1
  5. package/lib/aws/control/resource.js.map +1 -1
  6. package/lib/aws/function.d.ts.map +1 -1
  7. package/lib/aws/function.js +1 -4
  8. package/lib/aws/function.js.map +1 -1
  9. package/lib/cloudflare/ai.d.ts +21 -0
  10. package/lib/cloudflare/ai.d.ts.map +1 -1
  11. package/lib/cloudflare/ai.js +21 -0
  12. package/lib/cloudflare/ai.js.map +1 -1
  13. package/lib/cloudflare/bindings.d.ts +3 -3
  14. package/lib/cloudflare/bindings.d.ts.map +1 -1
  15. package/lib/cloudflare/bindings.js.map +1 -1
  16. package/lib/cloudflare/bound.d.ts +3 -3
  17. package/lib/cloudflare/bound.d.ts.map +1 -1
  18. package/lib/cloudflare/do-state-store/index.d.ts +2 -0
  19. package/lib/cloudflare/do-state-store/index.d.ts.map +1 -0
  20. package/lib/cloudflare/do-state-store/index.js +2 -0
  21. package/lib/cloudflare/do-state-store/index.js.map +1 -0
  22. package/lib/cloudflare/do-state-store/internal.d.ts +19 -0
  23. package/lib/cloudflare/do-state-store/internal.d.ts.map +1 -0
  24. package/lib/cloudflare/do-state-store/internal.js +163 -0
  25. package/lib/cloudflare/do-state-store/internal.js.map +1 -0
  26. package/lib/cloudflare/do-state-store/store.d.ts +59 -0
  27. package/lib/cloudflare/do-state-store/store.d.ts.map +1 -0
  28. package/lib/cloudflare/do-state-store/store.js +120 -0
  29. package/lib/cloudflare/do-state-store/store.js.map +1 -0
  30. package/lib/cloudflare/do-state-store/types.d.ts +82 -0
  31. package/lib/cloudflare/do-state-store/types.d.ts.map +1 -0
  32. package/lib/cloudflare/do-state-store/types.js +1 -0
  33. package/lib/cloudflare/do-state-store/types.js.map +1 -0
  34. package/lib/cloudflare/do-state-store/worker.d.ts +27 -0
  35. package/lib/cloudflare/do-state-store/worker.d.ts.map +1 -0
  36. package/lib/cloudflare/do-state-store/worker.js +170 -0
  37. package/lib/cloudflare/do-state-store/worker.js.map +1 -0
  38. package/lib/cloudflare/index.d.ts +1 -0
  39. package/lib/cloudflare/index.d.ts.map +1 -1
  40. package/lib/cloudflare/index.js +1 -0
  41. package/lib/cloudflare/index.js.map +1 -1
  42. package/lib/cloudflare/pipeline.js +1 -1
  43. package/lib/cloudflare/pipeline.js.map +1 -1
  44. package/lib/cloudflare/queue-consumer.d.ts +14 -1
  45. package/lib/cloudflare/queue-consumer.d.ts.map +1 -1
  46. package/lib/cloudflare/queue-consumer.js +17 -0
  47. package/lib/cloudflare/queue-consumer.js.map +1 -1
  48. package/lib/cloudflare/website.d.ts +30 -1
  49. package/lib/cloudflare/website.d.ts.map +1 -1
  50. package/lib/cloudflare/website.js +8 -5
  51. package/lib/cloudflare/website.js.map +1 -1
  52. package/lib/cloudflare/worker-assets.d.ts.map +1 -1
  53. package/lib/cloudflare/worker-assets.js +13 -3
  54. package/lib/cloudflare/worker-assets.js.map +1 -1
  55. package/lib/cloudflare/worker-metadata.js +1 -1
  56. package/lib/cloudflare/worker-metadata.js.map +1 -1
  57. package/lib/cloudflare/worker.d.ts +40 -1
  58. package/lib/cloudflare/worker.d.ts.map +1 -1
  59. package/lib/cloudflare/worker.js +40 -38
  60. package/lib/cloudflare/worker.js.map +1 -1
  61. package/lib/cloudflare/wrangler.json.js +1 -1
  62. package/lib/cloudflare/wrangler.json.js.map +1 -1
  63. package/lib/cloudflare/zone.d.ts +30 -2
  64. package/lib/cloudflare/zone.d.ts.map +1 -1
  65. package/lib/cloudflare/zone.js +53 -0
  66. package/lib/cloudflare/zone.js.map +1 -1
  67. package/lib/fs/file-system-state-store.d.ts +3 -1
  68. package/lib/fs/file-system-state-store.d.ts.map +1 -1
  69. package/lib/fs/file-system-state-store.js +2 -2
  70. package/lib/fs/file-system-state-store.js.map +1 -1
  71. package/lib/stripe/card.d.ts +171 -0
  72. package/lib/stripe/card.d.ts.map +1 -0
  73. package/lib/stripe/card.js +148 -0
  74. package/lib/stripe/card.js.map +1 -0
  75. package/lib/stripe/client.d.ts +9 -0
  76. package/lib/stripe/client.d.ts.map +1 -0
  77. package/lib/stripe/client.js +94 -0
  78. package/lib/stripe/client.js.map +1 -0
  79. package/lib/stripe/coupon.d.ts +135 -0
  80. package/lib/stripe/coupon.d.ts.map +1 -0
  81. package/lib/stripe/coupon.js +161 -0
  82. package/lib/stripe/coupon.js.map +1 -0
  83. package/lib/stripe/customer.d.ts +269 -0
  84. package/lib/stripe/customer.d.ts.map +1 -0
  85. package/lib/stripe/customer.js +289 -0
  86. package/lib/stripe/customer.js.map +1 -0
  87. package/lib/stripe/entitlements-feature.d.ts +84 -0
  88. package/lib/stripe/entitlements-feature.d.ts.map +1 -0
  89. package/lib/stripe/entitlements-feature.js +102 -0
  90. package/lib/stripe/entitlements-feature.js.map +1 -0
  91. package/lib/stripe/file.d.ts +143 -0
  92. package/lib/stripe/file.d.ts.map +1 -0
  93. package/lib/stripe/file.js +97 -0
  94. package/lib/stripe/file.js.map +1 -0
  95. package/lib/stripe/index.d.ts +11 -0
  96. package/lib/stripe/index.d.ts.map +1 -1
  97. package/lib/stripe/index.js +11 -0
  98. package/lib/stripe/index.js.map +1 -1
  99. package/lib/stripe/meter.d.ts +10 -1
  100. package/lib/stripe/meter.d.ts.map +1 -1
  101. package/lib/stripe/meter.js +13 -7
  102. package/lib/stripe/meter.js.map +1 -1
  103. package/lib/stripe/portal-configuration.d.ts +286 -0
  104. package/lib/stripe/portal-configuration.d.ts.map +1 -0
  105. package/lib/stripe/portal-configuration.js +320 -0
  106. package/lib/stripe/portal-configuration.js.map +1 -0
  107. package/lib/stripe/price.d.ts +10 -1
  108. package/lib/stripe/price.d.ts.map +1 -1
  109. package/lib/stripe/price.js +34 -13
  110. package/lib/stripe/price.js.map +1 -1
  111. package/lib/stripe/product-feature.d.ts +76 -0
  112. package/lib/stripe/product-feature.d.ts.map +1 -0
  113. package/lib/stripe/product-feature.js +74 -0
  114. package/lib/stripe/product-feature.js.map +1 -0
  115. package/lib/stripe/product.d.ts +10 -1
  116. package/lib/stripe/product.d.ts.map +1 -1
  117. package/lib/stripe/product.js +33 -14
  118. package/lib/stripe/product.js.map +1 -1
  119. package/lib/stripe/promotion-code.d.ts +138 -0
  120. package/lib/stripe/promotion-code.d.ts.map +1 -0
  121. package/lib/stripe/promotion-code.js +150 -0
  122. package/lib/stripe/promotion-code.js.map +1 -0
  123. package/lib/stripe/shipping-rate.d.ts +188 -0
  124. package/lib/stripe/shipping-rate.d.ts.map +1 -0
  125. package/lib/stripe/shipping-rate.js +198 -0
  126. package/lib/stripe/shipping-rate.js.map +1 -0
  127. package/lib/stripe/tax-rate.d.ts +130 -0
  128. package/lib/stripe/tax-rate.d.ts.map +1 -0
  129. package/lib/stripe/tax-rate.js +126 -0
  130. package/lib/stripe/tax-rate.js.map +1 -0
  131. package/lib/stripe/webhook.d.ts +10 -1
  132. package/lib/stripe/webhook.d.ts.map +1 -1
  133. package/lib/stripe/webhook.js +59 -14
  134. package/lib/stripe/webhook.js.map +1 -1
  135. package/lib/test/bun.d.ts.map +1 -1
  136. package/lib/test/bun.js +2 -6
  137. package/lib/test/bun.js.map +1 -1
  138. package/lib/test/vitest.d.ts.map +1 -1
  139. package/lib/test/vitest.js +3 -7
  140. package/lib/test/vitest.js.map +1 -1
  141. package/lib/util/retry.d.ts +1 -1
  142. package/lib/util/retry.d.ts.map +1 -1
  143. package/lib/util/retry.js.map +1 -1
  144. package/package.json +3 -2
  145. package/src/aws/control/client.ts +1 -1
  146. package/src/aws/control/resource.ts +2 -1
  147. package/src/aws/function.ts +3 -6
  148. package/src/cloudflare/ai.ts +21 -0
  149. package/src/cloudflare/bindings.ts +3 -2
  150. package/src/cloudflare/bound.ts +3 -3
  151. package/src/cloudflare/do-state-store/index.ts +1 -0
  152. package/src/cloudflare/do-state-store/internal.ts +215 -0
  153. package/src/cloudflare/do-state-store/store.ts +188 -0
  154. package/src/cloudflare/do-state-store/types.ts +74 -0
  155. package/src/cloudflare/do-state-store/worker.ts +199 -0
  156. package/src/cloudflare/index.ts +1 -0
  157. package/src/cloudflare/pipeline.ts +1 -1
  158. package/src/cloudflare/queue-consumer.ts +59 -1
  159. package/src/cloudflare/website.ts +38 -6
  160. package/src/cloudflare/worker-assets.ts +15 -3
  161. package/src/cloudflare/worker-metadata.ts +1 -1
  162. package/src/cloudflare/worker.ts +66 -46
  163. package/src/cloudflare/wrangler.json.ts +1 -1
  164. package/src/cloudflare/zone.ts +75 -2
  165. package/src/fs/file-system-state-store.ts +7 -2
  166. package/src/stripe/card.ts +320 -0
  167. package/src/stripe/client.ts +145 -0
  168. package/src/stripe/coupon.ts +280 -0
  169. package/src/stripe/customer.ts +528 -0
  170. package/src/stripe/entitlements-feature.ts +169 -0
  171. package/src/stripe/file.ts +219 -0
  172. package/src/stripe/index.ts +11 -0
  173. package/src/stripe/meter.ts +23 -7
  174. package/src/stripe/portal-configuration.ts +609 -0
  175. package/src/stripe/price.ts +47 -14
  176. package/src/stripe/product-feature.ts +150 -0
  177. package/src/stripe/product.ts +48 -15
  178. package/src/stripe/promotion-code.ts +268 -0
  179. package/src/stripe/shipping-rate.ts +362 -0
  180. package/src/stripe/tax-rate.ts +218 -0
  181. package/src/stripe/webhook.ts +82 -15
  182. package/src/test/bun.ts +2 -7
  183. package/src/test/vitest.ts +3 -8
  184. package/src/util/retry.ts +1 -1
@@ -0,0 +1,280 @@
1
+ import type Stripe from "stripe";
2
+ import type { Context } from "../context.ts";
3
+ import { Resource } from "../resource.ts";
4
+ import type { Secret } from "../secret.ts";
5
+ import {
6
+ createStripeClient,
7
+ handleStripeDeleteError,
8
+ isStripeConflictError,
9
+ } from "./client.ts";
10
+
11
+ type CouponDuration = Stripe.CouponCreateParams.Duration;
12
+
13
+ /**
14
+ * Properties for creating a Stripe coupon
15
+ */
16
+ export interface CouponProps {
17
+ /**
18
+ * Unique string of your choice that will be used to identify this coupon when applying it to a customer
19
+ */
20
+ id?: string;
21
+
22
+ /**
23
+ * Specifies how long the discount will be in effect
24
+ */
25
+ duration: CouponDuration;
26
+
27
+ /**
28
+ * A positive integer representing the amount to subtract from an invoice total (required if percent_off is not passed)
29
+ */
30
+ amountOff?: number;
31
+
32
+ /**
33
+ * Three-letter ISO code for the currency of the amount_off parameter (required if amount_off is passed)
34
+ */
35
+ currency?: string;
36
+
37
+ /**
38
+ * Required only if duration is repeating, in which case it must be a positive integer that specifies the number of months the discount will be in effect
39
+ */
40
+ durationInMonths?: number;
41
+
42
+ /**
43
+ * A positive integer specifying the number of times the coupon can be redeemed before it's no longer valid
44
+ */
45
+ maxRedemptions?: number;
46
+
47
+ /**
48
+ * Name of the coupon displayed to customers on, for instance invoices, or receipts
49
+ */
50
+ name?: string;
51
+
52
+ /**
53
+ * A positive float larger than 0, and smaller or equal to 100, that represents the discount the coupon will apply (required if amount_off is not passed)
54
+ */
55
+ percentOff?: number;
56
+
57
+ /**
58
+ * Unix timestamp specifying the last time at which the coupon can be redeemed
59
+ */
60
+ redeemBy?: number;
61
+
62
+ /**
63
+ * Number of times this coupon has been applied to a customer
64
+ */
65
+ timesRedeemed?: number;
66
+
67
+ /**
68
+ * Taking account of the above properties, whether this coupon can still be applied to a customer
69
+ */
70
+ valid?: boolean;
71
+
72
+ /**
73
+ * Set of key-value pairs that you can attach to an object
74
+ */
75
+ metadata?: Record<string, string>;
76
+
77
+ /**
78
+ * API key to use (overrides environment variable)
79
+ */
80
+ apiKey?: Secret;
81
+
82
+ /**
83
+ * If true, adopt existing resource if creation fails due to conflict
84
+ */
85
+ adopt?: boolean;
86
+ }
87
+
88
+ /**
89
+ * Output from the Stripe coupon
90
+ */
91
+ export interface Coupon extends Resource<"stripe::Coupon">, CouponProps {
92
+ /**
93
+ * The ID of the coupon
94
+ */
95
+ id: string;
96
+
97
+ /**
98
+ * String representing the object's type
99
+ */
100
+ object: "coupon";
101
+
102
+ /**
103
+ * Time at which the object was created
104
+ */
105
+ created: number;
106
+
107
+ /**
108
+ * Has the value true if the object exists in live mode or the value false if the object exists in test mode
109
+ */
110
+ livemode: boolean;
111
+ }
112
+
113
+ /**
114
+ * Create and manage Stripe coupons for discounts
115
+ *
116
+ * @example
117
+ * // Create a percentage-based coupon
118
+ * const percentageCoupon = await Coupon("summer-sale", {
119
+ * id: "SUMMER25",
120
+ * duration: "once",
121
+ * percentOff: 25,
122
+ * name: "Summer Sale 25% Off",
123
+ * maxRedemptions: 1000,
124
+ * metadata: {
125
+ * campaign: "summer_2024",
126
+ * type: "seasonal"
127
+ * }
128
+ * });
129
+ *
130
+ * @example
131
+ * // Create a fixed amount coupon
132
+ * const fixedAmountCoupon = await Coupon("new-customer", {
133
+ * id: "WELCOME10",
134
+ * duration: "once",
135
+ * amountOff: 1000,
136
+ * currency: "usd",
137
+ * name: "Welcome $10 Off",
138
+ * metadata: {
139
+ * type: "welcome",
140
+ * target: "new_customers"
141
+ * }
142
+ * });
143
+ *
144
+ * @example
145
+ * // Create a repeating coupon for subscriptions
146
+ * const subscriptionCoupon = await Coupon("loyal-customer", {
147
+ * id: "LOYAL15",
148
+ * duration: "repeating",
149
+ * durationInMonths: 6,
150
+ * percentOff: 15,
151
+ * name: "Loyal Customer 15% Off",
152
+ * metadata: {
153
+ * type: "loyalty",
154
+ * tier: "premium"
155
+ * }
156
+ * });
157
+ */
158
+ export const Coupon = Resource(
159
+ "stripe::Coupon",
160
+ async function (
161
+ this: Context<Coupon>,
162
+ _id: string,
163
+ props: CouponProps,
164
+ ): Promise<Coupon> {
165
+ const stripe = createStripeClient({ apiKey: props.apiKey });
166
+
167
+ if (this.phase === "delete") {
168
+ try {
169
+ if (this.output?.id) {
170
+ await stripe.coupons.del(this.output.id);
171
+ }
172
+ } catch (error) {
173
+ handleStripeDeleteError(error, "Coupon", this.output?.id);
174
+ }
175
+ return this.destroy();
176
+ }
177
+
178
+ try {
179
+ let coupon: Stripe.Coupon;
180
+
181
+ if (this.phase === "update" && this.output?.id) {
182
+ coupon = await stripe.coupons.update(this.output.id, {
183
+ name: props.name,
184
+ metadata: props.metadata,
185
+ });
186
+ } else {
187
+ const createParams: Stripe.CouponCreateParams = {
188
+ duration: props.duration,
189
+ name: props.name,
190
+ metadata: props.metadata,
191
+ };
192
+
193
+ if (props.id) {
194
+ createParams.id = props.id;
195
+ }
196
+ if (props.amountOff !== undefined) {
197
+ createParams.amount_off = props.amountOff;
198
+ }
199
+ if (props.currency) {
200
+ createParams.currency = props.currency;
201
+ }
202
+ if (props.durationInMonths !== undefined) {
203
+ createParams.duration_in_months = props.durationInMonths;
204
+ }
205
+ if (props.maxRedemptions !== undefined) {
206
+ createParams.max_redemptions = props.maxRedemptions;
207
+ }
208
+ if (props.percentOff !== undefined) {
209
+ createParams.percent_off = props.percentOff;
210
+ }
211
+ if (props.redeemBy !== undefined) {
212
+ createParams.redeem_by = props.redeemBy;
213
+ }
214
+
215
+ try {
216
+ try {
217
+ coupon = await stripe.coupons.create(createParams);
218
+ } catch (error) {
219
+ if (isStripeConflictError(error) && props.adopt) {
220
+ if (props.id) {
221
+ const existingCoupon = await stripe.coupons.retrieve(props.id);
222
+ const updateParams: Stripe.CouponUpdateParams = {
223
+ metadata: props.metadata,
224
+ name: props.name,
225
+ };
226
+ coupon = await stripe.coupons.update(
227
+ existingCoupon.id,
228
+ updateParams,
229
+ );
230
+ } else {
231
+ throw error;
232
+ }
233
+ } else {
234
+ throw error;
235
+ }
236
+ }
237
+ } catch (error) {
238
+ if (isStripeConflictError(error) && props.adopt) {
239
+ if (props.id) {
240
+ const existingCoupon = await stripe.coupons.retrieve(props.id);
241
+ const updateParams: Stripe.CouponUpdateParams = {
242
+ metadata: props.metadata,
243
+ name: props.name,
244
+ };
245
+ coupon = await stripe.coupons.update(
246
+ existingCoupon.id,
247
+ updateParams,
248
+ );
249
+ } else {
250
+ throw error;
251
+ }
252
+ } else {
253
+ throw error;
254
+ }
255
+ }
256
+ }
257
+
258
+ return this({
259
+ id: coupon.id,
260
+ object: coupon.object,
261
+ duration: coupon.duration as CouponDuration,
262
+ amountOff: coupon.amount_off || undefined,
263
+ currency: coupon.currency || undefined,
264
+ durationInMonths: coupon.duration_in_months || undefined,
265
+ maxRedemptions: coupon.max_redemptions || undefined,
266
+ name: coupon.name || undefined,
267
+ percentOff: coupon.percent_off || undefined,
268
+ redeemBy: coupon.redeem_by || undefined,
269
+ timesRedeemed: coupon.times_redeemed || undefined,
270
+ valid: coupon.valid,
271
+ metadata: coupon.metadata || undefined,
272
+ created: coupon.created,
273
+ livemode: coupon.livemode,
274
+ });
275
+ } catch (error) {
276
+ console.error("Error creating/updating coupon:", error);
277
+ throw error;
278
+ }
279
+ },
280
+ );