Skip to content

Commit ef00e71

Browse files
committed
fix post creation
1 parent 53b269d commit ef00e71

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

src/pages/api/micropub.ts

+17-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { APIContext } from "astro";
22
import { db, Note } from "astro:db";
3+
import { record } from "zod";
34

45
interface SuccessfulIndieToken {
56
me: string;
@@ -28,14 +29,25 @@ function hasOwnProperty<T, K extends PropertyKey>(
2829
): obj is T & Record<K, unknown> {
2930
return Object.prototype.hasOwnProperty.call(obj, prop);
3031
}
32+
function paramsToObject(entries: any) {
33+
const result: any = {}
34+
for (const [key, value] of entries) { // each 'entry' is a [key, value] tupple
35+
result[key] = value;
36+
}
37+
return result;
38+
}
3139

3240

3341

3442
export async function POST({ request, site, url }: APIContext) {
3543
const contentType = request.headers.get('Content-type')
3644
let bodyAuthToken;
45+
let formBodyObject;
3746
if (contentType === "application/x-www-form-urlencoded") {
38-
bodyAuthToken = new URLSearchParams(await request.text()).get('access_token')
47+
let formBody = new URLSearchParams(await request.text())
48+
formBodyObject = paramsToObject(formBody)
49+
50+
bodyAuthToken = formBody.get('access_token')
3951
}
4052

4153
const headerAuthToken = request.headers.get("Authorization")?.replace('Bearer ', '')
@@ -63,16 +75,16 @@ export async function POST({ request, site, url }: APIContext) {
6375

6476
// TODO: Create note here
6577

78+
console.log("hello world")
6679
if (contentType === "application/x-www-form-urlencoded") {
67-
let body = new URLSearchParams(await request.text())
6880

69-
const content = body.get('content')
70-
if (!content) {
81+
82+
if (!hasOwnProperty(formBodyObject, 'content')) {
7183
return Error(422)
7284
}
7385

7486
const records = await db.insert(Note).values({
75-
content: content
87+
content: formBodyObject.content
7688
}).returning()
7789

7890
return new Response(null, {
@@ -82,7 +94,6 @@ export async function POST({ request, site, url }: APIContext) {
8294
"Location": "https://yusuf.fyi/notes/" + records[0].published
8395
}
8496
})
85-
8697
}
8798

8899
} else {

0 commit comments

Comments
 (0)