@proofkit/fmodata
Version:
FileMaker OData API client
36 lines (35 loc) • 1.7 kB
TypeScript
/**
* FileMaker OData API sometimes returns invalid JSON containing unquoted `?`
* characters as field values (e.g., `"fieldName": ?`), which causes JSON.parse()
* to fail. This module provides utilities to sanitize such responses before parsing.
*/
/**
* Sanitizes FileMaker OData JSON responses by replacing unquoted `?` values with `null`.
*
* FileMaker uses `?` to represent undefined/null values in its OData responses,
* but this is not valid JSON. This function converts those to proper `null` values.
*
* The regex uses two patterns:
* 1. `/:\s*\?(?=\s*[,}\]])/g` - for values in objects (after `:`)
* 2. `/(?<=[\[,])\s*\?(?=\s*[,\]])/g` - for values in arrays (after `[` or `,`)
*
* @param text - The raw response text from FileMaker OData API
* @returns Sanitized JSON string with `?` values replaced by `null`
*
* @example
* sanitizeFileMakerJson('{"field1": "valid", "field2": ?, "field3": null}')
* // Returns: '{"field1": "valid", "field2": null, "field3": null}'
*/
export declare function sanitizeFileMakerJson(text: string): string;
/**
* Safely parses a Response body as JSON, handling FileMaker's invalid JSON responses.
*
* This function reads the response as text first, sanitizes any invalid `?` values,
* and then parses the sanitized JSON. This approach handles the case where FileMaker
* returns a Content-Type of application/json but the body contains invalid JSON.
*
* @param response - The fetch Response object
* @returns Parsed JSON data
* @throws ResponseParseError if the JSON is still invalid after sanitization (includes sanitized text for debugging)
*/
export declare function safeJsonParse<T = unknown>(response: Response): Promise<T>;