From df3a9e8f74de2bf7ddfc41757740a1edfc71d5eb Mon Sep 17 00:00:00 2001 From: Ylruhc Date: Sat, 12 Apr 2025 05:48:07 +0100 Subject: [PATCH] Add Headers,Status to Response Object (#76) --- .../JavaScriptCore+Extensions.swift | 43 ++++++++----------- 1 file changed, 18 insertions(+), 25 deletions(-) diff --git a/Sora/Utils/Extensions/JavaScriptCore+Extensions.swift b/Sora/Utils/Extensions/JavaScriptCore+Extensions.swift index eda6f2a..9bb54b4 100644 --- a/Sora/Utils/Extensions/JavaScriptCore+Extensions.swift +++ b/Sora/Utils/Extensions/JavaScriptCore+Extensions.swift @@ -126,9 +126,15 @@ extension JSContext { reject.call(withArguments: ["Response exceeds maximum size"]) return } - + if let text = String(data: data, encoding: .utf8) { - resolve.call(withArguments: [text]) + // Create a response object to return + let responseDict: [String: Any] = [ + "status": (response as? HTTPURLResponse)?.statusCode ?? 0, + "headers": (response as? HTTPURLResponse)?.allHeaderFields ?? [:], + "body": text + ] + resolve.call(withArguments: [responseDict]) } else { Logger.shared.log("Unable to decode data to text", type: "Error") reject.call(withArguments: ["Unable to decode data"]) @@ -147,34 +153,21 @@ extension JSContext { let fetchv2Definition = """ function fetchv2(url, headers = {}, method = "GET", body = null) { - if (method === "GET") { - return new Promise(function(resolve, reject) { - fetchV2Native(url, headers, method, null, function(rawText) { // Pass `null` explicitly - const responseObj = { - _data: rawText, - text: function() { - return Promise.resolve(this._data); - }, - json: function() { - try { - return Promise.resolve(JSON.parse(this._data)); - } catch (e) { - return Promise.reject("JSON parse error: " + e.message); - } - } - }; - resolve(responseObj); - }, reject); - }); - } - + + + var processedBody = null; + if(method != "GET") + { // Ensure body is properly serialized - const processedBody = body ? JSON.stringify(body) : null; + processedBody = body ? JSON.stringify(body) : null + } return new Promise(function(resolve, reject) { fetchV2Native(url, headers, method, processedBody, function(rawText) { const responseObj = { - _data: rawText, + headers: rawText.headers, + status: rawText.status, + _data: rawText.body, text: function() { return Promise.resolve(this._data); },