@bbki.ng/backend 0.3.11 → 0.3.13

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/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # backend
2
2
 
3
+ ## 0.3.13
4
+
5
+ ### Patch Changes
6
+
7
+ - aaee63e: fix style
8
+
9
+ ## 0.3.12
10
+
11
+ ### Patch Changes
12
+
13
+ - 25738cb: fix: cors error
14
+
3
15
  ## 0.3.11
4
16
 
5
17
  ### Patch Changes
package/package.json CHANGED
@@ -1,16 +1,18 @@
1
1
  {
2
2
  "name": "@bbki.ng/backend",
3
- "version": "0.3.11",
3
+ "version": "0.3.13",
4
4
  "type": "module",
5
5
  "dependencies": {
6
6
  "@simplewebauthn/server": "13.2.2",
7
- "hono": "^4.10.7"
7
+ "hono": "^4.10.7",
8
+ "showdown": "^2.1.0"
8
9
  },
9
10
  "devDependencies": {
10
11
  "@cloudflare/workers-types": "4.20251128.0",
11
12
  "@eslint/compat": "^1.0.0",
12
13
  "@eslint/js": "^8.57.0",
13
14
  "@types/node": "^20.0.0",
15
+ "@types/showdown": "^2.0.6",
14
16
  "@typescript-eslint/eslint-plugin": "^7.0.0",
15
17
  "@typescript-eslint/parser": "^7.0.0",
16
18
  "eslint": "^8.57.0",
@@ -12,7 +12,7 @@ const app = new Hono<{ Bindings: Bindings }>();
12
12
  app.use(
13
13
  '*',
14
14
  cors({
15
- origin: ['https://bbki.ng', '*'],
15
+ origin: '*',
16
16
  })
17
17
  );
18
18
 
@@ -1,5 +1,9 @@
1
1
  import { Context } from 'hono';
2
2
  import { HTTPException } from 'hono/http-exception';
3
+ import showdown from 'showdown';
4
+
5
+ const converter = new showdown.Converter();
6
+ converter.setFlavor('github');
3
7
 
4
8
  interface AddPostRequest {
5
9
  title: string;
@@ -13,7 +17,7 @@ const MAX_CONTENT_LENGTH = 100000;
13
17
  export const addPost = async (c: Context) => {
14
18
  try {
15
19
  // Parse and validate request body
16
- const body = await c.req.json<AddPostRequest>();
20
+ const body = (await c.req.json()) as AddPostRequest;
17
21
 
18
22
  if (!body.title || typeof body.title !== 'string') {
19
23
  throw new HTTPException(400, { message: 'Title is required' });
@@ -44,32 +48,48 @@ export const addPost = async (c: Context) => {
44
48
 
45
49
  const author = (body.author ?? 'bbki.ng').trim();
46
50
 
51
+ // Convert markdown to HTML
52
+ const html = converter.makeHtml(content);
53
+ const wrappedHtml = html.includes('<p>') ? html : `<p>${html}</p>`;
54
+
47
55
  // Check database availability
48
56
  if (!c.env?.DB) {
49
57
  throw new HTTPException(503, { message: 'Database unavailable' });
50
58
  }
51
59
 
52
- // Insert post
53
- const id = crypto.randomUUID();
54
60
  const now = new Date().toISOString();
55
61
 
56
- try {
62
+ // Check if post with same title already exists (upsert)
63
+ const existingPost = (await c.env.DB.prepare('SELECT id FROM posts WHERE title = ?')
64
+ .bind(title)
65
+ .first()) as { id: string } | null;
66
+
67
+ if (existingPost) {
57
68
  await c.env.DB.prepare(
58
- 'INSERT INTO posts (id, title, content, author, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?)'
69
+ 'UPDATE posts SET content = ?, author = ?, updated_at = ? WHERE id = ?'
59
70
  )
60
- .bind(id, title, content, author, now, now)
71
+ .bind(wrappedHtml, author, now, existingPost.id)
61
72
  .run();
62
- } catch (dbError: any) {
63
- if (dbError.message?.includes('UNIQUE')) {
64
- throw new HTTPException(409, { message: 'Post with this title already exists' });
65
- }
66
- throw dbError;
73
+
74
+ return c.json({
75
+ status: 'success',
76
+ data: { id: existingPost.id, title, content: wrappedHtml, author, updatedAt: now },
77
+ });
67
78
  }
68
79
 
80
+ // Insert new post
81
+ const id = crypto.randomUUID();
82
+
83
+ await c.env.DB.prepare(
84
+ 'INSERT INTO posts (id, title, content, author, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?)'
85
+ )
86
+ .bind(id, title, wrappedHtml, author, now, now)
87
+ .run();
88
+
69
89
  return c.json(
70
90
  {
71
91
  status: 'success',
72
- data: { id, title, content, author, createdAt: now, updatedAt: now },
92
+ data: { id, title, content: wrappedHtml, author, createdAt: now, updatedAt: now },
73
93
  },
74
94
  201
75
95
  );