@hintergrund/worker 0.1.2 → 0.1.5

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 (79) hide show
  1. package/dist/api/cms/handlers/assets.handler.d.ts +24 -1
  2. package/dist/api/cms/handlers/assets.handler.d.ts.map +1 -1
  3. package/dist/api/cms/handlers/assets.handler.js +111 -210
  4. package/dist/api/cms/handlers/auth.handler.d.ts +4 -3
  5. package/dist/api/cms/handlers/auth.handler.d.ts.map +1 -1
  6. package/dist/api/cms/handlers/auth.handler.js +24 -14
  7. package/dist/api/cms/handlers/config.handler.d.ts +4 -0
  8. package/dist/api/cms/handlers/config.handler.d.ts.map +1 -1
  9. package/dist/api/cms/handlers/config.handler.js +20 -71
  10. package/dist/api/cms/handlers/content.handler.d.ts +12 -1
  11. package/dist/api/cms/handlers/content.handler.d.ts.map +1 -1
  12. package/dist/api/cms/handlers/content.handler.js +57 -233
  13. package/dist/api/cms/handlers/dashboard.handler.js +1 -1
  14. package/dist/api/cms/routes/auth.routes.d.ts +2 -1
  15. package/dist/api/cms/routes/auth.routes.d.ts.map +1 -1
  16. package/dist/api/cms/routes/auth.routes.js +7 -0
  17. package/dist/api/cms/routes/index.d.ts +2 -1
  18. package/dist/api/cms/routes/index.d.ts.map +1 -1
  19. package/dist/api/emails/handlers/branding.handler.d.ts +17 -0
  20. package/dist/api/emails/handlers/branding.handler.d.ts.map +1 -0
  21. package/dist/api/emails/handlers/branding.handler.js +120 -0
  22. package/dist/api/emails/handlers/campaigns.handler.d.ts +32 -0
  23. package/dist/api/emails/handlers/campaigns.handler.d.ts.map +1 -0
  24. package/dist/api/emails/handlers/campaigns.handler.js +139 -0
  25. package/dist/api/emails/handlers/contacts.handler.d.ts +37 -0
  26. package/dist/api/emails/handlers/contacts.handler.d.ts.map +1 -0
  27. package/dist/api/emails/handlers/contacts.handler.js +161 -0
  28. package/dist/api/emails/handlers/invoice.handler.d.ts +30 -0
  29. package/dist/api/emails/handlers/invoice.handler.d.ts.map +1 -0
  30. package/dist/api/emails/handlers/invoice.handler.js +463 -0
  31. package/dist/api/emails/handlers/public.handler.d.ts +36 -0
  32. package/dist/api/emails/handlers/public.handler.d.ts.map +1 -0
  33. package/dist/api/emails/handlers/public.handler.js +350 -0
  34. package/dist/api/emails/handlers/send.handler.d.ts +23 -0
  35. package/dist/api/emails/handlers/send.handler.d.ts.map +1 -0
  36. package/dist/api/emails/handlers/send.handler.js +117 -0
  37. package/dist/api/emails/handlers/settings.handler.d.ts +36 -0
  38. package/dist/api/emails/handlers/settings.handler.d.ts.map +1 -0
  39. package/dist/api/emails/handlers/settings.handler.js +312 -0
  40. package/dist/api/emails/handlers/templates.handler.d.ts +32 -0
  41. package/dist/api/emails/handlers/templates.handler.d.ts.map +1 -0
  42. package/dist/api/emails/handlers/templates.handler.js +132 -0
  43. package/dist/api/emails/handlers/webhook.handler.d.ts +10 -0
  44. package/dist/api/emails/handlers/webhook.handler.d.ts.map +1 -0
  45. package/dist/api/emails/handlers/webhook.handler.js +261 -0
  46. package/dist/api/emails/routes/admin.routes.d.ts +9 -0
  47. package/dist/api/emails/routes/admin.routes.d.ts.map +1 -0
  48. package/dist/api/emails/routes/admin.routes.js +77 -0
  49. package/dist/api/emails/routes/index.d.ts +9 -0
  50. package/dist/api/emails/routes/index.d.ts.map +1 -0
  51. package/dist/api/emails/routes/index.js +14 -0
  52. package/dist/api/emails/routes/public.routes.d.ts +9 -0
  53. package/dist/api/emails/routes/public.routes.d.ts.map +1 -0
  54. package/dist/api/emails/routes/public.routes.js +18 -0
  55. package/dist/api/shop/handlers/webhook.handler.d.ts.map +1 -1
  56. package/dist/api/shop/handlers/webhook.handler.js +66 -60
  57. package/dist/api/shop/routes.d.ts.map +1 -1
  58. package/dist/api/shop/routes.js +0 -3
  59. package/dist/hintergrund-assets.d.ts +2 -2
  60. package/dist/hintergrund-assets.d.ts.map +1 -1
  61. package/dist/hintergrund-assets.js +2 -2
  62. package/dist/index.d.ts +20 -19
  63. package/dist/index.d.ts.map +1 -1
  64. package/dist/index.js +4 -7
  65. package/dist/lib/config.d.ts +13 -7
  66. package/dist/lib/config.d.ts.map +1 -1
  67. package/dist/lib/config.js +44 -46
  68. package/dist/lib/email.d.ts +2 -2
  69. package/dist/lib/email.d.ts.map +1 -1
  70. package/dist/lib/email.js +7 -9
  71. package/dist/lib/github.d.ts +124 -0
  72. package/dist/lib/github.d.ts.map +1 -0
  73. package/dist/lib/github.js +286 -0
  74. package/dist/lib/plunk.d.ts +204 -0
  75. package/dist/lib/plunk.d.ts.map +1 -0
  76. package/dist/lib/plunk.js +259 -0
  77. package/dist/middleware/auth.d.ts.map +1 -1
  78. package/dist/middleware/auth.js +2 -5
  79. package/package.json +32 -32
@@ -0,0 +1,259 @@
1
+ /**
2
+ * Plunk API Client
3
+ *
4
+ * Provides methods for interacting with Plunk's email marketing API:
5
+ * - Contacts management
6
+ * - Templates management
7
+ * - Campaigns management
8
+ * - Transactional email sending
9
+ * - Event tracking
10
+ */
11
+ export class PlunkClient {
12
+ constructor(config) {
13
+ this.baseUrl = 'https://next-api.useplunk.com';
14
+ this.secretKey = config.secretKey;
15
+ this.publicKey = config.publicKey;
16
+ }
17
+ async request(endpoint, options = {}, usePublicKey = false) {
18
+ const apiKey = usePublicKey && this.publicKey ? this.publicKey : this.secretKey;
19
+ const response = await fetch(`${this.baseUrl}${endpoint}`, {
20
+ ...options,
21
+ headers: {
22
+ 'Content-Type': 'application/json',
23
+ 'Authorization': `Bearer ${apiKey}`,
24
+ ...options.headers,
25
+ },
26
+ });
27
+ const data = await response.json();
28
+ if (!response.ok) {
29
+ const error = data;
30
+ // Enhanced error logging
31
+ console.error(`Plunk API Error: ${error.error || error.message}`);
32
+ throw new Error(error.message || error.error || `Plunk API error: ${response.status}`);
33
+ }
34
+ return data;
35
+ }
36
+ // ==========================================================================
37
+ // Contacts
38
+ // ==========================================================================
39
+ /**
40
+ * Get all contacts
41
+ */
42
+ async getContacts() {
43
+ const response = await this.request('/contacts');
44
+ return {
45
+ contacts: response.data || [],
46
+ count: response.count || 0,
47
+ };
48
+ }
49
+ /**
50
+ * Get a single contact by ID
51
+ */
52
+ async getContact(id) {
53
+ return this.request(`/contacts/${id}`);
54
+ }
55
+ /**
56
+ * Create a new contact
57
+ */
58
+ async createContact(data) {
59
+ return this.request('/contacts', {
60
+ method: 'POST',
61
+ body: JSON.stringify(data),
62
+ });
63
+ }
64
+ /**
65
+ * Update an existing contact
66
+ */
67
+ async updateContact(id, data) {
68
+ return this.request(`/contacts/${id}`, {
69
+ method: 'PUT',
70
+ body: JSON.stringify(data),
71
+ });
72
+ }
73
+ /**
74
+ * Delete a contact
75
+ */
76
+ async deleteContact(id) {
77
+ return this.request(`/contacts/${id}`, {
78
+ method: 'DELETE',
79
+ });
80
+ }
81
+ /**
82
+ * Subscribe a contact (set subscribed to true)
83
+ */
84
+ async subscribeContact(id) {
85
+ return this.updateContact(id, { subscribed: true });
86
+ }
87
+ /**
88
+ * Unsubscribe a contact (set subscribed to false)
89
+ */
90
+ async unsubscribeContact(id) {
91
+ return this.updateContact(id, { subscribed: false });
92
+ }
93
+ // ==========================================================================
94
+ // Templates
95
+ // ==========================================================================
96
+ /**
97
+ * Get all templates
98
+ */
99
+ async getTemplates() {
100
+ const response = await this.request('/templates');
101
+ return {
102
+ templates: response.data || [],
103
+ };
104
+ }
105
+ /**
106
+ * Get a single template by ID
107
+ */
108
+ async getTemplate(id) {
109
+ return this.request(`/templates/${id}`);
110
+ }
111
+ /**
112
+ * Create a new template
113
+ */
114
+ async createTemplate(data) {
115
+ return this.request('/templates', {
116
+ method: 'POST',
117
+ body: JSON.stringify({
118
+ ...data,
119
+ type: data.type || 'TRANSACTIONAL',
120
+ }),
121
+ });
122
+ }
123
+ /**
124
+ * Update an existing template
125
+ */
126
+ async updateTemplate(id, data) {
127
+ return this.request(`/templates/${id}`, {
128
+ method: 'PUT',
129
+ body: JSON.stringify(data),
130
+ });
131
+ }
132
+ /**
133
+ * Delete a template
134
+ */
135
+ async deleteTemplate(id) {
136
+ return this.request(`/templates/${id}`, {
137
+ method: 'DELETE',
138
+ });
139
+ }
140
+ // ==========================================================================
141
+ // Campaigns
142
+ // ==========================================================================
143
+ /**
144
+ * Get all campaigns
145
+ */
146
+ async getCampaigns() {
147
+ const response = await this.request('/campaigns');
148
+ return {
149
+ campaigns: response.data || [],
150
+ };
151
+ }
152
+ /**
153
+ * Get a single campaign by ID
154
+ */
155
+ async getCampaign(id) {
156
+ return this.request(`/campaigns/${id}`);
157
+ }
158
+ /**
159
+ * Create a new campaign (draft)
160
+ */
161
+ async createCampaign(data) {
162
+ return this.request('/campaigns', {
163
+ method: 'POST',
164
+ body: JSON.stringify({
165
+ ...data,
166
+ recipients: 'all', // Send to all subscribed contacts
167
+ }),
168
+ });
169
+ }
170
+ /**
171
+ * Send a campaign to all subscribed contacts
172
+ */
173
+ async sendCampaign(id) {
174
+ return this.request(`/campaigns/${id}/send`, {
175
+ method: 'POST',
176
+ });
177
+ }
178
+ /**
179
+ * Send a test email for a campaign to a specific email
180
+ */
181
+ async testCampaign(id, email) {
182
+ // Get the campaign to use its content
183
+ const campaign = await this.getCampaign(id);
184
+ // Send as transactional to the test email
185
+ return this.send({
186
+ to: email,
187
+ subject: `[TEST] ${campaign.subject}`,
188
+ body: campaign.body || '',
189
+ });
190
+ }
191
+ // ==========================================================================
192
+ // Transactional Emails
193
+ // ==========================================================================
194
+ /**
195
+ * Send a transactional email
196
+ */
197
+ async send(data) {
198
+ return this.request('/v1/send', {
199
+ method: 'POST',
200
+ body: JSON.stringify(data),
201
+ });
202
+ }
203
+ // ==========================================================================
204
+ // Event Tracking
205
+ // ==========================================================================
206
+ /**
207
+ * Track an event for a contact
208
+ * Uses the public key if available for client-side tracking
209
+ */
210
+ async track(data) {
211
+ return this.request('/v1/track', {
212
+ method: 'POST',
213
+ body: JSON.stringify(data),
214
+ }, true // Use public key for tracking
215
+ );
216
+ }
217
+ // ==========================================================================
218
+ // Utility Methods
219
+ // ==========================================================================
220
+ /**
221
+ * Find a contact by email
222
+ */
223
+ async findContactByEmail(email) {
224
+ const { contacts } = await this.getContacts();
225
+ return contacts.find(c => c.email.toLowerCase() === email.toLowerCase()) || null;
226
+ }
227
+ /**
228
+ * Create or update a contact by email
229
+ */
230
+ async upsertContact(data) {
231
+ const existing = await this.findContactByEmail(data.email);
232
+ if (existing) {
233
+ return this.updateContact(existing.id, {
234
+ subscribed: data.subscribed,
235
+ data: data.data,
236
+ });
237
+ }
238
+ return this.createContact(data);
239
+ }
240
+ /**
241
+ * Get count of subscribed contacts
242
+ */
243
+ async getSubscribedCount() {
244
+ const { contacts } = await this.getContacts();
245
+ return contacts.filter(c => c.subscribed).length;
246
+ }
247
+ }
248
+ /**
249
+ * Create a Plunk client from environment configuration
250
+ */
251
+ export function createPlunkClient(env) {
252
+ if (!env.HG_EMAILS_PLUNK_SECRET_KEY) {
253
+ throw new Error('HG_EMAILS_PLUNK_SECRET_KEY is required');
254
+ }
255
+ return new PlunkClient({
256
+ secretKey: env.HG_EMAILS_PLUNK_SECRET_KEY,
257
+ publicKey: env.HG_EMAILS_PLUNK_PUBLIC_KEY,
258
+ });
259
+ }
@@ -1 +1 @@
1
- {"version":3,"file":"auth.d.ts","sourceRoot":"","sources":["../../src/middleware/auth.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAyCrC,wBAAsB,cAAc,CAAC,CAAC,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI;;;;;;0FAuE1D"}
1
+ {"version":3,"file":"auth.d.ts","sourceRoot":"","sources":["../../src/middleware/auth.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAsCrC,wBAAsB,cAAc,CAAC,CAAC,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI;;;;;;0FAuE1D"}
@@ -8,15 +8,12 @@ function getModuleFromPath(pathname) {
8
8
  if (pathname.includes('/api/admin/cms') || pathname.includes('/api/admin/collections') || pathname.includes('/api/admin/assets')) {
9
9
  return 'cms';
10
10
  }
11
- if (pathname.includes('/api/admin/newsletter')) {
12
- return 'newsletter';
11
+ if (pathname.includes('/api/email/admin')) {
12
+ return 'emails';
13
13
  }
14
14
  if (pathname.includes('/api/admin/shop')) {
15
15
  return 'shop';
16
16
  }
17
- /* if (pathname.includes('/api/admin/builder')) {
18
- return 'builder';
19
- } */
20
17
  return null;
21
18
  }
22
19
  /**
package/package.json CHANGED
@@ -1,33 +1,33 @@
1
1
  {
2
- "name": "@hintergrund/worker",
3
- "version": "0.1.2",
4
- "type": "module",
5
- "main": "./dist/index.js",
6
- "types": "./dist/index.d.ts",
7
- "exports": {
8
- ".": {
9
- "types": "./dist/index.d.ts",
10
- "import": "./dist/index.js"
11
- }
12
- },
13
- "files": [
14
- "dist"
15
- ],
16
- "devDependencies": {
17
- "@cloudflare/workers-types": "^4.20250111.0",
18
- "concurrently": "^9.2.1",
19
- "typescript": "^5.5.2"
20
- },
21
- "dependencies": {
22
- "@octokit/rest": "^22.0.0",
23
- "cookie": "^1.0.2",
24
- "hono": "^4.10.2",
25
- "pdf-lib": "^1.17.1",
26
- "stripe": "^17.5.0",
27
- "@hintergrund/shared": "0.1.1"
28
- },
29
- "scripts": {
30
- "build": "tsc",
31
- "dev": "tsc --watch"
32
- }
33
- }
2
+ "name": "@hintergrund/worker",
3
+ "version": "0.1.5",
4
+ "type": "module",
5
+ "main": "./dist/index.js",
6
+ "types": "./dist/index.d.ts",
7
+ "exports": {
8
+ ".": {
9
+ "types": "./dist/index.d.ts",
10
+ "import": "./dist/index.js"
11
+ }
12
+ },
13
+ "scripts": {
14
+ "build": "tsc",
15
+ "dev": "tsc --watch"
16
+ },
17
+ "files": [
18
+ "dist"
19
+ ],
20
+ "devDependencies": {
21
+ "@cloudflare/workers-types": "^4.20250111.0",
22
+ "concurrently": "^9.2.1",
23
+ "typescript": "^5.5.2"
24
+ },
25
+ "dependencies": {
26
+ "@hintergrund/shared": "workspace:*",
27
+ "@octokit/rest": "^22.0.0",
28
+ "cookie": "^1.0.2",
29
+ "hono": "^4.10.2",
30
+ "pdf-lib": "^1.17.1",
31
+ "stripe": "^17.5.0"
32
+ }
33
+ }