@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.
- package/README.md +45 -0
- package/dist/api/builder/handlers/generate.handler.d.ts +12 -0
- package/dist/api/builder/handlers/generate.handler.d.ts.map +1 -0
- package/dist/api/builder/handlers/generate.handler.js +203 -0
- package/dist/api/builder/handlers/template.handler.d.ts +12 -0
- package/dist/api/builder/handlers/template.handler.d.ts.map +1 -0
- package/dist/api/builder/handlers/template.handler.js +208 -0
- package/dist/api/builder/routes.d.ts +7 -0
- package/dist/api/builder/routes.d.ts.map +1 -0
- package/dist/api/builder/routes.js +15 -0
- package/dist/api/cms/handlers/assets.handler.d.ts +8 -0
- package/dist/api/cms/handlers/assets.handler.d.ts.map +1 -0
- package/dist/api/cms/handlers/assets.handler.js +299 -0
- package/dist/api/cms/handlers/auth.handler.d.ts +11 -0
- package/dist/api/cms/handlers/auth.handler.d.ts.map +1 -0
- package/dist/api/cms/handlers/auth.handler.js +214 -0
- package/dist/api/cms/handlers/config.handler.d.ts +3 -0
- package/dist/api/cms/handlers/config.handler.d.ts.map +1 -0
- package/dist/api/cms/handlers/config.handler.js +88 -0
- package/dist/api/cms/handlers/content.handler.d.ts +10 -0
- package/dist/api/cms/handlers/content.handler.d.ts.map +1 -0
- package/dist/api/cms/handlers/content.handler.js +293 -0
- package/dist/api/cms/handlers/dashboard.handler.d.ts +6 -0
- package/dist/api/cms/handlers/dashboard.handler.d.ts.map +1 -0
- package/dist/api/cms/handlers/dashboard.handler.js +15 -0
- package/dist/api/cms/routes/assets.routes.d.ts +4 -0
- package/dist/api/cms/routes/assets.routes.d.ts.map +1 -0
- package/dist/api/cms/routes/assets.routes.js +13 -0
- package/dist/api/cms/routes/auth.routes.d.ts +4 -0
- package/dist/api/cms/routes/auth.routes.d.ts.map +1 -0
- package/dist/api/cms/routes/auth.routes.js +7 -0
- package/dist/api/cms/routes/content.routes.d.ts +4 -0
- package/dist/api/cms/routes/content.routes.d.ts.map +1 -0
- package/dist/api/cms/routes/content.routes.js +11 -0
- package/dist/api/cms/routes/index.d.ts +4 -0
- package/dist/api/cms/routes/index.d.ts.map +1 -0
- package/dist/api/cms/routes/index.js +16 -0
- package/dist/api/newsletter/handlers/email.handler.d.ts +7 -0
- package/dist/api/newsletter/handlers/email.handler.d.ts.map +1 -0
- package/dist/api/newsletter/handlers/email.handler.js +102 -0
- package/dist/api/newsletter/handlers/emails.handler.d.ts +21 -0
- package/dist/api/newsletter/handlers/emails.handler.d.ts.map +1 -0
- package/dist/api/newsletter/handlers/emails.handler.js +134 -0
- package/dist/api/newsletter/handlers/public.handler.d.ts +22 -0
- package/dist/api/newsletter/handlers/public.handler.d.ts.map +1 -0
- package/dist/api/newsletter/handlers/public.handler.js +242 -0
- package/dist/api/newsletter/handlers/send.handler.d.ts +12 -0
- package/dist/api/newsletter/handlers/send.handler.d.ts.map +1 -0
- package/dist/api/newsletter/handlers/send.handler.js +153 -0
- package/dist/api/newsletter/handlers/subscribers.handler.d.ts +22 -0
- package/dist/api/newsletter/handlers/subscribers.handler.d.ts.map +1 -0
- package/dist/api/newsletter/handlers/subscribers.handler.js +191 -0
- package/dist/api/newsletter/routes/admin.routes.d.ts +4 -0
- package/dist/api/newsletter/routes/admin.routes.d.ts.map +1 -0
- package/dist/api/newsletter/routes/admin.routes.js +21 -0
- package/dist/api/newsletter/routes/index.d.ts +4 -0
- package/dist/api/newsletter/routes/index.d.ts.map +1 -0
- package/dist/api/newsletter/routes/index.js +9 -0
- package/dist/api/newsletter/routes/public.routes.d.ts +4 -0
- package/dist/api/newsletter/routes/public.routes.d.ts.map +1 -0
- package/dist/api/newsletter/routes/public.routes.js +9 -0
- package/dist/api/shop/handlers/customers.handler.d.ts +17 -0
- package/dist/api/shop/handlers/customers.handler.d.ts.map +1 -0
- package/dist/api/shop/handlers/customers.handler.js +89 -0
- package/dist/api/shop/handlers/orders.handler.d.ts +17 -0
- package/dist/api/shop/handlers/orders.handler.d.ts.map +1 -0
- package/dist/api/shop/handlers/orders.handler.js +84 -0
- package/dist/api/shop/handlers/prices.handler.d.ts +17 -0
- package/dist/api/shop/handlers/prices.handler.d.ts.map +1 -0
- package/dist/api/shop/handlers/prices.handler.js +87 -0
- package/dist/api/shop/handlers/products.handler.d.ts +45 -0
- package/dist/api/shop/handlers/products.handler.d.ts.map +1 -0
- package/dist/api/shop/handlers/products.handler.js +243 -0
- package/dist/api/shop/handlers/stats.handler.d.ts +10 -0
- package/dist/api/shop/handlers/stats.handler.d.ts.map +1 -0
- package/dist/api/shop/handlers/stats.handler.js +71 -0
- package/dist/api/shop/routes.d.ts +7 -0
- package/dist/api/shop/routes.d.ts.map +1 -0
- package/dist/api/shop/routes.js +31 -0
- package/dist/cms-assets.d.ts +4 -0
- package/dist/cms-assets.d.ts.map +1 -0
- package/dist/cms-assets.js +5 -0
- package/dist/cms-bundle.d.ts +3 -0
- package/dist/cms-bundle.d.ts.map +1 -0
- package/dist/cms-bundle.js +4 -0
- package/dist/config-loader.d.ts +26 -0
- package/dist/config-loader.d.ts.map +1 -0
- package/dist/config-loader.js +98 -0
- package/dist/hintergrund-assets.d.ts +4 -0
- package/dist/hintergrund-assets.d.ts.map +1 -0
- package/dist/hintergrund-assets.js +9 -0
- package/dist/index.d.ts +35 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +66 -0
- package/dist/jwt.d.ts +26 -0
- package/dist/jwt.d.ts.map +1 -0
- package/dist/jwt.js +102 -0
- package/dist/lib/auth/crypto.d.ts +23 -0
- package/dist/lib/auth/crypto.d.ts.map +1 -0
- package/dist/lib/auth/crypto.js +110 -0
- package/dist/lib/auth/jwt.d.ts +38 -0
- package/dist/lib/auth/jwt.d.ts.map +1 -0
- package/dist/lib/auth/jwt.js +120 -0
- package/dist/lib/config.d.ts +31 -0
- package/dist/lib/config.d.ts.map +1 -0
- package/dist/lib/config.js +174 -0
- package/dist/lib/email.d.ts +16 -0
- package/dist/lib/email.d.ts.map +1 -0
- package/dist/lib/email.js +83 -0
- package/dist/lib/template.d.ts +2 -0
- package/dist/lib/template.d.ts.map +1 -0
- package/dist/lib/template.js +226 -0
- package/dist/middleware/auth.d.ts +9 -0
- package/dist/middleware/auth.d.ts.map +1 -0
- package/dist/middleware/auth.js +93 -0
- package/dist/template.d.ts +2 -0
- package/dist/template.d.ts.map +1 -0
- package/dist/template.js +61 -0
- package/dist/utils.d.ts +23 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +110 -0
- package/package.json +32 -0
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
import { Octokit } from '@octokit/rest';
|
|
2
|
+
import { getCmsConfig } from '../../../lib/config';
|
|
3
|
+
/**
|
|
4
|
+
* Decode base64 content to UTF-8 string.
|
|
5
|
+
* atob() decodes to Latin-1, which corrupts multi-byte UTF-8 characters.
|
|
6
|
+
*/
|
|
7
|
+
function decodeBase64UTF8(base64) {
|
|
8
|
+
const binaryString = atob(base64);
|
|
9
|
+
const bytes = new Uint8Array(binaryString.length);
|
|
10
|
+
for (let i = 0; i < binaryString.length; i++) {
|
|
11
|
+
bytes[i] = binaryString.charCodeAt(i);
|
|
12
|
+
}
|
|
13
|
+
return new TextDecoder('utf-8').decode(bytes);
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Encode UTF-8 string to base64.
|
|
17
|
+
* btoa() only works with Latin-1, which corrupts multi-byte UTF-8 characters like ß.
|
|
18
|
+
*/
|
|
19
|
+
function encodeUTF8Base64(str) {
|
|
20
|
+
const bytes = new TextEncoder().encode(str);
|
|
21
|
+
let binaryString = '';
|
|
22
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
23
|
+
binaryString += String.fromCharCode(bytes[i]);
|
|
24
|
+
}
|
|
25
|
+
return btoa(binaryString);
|
|
26
|
+
}
|
|
27
|
+
export async function getContents(c) {
|
|
28
|
+
try {
|
|
29
|
+
const config = getCmsConfig(c.env);
|
|
30
|
+
const octokit = new Octokit({
|
|
31
|
+
auth: config.gitToken
|
|
32
|
+
});
|
|
33
|
+
// Get current commit SHA from branch
|
|
34
|
+
const ref = await octokit.rest.git.getRef({
|
|
35
|
+
owner: config.gitOwner,
|
|
36
|
+
repo: config.gitRepo,
|
|
37
|
+
ref: `heads/${config.gitBranch}`
|
|
38
|
+
});
|
|
39
|
+
const commitSha = ref.data.object.sha;
|
|
40
|
+
const configFile = await octokit.rest.repos.getContent({
|
|
41
|
+
owner: config.gitOwner,
|
|
42
|
+
repo: config.gitRepo,
|
|
43
|
+
path: `${config.contentDir}/config.json`
|
|
44
|
+
});
|
|
45
|
+
// Type guard to check if data is a file (not array or directory)
|
|
46
|
+
if (Array.isArray(configFile.data) || !('content' in configFile.data)) {
|
|
47
|
+
throw new Error('Config file not found or is not a file');
|
|
48
|
+
}
|
|
49
|
+
const contentConfig = JSON.parse(decodeBase64UTF8(configFile.data.content || ''));
|
|
50
|
+
const collections = await Promise.all(Object.keys(contentConfig).map(async (content) => {
|
|
51
|
+
const contentFile = await octokit.rest.repos.getContent({
|
|
52
|
+
owner: config.gitOwner,
|
|
53
|
+
repo: config.gitRepo,
|
|
54
|
+
path: `${config.contentDir}/${content}.json`
|
|
55
|
+
});
|
|
56
|
+
// Type guard to check if data is a file (not array or directory)
|
|
57
|
+
if (Array.isArray(contentFile.data) || !('content' in contentFile.data)) {
|
|
58
|
+
throw new Error(`Content file ${content}.json not found or is not a file`);
|
|
59
|
+
}
|
|
60
|
+
return {
|
|
61
|
+
[content]: JSON.parse(decodeBase64UTF8(contentFile.data.content || ''))
|
|
62
|
+
};
|
|
63
|
+
}));
|
|
64
|
+
const collectionsObject = collections.reduce((acc, cur) => ({ ...acc, ...cur }), {});
|
|
65
|
+
const response = {
|
|
66
|
+
config: contentConfig,
|
|
67
|
+
collections: collectionsObject,
|
|
68
|
+
commitSha
|
|
69
|
+
};
|
|
70
|
+
return new Response(JSON.stringify(response), {
|
|
71
|
+
status: 200
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
catch (error) {
|
|
75
|
+
console.error('Error fetching contents:', error);
|
|
76
|
+
return new Response(JSON.stringify({
|
|
77
|
+
error: error.message || 'Failed to fetch contents',
|
|
78
|
+
details: error.toString()
|
|
79
|
+
}), {
|
|
80
|
+
status: 500,
|
|
81
|
+
headers: { 'Content-Type': 'application/json' }
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Sync contents from GitHub - optimized for conditional fetching
|
|
87
|
+
* Only fetches content data from GitHub if commitSha has changed
|
|
88
|
+
*/
|
|
89
|
+
export async function syncContents(c) {
|
|
90
|
+
try {
|
|
91
|
+
const config = getCmsConfig(c.env);
|
|
92
|
+
const octokit = new Octokit({
|
|
93
|
+
auth: config.gitToken
|
|
94
|
+
});
|
|
95
|
+
// Get current commit SHA from branch
|
|
96
|
+
const ref = await octokit.rest.git.getRef({
|
|
97
|
+
owner: config.gitOwner,
|
|
98
|
+
repo: config.gitRepo,
|
|
99
|
+
ref: `heads/${config.gitBranch}`
|
|
100
|
+
});
|
|
101
|
+
const commitSha = ref.data.object.sha;
|
|
102
|
+
// Always fetch config (it's small and may change without commitSha changing)
|
|
103
|
+
const configFile = await octokit.rest.repos.getContent({
|
|
104
|
+
owner: config.gitOwner,
|
|
105
|
+
repo: config.gitRepo,
|
|
106
|
+
path: `${config.contentDir}/config.json`
|
|
107
|
+
});
|
|
108
|
+
// Type guard to check if data is a file (not array or directory)
|
|
109
|
+
if (Array.isArray(configFile.data) || !('content' in configFile.data)) {
|
|
110
|
+
throw new Error('Config file not found or is not a file');
|
|
111
|
+
}
|
|
112
|
+
const contentConfig = JSON.parse(decodeBase64UTF8(configFile.data.content || ''));
|
|
113
|
+
// Fetch all content files from GitHub
|
|
114
|
+
const collections = await Promise.all(Object.keys(contentConfig).map(async (content) => {
|
|
115
|
+
const contentFile = await octokit.rest.repos.getContent({
|
|
116
|
+
owner: config.gitOwner,
|
|
117
|
+
repo: config.gitRepo,
|
|
118
|
+
path: `${config.contentDir}/${content}.json`
|
|
119
|
+
});
|
|
120
|
+
// Type guard to check if data is a file (not array or directory)
|
|
121
|
+
if (Array.isArray(contentFile.data) || !('content' in contentFile.data)) {
|
|
122
|
+
throw new Error(`Content file ${content}.json not found or is not a file`);
|
|
123
|
+
}
|
|
124
|
+
return {
|
|
125
|
+
[content]: JSON.parse(decodeBase64UTF8(contentFile.data.content || ''))
|
|
126
|
+
};
|
|
127
|
+
}));
|
|
128
|
+
const collectionsObject = collections.reduce((acc, cur) => ({ ...acc, ...cur }), {});
|
|
129
|
+
const response = {
|
|
130
|
+
config: contentConfig,
|
|
131
|
+
collections: collectionsObject,
|
|
132
|
+
commitSha
|
|
133
|
+
};
|
|
134
|
+
return new Response(JSON.stringify(response), {
|
|
135
|
+
status: 200,
|
|
136
|
+
headers: {
|
|
137
|
+
'Content-Type': 'application/json'
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
catch (error) {
|
|
142
|
+
console.error('Error syncing contents:', error);
|
|
143
|
+
return new Response(JSON.stringify({
|
|
144
|
+
error: error.message || 'Failed to sync contents',
|
|
145
|
+
details: error.toString()
|
|
146
|
+
}), {
|
|
147
|
+
status: 500,
|
|
148
|
+
headers: { 'Content-Type': 'application/json' }
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
export async function updateContents(c) {
|
|
153
|
+
try {
|
|
154
|
+
const request = await c.req.json();
|
|
155
|
+
const config = getCmsConfig(c.env);
|
|
156
|
+
const octokit = new Octokit({
|
|
157
|
+
auth: config.gitToken
|
|
158
|
+
});
|
|
159
|
+
// Get reference from branch
|
|
160
|
+
const ref = await octokit.rest.git.getRef({
|
|
161
|
+
owner: config.gitOwner,
|
|
162
|
+
repo: config.gitRepo,
|
|
163
|
+
ref: `heads/${config.gitBranch}`
|
|
164
|
+
});
|
|
165
|
+
const currentCommitSha = ref.data.object.sha;
|
|
166
|
+
// Check for conflicts - if commit SHA doesn't match, someone else has committed
|
|
167
|
+
if (request.commitSha !== currentCommitSha) {
|
|
168
|
+
// Fetch current collections to show in conflict UI
|
|
169
|
+
const configFile = await octokit.rest.repos.getContent({
|
|
170
|
+
owner: config.gitOwner,
|
|
171
|
+
repo: config.gitRepo,
|
|
172
|
+
path: `${config.contentDir}/config.json`
|
|
173
|
+
});
|
|
174
|
+
if (Array.isArray(configFile.data) || !('content' in configFile.data)) {
|
|
175
|
+
throw new Error('Config file not found or is not a file');
|
|
176
|
+
}
|
|
177
|
+
const contentConfig = JSON.parse(decodeBase64UTF8(configFile.data.content || ''));
|
|
178
|
+
const collections = await Promise.all(Object.keys(contentConfig).map(async (content) => {
|
|
179
|
+
const contentFile = await octokit.rest.repos.getContent({
|
|
180
|
+
owner: config.gitOwner,
|
|
181
|
+
repo: config.gitRepo,
|
|
182
|
+
path: `${config.contentDir}/${content}.json`
|
|
183
|
+
});
|
|
184
|
+
if (Array.isArray(contentFile.data) || !('content' in contentFile.data)) {
|
|
185
|
+
throw new Error(`Content file ${content}.json not found or is not a file`);
|
|
186
|
+
}
|
|
187
|
+
return {
|
|
188
|
+
[content]: JSON.parse(decodeBase64UTF8(contentFile.data.content || ''))
|
|
189
|
+
};
|
|
190
|
+
}));
|
|
191
|
+
const serverCollections = collections.reduce((acc, cur) => ({ ...acc, ...cur }), {});
|
|
192
|
+
const conflictResponse = {
|
|
193
|
+
conflict: true,
|
|
194
|
+
currentCommitSha,
|
|
195
|
+
serverCollections,
|
|
196
|
+
message: 'Content has been modified by another user. Please resolve conflicts.'
|
|
197
|
+
};
|
|
198
|
+
return new Response(JSON.stringify(conflictResponse), { status: 409 });
|
|
199
|
+
}
|
|
200
|
+
// Get latest commit
|
|
201
|
+
const commit = await octokit.rest.git.getCommit({
|
|
202
|
+
owner: config.gitOwner,
|
|
203
|
+
repo: config.gitRepo,
|
|
204
|
+
commit_sha: currentCommitSha
|
|
205
|
+
});
|
|
206
|
+
// Create blob for each change and save file tree
|
|
207
|
+
const blobs = await Promise.all(Object.keys(request.collections).map(async (content) => {
|
|
208
|
+
const blob = await octokit.rest.git.createBlob({
|
|
209
|
+
owner: config.gitOwner,
|
|
210
|
+
repo: config.gitRepo,
|
|
211
|
+
content: JSON.stringify(request.collections[content], null, 4),
|
|
212
|
+
encoding: 'utf-8'
|
|
213
|
+
});
|
|
214
|
+
return {
|
|
215
|
+
path: `${config.contentDir}/${content}.json`,
|
|
216
|
+
mode: '100644',
|
|
217
|
+
type: 'blob',
|
|
218
|
+
sha: blob.data.sha
|
|
219
|
+
};
|
|
220
|
+
}));
|
|
221
|
+
// Create tree
|
|
222
|
+
const newTree = await octokit.rest.git.createTree({
|
|
223
|
+
owner: config.gitOwner,
|
|
224
|
+
repo: config.gitRepo,
|
|
225
|
+
tree: blobs,
|
|
226
|
+
base_tree: commit.data.tree.sha
|
|
227
|
+
});
|
|
228
|
+
// Create commit
|
|
229
|
+
const newCommit = await octokit.rest.git.createCommit({
|
|
230
|
+
owner: config.gitOwner,
|
|
231
|
+
repo: config.gitRepo,
|
|
232
|
+
message: 'Update content',
|
|
233
|
+
tree: newTree.data.sha,
|
|
234
|
+
parents: [commit.data.sha]
|
|
235
|
+
});
|
|
236
|
+
// Update reference
|
|
237
|
+
await octokit.rest.git.updateRef({
|
|
238
|
+
owner: config.gitOwner,
|
|
239
|
+
repo: config.gitRepo,
|
|
240
|
+
ref: `heads/${config.gitBranch}`,
|
|
241
|
+
sha: newCommit.data.sha
|
|
242
|
+
});
|
|
243
|
+
const response = { success: true };
|
|
244
|
+
return new Response(JSON.stringify(response), { status: 200 });
|
|
245
|
+
}
|
|
246
|
+
catch (error) {
|
|
247
|
+
console.log(error);
|
|
248
|
+
return new Response(JSON.stringify(error), {
|
|
249
|
+
status: 500
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
export async function deleteContent(c) {
|
|
254
|
+
try {
|
|
255
|
+
const deleteData = await c.req.json();
|
|
256
|
+
const contentId = Object.keys(deleteData)[0];
|
|
257
|
+
const config = getCmsConfig(c.env);
|
|
258
|
+
const octokit = new Octokit({
|
|
259
|
+
auth: config.gitToken
|
|
260
|
+
});
|
|
261
|
+
const fileInfo = await octokit.rest.repos.getContent({
|
|
262
|
+
owner: config.gitOwner,
|
|
263
|
+
repo: config.gitRepo,
|
|
264
|
+
path: `${config.contentDir}/${contentId}.json`,
|
|
265
|
+
ref: config.gitBranch
|
|
266
|
+
});
|
|
267
|
+
// Type guard to check if data is a file (not array or directory)
|
|
268
|
+
if (Array.isArray(fileInfo.data) || !('sha' in fileInfo.data)) {
|
|
269
|
+
throw new Error(`File ${contentId}.json not found or is not a file`);
|
|
270
|
+
}
|
|
271
|
+
const response = await octokit.rest.repos.createOrUpdateFileContents({
|
|
272
|
+
owner: config.gitOwner,
|
|
273
|
+
repo: config.gitRepo,
|
|
274
|
+
path: `${config.contentDir}/${contentId}.json`,
|
|
275
|
+
message: `Delete record(s) from ${contentId} [CI SKIP]`,
|
|
276
|
+
content: encodeUTF8Base64(JSON.stringify(deleteData[contentId], null, 4)),
|
|
277
|
+
branch: config.gitBranch,
|
|
278
|
+
sha: fileInfo.data.sha
|
|
279
|
+
});
|
|
280
|
+
if (response.status !== 200) {
|
|
281
|
+
return new Response(`Error deleting record(s) from ${contentId}`, {
|
|
282
|
+
status: 500
|
|
283
|
+
});
|
|
284
|
+
}
|
|
285
|
+
return new Response(`Deleted record from ${contentId}`, { status: 200 });
|
|
286
|
+
}
|
|
287
|
+
catch (error) {
|
|
288
|
+
console.log(error);
|
|
289
|
+
return new Response(error.message, {
|
|
290
|
+
status: 500
|
|
291
|
+
});
|
|
292
|
+
}
|
|
293
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dashboard.handler.d.ts","sourceRoot":"","sources":["../../../../src/api/cms/handlers/dashboard.handler.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AAE/B,OAAO,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAC;AAGpC,wBAAsB,YAAY,CAAC,CAAC,EAAE,OAAO,CAAC;IAAE,QAAQ,EAAE,QAAQ,CAAA;CAAE,CAAC,GAAG,OAAO,CAAC,QAAQ,CAAC,CAYxF"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { getTemplate } from '../../../lib/template';
|
|
2
|
+
import { loadConfig, getAdminConfig } from '../../../lib/config';
|
|
3
|
+
export async function getDashboard(c) {
|
|
4
|
+
try {
|
|
5
|
+
const appConfig = loadConfig(c.env);
|
|
6
|
+
const adminConfig = getAdminConfig(appConfig);
|
|
7
|
+
const configParams = `config='${JSON.stringify(adminConfig)}'`;
|
|
8
|
+
return c.html(getTemplate('admin', c.env.UI_BASE_URL, configParams));
|
|
9
|
+
}
|
|
10
|
+
catch (error) {
|
|
11
|
+
// Fallback to default config if parsing fails
|
|
12
|
+
const configParams = 'config=\'{"cms":true,"newsletter":false,"shop":false}\'';
|
|
13
|
+
return c.html(getTemplate('admin', c.env.UI_BASE_URL, configParams));
|
|
14
|
+
}
|
|
15
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"assets.routes.d.ts","sourceRoot":"","sources":["../../../../src/api/cms/routes/assets.routes.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAG5B,QAAA,MAAM,YAAY,4EAAa,CAAC;AAYhC,eAAe,YAAY,CAAC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Hono } from 'hono';
|
|
2
|
+
import { listAssets, uploadAsset, deleteAsset, getAssetByPath, syncAssets, syncAssetsMock } from '../handlers/assets.handler';
|
|
3
|
+
const assetsRoutes = new Hono();
|
|
4
|
+
assetsRoutes.get('/', listAssets);
|
|
5
|
+
assetsRoutes.put('/', uploadAsset);
|
|
6
|
+
assetsRoutes.delete('/', deleteAsset);
|
|
7
|
+
assetsRoutes.get('/sync', (c) => {
|
|
8
|
+
// Check if we should use mock data
|
|
9
|
+
const useMock = c.req.header('X-Use-Mock') === 'true';
|
|
10
|
+
return useMock ? syncAssetsMock(c) : syncAssets(c);
|
|
11
|
+
});
|
|
12
|
+
assetsRoutes.get('/:path', getAssetByPath);
|
|
13
|
+
export default assetsRoutes;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auth.routes.d.ts","sourceRoot":"","sources":["../../../../src/api/cms/routes/auth.routes.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAG5B,QAAA,MAAM,UAAU,4EAAa,CAAC;AAM9B,eAAe,UAAU,CAAC"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { Hono } from 'hono';
|
|
2
|
+
import { login, logout, verifyMagicLink } from '../handlers/auth.handler';
|
|
3
|
+
const authRoutes = new Hono();
|
|
4
|
+
authRoutes.post('/login', login);
|
|
5
|
+
authRoutes.post('/logout', logout);
|
|
6
|
+
authRoutes.get('/verify-magic-link', verifyMagicLink);
|
|
7
|
+
export default authRoutes;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"content.routes.d.ts","sourceRoot":"","sources":["../../../../src/api/cms/routes/content.routes.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAI5B,QAAA,MAAM,aAAa,4EAAa,CAAC;AASjC,eAAe,aAAa,CAAC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Hono } from 'hono';
|
|
2
|
+
import { getContents, updateContents, deleteContent, syncContents } from '../handlers/content.handler';
|
|
3
|
+
import { updateConfig } from '../handlers/config.handler';
|
|
4
|
+
const contentRoutes = new Hono();
|
|
5
|
+
// IMPORTANT: More specific routes must come before generic routes
|
|
6
|
+
contentRoutes.get('/collections/sync', syncContents);
|
|
7
|
+
contentRoutes.get('/collections', getContents);
|
|
8
|
+
contentRoutes.put('/collections', updateContents);
|
|
9
|
+
contentRoutes.delete('/collections', deleteContent);
|
|
10
|
+
contentRoutes.put('/config', updateConfig);
|
|
11
|
+
export default contentRoutes;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/api/cms/routes/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAO5B,QAAA,MAAM,SAAS,4EAAa,CAAC;AAa7B,eAAe,SAAS,CAAC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { Hono } from 'hono';
|
|
2
|
+
import { getDashboard } from '../handlers/dashboard.handler';
|
|
3
|
+
import { authMiddleware } from '../../../middleware/auth';
|
|
4
|
+
import authRoutes from './auth.routes';
|
|
5
|
+
import contentRoutes from './content.routes';
|
|
6
|
+
import assetsRoutes from './assets.routes';
|
|
7
|
+
const cmsRoutes = new Hono();
|
|
8
|
+
// Dashboard route (protected)
|
|
9
|
+
cmsRoutes.get('/', authMiddleware, getDashboard);
|
|
10
|
+
// Auth routes (public)
|
|
11
|
+
cmsRoutes.route('/', authRoutes);
|
|
12
|
+
// Protected routes
|
|
13
|
+
cmsRoutes.use('/*', authMiddleware);
|
|
14
|
+
cmsRoutes.route('/', contentRoutes);
|
|
15
|
+
cmsRoutes.route('/assets', assetsRoutes);
|
|
16
|
+
export default cmsRoutes;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"email.handler.d.ts","sourceRoot":"","sources":["../../../../src/api/newsletter/handlers/email.handler.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AAsC/B;;;GAGG;AACH,wBAAsB,aAAa,CAAC,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,QAAQ,CAAC,CAuFjE"}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
// Send email via Resend
|
|
2
|
+
async function sendEmail(to, subject, html, apiKey, from, fromName, replyTo) {
|
|
3
|
+
const response = await fetch('https://api.resend.com/emails', {
|
|
4
|
+
method: 'POST',
|
|
5
|
+
headers: {
|
|
6
|
+
Authorization: `Bearer ${apiKey}`,
|
|
7
|
+
'Content-Type': 'application/json',
|
|
8
|
+
},
|
|
9
|
+
body: JSON.stringify({
|
|
10
|
+
from: `${fromName} <${from}>`,
|
|
11
|
+
to,
|
|
12
|
+
subject,
|
|
13
|
+
html,
|
|
14
|
+
reply_to: replyTo,
|
|
15
|
+
}),
|
|
16
|
+
});
|
|
17
|
+
if (!response.ok) {
|
|
18
|
+
const error = await response.text();
|
|
19
|
+
throw new Error(`Failed to send email to ${to}: ${error}`);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
// Generate email ID
|
|
23
|
+
function generateId() {
|
|
24
|
+
return Math.random().toString(36).substring(2, 14);
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* POST /api/newsletter/admin/send
|
|
28
|
+
* Send bulk email to all subscribed users (admin only)
|
|
29
|
+
*/
|
|
30
|
+
export async function sendBulkEmail(c) {
|
|
31
|
+
try {
|
|
32
|
+
const body = (await c.req.json());
|
|
33
|
+
const { subject, html } = body;
|
|
34
|
+
if (!subject || !html) {
|
|
35
|
+
return c.json({ success: false, message: 'Subject and HTML content are required' }, 400);
|
|
36
|
+
}
|
|
37
|
+
const db = c.env.DB;
|
|
38
|
+
const apiKey = c.env.HG_NEWSLETTER_EMAIL_API_KEY;
|
|
39
|
+
const fromEmail = c.env.HG_NEWSLETTER_FROM_EMAIL;
|
|
40
|
+
const fromName = c.env.HG_NEWSLETTER_FROM_NAME;
|
|
41
|
+
const replyTo = c.env.HG_NEWSLETTER_REPLY_TO;
|
|
42
|
+
if (!db || !apiKey || !fromEmail) {
|
|
43
|
+
return c.json({ success: false, message: 'Newsletter not configured' }, 500);
|
|
44
|
+
}
|
|
45
|
+
// Fetch all subscribed users
|
|
46
|
+
const result = await db.prepare('SELECT id, email, unsubscribe_token FROM subscribers WHERE status = \'subscribed\'').all();
|
|
47
|
+
const subscribers = result.results;
|
|
48
|
+
if (subscribers.length === 0) {
|
|
49
|
+
return c.json({ success: true, sent: 0, message: 'No subscribed users found' });
|
|
50
|
+
}
|
|
51
|
+
// Send emails to all subscribers
|
|
52
|
+
let sent = 0;
|
|
53
|
+
const errors = [];
|
|
54
|
+
for (const subscriber of subscribers) {
|
|
55
|
+
try {
|
|
56
|
+
// Append unsubscribe link to HTML
|
|
57
|
+
const unsubscribeUrl = `${c.req.url.split('/api')[0]}/api/newsletter/unsubscribe?token=${subscriber.unsubscribe_token}`;
|
|
58
|
+
const htmlWithUnsubscribe = `
|
|
59
|
+
${html}
|
|
60
|
+
<hr style="margin-top: 2rem; margin-bottom: 1rem; border: none; border-top: 1px solid #eee;">
|
|
61
|
+
<p style="font-size: 0.875rem; color: #666;">
|
|
62
|
+
<a href="${unsubscribeUrl}" style="color: #666;">Unsubscribe from this newsletter</a>
|
|
63
|
+
</p>
|
|
64
|
+
`;
|
|
65
|
+
await sendEmail(subscriber.email, subject, htmlWithUnsubscribe, apiKey, fromEmail, fromName || 'Newsletter', replyTo);
|
|
66
|
+
sent++;
|
|
67
|
+
}
|
|
68
|
+
catch (error) {
|
|
69
|
+
console.error(`Failed to send email to ${subscriber.email}:`, error);
|
|
70
|
+
errors.push(subscriber.email);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
// Save sent email to database
|
|
74
|
+
const emailId = generateId();
|
|
75
|
+
const now = Date.now();
|
|
76
|
+
await db
|
|
77
|
+
.prepare(`INSERT INTO emails
|
|
78
|
+
(id, subject, html_content, status, recipient_count, sent_at, created_at, updated_at)
|
|
79
|
+
VALUES (?, ?, ?, 'sent', ?, ?, ?, ?)`)
|
|
80
|
+
.bind(emailId, subject, html, sent, now, now, now)
|
|
81
|
+
.run();
|
|
82
|
+
if (errors.length > 0) {
|
|
83
|
+
return c.json({
|
|
84
|
+
success: true,
|
|
85
|
+
sent,
|
|
86
|
+
message: `Sent to ${sent} subscribers. Failed to send to: ${errors.join(', ')}`,
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
return c.json({
|
|
90
|
+
success: true,
|
|
91
|
+
sent,
|
|
92
|
+
message: `Successfully sent email to ${sent} subscriber${sent === 1 ? '' : 's'}`,
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
catch (error) {
|
|
96
|
+
console.error('Send bulk email error:', error);
|
|
97
|
+
return c.json({
|
|
98
|
+
success: false,
|
|
99
|
+
message: error instanceof Error ? error.message : 'Failed to send emails',
|
|
100
|
+
}, 500);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { Context } from 'hono';
|
|
2
|
+
/**
|
|
3
|
+
* GET /api/newsletter/admin/emails
|
|
4
|
+
* Get all emails (sent emails and drafts)
|
|
5
|
+
*/
|
|
6
|
+
export declare function getEmails(c: Context): Promise<Response>;
|
|
7
|
+
/**
|
|
8
|
+
* POST /api/newsletter/admin/emails/draft
|
|
9
|
+
* Save email as draft
|
|
10
|
+
*/
|
|
11
|
+
export declare function saveDraft(c: Context): Promise<Response>;
|
|
12
|
+
/**
|
|
13
|
+
* DELETE /api/newsletter/admin/emails/:id
|
|
14
|
+
* Delete email (draft or sent)
|
|
15
|
+
*/
|
|
16
|
+
export declare function deleteEmail(c: Context): Promise<Response>;
|
|
17
|
+
/**
|
|
18
|
+
* Save sent email record to database
|
|
19
|
+
*/
|
|
20
|
+
export declare function saveSentEmail(db: D1Database, subject: string, htmlContent: string, recipientCount: number): Promise<void>;
|
|
21
|
+
//# sourceMappingURL=emails.handler.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"emails.handler.d.ts","sourceRoot":"","sources":["../../../../src/api/newsletter/handlers/emails.handler.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AAwB/B;;;GAGG;AACH,wBAAsB,SAAS,CAAC,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,QAAQ,CAAC,CAmB7D;AAED;;;GAGG;AACH,wBAAsB,SAAS,CAAC,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,QAAQ,CAAC,CAsE7D;AAED;;;GAGG;AACH,wBAAsB,WAAW,CAAC,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,QAAQ,CAAC,CA+B/D;AAED;;GAEG;AACH,wBAAsB,aAAa,CAClC,EAAE,EAAE,UAAU,EACd,OAAO,EAAE,MAAM,EACf,WAAW,EAAE,MAAM,EACnB,cAAc,EAAE,MAAM,GACpB,OAAO,CAAC,IAAI,CAAC,CAWf"}
|