Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions src/components/Preview.vue
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,29 @@ const submitRequest = async () => {
if (url.value) {
switch (method.value) {
case 'POST': {
highlightCode(await create(url.value, JSON.stringify(exampleRequestBody.value)))
highlightCode(
await create(
url.value,
(() => {
const rawBody = exampleRequestBody.value
const maybeBody = typeof rawBody === 'string' ? rawBody.trim() : rawBody
return maybeBody ? maybeBody : undefined
})()
)
)
break
}
case 'PUT': {
highlightCode(await update(url.value, JSON.stringify(exampleRequestBody.value)))
highlightCode(
await update(
url.value,
(() => {
const rawBody = exampleRequestBody.value
const maybeBody = typeof rawBody === 'string' ? rawBody.trim() : rawBody
return maybeBody ? maybeBody : undefined
})()
)
)
break
}
case 'DELETE': {
Expand Down
45 changes: 36 additions & 9 deletions src/obp/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,23 +50,50 @@ export async function get(path: string): Promise<any> {
}
}

export async function create(path: string, body: any): Promise<any> {
export async function create(path: string, body?: any): Promise<any> {
try {
return (await superagent.post(`/api/create?path=${path}`)
.set('Content-Type', 'application/json')
.send(JSON.parse(body)))
.body
let request = superagent.post(`/api/create?path=${path}`)
// Only send JSON body if provided and non-empty
if (body !== undefined && body !== null) {
if (typeof body === 'string') {
const trimmed = body.trim()
if (trimmed !== '') {
try {
request = request.set('Content-Type', 'application/json').send(JSON.parse(trimmed))
} catch (e) {
// If parsing fails, omit body to avoid client-side JSON errors
}
}
} else {
request = request.set('Content-Type', 'application/json').send(body)
}
}
return (await request).body
} catch (error) {
console.log(error)
return { error }
}
}

export async function update(path: string, body: any): Promise<any> {
export async function update(path: string, body?: any): Promise<any> {
try {
return (await superagent.put(`/api/update?path=${path}`)
.set('Content-Type', 'application/json')
.send(JSON.parse(body))).body
let request = superagent.put(`/api/update?path=${path}`)
// Only send JSON body if provided and non-empty
if (body !== undefined && body !== null) {
if (typeof body === 'string') {
const trimmed = body.trim()
if (trimmed !== '') {
try {
request = request.set('Content-Type', 'application/json').send(JSON.parse(trimmed))
} catch (e) {
// If parsing fails, omit body to avoid client-side JSON errors
}
}
} else {
request = request.set('Content-Type', 'application/json').send(body)
}
}
return (await request).body
} catch (error) {
console.log(error)
return { error }
Expand Down