alchemy 0.24.0 → 0.24.1

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 (163) hide show
  1. package/lib/aws/function.d.ts.map +1 -1
  2. package/lib/aws/function.js +1 -4
  3. package/lib/aws/function.js.map +1 -1
  4. package/lib/cloudflare/bindings.d.ts +3 -3
  5. package/lib/cloudflare/bindings.d.ts.map +1 -1
  6. package/lib/cloudflare/bindings.js.map +1 -1
  7. package/lib/cloudflare/bound.d.ts +3 -3
  8. package/lib/cloudflare/bound.d.ts.map +1 -1
  9. package/lib/cloudflare/do-state-store/index.d.ts +2 -0
  10. package/lib/cloudflare/do-state-store/index.d.ts.map +1 -0
  11. package/lib/cloudflare/do-state-store/index.js +2 -0
  12. package/lib/cloudflare/do-state-store/index.js.map +1 -0
  13. package/lib/cloudflare/do-state-store/internal.d.ts +19 -0
  14. package/lib/cloudflare/do-state-store/internal.d.ts.map +1 -0
  15. package/lib/cloudflare/do-state-store/internal.js +163 -0
  16. package/lib/cloudflare/do-state-store/internal.js.map +1 -0
  17. package/lib/cloudflare/do-state-store/store.d.ts +59 -0
  18. package/lib/cloudflare/do-state-store/store.d.ts.map +1 -0
  19. package/lib/cloudflare/do-state-store/store.js +120 -0
  20. package/lib/cloudflare/do-state-store/store.js.map +1 -0
  21. package/lib/cloudflare/do-state-store/types.d.ts +82 -0
  22. package/lib/cloudflare/do-state-store/types.d.ts.map +1 -0
  23. package/lib/cloudflare/do-state-store/types.js +1 -0
  24. package/lib/cloudflare/do-state-store/types.js.map +1 -0
  25. package/lib/cloudflare/do-state-store/worker.d.ts +27 -0
  26. package/lib/cloudflare/do-state-store/worker.d.ts.map +1 -0
  27. package/lib/cloudflare/do-state-store/worker.js +170 -0
  28. package/lib/cloudflare/do-state-store/worker.js.map +1 -0
  29. package/lib/cloudflare/index.d.ts +1 -0
  30. package/lib/cloudflare/index.d.ts.map +1 -1
  31. package/lib/cloudflare/index.js +1 -0
  32. package/lib/cloudflare/index.js.map +1 -1
  33. package/lib/cloudflare/pipeline.js +1 -1
  34. package/lib/cloudflare/pipeline.js.map +1 -1
  35. package/lib/cloudflare/queue-consumer.d.ts +14 -1
  36. package/lib/cloudflare/queue-consumer.d.ts.map +1 -1
  37. package/lib/cloudflare/queue-consumer.js +17 -0
  38. package/lib/cloudflare/queue-consumer.js.map +1 -1
  39. package/lib/cloudflare/website.d.ts +30 -1
  40. package/lib/cloudflare/website.d.ts.map +1 -1
  41. package/lib/cloudflare/website.js +8 -5
  42. package/lib/cloudflare/website.js.map +1 -1
  43. package/lib/cloudflare/worker-metadata.js +1 -1
  44. package/lib/cloudflare/worker-metadata.js.map +1 -1
  45. package/lib/cloudflare/worker.d.ts +40 -1
  46. package/lib/cloudflare/worker.d.ts.map +1 -1
  47. package/lib/cloudflare/worker.js +40 -38
  48. package/lib/cloudflare/worker.js.map +1 -1
  49. package/lib/cloudflare/wrangler.json.js +1 -1
  50. package/lib/cloudflare/wrangler.json.js.map +1 -1
  51. package/lib/fs/file-system-state-store.d.ts +3 -1
  52. package/lib/fs/file-system-state-store.d.ts.map +1 -1
  53. package/lib/fs/file-system-state-store.js +2 -2
  54. package/lib/fs/file-system-state-store.js.map +1 -1
  55. package/lib/stripe/card.d.ts +171 -0
  56. package/lib/stripe/card.d.ts.map +1 -0
  57. package/lib/stripe/card.js +148 -0
  58. package/lib/stripe/card.js.map +1 -0
  59. package/lib/stripe/client.d.ts +9 -0
  60. package/lib/stripe/client.d.ts.map +1 -0
  61. package/lib/stripe/client.js +94 -0
  62. package/lib/stripe/client.js.map +1 -0
  63. package/lib/stripe/coupon.d.ts +135 -0
  64. package/lib/stripe/coupon.d.ts.map +1 -0
  65. package/lib/stripe/coupon.js +161 -0
  66. package/lib/stripe/coupon.js.map +1 -0
  67. package/lib/stripe/customer.d.ts +269 -0
  68. package/lib/stripe/customer.d.ts.map +1 -0
  69. package/lib/stripe/customer.js +289 -0
  70. package/lib/stripe/customer.js.map +1 -0
  71. package/lib/stripe/entitlements-feature.d.ts +84 -0
  72. package/lib/stripe/entitlements-feature.d.ts.map +1 -0
  73. package/lib/stripe/entitlements-feature.js +102 -0
  74. package/lib/stripe/entitlements-feature.js.map +1 -0
  75. package/lib/stripe/file.d.ts +143 -0
  76. package/lib/stripe/file.d.ts.map +1 -0
  77. package/lib/stripe/file.js +97 -0
  78. package/lib/stripe/file.js.map +1 -0
  79. package/lib/stripe/index.d.ts +11 -0
  80. package/lib/stripe/index.d.ts.map +1 -1
  81. package/lib/stripe/index.js +11 -0
  82. package/lib/stripe/index.js.map +1 -1
  83. package/lib/stripe/meter.d.ts +10 -1
  84. package/lib/stripe/meter.d.ts.map +1 -1
  85. package/lib/stripe/meter.js +13 -7
  86. package/lib/stripe/meter.js.map +1 -1
  87. package/lib/stripe/portal-configuration.d.ts +286 -0
  88. package/lib/stripe/portal-configuration.d.ts.map +1 -0
  89. package/lib/stripe/portal-configuration.js +320 -0
  90. package/lib/stripe/portal-configuration.js.map +1 -0
  91. package/lib/stripe/price.d.ts +10 -1
  92. package/lib/stripe/price.d.ts.map +1 -1
  93. package/lib/stripe/price.js +34 -13
  94. package/lib/stripe/price.js.map +1 -1
  95. package/lib/stripe/product-feature.d.ts +76 -0
  96. package/lib/stripe/product-feature.d.ts.map +1 -0
  97. package/lib/stripe/product-feature.js +74 -0
  98. package/lib/stripe/product-feature.js.map +1 -0
  99. package/lib/stripe/product.d.ts +10 -1
  100. package/lib/stripe/product.d.ts.map +1 -1
  101. package/lib/stripe/product.js +33 -14
  102. package/lib/stripe/product.js.map +1 -1
  103. package/lib/stripe/promotion-code.d.ts +138 -0
  104. package/lib/stripe/promotion-code.d.ts.map +1 -0
  105. package/lib/stripe/promotion-code.js +150 -0
  106. package/lib/stripe/promotion-code.js.map +1 -0
  107. package/lib/stripe/shipping-rate.d.ts +188 -0
  108. package/lib/stripe/shipping-rate.d.ts.map +1 -0
  109. package/lib/stripe/shipping-rate.js +198 -0
  110. package/lib/stripe/shipping-rate.js.map +1 -0
  111. package/lib/stripe/tax-rate.d.ts +130 -0
  112. package/lib/stripe/tax-rate.d.ts.map +1 -0
  113. package/lib/stripe/tax-rate.js +126 -0
  114. package/lib/stripe/tax-rate.js.map +1 -0
  115. package/lib/stripe/webhook.d.ts +10 -1
  116. package/lib/stripe/webhook.d.ts.map +1 -1
  117. package/lib/stripe/webhook.js +59 -14
  118. package/lib/stripe/webhook.js.map +1 -1
  119. package/lib/test/bun.d.ts.map +1 -1
  120. package/lib/test/bun.js +2 -6
  121. package/lib/test/bun.js.map +1 -1
  122. package/lib/test/vitest.d.ts.map +1 -1
  123. package/lib/test/vitest.js +3 -7
  124. package/lib/test/vitest.js.map +1 -1
  125. package/lib/util/retry.d.ts +1 -1
  126. package/lib/util/retry.d.ts.map +1 -1
  127. package/lib/util/retry.js.map +1 -1
  128. package/package.json +2 -1
  129. package/src/aws/function.ts +3 -6
  130. package/src/cloudflare/bindings.ts +3 -2
  131. package/src/cloudflare/bound.ts +3 -3
  132. package/src/cloudflare/do-state-store/index.ts +1 -0
  133. package/src/cloudflare/do-state-store/internal.ts +215 -0
  134. package/src/cloudflare/do-state-store/store.ts +188 -0
  135. package/src/cloudflare/do-state-store/types.ts +74 -0
  136. package/src/cloudflare/do-state-store/worker.ts +199 -0
  137. package/src/cloudflare/index.ts +1 -0
  138. package/src/cloudflare/pipeline.ts +1 -1
  139. package/src/cloudflare/queue-consumer.ts +59 -1
  140. package/src/cloudflare/website.ts +38 -6
  141. package/src/cloudflare/worker-metadata.ts +1 -1
  142. package/src/cloudflare/worker.ts +66 -46
  143. package/src/cloudflare/wrangler.json.ts +1 -1
  144. package/src/fs/file-system-state-store.ts +7 -2
  145. package/src/stripe/card.ts +320 -0
  146. package/src/stripe/client.ts +145 -0
  147. package/src/stripe/coupon.ts +280 -0
  148. package/src/stripe/customer.ts +528 -0
  149. package/src/stripe/entitlements-feature.ts +169 -0
  150. package/src/stripe/file.ts +219 -0
  151. package/src/stripe/index.ts +11 -0
  152. package/src/stripe/meter.ts +23 -7
  153. package/src/stripe/portal-configuration.ts +609 -0
  154. package/src/stripe/price.ts +47 -14
  155. package/src/stripe/product-feature.ts +150 -0
  156. package/src/stripe/product.ts +48 -15
  157. package/src/stripe/promotion-code.ts +268 -0
  158. package/src/stripe/shipping-rate.ts +362 -0
  159. package/src/stripe/tax-rate.ts +218 -0
  160. package/src/stripe/webhook.ts +82 -15
  161. package/src/test/bun.ts +2 -7
  162. package/src/test/vitest.ts +3 -8
  163. package/src/util/retry.ts +1 -1
@@ -0,0 +1,145 @@
1
+ import Stripe from "stripe";
2
+ import type { Secret } from "../secret.ts";
3
+ import { withExponentialBackoff } from "../util/retry.ts";
4
+
5
+ export interface StripeClientOptions {
6
+ apiKey?: Secret | string;
7
+ }
8
+
9
+ export function createStripeClient(options: StripeClientOptions = {}): Stripe {
10
+ let apiKey: string;
11
+
12
+ if (options.apiKey) {
13
+ apiKey =
14
+ typeof options.apiKey === "string"
15
+ ? options.apiKey
16
+ : options.apiKey.unencrypted;
17
+ } else {
18
+ const envApiKey = process.env.STRIPE_API_KEY;
19
+ if (!envApiKey) {
20
+ throw new Error(
21
+ "Stripe API key is required. Provide it via the apiKey parameter or set the STRIPE_API_KEY environment variable.",
22
+ );
23
+ }
24
+ apiKey = envApiKey;
25
+ }
26
+
27
+ const stripe = new Stripe(apiKey);
28
+ return createStripeProxy(stripe);
29
+ }
30
+
31
+ export function handleStripeDeleteError(
32
+ error: any,
33
+ resourceType: string,
34
+ resourceId?: string,
35
+ ): void {
36
+ if (error?.code === "resource_missing" || error?.status === 404) {
37
+ console.log(
38
+ `${resourceType} ${resourceId || "unknown"} not found during deletion (already deleted)`,
39
+ );
40
+ return;
41
+ }
42
+
43
+ console.error(
44
+ `Error deleting ${resourceType} ${resourceId || "unknown"}:`,
45
+ error,
46
+ );
47
+ throw error;
48
+ }
49
+
50
+ export function isStripeConflictError(error: any): boolean {
51
+ return error?.status === 409 || error?.statusCode === 409;
52
+ }
53
+
54
+ /**
55
+ * Creates a proxy that wraps Stripe API calls with retry logic
56
+ */
57
+ function createStripeProxy(stripe: Stripe): Stripe {
58
+ // Cache for proxied resources to avoid creating new proxies for the same resource
59
+ const resourceProxyCache = new Map<string, any>();
60
+
61
+ return new Proxy(stripe, {
62
+ get(target, prop, receiver) {
63
+ const value = Reflect.get(target, prop, receiver);
64
+
65
+ // If it's not a property we want to proxy, return as-is
66
+ if (
67
+ typeof prop !== "string" ||
68
+ typeof value !== "object" ||
69
+ value === null
70
+ ) {
71
+ return value;
72
+ }
73
+
74
+ // Check if this looks like a Stripe resource (has methods like create, retrieve, etc.)
75
+ const hasStripeResourceMethods =
76
+ value &&
77
+ typeof value === "object" &&
78
+ (typeof value.create === "function" ||
79
+ typeof value.retrieve === "function" ||
80
+ typeof value.list === "function" ||
81
+ typeof value.update === "function" ||
82
+ typeof value.del === "function");
83
+
84
+ if (!hasStripeResourceMethods) {
85
+ return value;
86
+ }
87
+
88
+ // Return cached proxy if it exists
89
+ if (resourceProxyCache.has(prop)) {
90
+ return resourceProxyCache.get(prop);
91
+ }
92
+
93
+ // Create a new proxy for this resource
94
+ const resourceProxy = new Proxy(value, {
95
+ get(resourceTarget, methodName, resourceReceiver) {
96
+ const method = Reflect.get(
97
+ resourceTarget,
98
+ methodName,
99
+ resourceReceiver,
100
+ );
101
+
102
+ // If it's not a function, return as-is
103
+ if (typeof method !== "function") {
104
+ return method;
105
+ }
106
+
107
+ // Wrap the method with retry logic
108
+ return (...args: any[]) =>
109
+ withStripeRetry(() => method.apply(resourceTarget, args));
110
+ },
111
+ });
112
+
113
+ // Cache the proxy
114
+ resourceProxyCache.set(prop, resourceProxy);
115
+ return resourceProxy;
116
+ },
117
+ });
118
+ }
119
+
120
+ /**
121
+ * Determines if a Stripe error should trigger a retry
122
+ */
123
+ function isStripeRetryableError(error: any): boolean {
124
+ return (
125
+ error?.raw?.statusCode === 429 ||
126
+ error?.code === "rate_limit" ||
127
+ error?.type === "rate_limit_error"
128
+ );
129
+ }
130
+
131
+ /**
132
+ * Wraps a Stripe API operation with retry logic for rate limiting
133
+ */
134
+ async function withStripeRetry<T>(
135
+ operation: () => Promise<T>,
136
+ maxAttempts = 5,
137
+ initialDelayMs = 1000,
138
+ ): Promise<T> {
139
+ return withExponentialBackoff(
140
+ operation,
141
+ isStripeRetryableError,
142
+ maxAttempts,
143
+ initialDelayMs,
144
+ );
145
+ }
@@ -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
+ );