1
1
import type { APIContext } from "astro" ;
2
2
import { db , Note } from "astro:db" ;
3
+ import { record } from "zod" ;
3
4
4
5
interface SuccessfulIndieToken {
5
6
me : string ;
@@ -28,14 +29,25 @@ function hasOwnProperty<T, K extends PropertyKey>(
28
29
) : obj is T & Record < K , unknown > {
29
30
return Object . prototype . hasOwnProperty . call ( obj , prop ) ;
30
31
}
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
+ }
31
39
32
40
33
41
34
42
export async function POST ( { request, site, url } : APIContext ) {
35
43
const contentType = request . headers . get ( 'Content-type' )
36
44
let bodyAuthToken ;
45
+ let formBodyObject ;
37
46
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' )
39
51
}
40
52
41
53
const headerAuthToken = request . headers . get ( "Authorization" ) ?. replace ( 'Bearer ' , '' )
@@ -63,16 +75,16 @@ export async function POST({ request, site, url }: APIContext) {
63
75
64
76
// TODO: Create note here
65
77
78
+ console . log ( "hello world" )
66
79
if ( contentType === "application/x-www-form-urlencoded" ) {
67
- let body = new URLSearchParams ( await request . text ( ) )
68
80
69
- const content = body . get ( 'content' )
70
- if ( ! content ) {
81
+
82
+ if ( ! hasOwnProperty ( formBodyObject , ' content' ) ) {
71
83
return Error ( 422 )
72
84
}
73
85
74
86
const records = await db . insert ( Note ) . values ( {
75
- content : content
87
+ content : formBodyObject . content
76
88
} ) . returning ( )
77
89
78
90
return new Response ( null , {
@@ -82,7 +94,6 @@ export async function POST({ request, site, url }: APIContext) {
82
94
"Location" : "https://yusuf.fyi/notes/" + records [ 0 ] . published
83
95
}
84
96
} )
85
-
86
97
}
87
98
88
99
} else {
0 commit comments