mirror of
https://github.com/langgenius/webapp-conversation.git
synced 2026-01-03 20:35:27 +08:00
fix: res is not json
This commit is contained in:
@ -62,7 +62,8 @@ const handleStream = (response: any, onData: IOnData, onCompleted?: IOnCompleted
|
|||||||
const lines = buffer.split('\n')
|
const lines = buffer.split('\n')
|
||||||
try {
|
try {
|
||||||
lines.forEach((message) => {
|
lines.forEach((message) => {
|
||||||
if (!message) return
|
if (!message)
|
||||||
|
return
|
||||||
bufferObj = JSON.parse(message) // remove data: and parse as json
|
bufferObj = JSON.parse(message) // remove data: and parse as json
|
||||||
onData(unicodeToChar(bufferObj.answer), isFirstMessage, {
|
onData(unicodeToChar(bufferObj.answer), isFirstMessage, {
|
||||||
conversationId: bufferObj.conversation_id,
|
conversationId: bufferObj.conversation_id,
|
||||||
@ -71,11 +72,12 @@ const handleStream = (response: any, onData: IOnData, onCompleted?: IOnCompleted
|
|||||||
isFirstMessage = false
|
isFirstMessage = false
|
||||||
})
|
})
|
||||||
buffer = lines[lines.length - 1]
|
buffer = lines[lines.length - 1]
|
||||||
} catch (e) {
|
}
|
||||||
|
catch (e) {
|
||||||
onData('', false, {
|
onData('', false, {
|
||||||
conversationId: undefined,
|
conversationId: undefined,
|
||||||
messageId: '',
|
messageId: '',
|
||||||
errorMessage: e + ''
|
errorMessage: `${e}`,
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -89,7 +91,7 @@ const handleStream = (response: any, onData: IOnData, onCompleted?: IOnCompleted
|
|||||||
const baseFetch = (url: string, fetchOptions: any, { needAllResponseContent }: IOtherOptions) => {
|
const baseFetch = (url: string, fetchOptions: any, { needAllResponseContent }: IOtherOptions) => {
|
||||||
const options = Object.assign({}, baseOptions, fetchOptions)
|
const options = Object.assign({}, baseOptions, fetchOptions)
|
||||||
|
|
||||||
let urlPrefix = API_PREFIX
|
const urlPrefix = API_PREFIX
|
||||||
|
|
||||||
let urlWithPrefix = `${urlPrefix}${url.startsWith('/') ? url : `/${url}`}`
|
let urlWithPrefix = `${urlPrefix}${url.startsWith('/') ? url : `/${url}`}`
|
||||||
|
|
||||||
@ -125,27 +127,32 @@ const baseFetch = (url: string, fetchOptions: any, { needAllResponseContent }: I
|
|||||||
const resClone = res.clone()
|
const resClone = res.clone()
|
||||||
// Error handler
|
// Error handler
|
||||||
if (!/^(2|3)\d{2}$/.test(res.status)) {
|
if (!/^(2|3)\d{2}$/.test(res.status)) {
|
||||||
const bodyJson = res.json()
|
try {
|
||||||
switch (res.status) {
|
const bodyJson = res.json()
|
||||||
case 401: {
|
switch (res.status) {
|
||||||
Toast.notify({ type: 'error', message: 'Invalid token' })
|
case 401: {
|
||||||
return
|
Toast.notify({ type: 'error', message: 'Invalid token' })
|
||||||
|
return
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
// eslint-disable-next-line no-new
|
// eslint-disable-next-line no-new
|
||||||
new Promise(() => {
|
new Promise(() => {
|
||||||
bodyJson.then((data: any) => {
|
bodyJson.then((data: any) => {
|
||||||
Toast.notify({ type: 'error', message: data.message })
|
Toast.notify({ type: 'error', message: data.message })
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
}
|
||||||
}
|
}
|
||||||
|
catch (e) {
|
||||||
|
Toast.notify({ type: 'error', message: `${e}` })
|
||||||
|
}
|
||||||
|
|
||||||
return Promise.reject(resClone)
|
return Promise.reject(resClone)
|
||||||
}
|
}
|
||||||
|
|
||||||
// handle delete api. Delete api not return content.
|
// handle delete api. Delete api not return content.
|
||||||
if (res.status === 204) {
|
if (res.status === 204) {
|
||||||
resolve({ result: "success" })
|
resolve({ result: 'success' })
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -162,8 +169,7 @@ const baseFetch = (url: string, fetchOptions: any, { needAllResponseContent }: I
|
|||||||
])
|
])
|
||||||
}
|
}
|
||||||
|
|
||||||
export const ssePost = (url: string, fetchOptions: any, {
|
export const ssePost = (url: string, fetchOptions: any, { onData, onCompleted, onError }: IOtherOptions) => {
|
||||||
onData, onCompleted, onError }: IOtherOptions) => {
|
|
||||||
const options = Object.assign({}, baseOptions, {
|
const options = Object.assign({}, baseOptions, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
}, fetchOptions)
|
}, fetchOptions)
|
||||||
|
|||||||
Reference in New Issue
Block a user