@@ -50,23 +50,50 @@ export async function get(path: string): Promise<any> {
5050 }
5151}
5252
53- export async function create ( path : string , body : any ) : Promise < any > {
53+ export async function create ( path : string , body ? : any ) : Promise < any > {
5454 try {
55- return ( await superagent . post ( `/api/create?path=${ path } ` )
56- . set ( 'Content-Type' , 'application/json' )
57- . send ( JSON . parse ( body ) ) )
58- . body
55+ let request = superagent . post ( `/api/create?path=${ path } ` )
56+ // Only send JSON body if provided and non-empty
57+ if ( body !== undefined && body !== null ) {
58+ if ( typeof body === 'string' ) {
59+ const trimmed = body . trim ( )
60+ if ( trimmed !== '' ) {
61+ try {
62+ request = request . set ( 'Content-Type' , 'application/json' ) . send ( JSON . parse ( trimmed ) )
63+ } catch ( e ) {
64+ // If parsing fails, omit body to avoid client-side JSON errors
65+ }
66+ }
67+ } else {
68+ request = request . set ( 'Content-Type' , 'application/json' ) . send ( body )
69+ }
70+ }
71+ return ( await request ) . body
5972 } catch ( error ) {
6073 console . log ( error )
6174 return { error }
6275 }
6376}
6477
65- export async function update ( path : string , body : any ) : Promise < any > {
78+ export async function update ( path : string , body ? : any ) : Promise < any > {
6679 try {
67- return ( await superagent . put ( `/api/update?path=${ path } ` )
68- . set ( 'Content-Type' , 'application/json' )
69- . send ( JSON . parse ( body ) ) ) . body
80+ let request = superagent . put ( `/api/update?path=${ path } ` )
81+ // Only send JSON body if provided and non-empty
82+ if ( body !== undefined && body !== null ) {
83+ if ( typeof body === 'string' ) {
84+ const trimmed = body . trim ( )
85+ if ( trimmed !== '' ) {
86+ try {
87+ request = request . set ( 'Content-Type' , 'application/json' ) . send ( JSON . parse ( trimmed ) )
88+ } catch ( e ) {
89+ // If parsing fails, omit body to avoid client-side JSON errors
90+ }
91+ }
92+ } else {
93+ request = request . set ( 'Content-Type' , 'application/json' ) . send ( body )
94+ }
95+ }
96+ return ( await request ) . body
7097 } catch ( error ) {
7198 console . log ( error )
7299 return { error }
0 commit comments