mirror of
https://github.com/qf3l3k/obsidian-data-fetcher.git
synced 2026-07-22 05:43:10 +00:00
fix: improve mobile REST response parsing
This commit is contained in:
parent
121562cfaf
commit
ac1e6e239a
2 changed files with 40 additions and 6 deletions
|
|
@ -2,6 +2,11 @@
|
|||
|
||||
All notable changes to this project will be documented in this file.
|
||||
|
||||
## [Unreleased]
|
||||
|
||||
### Fixed
|
||||
- Improved REST response parsing for mobile platforms by handling `Content-Type` headers case-insensitively and falling back to JSON body parsing when the payload is JSON-like. This fixes `path` lookups such as `path: 0` failing on Android and iPad for endpoints that return JSON arrays.
|
||||
|
||||
## [1.2.0] - 2026-05-02
|
||||
|
||||
### Added
|
||||
|
|
|
|||
|
|
@ -23,6 +23,32 @@ export interface QueryResult {
|
|||
error?: string;
|
||||
}
|
||||
|
||||
function findHeaderValue(headers: Record<string, string>, targetName: string): string | undefined {
|
||||
const normalizedTarget = targetName.toLowerCase();
|
||||
|
||||
for (const [name, value] of Object.entries(headers || {})) {
|
||||
if (name.toLowerCase() === normalizedTarget) {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function tryParseJsonText(text: string): any | undefined {
|
||||
const trimmed = text.trim();
|
||||
|
||||
if (!trimmed.startsWith('{') && !trimmed.startsWith('[')) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
try {
|
||||
return JSON.parse(trimmed);
|
||||
} catch {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
function formatRequestError(error: unknown): string {
|
||||
const message = error instanceof Error ? error.message : String(error);
|
||||
const normalized = message.toLowerCase();
|
||||
|
|
@ -291,14 +317,17 @@ async function executeRestQuery(params: QueryParams): Promise<any> {
|
|||
|
||||
const response = await requestUrl(requestParams);
|
||||
|
||||
// Parse response based on content type
|
||||
const contentType = response.headers['content-type'];
|
||||
|
||||
if (contentType && contentType.includes('application/json')) {
|
||||
// Mobile builds may expose response headers with different casing, so
|
||||
// treat Content-Type lookup as case-insensitive and fall back to parsing
|
||||
// the body when JSON-looking payloads arrive without a usable header.
|
||||
const contentType = findHeaderValue(response.headers || {}, 'content-type');
|
||||
|
||||
if (contentType && contentType.toLowerCase().includes('application/json')) {
|
||||
return response.json;
|
||||
} else {
|
||||
return response.text;
|
||||
}
|
||||
|
||||
const parsedText = tryParseJsonText(response.text);
|
||||
return parsedText !== undefined ? parsedText : response.text;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
Loading…
Reference in a new issue