@hintergrund/worker 0.1.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 (122) hide show
  1. package/README.md +45 -0
  2. package/dist/api/builder/handlers/generate.handler.d.ts +12 -0
  3. package/dist/api/builder/handlers/generate.handler.d.ts.map +1 -0
  4. package/dist/api/builder/handlers/generate.handler.js +203 -0
  5. package/dist/api/builder/handlers/template.handler.d.ts +12 -0
  6. package/dist/api/builder/handlers/template.handler.d.ts.map +1 -0
  7. package/dist/api/builder/handlers/template.handler.js +208 -0
  8. package/dist/api/builder/routes.d.ts +7 -0
  9. package/dist/api/builder/routes.d.ts.map +1 -0
  10. package/dist/api/builder/routes.js +15 -0
  11. package/dist/api/cms/handlers/assets.handler.d.ts +8 -0
  12. package/dist/api/cms/handlers/assets.handler.d.ts.map +1 -0
  13. package/dist/api/cms/handlers/assets.handler.js +299 -0
  14. package/dist/api/cms/handlers/auth.handler.d.ts +11 -0
  15. package/dist/api/cms/handlers/auth.handler.d.ts.map +1 -0
  16. package/dist/api/cms/handlers/auth.handler.js +214 -0
  17. package/dist/api/cms/handlers/config.handler.d.ts +3 -0
  18. package/dist/api/cms/handlers/config.handler.d.ts.map +1 -0
  19. package/dist/api/cms/handlers/config.handler.js +88 -0
  20. package/dist/api/cms/handlers/content.handler.d.ts +10 -0
  21. package/dist/api/cms/handlers/content.handler.d.ts.map +1 -0
  22. package/dist/api/cms/handlers/content.handler.js +293 -0
  23. package/dist/api/cms/handlers/dashboard.handler.d.ts +6 -0
  24. package/dist/api/cms/handlers/dashboard.handler.d.ts.map +1 -0
  25. package/dist/api/cms/handlers/dashboard.handler.js +15 -0
  26. package/dist/api/cms/routes/assets.routes.d.ts +4 -0
  27. package/dist/api/cms/routes/assets.routes.d.ts.map +1 -0
  28. package/dist/api/cms/routes/assets.routes.js +13 -0
  29. package/dist/api/cms/routes/auth.routes.d.ts +4 -0
  30. package/dist/api/cms/routes/auth.routes.d.ts.map +1 -0
  31. package/dist/api/cms/routes/auth.routes.js +7 -0
  32. package/dist/api/cms/routes/content.routes.d.ts +4 -0
  33. package/dist/api/cms/routes/content.routes.d.ts.map +1 -0
  34. package/dist/api/cms/routes/content.routes.js +11 -0
  35. package/dist/api/cms/routes/index.d.ts +4 -0
  36. package/dist/api/cms/routes/index.d.ts.map +1 -0
  37. package/dist/api/cms/routes/index.js +16 -0
  38. package/dist/api/newsletter/handlers/email.handler.d.ts +7 -0
  39. package/dist/api/newsletter/handlers/email.handler.d.ts.map +1 -0
  40. package/dist/api/newsletter/handlers/email.handler.js +102 -0
  41. package/dist/api/newsletter/handlers/emails.handler.d.ts +21 -0
  42. package/dist/api/newsletter/handlers/emails.handler.d.ts.map +1 -0
  43. package/dist/api/newsletter/handlers/emails.handler.js +134 -0
  44. package/dist/api/newsletter/handlers/public.handler.d.ts +22 -0
  45. package/dist/api/newsletter/handlers/public.handler.d.ts.map +1 -0
  46. package/dist/api/newsletter/handlers/public.handler.js +242 -0
  47. package/dist/api/newsletter/handlers/send.handler.d.ts +12 -0
  48. package/dist/api/newsletter/handlers/send.handler.d.ts.map +1 -0
  49. package/dist/api/newsletter/handlers/send.handler.js +153 -0
  50. package/dist/api/newsletter/handlers/subscribers.handler.d.ts +22 -0
  51. package/dist/api/newsletter/handlers/subscribers.handler.d.ts.map +1 -0
  52. package/dist/api/newsletter/handlers/subscribers.handler.js +191 -0
  53. package/dist/api/newsletter/routes/admin.routes.d.ts +4 -0
  54. package/dist/api/newsletter/routes/admin.routes.d.ts.map +1 -0
  55. package/dist/api/newsletter/routes/admin.routes.js +21 -0
  56. package/dist/api/newsletter/routes/index.d.ts +4 -0
  57. package/dist/api/newsletter/routes/index.d.ts.map +1 -0
  58. package/dist/api/newsletter/routes/index.js +9 -0
  59. package/dist/api/newsletter/routes/public.routes.d.ts +4 -0
  60. package/dist/api/newsletter/routes/public.routes.d.ts.map +1 -0
  61. package/dist/api/newsletter/routes/public.routes.js +9 -0
  62. package/dist/api/shop/handlers/customers.handler.d.ts +17 -0
  63. package/dist/api/shop/handlers/customers.handler.d.ts.map +1 -0
  64. package/dist/api/shop/handlers/customers.handler.js +89 -0
  65. package/dist/api/shop/handlers/orders.handler.d.ts +17 -0
  66. package/dist/api/shop/handlers/orders.handler.d.ts.map +1 -0
  67. package/dist/api/shop/handlers/orders.handler.js +84 -0
  68. package/dist/api/shop/handlers/prices.handler.d.ts +17 -0
  69. package/dist/api/shop/handlers/prices.handler.d.ts.map +1 -0
  70. package/dist/api/shop/handlers/prices.handler.js +87 -0
  71. package/dist/api/shop/handlers/products.handler.d.ts +45 -0
  72. package/dist/api/shop/handlers/products.handler.d.ts.map +1 -0
  73. package/dist/api/shop/handlers/products.handler.js +243 -0
  74. package/dist/api/shop/handlers/stats.handler.d.ts +10 -0
  75. package/dist/api/shop/handlers/stats.handler.d.ts.map +1 -0
  76. package/dist/api/shop/handlers/stats.handler.js +71 -0
  77. package/dist/api/shop/routes.d.ts +7 -0
  78. package/dist/api/shop/routes.d.ts.map +1 -0
  79. package/dist/api/shop/routes.js +31 -0
  80. package/dist/cms-assets.d.ts +4 -0
  81. package/dist/cms-assets.d.ts.map +1 -0
  82. package/dist/cms-assets.js +5 -0
  83. package/dist/cms-bundle.d.ts +3 -0
  84. package/dist/cms-bundle.d.ts.map +1 -0
  85. package/dist/cms-bundle.js +4 -0
  86. package/dist/config-loader.d.ts +26 -0
  87. package/dist/config-loader.d.ts.map +1 -0
  88. package/dist/config-loader.js +98 -0
  89. package/dist/hintergrund-assets.d.ts +4 -0
  90. package/dist/hintergrund-assets.d.ts.map +1 -0
  91. package/dist/hintergrund-assets.js +9 -0
  92. package/dist/index.d.ts +35 -0
  93. package/dist/index.d.ts.map +1 -0
  94. package/dist/index.js +66 -0
  95. package/dist/jwt.d.ts +26 -0
  96. package/dist/jwt.d.ts.map +1 -0
  97. package/dist/jwt.js +102 -0
  98. package/dist/lib/auth/crypto.d.ts +23 -0
  99. package/dist/lib/auth/crypto.d.ts.map +1 -0
  100. package/dist/lib/auth/crypto.js +110 -0
  101. package/dist/lib/auth/jwt.d.ts +38 -0
  102. package/dist/lib/auth/jwt.d.ts.map +1 -0
  103. package/dist/lib/auth/jwt.js +120 -0
  104. package/dist/lib/config.d.ts +31 -0
  105. package/dist/lib/config.d.ts.map +1 -0
  106. package/dist/lib/config.js +174 -0
  107. package/dist/lib/email.d.ts +16 -0
  108. package/dist/lib/email.d.ts.map +1 -0
  109. package/dist/lib/email.js +83 -0
  110. package/dist/lib/template.d.ts +2 -0
  111. package/dist/lib/template.d.ts.map +1 -0
  112. package/dist/lib/template.js +226 -0
  113. package/dist/middleware/auth.d.ts +9 -0
  114. package/dist/middleware/auth.d.ts.map +1 -0
  115. package/dist/middleware/auth.js +93 -0
  116. package/dist/template.d.ts +2 -0
  117. package/dist/template.d.ts.map +1 -0
  118. package/dist/template.js +61 -0
  119. package/dist/utils.d.ts +23 -0
  120. package/dist/utils.d.ts.map +1 -0
  121. package/dist/utils.js +110 -0
  122. package/package.json +32 -0
@@ -0,0 +1,191 @@
1
+ // Email validation
2
+ function isValidEmail(email) {
3
+ const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
4
+ return emailRegex.test(email);
5
+ }
6
+ // Generate subscriber ID
7
+ function generateId() {
8
+ return Math.random().toString(36).substring(2, 14);
9
+ }
10
+ // Generate unsubscribe token
11
+ function generateToken() {
12
+ const array = new Uint8Array(32);
13
+ crypto.getRandomValues(array);
14
+ return Array.from(array, (b) => b.toString(16).padStart(2, '0')).join('');
15
+ }
16
+ // Parse D1 row to Subscriber
17
+ function parseSubscriber(row) {
18
+ return {
19
+ id: row.id,
20
+ email: row.email,
21
+ name: row.name || undefined,
22
+ status: row.status,
23
+ source: row.source,
24
+ confirmationToken: row.confirmation_token || undefined,
25
+ confirmationSentAt: row.confirmation_sent_at || undefined,
26
+ confirmedAt: row.confirmed_at || undefined,
27
+ subscribedAt: row.subscribed_at || undefined,
28
+ unsubscribedAt: row.unsubscribed_at || undefined,
29
+ unsubscribeToken: row.unsubscribe_token || undefined,
30
+ createdAt: row.created_at,
31
+ updatedAt: row.updated_at,
32
+ };
33
+ }
34
+ /**
35
+ * GET /api/newsletter/admin/subscribers
36
+ * Get all subscribers (admin only)
37
+ */
38
+ export async function getSubscribers(c) {
39
+ try {
40
+ const db = c.env.DB;
41
+ const result = await db.prepare('SELECT * FROM subscribers ORDER BY created_at DESC').all();
42
+ const subscribers = result.results.map((row) => parseSubscriber(row));
43
+ return c.json({ subscribers });
44
+ }
45
+ catch (error) {
46
+ console.error('Get subscribers error:', error);
47
+ return c.json({
48
+ success: false,
49
+ message: error instanceof Error ? error.message : 'Failed to fetch subscribers',
50
+ }, 500);
51
+ }
52
+ }
53
+ /**
54
+ * POST /api/newsletter/admin/subscribers
55
+ * Manually add subscriber (admin only, bypasses confirmation)
56
+ */
57
+ export async function addSubscriber(c) {
58
+ try {
59
+ const body = (await c.req.json());
60
+ const { email, name } = body;
61
+ // Validate email
62
+ if (!email || !isValidEmail(email)) {
63
+ return c.json({ success: false, message: 'Invalid email address' }, 400);
64
+ }
65
+ const normalizedEmail = email.toLowerCase().trim();
66
+ const db = c.env.DB;
67
+ // Check for duplicates or unsubscribed
68
+ const existing = await db.prepare('SELECT id, status FROM subscribers WHERE email = ?').bind(normalizedEmail).first();
69
+ if (existing) {
70
+ // Block manual re-add of unsubscribed users
71
+ if (existing.status === 'unsubscribed') {
72
+ return c.json({
73
+ success: false,
74
+ message: 'This user has unsubscribed. They can only re-subscribe via the public subscription form.'
75
+ }, 403);
76
+ }
77
+ return c.json({ success: false, message: 'Subscriber already exists' }, 409);
78
+ }
79
+ // Create new subscriber with 'subscribed' status (bypass confirmation)
80
+ const id = generateId();
81
+ const unsubscribeToken = generateToken();
82
+ const now = Date.now();
83
+ await db
84
+ .prepare(`INSERT INTO subscribers
85
+ (id, email, name, status, source, unsubscribe_token, subscribed_at, created_at, updated_at)
86
+ VALUES (?, ?, ?, 'subscribed', 'manual', ?, ?, ?, ?)`)
87
+ .bind(id, normalizedEmail, name || null, unsubscribeToken, now, now, now)
88
+ .run();
89
+ // Fetch the created subscriber
90
+ const row = await db.prepare('SELECT * FROM subscribers WHERE id = ?').bind(id).first();
91
+ if (!row) {
92
+ return c.json({ success: false, message: 'Failed to create subscriber' }, 500);
93
+ }
94
+ const subscriber = parseSubscriber(row);
95
+ return c.json({ success: true, subscriber });
96
+ }
97
+ catch (error) {
98
+ console.error('Add subscriber error:', error);
99
+ return c.json({
100
+ success: false,
101
+ message: error instanceof Error ? error.message : 'Failed to add subscriber',
102
+ }, 500);
103
+ }
104
+ }
105
+ /**
106
+ * DELETE /api/newsletter/admin/subscribers/:id
107
+ * Delete subscriber by ID (admin only)
108
+ */
109
+ export async function deleteSubscriber(c) {
110
+ try {
111
+ const id = c.req.param('id');
112
+ if (!id) {
113
+ return c.json({ success: false, message: 'Missing subscriber ID' }, 400);
114
+ }
115
+ const db = c.env.DB;
116
+ // Check if subscriber exists
117
+ const existing = await db.prepare('SELECT id FROM subscribers WHERE id = ?').bind(id).first();
118
+ if (!existing) {
119
+ return c.json({ success: false, message: 'Subscriber not found' }, 404);
120
+ }
121
+ // Delete subscriber
122
+ await db.prepare('DELETE FROM subscribers WHERE id = ?').bind(id).run();
123
+ return c.json({ success: true });
124
+ }
125
+ catch (error) {
126
+ console.error('Delete subscriber error:', error);
127
+ return c.json({
128
+ success: false,
129
+ message: error instanceof Error ? error.message : 'Failed to delete subscriber',
130
+ }, 500);
131
+ }
132
+ }
133
+ /**
134
+ * PATCH /api/newsletter/admin/subscribers/:id
135
+ * Update subscriber (admin only)
136
+ */
137
+ export async function updateSubscriber(c) {
138
+ try {
139
+ const id = c.req.param('id');
140
+ if (!id) {
141
+ return c.json({ success: false, message: 'Missing subscriber ID' }, 400);
142
+ }
143
+ const body = (await c.req.json());
144
+ const db = c.env.DB;
145
+ // Check if subscriber exists
146
+ const existing = await db.prepare('SELECT id FROM subscribers WHERE id = ?').bind(id).first();
147
+ if (!existing) {
148
+ return c.json({ success: false, message: 'Subscriber not found' }, 404);
149
+ }
150
+ // Build update query dynamically
151
+ const updates = [];
152
+ const params = [];
153
+ if (body.name !== undefined) {
154
+ updates.push('name = ?');
155
+ params.push(body.name || null);
156
+ }
157
+ if (body.status !== undefined) {
158
+ updates.push('status = ?');
159
+ params.push(body.status);
160
+ // Update status timestamps
161
+ if (body.status === 'subscribed') {
162
+ updates.push('subscribed_at = ?');
163
+ params.push(Date.now());
164
+ }
165
+ }
166
+ // Always update updated_at
167
+ updates.push('updated_at = ?');
168
+ params.push(Date.now());
169
+ if (updates.length === 0) {
170
+ return c.json({ success: false, message: 'No fields to update' }, 400);
171
+ }
172
+ // Add ID to params
173
+ params.push(id);
174
+ const query = `UPDATE subscribers SET ${updates.join(', ')} WHERE id = ?`;
175
+ await db.prepare(query).bind(...params).run();
176
+ // Fetch updated subscriber
177
+ const row = await db.prepare('SELECT * FROM subscribers WHERE id = ?').bind(id).first();
178
+ if (!row) {
179
+ return c.json({ success: false, message: 'Failed to update subscriber' }, 500);
180
+ }
181
+ const subscriber = parseSubscriber(row);
182
+ return c.json({ success: true, subscriber });
183
+ }
184
+ catch (error) {
185
+ console.error('Update subscriber error:', error);
186
+ return c.json({
187
+ success: false,
188
+ message: error instanceof Error ? error.message : 'Failed to update subscriber',
189
+ }, 500);
190
+ }
191
+ }
@@ -0,0 +1,4 @@
1
+ import { Hono } from 'hono';
2
+ declare const adminRoutes: Hono<import("hono/types").BlankEnv, import("hono/types").BlankSchema, "/">;
3
+ export default adminRoutes;
4
+ //# sourceMappingURL=admin.routes.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"admin.routes.d.ts","sourceRoot":"","sources":["../../../../src/api/newsletter/routes/admin.routes.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAM5B,QAAA,MAAM,WAAW,4EAAa,CAAC;AAoB/B,eAAe,WAAW,CAAC"}
@@ -0,0 +1,21 @@
1
+ import { Hono } from 'hono';
2
+ import { authMiddleware } from '../../../middleware/auth';
3
+ import { getSubscribers, addSubscriber, deleteSubscriber, updateSubscriber } from '../handlers/subscribers.handler';
4
+ import { sendBulkEmail, sendTestEmail } from '../handlers/send.handler';
5
+ import { getEmails, saveDraft, deleteEmail } from '../handlers/emails.handler';
6
+ const adminRoutes = new Hono();
7
+ // Protect all admin routes with authentication
8
+ adminRoutes.use('*', authMiddleware);
9
+ // Admin subscriber management endpoints
10
+ adminRoutes.get('/subscribers', getSubscribers);
11
+ adminRoutes.post('/subscribers', addSubscriber);
12
+ adminRoutes.delete('/subscribers/:id', deleteSubscriber);
13
+ adminRoutes.patch('/subscribers/:id', updateSubscriber);
14
+ // Admin email sending endpoint
15
+ adminRoutes.post('/send', sendBulkEmail);
16
+ // Admin email history/drafts endpoints
17
+ adminRoutes.get('/emails', getEmails);
18
+ adminRoutes.post('/emails/draft', saveDraft);
19
+ adminRoutes.post('/emails/test', sendTestEmail);
20
+ adminRoutes.delete('/emails/:id', deleteEmail);
21
+ export default adminRoutes;
@@ -0,0 +1,4 @@
1
+ import { Hono } from 'hono';
2
+ declare const newsletterRoutes: Hono<import("hono/types").BlankEnv, import("hono/types").BlankSchema, "/">;
3
+ export default newsletterRoutes;
4
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/api/newsletter/routes/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAI5B,QAAA,MAAM,gBAAgB,4EAAa,CAAC;AAQpC,eAAe,gBAAgB,CAAC"}
@@ -0,0 +1,9 @@
1
+ import { Hono } from 'hono';
2
+ import publicRoutes from './public.routes';
3
+ import adminRoutes from './admin.routes';
4
+ const newsletterRoutes = new Hono();
5
+ // Mount public routes (no /admin prefix)
6
+ newsletterRoutes.route('/', publicRoutes);
7
+ // Mount admin routes (with /admin prefix)
8
+ newsletterRoutes.route('/admin', adminRoutes);
9
+ export default newsletterRoutes;
@@ -0,0 +1,4 @@
1
+ import { Hono } from 'hono';
2
+ declare const publicRoutes: Hono<import("hono/types").BlankEnv, import("hono/types").BlankSchema, "/">;
3
+ export default publicRoutes;
4
+ //# sourceMappingURL=public.routes.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"public.routes.d.ts","sourceRoot":"","sources":["../../../../src/api/newsletter/routes/public.routes.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAG5B,QAAA,MAAM,YAAY,4EAAa,CAAC;AAQhC,eAAe,YAAY,CAAC"}
@@ -0,0 +1,9 @@
1
+ import { Hono } from 'hono';
2
+ import { subscribe, confirm, unsubscribe, showUnsubscribePage } from '../handlers/public.handler';
3
+ const publicRoutes = new Hono();
4
+ // Public newsletter endpoints (no authentication required)
5
+ publicRoutes.post('/subscribe', subscribe);
6
+ publicRoutes.get('/confirm', confirm);
7
+ publicRoutes.get('/unsubscribe', showUnsubscribePage);
8
+ publicRoutes.post('/unsubscribe', unsubscribe);
9
+ export default publicRoutes;
@@ -0,0 +1,17 @@
1
+ import type { Context } from 'hono';
2
+ import type { Bindings } from '../../../index';
3
+ /**
4
+ * GET /api/shop/admin/customers
5
+ * List all customers
6
+ */
7
+ export declare function getCustomers(c: Context<{
8
+ Bindings: Bindings;
9
+ }>): Promise<Response>;
10
+ /**
11
+ * GET /api/shop/admin/customers/:id
12
+ * Get a single customer with their orders
13
+ */
14
+ export declare function getCustomer(c: Context<{
15
+ Bindings: Bindings;
16
+ }>): Promise<Response>;
17
+ //# sourceMappingURL=customers.handler.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"customers.handler.d.ts","sourceRoot":"","sources":["../../../../src/api/shop/handlers/customers.handler.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AACpC,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AA4B/C;;;GAGG;AACH,wBAAsB,YAAY,CAAC,CAAC,EAAE,OAAO,CAAC;IAAE,QAAQ,EAAE,QAAQ,CAAA;CAAE,CAAC,GAAG,OAAO,CAAC,QAAQ,CAAC,CAoBxF;AAED;;;GAGG;AACH,wBAAsB,WAAW,CAAC,CAAC,EAAE,OAAO,CAAC;IAAE,QAAQ,EAAE,QAAQ,CAAA;CAAE,CAAC,GAAG,OAAO,CAAC,QAAQ,CAAC,CAkDvF"}
@@ -0,0 +1,89 @@
1
+ import { getShopConfig } from '../../../lib/config';
2
+ import Stripe from 'stripe';
3
+ function getStripe(env) {
4
+ const config = getShopConfig(env);
5
+ return new Stripe(config.stripeSecretKey, {
6
+ apiVersion: '2025-02-24.acacia'
7
+ });
8
+ }
9
+ function mapStripeCustomer(customer) {
10
+ return {
11
+ id: customer.id,
12
+ email: customer.email,
13
+ name: customer.name || null,
14
+ phone: customer.phone || null,
15
+ description: customer.description,
16
+ metadata: customer.metadata || {},
17
+ createdAt: customer.created
18
+ };
19
+ }
20
+ /**
21
+ * GET /api/shop/admin/customers
22
+ * List all customers
23
+ */
24
+ export async function getCustomers(c) {
25
+ try {
26
+ const stripe = getStripe(c.env);
27
+ const limit = parseInt(c.req.query('limit') || '25');
28
+ const customers = await stripe.customers.list({
29
+ limit
30
+ });
31
+ const response = {
32
+ customers: customers.data.map(mapStripeCustomer),
33
+ hasMore: customers.has_more
34
+ };
35
+ return c.json(response);
36
+ }
37
+ catch (error) {
38
+ console.error('Failed to fetch customers:', error);
39
+ return c.json({ success: false, error: 'Failed to fetch customers' }, 500);
40
+ }
41
+ }
42
+ /**
43
+ * GET /api/shop/admin/customers/:id
44
+ * Get a single customer with their orders
45
+ */
46
+ export async function getCustomer(c) {
47
+ try {
48
+ const stripe = getStripe(c.env);
49
+ const customerId = c.req.param('id');
50
+ const [customer, paymentIntents] = await Promise.all([
51
+ stripe.customers.retrieve(customerId),
52
+ stripe.paymentIntents.list({
53
+ customer: customerId,
54
+ limit: 100
55
+ })
56
+ ]);
57
+ if (customer.deleted) {
58
+ return c.json({ success: false, error: 'Customer not found' }, 404);
59
+ }
60
+ const mappedCustomer = mapStripeCustomer(customer);
61
+ // Calculate totals
62
+ mappedCustomer.totalOrders = paymentIntents.data.filter((pi) => pi.status === 'succeeded').length;
63
+ mappedCustomer.totalSpent = paymentIntents.data
64
+ .filter((pi) => pi.status === 'succeeded')
65
+ .reduce((sum, pi) => sum + pi.amount_received, 0);
66
+ const response = {
67
+ customer: mappedCustomer,
68
+ orders: paymentIntents.data.map((pi) => ({
69
+ id: pi.id,
70
+ customerId: customerId,
71
+ customerEmail: customer.email || null,
72
+ customerName: customer.name || null,
73
+ amount: pi.amount,
74
+ amountReceived: pi.amount_received,
75
+ currency: pi.currency,
76
+ status: pi.status,
77
+ paymentMethod: typeof pi.payment_method === 'string' ? pi.payment_method : pi.payment_method?.id || null,
78
+ lineItems: [],
79
+ metadata: pi.metadata || {},
80
+ createdAt: pi.created
81
+ }))
82
+ };
83
+ return c.json(response);
84
+ }
85
+ catch (error) {
86
+ console.error('Failed to fetch customer:', error);
87
+ return c.json({ success: false, error: 'Failed to fetch customer' }, 500);
88
+ }
89
+ }
@@ -0,0 +1,17 @@
1
+ import type { Context } from 'hono';
2
+ import type { Bindings } from '../../../index';
3
+ /**
4
+ * GET /api/shop/admin/orders
5
+ * List payment intents (orders)
6
+ */
7
+ export declare function getOrders(c: Context<{
8
+ Bindings: Bindings;
9
+ }>): Promise<Response>;
10
+ /**
11
+ * GET /api/shop/admin/orders/:id
12
+ * Get a single order
13
+ */
14
+ export declare function getOrder(c: Context<{
15
+ Bindings: Bindings;
16
+ }>): Promise<Response>;
17
+ //# sourceMappingURL=orders.handler.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"orders.handler.d.ts","sourceRoot":"","sources":["../../../../src/api/shop/handlers/orders.handler.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AACpC,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAsC/C;;;GAGG;AACH,wBAAsB,SAAS,CAAC,CAAC,EAAE,OAAO,CAAC;IAAE,QAAQ,EAAE,QAAQ,CAAA;CAAE,CAAC,GAAG,OAAO,CAAC,QAAQ,CAAC,CAkCrF;AAED;;;GAGG;AACH,wBAAsB,QAAQ,CAAC,CAAC,EAAE,OAAO,CAAC;IAAE,QAAQ,EAAE,QAAQ,CAAA;CAAE,CAAC,GAAG,OAAO,CAAC,QAAQ,CAAC,CAsBpF"}
@@ -0,0 +1,84 @@
1
+ import { getShopConfig } from '../../../lib/config';
2
+ import Stripe from 'stripe';
3
+ function getStripe(env) {
4
+ const config = getShopConfig(env);
5
+ return new Stripe(config.stripeSecretKey, {
6
+ apiVersion: '2025-02-24.acacia'
7
+ });
8
+ }
9
+ function mapPaymentIntentStatus(status) {
10
+ return status;
11
+ }
12
+ function mapPaymentIntent(pi) {
13
+ return {
14
+ id: pi.id,
15
+ customerId: typeof pi.customer === 'string' ? pi.customer : pi.customer?.id || null,
16
+ customerEmail: pi.receipt_email || null,
17
+ customerName: null,
18
+ amount: pi.amount,
19
+ amountReceived: pi.amount_received,
20
+ currency: pi.currency,
21
+ status: mapPaymentIntentStatus(pi.status),
22
+ paymentMethod: typeof pi.payment_method === 'string' ? pi.payment_method : pi.payment_method?.id || null,
23
+ lineItems: [],
24
+ metadata: pi.metadata || {},
25
+ createdAt: pi.created
26
+ };
27
+ }
28
+ /**
29
+ * GET /api/shop/admin/orders
30
+ * List payment intents (orders)
31
+ */
32
+ export async function getOrders(c) {
33
+ try {
34
+ const stripe = getStripe(c.env);
35
+ const limit = parseInt(c.req.query('limit') || '25');
36
+ const paymentIntents = await stripe.paymentIntents.list({
37
+ limit,
38
+ expand: ['data.customer']
39
+ });
40
+ const orders = paymentIntents.data.map((pi) => {
41
+ const order = mapPaymentIntent(pi);
42
+ // Add customer info if expanded
43
+ if (pi.customer && typeof pi.customer !== 'string' && !pi.customer.deleted) {
44
+ const customer = pi.customer;
45
+ order.customerEmail = customer.email || order.customerEmail;
46
+ order.customerName = customer.name || null;
47
+ }
48
+ return order;
49
+ });
50
+ const response = {
51
+ orders,
52
+ hasMore: paymentIntents.has_more
53
+ };
54
+ return c.json(response);
55
+ }
56
+ catch (error) {
57
+ console.error('Failed to fetch orders:', error);
58
+ return c.json({ success: false, error: 'Failed to fetch orders' }, 500);
59
+ }
60
+ }
61
+ /**
62
+ * GET /api/shop/admin/orders/:id
63
+ * Get a single order
64
+ */
65
+ export async function getOrder(c) {
66
+ try {
67
+ const stripe = getStripe(c.env);
68
+ const orderId = c.req.param('id');
69
+ const paymentIntent = await stripe.paymentIntents.retrieve(orderId, {
70
+ expand: ['customer']
71
+ });
72
+ const order = mapPaymentIntent(paymentIntent);
73
+ if (paymentIntent.customer && typeof paymentIntent.customer !== 'string' && !paymentIntent.customer.deleted) {
74
+ const customer = paymentIntent.customer;
75
+ order.customerEmail = customer.email || order.customerEmail;
76
+ order.customerName = customer.name || null;
77
+ }
78
+ return c.json({ order });
79
+ }
80
+ catch (error) {
81
+ console.error('Failed to fetch order:', error);
82
+ return c.json({ success: false, error: 'Failed to fetch order' }, 500);
83
+ }
84
+ }
@@ -0,0 +1,17 @@
1
+ import type { Context } from 'hono';
2
+ import type { Bindings } from '../../../index';
3
+ /**
4
+ * POST /api/shop/admin/prices
5
+ * Create a new price for a product
6
+ */
7
+ export declare function createPrice(c: Context<{
8
+ Bindings: Bindings;
9
+ }>): Promise<Response>;
10
+ /**
11
+ * PATCH /api/shop/admin/prices/:id
12
+ * Update a price (limited - prices are mostly immutable in Stripe)
13
+ */
14
+ export declare function updatePrice(c: Context<{
15
+ Bindings: Bindings;
16
+ }>): Promise<Response>;
17
+ //# sourceMappingURL=prices.handler.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"prices.handler.d.ts","sourceRoot":"","sources":["../../../../src/api/shop/handlers/prices.handler.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AACpC,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAsC/C;;;GAGG;AACH,wBAAsB,WAAW,CAAC,CAAC,EAAE,OAAO,CAAC;IAAE,QAAQ,EAAE,QAAQ,CAAA;CAAE,CAAC,GAAG,OAAO,CAAC,QAAQ,CAAC,CAiCvF;AAED;;;GAGG;AACH,wBAAsB,WAAW,CAAC,CAAC,EAAE,OAAO,CAAC;IAAE,QAAQ,EAAE,QAAQ,CAAA;CAAE,CAAC,GAAG,OAAO,CAAC,QAAQ,CAAC,CAqBvF"}
@@ -0,0 +1,87 @@
1
+ import { getShopConfig } from '../../../lib/config';
2
+ import Stripe from 'stripe';
3
+ function getStripe(env) {
4
+ const config = getShopConfig(env);
5
+ return new Stripe(config.stripeSecretKey, {
6
+ apiVersion: '2025-02-24.acacia'
7
+ });
8
+ }
9
+ function mapStripePrice(price) {
10
+ return {
11
+ id: price.id,
12
+ productId: typeof price.product === 'string' ? price.product : price.product?.id || '',
13
+ active: price.active,
14
+ currency: price.currency,
15
+ unitAmount: price.unit_amount,
16
+ unitAmountDecimal: price.unit_amount_decimal,
17
+ billingScheme: price.billing_scheme,
18
+ type: price.type,
19
+ recurring: price.recurring
20
+ ? {
21
+ interval: price.recurring.interval,
22
+ intervalCount: price.recurring.interval_count
23
+ }
24
+ : undefined,
25
+ metadata: price.metadata || {},
26
+ createdAt: price.created
27
+ };
28
+ }
29
+ /**
30
+ * POST /api/shop/admin/prices
31
+ * Create a new price for a product
32
+ */
33
+ export async function createPrice(c) {
34
+ try {
35
+ const stripe = getStripe(c.env);
36
+ const body = await c.req.json();
37
+ const priceParams = {
38
+ product: body.productId,
39
+ currency: body.currency,
40
+ unit_amount: body.unitAmount,
41
+ active: body.active ?? true,
42
+ billing_scheme: body.billingScheme || 'per_unit',
43
+ metadata: body.metadata
44
+ };
45
+ if (body.recurring) {
46
+ priceParams.recurring = {
47
+ interval: body.recurring.interval,
48
+ interval_count: body.recurring.intervalCount || 1
49
+ };
50
+ }
51
+ const price = await stripe.prices.create(priceParams);
52
+ const response = {
53
+ success: true,
54
+ price: mapStripePrice(price)
55
+ };
56
+ return c.json(response, 201);
57
+ }
58
+ catch (error) {
59
+ console.error('Failed to create price:', error);
60
+ return c.json({ success: false, error: 'Failed to create price' }, 500);
61
+ }
62
+ }
63
+ /**
64
+ * PATCH /api/shop/admin/prices/:id
65
+ * Update a price (limited - prices are mostly immutable in Stripe)
66
+ */
67
+ export async function updatePrice(c) {
68
+ try {
69
+ const stripe = getStripe(c.env);
70
+ const priceId = c.req.param('id');
71
+ const body = await c.req.json();
72
+ const updateParams = {};
73
+ if (body.active !== undefined)
74
+ updateParams.active = body.active;
75
+ if (body.metadata !== undefined)
76
+ updateParams.metadata = body.metadata;
77
+ const price = await stripe.prices.update(priceId, updateParams);
78
+ return c.json({
79
+ success: true,
80
+ price: mapStripePrice(price)
81
+ });
82
+ }
83
+ catch (error) {
84
+ console.error('Failed to update price:', error);
85
+ return c.json({ success: false, error: 'Failed to update price' }, 500);
86
+ }
87
+ }
@@ -0,0 +1,45 @@
1
+ import type { Context } from 'hono';
2
+ import type { Bindings } from '../../../index';
3
+ /**
4
+ * GET /api/shop/products
5
+ * List all active products (public endpoint)
6
+ */
7
+ export declare function getPublicProducts(c: Context<{
8
+ Bindings: Bindings;
9
+ }>): Promise<Response>;
10
+ /**
11
+ * GET /api/shop/admin/products
12
+ * List all products
13
+ */
14
+ export declare function getProducts(c: Context<{
15
+ Bindings: Bindings;
16
+ }>): Promise<Response>;
17
+ /**
18
+ * GET /api/shop/admin/products/:id
19
+ * Get a single product with all its prices
20
+ */
21
+ export declare function getProduct(c: Context<{
22
+ Bindings: Bindings;
23
+ }>): Promise<Response>;
24
+ /**
25
+ * POST /api/shop/admin/products
26
+ * Create a new product
27
+ */
28
+ export declare function createProduct(c: Context<{
29
+ Bindings: Bindings;
30
+ }>): Promise<Response>;
31
+ /**
32
+ * PATCH /api/shop/admin/products/:id
33
+ * Update a product
34
+ */
35
+ export declare function updateProduct(c: Context<{
36
+ Bindings: Bindings;
37
+ }>): Promise<Response>;
38
+ /**
39
+ * DELETE /api/shop/admin/products/:id
40
+ * Delete a product (archive if has prices)
41
+ */
42
+ export declare function deleteProduct(c: Context<{
43
+ Bindings: Bindings;
44
+ }>): Promise<Response>;
45
+ //# sourceMappingURL=products.handler.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"products.handler.d.ts","sourceRoot":"","sources":["../../../../src/api/shop/handlers/products.handler.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AACpC,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AA8D/C;;;GAGG;AACH,wBAAsB,iBAAiB,CAAC,CAAC,EAAE,OAAO,CAAC;IAAE,QAAQ,EAAE,QAAQ,CAAA;CAAE,CAAC,GAAG,OAAO,CAAC,QAAQ,CAAC,CAsB7F;AAED;;;GAGG;AACH,wBAAsB,WAAW,CAAC,CAAC,EAAE,OAAO,CAAC;IAAE,QAAQ,EAAE,QAAQ,CAAA;CAAE,CAAC,GAAG,OAAO,CAAC,QAAQ,CAAC,CA8BvF;AAED;;;GAGG;AACH,wBAAsB,UAAU,CAAC,CAAC,EAAE,OAAO,CAAC;IAAE,QAAQ,EAAE,QAAQ,CAAA;CAAE,CAAC,GAAG,OAAO,CAAC,QAAQ,CAAC,CAqBtF;AAED;;;GAGG;AACH,wBAAsB,aAAa,CAAC,CAAC,EAAE,OAAO,CAAC;IAAE,QAAQ,EAAE,QAAQ,CAAA;CAAE,CAAC,GAAG,OAAO,CAAC,QAAQ,CAAC,CA+CzF;AAED;;;GAGG;AACH,wBAAsB,aAAa,CAAC,CAAC,EAAE,OAAO,CAAC;IAAE,QAAQ,EAAE,QAAQ,CAAA;CAAE,CAAC,GAAG,OAAO,CAAC,QAAQ,CAAC,CAmCzF;AAED;;;GAGG;AACH,wBAAsB,aAAa,CAAC,CAAC,EAAE,OAAO,CAAC;IAAE,QAAQ,EAAE,QAAQ,CAAA;CAAE,CAAC,GAAG,OAAO,CAAC,QAAQ,CAAC,CA4BzF"}