fix: Add JSON sanitization to handle NaN values in streaming requests (#8736)
🔧 (api.tsx): add a helper function sanitizeJsonString to replace NaN values with null in JSON strings for proper parsing
This commit is contained in:
parent
ceb98af542
commit
d2d774d11c
1 changed files with 14 additions and 2 deletions
|
|
@ -272,6 +272,16 @@ export type StreamingRequestParams = {
|
|||
eventDeliveryConfig?: EventDeliveryType;
|
||||
};
|
||||
|
||||
// Helper function to sanitize JSON strings
|
||||
function sanitizeJsonString(jsonStr: string): string {
|
||||
// Replace NaN with null (valid JSON)
|
||||
return jsonStr
|
||||
.replace(/:\s*NaN\b/g, ": null")
|
||||
.replace(/\[\s*NaN\s*\]/g, "[null]")
|
||||
.replace(/,\s*NaN\s*,/g, ", null,")
|
||||
.replace(/,\s*NaN\s*\]/g, ", null]");
|
||||
}
|
||||
|
||||
async function performStreamingRequest({
|
||||
method,
|
||||
url,
|
||||
|
|
@ -323,7 +333,8 @@ async function performStreamingRequest({
|
|||
const allString = current.join("") + string;
|
||||
let data: object;
|
||||
try {
|
||||
data = JSON.parse(allString);
|
||||
const sanitizedJson = sanitizeJsonString(allString);
|
||||
data = JSON.parse(sanitizedJson);
|
||||
current = [];
|
||||
} catch (e) {
|
||||
current.push(string);
|
||||
|
|
@ -342,7 +353,8 @@ async function performStreamingRequest({
|
|||
if (current.length > 0) {
|
||||
const allString = current.join("");
|
||||
if (allString) {
|
||||
const data = JSON.parse(current.join(""));
|
||||
const sanitizedJson = sanitizeJsonString(allString);
|
||||
const data = JSON.parse(sanitizedJson);
|
||||
await onData(data);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue