Skip to content

Commit 6b617f7

Browse files
authored
Merge pull request #122 from hongwei1/develop
bugfix/Refactor API request handling in Preview component and obp mod…
2 parents de1feb9 + 2ae8f18 commit 6b617f7

File tree

2 files changed

+56
-11
lines changed

2 files changed

+56
-11
lines changed

src/components/Preview.vue

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,29 @@ const submitRequest = async () => {
125125
if (url.value) {
126126
switch (method.value) {
127127
case 'POST': {
128-
highlightCode(await create(url.value, JSON.stringify(exampleRequestBody.value)))
128+
highlightCode(
129+
await create(
130+
url.value,
131+
(() => {
132+
const rawBody = exampleRequestBody.value
133+
const maybeBody = typeof rawBody === 'string' ? rawBody.trim() : rawBody
134+
return maybeBody ? maybeBody : undefined
135+
})()
136+
)
137+
)
129138
break
130139
}
131140
case 'PUT': {
132-
highlightCode(await update(url.value, JSON.stringify(exampleRequestBody.value)))
141+
highlightCode(
142+
await update(
143+
url.value,
144+
(() => {
145+
const rawBody = exampleRequestBody.value
146+
const maybeBody = typeof rawBody === 'string' ? rawBody.trim() : rawBody
147+
return maybeBody ? maybeBody : undefined
148+
})()
149+
)
150+
)
133151
break
134152
}
135153
case 'DELETE': {

src/obp/index.ts

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)