autumn-js 0.0.54 → 0.0.55-beta.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.
@@ -0,0 +1,7 @@
1
+ import { AuthResult } from './utils/AuthFunction.mjs';
2
+
3
+ declare function autumnHandler(options: {
4
+ identify: (request: Request) => AuthResult;
5
+ }): (request: Request) => Promise<Response>;
6
+
7
+ export { autumnHandler };
@@ -0,0 +1,7 @@
1
+ import { AuthResult } from './utils/AuthFunction.js';
2
+
3
+ declare function autumnHandler(options: {
4
+ identify: (request: Request) => AuthResult;
5
+ }): (request: Request) => Promise<Response>;
6
+
7
+ export { autumnHandler };
@@ -0,0 +1,836 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/libraries/backend/supabase.ts
21
+ var supabase_exports = {};
22
+ __export(supabase_exports, {
23
+ autumnHandler: () => autumnHandler
24
+ });
25
+ module.exports = __toCommonJS(supabase_exports);
26
+ var import_rou35 = require("rou3");
27
+
28
+ // src/sdk/error.ts
29
+ var AutumnError = class extends Error {
30
+ message;
31
+ code;
32
+ constructor(response) {
33
+ super(response.message);
34
+ this.message = response.message;
35
+ this.code = response.code;
36
+ }
37
+ toString() {
38
+ return `${this.message} (code: ${this.code})`;
39
+ }
40
+ toJSON() {
41
+ return {
42
+ message: this.message,
43
+ code: this.code
44
+ };
45
+ }
46
+ };
47
+
48
+ // src/sdk/utils.ts
49
+ var staticWrapper = (callback, instance, args) => {
50
+ if (!instance) {
51
+ instance = new Autumn();
52
+ }
53
+ return callback({ instance, ...args });
54
+ };
55
+
56
+ // src/sdk/customers/cusMethods.ts
57
+ var customerMethods = (instance) => {
58
+ return {
59
+ get: (id, params) => staticWrapper(getCustomer, instance, { id, params }),
60
+ create: (params) => staticWrapper(createCustomer, instance, { params }),
61
+ update: (id, params) => staticWrapper(updateCustomer, instance, { id, params }),
62
+ delete: (id) => staticWrapper(deleteCustomer, instance, { id }),
63
+ billingPortal: (id, params) => staticWrapper(billingPortal, instance, { id, params })
64
+ };
65
+ };
66
+ var getExpandStr = (expand) => {
67
+ if (!expand) {
68
+ return "";
69
+ }
70
+ return `expand=${expand.join(",")}`;
71
+ };
72
+ var getCustomer = async ({
73
+ instance,
74
+ id,
75
+ params
76
+ }) => {
77
+ if (!id) {
78
+ return {
79
+ data: null,
80
+ error: new AutumnError({
81
+ message: "Customer ID is required",
82
+ code: "CUSTOMER_ID_REQUIRED"
83
+ })
84
+ };
85
+ }
86
+ return instance.get(`/customers/${id}?${getExpandStr(params?.expand)}`);
87
+ };
88
+ var createCustomer = async ({
89
+ instance,
90
+ params
91
+ }) => {
92
+ return instance.post(`/customers?${getExpandStr(params?.expand)}`, params);
93
+ };
94
+ var updateCustomer = async ({
95
+ instance,
96
+ id,
97
+ params
98
+ }) => {
99
+ return instance.post(`/customers/${id}`, params);
100
+ };
101
+ var deleteCustomer = async ({
102
+ instance,
103
+ id
104
+ }) => {
105
+ return instance.delete(`/customers/${id}`);
106
+ };
107
+ var billingPortal = async ({
108
+ instance,
109
+ id,
110
+ params
111
+ }) => {
112
+ return instance.post(`/customers/${id}/billing_portal`, params);
113
+ };
114
+
115
+ // src/sdk/customers/entities/entMethods.ts
116
+ var entityMethods = (instance) => {
117
+ return {
118
+ get: (customer_id, entity_id, params) => staticWrapper(getEntity, instance, {
119
+ customer_id,
120
+ entity_id,
121
+ params
122
+ }),
123
+ create: (customer_id, params) => staticWrapper(createEntity, instance, { customer_id, params }),
124
+ delete: (customer_id, entity_id) => staticWrapper(deleteEntity, instance, { customer_id, entity_id })
125
+ };
126
+ };
127
+ var getExpandStr2 = (expand) => {
128
+ if (!expand) {
129
+ return "";
130
+ }
131
+ return `expand=${expand.join(",")}`;
132
+ };
133
+ var getEntity = async ({
134
+ instance,
135
+ customer_id,
136
+ entity_id,
137
+ params
138
+ }) => {
139
+ return instance.get(
140
+ `/customers/${customer_id}/entities/${entity_id}?${getExpandStr2(
141
+ params?.expand
142
+ )}`
143
+ );
144
+ };
145
+ var createEntity = async ({
146
+ instance,
147
+ customer_id,
148
+ params
149
+ }) => {
150
+ return instance.post(`/customers/${customer_id}/entities`, params);
151
+ };
152
+ var deleteEntity = async ({
153
+ instance,
154
+ customer_id,
155
+ entity_id
156
+ }) => {
157
+ return instance.delete(`/customers/${customer_id}/entities/${entity_id}`);
158
+ };
159
+
160
+ // src/sdk/general/genMethods.ts
161
+ var handleAttach = async ({
162
+ instance,
163
+ params
164
+ }) => {
165
+ return instance.post("/attach", params);
166
+ };
167
+ var handleCancel = async ({
168
+ instance,
169
+ params
170
+ }) => {
171
+ return instance.post("/cancel", params);
172
+ };
173
+ var handleEntitled = async ({
174
+ instance,
175
+ params
176
+ }) => {
177
+ return instance.post("/entitled", params);
178
+ };
179
+ var handleEvent = async ({
180
+ instance,
181
+ params
182
+ }) => {
183
+ return instance.post("/events", params);
184
+ };
185
+ var handleTrack = async ({
186
+ instance,
187
+ params
188
+ }) => {
189
+ return instance.post("/track", params);
190
+ };
191
+ var handleUsage = async ({
192
+ instance,
193
+ params
194
+ }) => {
195
+ return instance.post("/usage", params);
196
+ };
197
+ var handleCheck = async ({
198
+ instance,
199
+ params
200
+ }) => {
201
+ return instance.post("/check", params);
202
+ };
203
+
204
+ // src/sdk/products/prodMethods.ts
205
+ var productMethods = (instance) => {
206
+ return {
207
+ get: (id) => staticWrapper(getProduct, instance, { id }),
208
+ create: (params) => staticWrapper(createProduct, instance, { params }),
209
+ list: (params) => staticWrapper(listProducts, instance, { params })
210
+ };
211
+ };
212
+ var listProducts = async ({
213
+ instance,
214
+ params
215
+ }) => {
216
+ let path = "/products";
217
+ if (params) {
218
+ const queryParams = new URLSearchParams();
219
+ for (const [key, value] of Object.entries(params)) {
220
+ if (value !== void 0) {
221
+ queryParams.append(key, String(value));
222
+ }
223
+ }
224
+ const queryString = queryParams.toString();
225
+ if (queryString) {
226
+ path += `?${queryString}`;
227
+ }
228
+ }
229
+ return instance.get(path);
230
+ };
231
+ var getProduct = async ({
232
+ instance,
233
+ id
234
+ }) => {
235
+ return instance.get(`/products/${id}`);
236
+ };
237
+ var createProduct = async ({
238
+ instance,
239
+ params
240
+ }) => {
241
+ return instance.post("/products", params);
242
+ };
243
+
244
+ // src/sdk/referrals/referralMethods.ts
245
+ var referralMethods = (instance) => {
246
+ return {
247
+ createCode: (params) => staticWrapper(createReferralCode, instance, { params }),
248
+ redeemCode: (params) => staticWrapper(redeemReferralCode, instance, { params })
249
+ };
250
+ };
251
+ var createReferralCode = async ({
252
+ instance,
253
+ params
254
+ }) => {
255
+ return instance.post("/referrals/code", params);
256
+ };
257
+ var redeemReferralCode = async ({
258
+ instance,
259
+ params
260
+ }) => {
261
+ return instance.post("/referrals/redeem", params);
262
+ };
263
+
264
+ // src/sdk/response.ts
265
+ var toContainerResult = async (response) => {
266
+ if (response.status < 200 || response.status >= 300) {
267
+ let error;
268
+ try {
269
+ error = await response.json();
270
+ } catch (error2) {
271
+ return {
272
+ data: null,
273
+ error: new AutumnError({
274
+ message: "Failed to parse JSON response from Autumn",
275
+ code: "internal_error"
276
+ }),
277
+ statusCode: response.status
278
+ };
279
+ }
280
+ return {
281
+ data: null,
282
+ error: new AutumnError({
283
+ message: error.message,
284
+ code: error.code
285
+ }),
286
+ statusCode: response.status
287
+ };
288
+ }
289
+ try {
290
+ let data = await response.json();
291
+ return {
292
+ data,
293
+ error: null,
294
+ statusCode: response?.status
295
+ };
296
+ } catch (error) {
297
+ return {
298
+ data: null,
299
+ error: new AutumnError({
300
+ message: "Failed to parse Autumn API response",
301
+ code: "internal_error"
302
+ }),
303
+ statusCode: response?.status
304
+ };
305
+ }
306
+ };
307
+
308
+ // src/sdk/client.ts
309
+ var LATEST_API_VERSION = "1.2";
310
+ var Autumn = class {
311
+ secretKey;
312
+ publishableKey;
313
+ level;
314
+ headers;
315
+ url;
316
+ constructor(options) {
317
+ try {
318
+ this.secretKey = options?.secretKey || process.env.AUTUMN_SECRET_KEY;
319
+ this.publishableKey = options?.publishableKey || process.env.AUTUMN_PUBLISHABLE_KEY;
320
+ } catch (error) {
321
+ }
322
+ if (!this.secretKey && !this.publishableKey) {
323
+ throw new Error("Autumn secret key or publishable key is required");
324
+ }
325
+ this.headers = {
326
+ Authorization: `Bearer ${this.secretKey || this.publishableKey}`,
327
+ "Content-Type": "application/json"
328
+ };
329
+ let version = options?.version || LATEST_API_VERSION;
330
+ this.headers["x-api-version"] = version;
331
+ this.url = options?.url || "https://api.useautumn.com/v1";
332
+ this.level = this.secretKey ? "secret" : "publishable";
333
+ }
334
+ getLevel() {
335
+ return this.level;
336
+ }
337
+ async get(path) {
338
+ const response = await fetch(`${this.url}${path}`, {
339
+ headers: this.headers
340
+ });
341
+ return toContainerResult(response);
342
+ }
343
+ async post(path, body) {
344
+ const response = await fetch(`${this.url}${path}`, {
345
+ method: "POST",
346
+ headers: this.headers,
347
+ body: JSON.stringify(body)
348
+ });
349
+ return toContainerResult(response);
350
+ }
351
+ async delete(path) {
352
+ const response = await fetch(`${this.url}${path}`, {
353
+ method: "DELETE",
354
+ headers: this.headers
355
+ });
356
+ return toContainerResult(response);
357
+ }
358
+ static customers = customerMethods();
359
+ static products = productMethods();
360
+ static entities = entityMethods();
361
+ static referrals = referralMethods();
362
+ customers = customerMethods(this);
363
+ products = productMethods(this);
364
+ entities = entityMethods(this);
365
+ referrals = referralMethods(this);
366
+ static attach = (params) => staticWrapper(handleAttach, void 0, { params });
367
+ static usage = (params) => staticWrapper(handleUsage, void 0, { params });
368
+ async attach(params) {
369
+ return handleAttach({
370
+ instance: this,
371
+ params
372
+ });
373
+ }
374
+ static cancel = (params) => staticWrapper(handleCancel, void 0, { params });
375
+ async cancel(params) {
376
+ return handleCancel({
377
+ instance: this,
378
+ params
379
+ });
380
+ }
381
+ /**
382
+ * @deprecated This method is deprecated and will be removed in a future version.
383
+ * Please use the new check() method instead.
384
+ */
385
+ static entitled = (params) => staticWrapper(handleEntitled, void 0, { params });
386
+ /**
387
+ * @deprecated This method is deprecated and will be removed in a future version.
388
+ * Please use the new check() method instead.
389
+ */
390
+ async entitled(params) {
391
+ return handleEntitled({
392
+ instance: this,
393
+ params
394
+ });
395
+ }
396
+ static check = (params) => staticWrapper(handleCheck, void 0, { params });
397
+ async check(params) {
398
+ return handleCheck({
399
+ instance: this,
400
+ params
401
+ });
402
+ }
403
+ /**
404
+ * @deprecated This method is deprecated and will be removed in a future version.
405
+ * Please use the new track() method instead.
406
+ */
407
+ static event = (params) => staticWrapper(handleEvent, void 0, { params });
408
+ /**
409
+ * @deprecated This method is deprecated and will be removed in a future version.
410
+ * Please use the new track() method instead.
411
+ */
412
+ async event(params) {
413
+ return handleEvent({
414
+ instance: this,
415
+ params
416
+ });
417
+ }
418
+ static track = (params) => staticWrapper(handleTrack, void 0, { params });
419
+ async track(params) {
420
+ return handleTrack({
421
+ instance: this,
422
+ params
423
+ });
424
+ }
425
+ async usage(params) {
426
+ return handleUsage({
427
+ instance: this,
428
+ params
429
+ });
430
+ }
431
+ };
432
+
433
+ // src/sdk/components/componentMethods.ts
434
+ var fetchPricingTable = async ({
435
+ instance,
436
+ params
437
+ }) => {
438
+ let path = "/components/pricing_table";
439
+ if (params) {
440
+ const queryParams = new URLSearchParams();
441
+ for (const [key, value] of Object.entries(params)) {
442
+ if (key === "products") {
443
+ continue;
444
+ }
445
+ if (value !== void 0) {
446
+ queryParams.append(key, String(value));
447
+ }
448
+ }
449
+ const queryString = queryParams.toString();
450
+ if (queryString) {
451
+ path += `?${queryString}`;
452
+ }
453
+ }
454
+ return await instance.get(path);
455
+ };
456
+
457
+ // src/libraries/backend/utils/backendRes.ts
458
+ var toBackendRes = ({ res }) => {
459
+ let statusCode = res.statusCode ? res.statusCode : res.error ? 500 : 200;
460
+ return {
461
+ body: res.data ? res.data : res.error,
462
+ statusCode
463
+ };
464
+ };
465
+ var toBackendError = ({
466
+ path,
467
+ message,
468
+ code,
469
+ statusCode = 500
470
+ }) => {
471
+ console.error(
472
+ `Autumn middleware error: ${message} (code: ${code}) | path: ${path}`
473
+ );
474
+ return {
475
+ statusCode,
476
+ body: new AutumnError({
477
+ message: message || "Internal server error",
478
+ code: code || "internal_server_error"
479
+ })
480
+ };
481
+ };
482
+
483
+ // src/libraries/backend/utils/withAuth.ts
484
+ var withAuth = ({
485
+ fn,
486
+ requireCustomer = true
487
+ }) => {
488
+ return async ({
489
+ autumn,
490
+ body,
491
+ path,
492
+ getCustomer: getCustomer2,
493
+ pathParams,
494
+ searchParams
495
+ }) => {
496
+ let authResult = await getCustomer2();
497
+ let customerId = authResult?.customerId;
498
+ if (!customerId && requireCustomer) {
499
+ if (body?.errorOnNotFound === false) {
500
+ return toBackendError({
501
+ path,
502
+ message: "No customer ID found",
503
+ code: "no_customer_id",
504
+ statusCode: 202
505
+ });
506
+ } else {
507
+ return toBackendError({
508
+ path,
509
+ message: "No customer ID found",
510
+ code: "no_customer_id",
511
+ statusCode: 401
512
+ });
513
+ }
514
+ }
515
+ let cusData = authResult?.customerData || body?.customer_data;
516
+ try {
517
+ let res = await fn({
518
+ body,
519
+ autumn,
520
+ customer_id: customerId,
521
+ customer_data: cusData,
522
+ pathParams,
523
+ searchParams
524
+ });
525
+ return toBackendRes({ res });
526
+ } catch (error) {
527
+ return toBackendError({
528
+ path,
529
+ message: error.message || "unknown error",
530
+ code: "internal_error"
531
+ });
532
+ }
533
+ };
534
+ };
535
+
536
+ // src/libraries/backend/routes/genRoutes.ts
537
+ var import_rou3 = require("rou3");
538
+
539
+ // src/libraries/backend/constants.ts
540
+ var autumnApiUrl = "https://api.useautumn.com/v1";
541
+ var BASE_PATH = "/api/autumn";
542
+
543
+ // src/libraries/backend/routes/genRoutes.ts
544
+ var sanitizeBody = (body) => {
545
+ let bodyCopy = { ...body };
546
+ delete bodyCopy.customer_id;
547
+ delete bodyCopy.customer_data;
548
+ return bodyCopy;
549
+ };
550
+ var attachHandler = withAuth({
551
+ fn: async ({
552
+ autumn,
553
+ customer_id,
554
+ customer_data,
555
+ body
556
+ }) => {
557
+ return await autumn.attach({
558
+ ...sanitizeBody(body),
559
+ customer_id,
560
+ customer_data
561
+ });
562
+ }
563
+ });
564
+ var cancelHandler = withAuth({
565
+ fn: async ({
566
+ autumn,
567
+ customer_id,
568
+ body
569
+ }) => {
570
+ return await autumn.cancel({
571
+ ...sanitizeBody(body),
572
+ customer_id
573
+ });
574
+ }
575
+ });
576
+ var checkHandler = withAuth({
577
+ fn: async ({
578
+ autumn,
579
+ customer_id,
580
+ customer_data,
581
+ body
582
+ }) => {
583
+ return await autumn.check({
584
+ ...sanitizeBody(body),
585
+ customer_id,
586
+ customer_data
587
+ });
588
+ }
589
+ });
590
+ var trackHandler = withAuth({
591
+ fn: async ({
592
+ autumn,
593
+ customer_id,
594
+ customer_data,
595
+ body
596
+ }) => {
597
+ return await autumn.track({
598
+ ...sanitizeBody(body),
599
+ customer_id,
600
+ customer_data
601
+ });
602
+ }
603
+ });
604
+ var openBillingPortalHandler = withAuth({
605
+ fn: async ({
606
+ autumn,
607
+ customer_id,
608
+ body
609
+ }) => {
610
+ return await autumn.customers.billingPortal(customer_id, body);
611
+ }
612
+ });
613
+ var addGenRoutes = (router) => {
614
+ (0, import_rou3.addRoute)(router, "POST", `${BASE_PATH}/attach`, {
615
+ handler: attachHandler
616
+ });
617
+ (0, import_rou3.addRoute)(router, "POST", `${BASE_PATH}/cancel`, {
618
+ handler: cancelHandler
619
+ });
620
+ (0, import_rou3.addRoute)(router, "POST", `${BASE_PATH}/check`, {
621
+ handler: checkHandler
622
+ });
623
+ (0, import_rou3.addRoute)(router, "POST", `${BASE_PATH}/track`, {
624
+ handler: trackHandler
625
+ });
626
+ (0, import_rou3.addRoute)(router, "POST", `${BASE_PATH}/billing_portal`, {
627
+ handler: openBillingPortalHandler
628
+ });
629
+ };
630
+
631
+ // src/libraries/backend/routes/backendRouter.ts
632
+ var import_rou34 = require("rou3");
633
+
634
+ // src/libraries/backend/routes/entityRoutes.ts
635
+ var import_rou32 = require("rou3");
636
+ var createEntityHandler = withAuth({
637
+ fn: async ({
638
+ autumn,
639
+ customer_id,
640
+ body
641
+ }) => {
642
+ return await autumn.entities.create(customer_id, body);
643
+ }
644
+ });
645
+ var getEntityHandler = withAuth({
646
+ fn: async ({
647
+ autumn,
648
+ customer_id,
649
+ pathParams,
650
+ searchParams
651
+ }) => {
652
+ if (!pathParams?.entityId) {
653
+ return {
654
+ statusCode: 400,
655
+ body: {
656
+ error: "no_entity_id",
657
+ message: "Entity ID is required"
658
+ }
659
+ };
660
+ }
661
+ let params = {
662
+ expand: searchParams?.expand?.split(",")
663
+ };
664
+ let res = await autumn.entities.get(
665
+ customer_id,
666
+ pathParams.entityId,
667
+ params
668
+ );
669
+ return res;
670
+ }
671
+ });
672
+ var deleteEntityHandler = withAuth({
673
+ fn: async ({
674
+ autumn,
675
+ customer_id,
676
+ pathParams
677
+ }) => {
678
+ if (!pathParams?.entityId) {
679
+ return {
680
+ statusCode: 400,
681
+ body: {
682
+ error: "no_entity_id",
683
+ message: "Entity ID is required"
684
+ }
685
+ };
686
+ }
687
+ return await autumn.entities.delete(customer_id, pathParams.entityId);
688
+ }
689
+ });
690
+ var addEntityRoutes = async (router) => {
691
+ (0, import_rou32.addRoute)(router, "POST", "/api/autumn/entities", {
692
+ handler: createEntityHandler
693
+ });
694
+ (0, import_rou32.addRoute)(router, "GET", "/api/autumn/entities/:entityId", {
695
+ handler: getEntityHandler
696
+ });
697
+ (0, import_rou32.addRoute)(router, "DELETE", "/api/autumn/entities/:entityId", {
698
+ handler: deleteEntityHandler
699
+ });
700
+ };
701
+
702
+ // src/libraries/backend/routes/referralRoutes.ts
703
+ var import_rou33 = require("rou3");
704
+ var createReferralCodeHandler = withAuth({
705
+ fn: async ({
706
+ autumn,
707
+ customer_id,
708
+ body
709
+ }) => {
710
+ return await autumn.referrals.createCode({
711
+ ...body,
712
+ customer_id
713
+ });
714
+ }
715
+ });
716
+ var redeemReferralCodeHandler = withAuth({
717
+ fn: async ({
718
+ autumn,
719
+ customer_id,
720
+ body
721
+ }) => {
722
+ return await autumn.referrals.redeemCode({
723
+ ...body,
724
+ customer_id
725
+ });
726
+ }
727
+ });
728
+ var addReferralRoutes = async (router) => {
729
+ (0, import_rou33.addRoute)(router, "POST", `${BASE_PATH}/referrals/code`, {
730
+ handler: createReferralCodeHandler
731
+ });
732
+ (0, import_rou33.addRoute)(router, "POST", `${BASE_PATH}/referrals/redeem`, {
733
+ handler: redeemReferralCodeHandler
734
+ });
735
+ };
736
+
737
+ // src/libraries/backend/routes/backendRouter.ts
738
+ var sanitizeCustomerBody = (body) => {
739
+ let bodyCopy = { ...body };
740
+ delete bodyCopy.id;
741
+ delete bodyCopy.name;
742
+ delete bodyCopy.email;
743
+ return bodyCopy;
744
+ };
745
+ var createCustomerHandler = withAuth({
746
+ fn: async ({
747
+ autumn,
748
+ customer_id,
749
+ customer_data = {},
750
+ body
751
+ }) => {
752
+ console.log;
753
+ let res = await autumn.customers.create({
754
+ id: customer_id,
755
+ ...customer_data,
756
+ ...sanitizeCustomerBody(body)
757
+ });
758
+ return res;
759
+ }
760
+ });
761
+ var getPricingTableHandler = withAuth({
762
+ fn: async ({
763
+ autumn,
764
+ customer_id
765
+ }) => {
766
+ return await fetchPricingTable({
767
+ instance: autumn,
768
+ params: {
769
+ customer_id: customer_id || void 0
770
+ }
771
+ });
772
+ },
773
+ requireCustomer: false
774
+ });
775
+ var createRouterWithOptions = ({ autumn }) => {
776
+ const router = (0, import_rou34.createRouter)();
777
+ (0, import_rou34.addRoute)(router, "POST", `${BASE_PATH}/customers`, {
778
+ handler: createCustomerHandler
779
+ });
780
+ (0, import_rou34.addRoute)(router, "GET", `${BASE_PATH}/components/pricing_table`, {
781
+ handler: getPricingTableHandler,
782
+ requireCustomer: false
783
+ });
784
+ addGenRoutes(router);
785
+ addEntityRoutes(router);
786
+ addReferralRoutes(router);
787
+ return router;
788
+ };
789
+
790
+ // src/libraries/backend/supabase.ts
791
+ function autumnHandler(options) {
792
+ const autumn = new Autumn({
793
+ url: autumnApiUrl
794
+ });
795
+ const router = createRouterWithOptions({
796
+ autumn
797
+ });
798
+ return async function handler(request) {
799
+ const method = request.method;
800
+ const url = new URL(request.url);
801
+ const searchParams = Object.fromEntries(url.searchParams);
802
+ const pathname = url.pathname;
803
+ const match = (0, import_rou35.findRoute)(router, method, pathname);
804
+ if (!match) {
805
+ return new Response(JSON.stringify({ error: "Not found" }), {
806
+ status: 404,
807
+ headers: { "Content-Type": "application/json" }
808
+ });
809
+ }
810
+ const { data, params: pathParams } = match;
811
+ const { handler: routeHandler } = data;
812
+ let body = null;
813
+ if (method === "POST" || method === "PUT" || method === "PATCH") {
814
+ try {
815
+ body = await request.json();
816
+ } catch (error) {
817
+ }
818
+ }
819
+ const result = await routeHandler({
820
+ autumn,
821
+ body,
822
+ path: url.pathname,
823
+ getCustomer: async () => await options.identify(request),
824
+ pathParams,
825
+ searchParams
826
+ });
827
+ return new Response(JSON.stringify(result.body), {
828
+ status: result.statusCode,
829
+ headers: { "Content-Type": "application/json" }
830
+ });
831
+ };
832
+ }
833
+ // Annotate the CommonJS export names for ESM import in node:
834
+ 0 && (module.exports = {
835
+ autumnHandler
836
+ });
@@ -0,0 +1,62 @@
1
+ import {
2
+ createRouterWithOptions
3
+ } from "./chunk-AMBZYTL6.mjs";
4
+ import "./chunk-EWE7GADM.mjs";
5
+ import "./chunk-I45FNCR5.mjs";
6
+ import "./chunk-ONFOZVN3.mjs";
7
+ import "./chunk-3OKYXLTN.mjs";
8
+ import {
9
+ Autumn
10
+ } from "./chunk-YQVUFNOA.mjs";
11
+ import {
12
+ autumnApiUrl
13
+ } from "./chunk-KSG3E4Q2.mjs";
14
+ import "./chunk-6DZX6EAA.mjs";
15
+
16
+ // src/libraries/backend/supabase.ts
17
+ import { findRoute } from "rou3";
18
+ function autumnHandler(options) {
19
+ const autumn = new Autumn({
20
+ url: autumnApiUrl
21
+ });
22
+ const router = createRouterWithOptions({
23
+ autumn
24
+ });
25
+ return async function handler(request) {
26
+ const method = request.method;
27
+ const url = new URL(request.url);
28
+ const searchParams = Object.fromEntries(url.searchParams);
29
+ const pathname = url.pathname;
30
+ const match = findRoute(router, method, pathname);
31
+ if (!match) {
32
+ return new Response(JSON.stringify({ error: "Not found" }), {
33
+ status: 404,
34
+ headers: { "Content-Type": "application/json" }
35
+ });
36
+ }
37
+ const { data, params: pathParams } = match;
38
+ const { handler: routeHandler } = data;
39
+ let body = null;
40
+ if (method === "POST" || method === "PUT" || method === "PATCH") {
41
+ try {
42
+ body = await request.json();
43
+ } catch (error) {
44
+ }
45
+ }
46
+ const result = await routeHandler({
47
+ autumn,
48
+ body,
49
+ path: url.pathname,
50
+ getCustomer: async () => await options.identify(request),
51
+ pathParams,
52
+ searchParams
53
+ });
54
+ return new Response(JSON.stringify(result.body), {
55
+ status: result.statusCode,
56
+ headers: { "Content-Type": "application/json" }
57
+ });
58
+ };
59
+ }
60
+ export {
61
+ autumnHandler
62
+ };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "autumn-js",
3
3
  "description": "Autumn JS Library",
4
- "version": "0.0.54",
4
+ "version": "0.0.55-beta.1",
5
5
  "main": "./dist/index.js",
6
6
  "scripts": {
7
7
  "cli": "tsx src/cli/index.ts",
@@ -52,6 +52,11 @@
52
52
  "types": "./dist/libraries/react/index.d.ts",
53
53
  "require": "./dist/libraries/react/index.js",
54
54
  "import": "./dist/libraries/react/index.mjs"
55
+ },
56
+ "./supabase": {
57
+ "types": "./dist/libraries/backend/supabase.d.ts",
58
+ "require": "./dist/libraries/backend/supabase.js",
59
+ "import": "./dist/libraries/backend/supabase.mjs"
55
60
  }
56
61
  },
57
62
  "keywords": [
@@ -113,6 +118,9 @@
113
118
  ],
114
119
  "react": [
115
120
  "./dist/libraries/react/index.d.ts"
121
+ ],
122
+ "supabase": [
123
+ "./dist/libraries/backend/supabase.d.ts"
116
124
  ]
117
125
  }
118
126
  }