Improve fetchV2 header handling in JSContext extension
Some checks are pending
Build and Release / Build IPA (push) Waiting to run
Build and Release / Build Mac Catalyst (push) Waiting to run

Updated the fetchV2 native function to accept headers as Any type and safely convert them to [String: String]. Added error logging for invalid header formats and non-string header values to improve robustness.
This commit is contained in:
cranci1 2025-06-24 10:52:59 +02:00
parent 22ba348959
commit 55dfa9cbf4

View file

@ -76,7 +76,7 @@ extension JSContext {
}
func setupFetchV2() {
let fetchV2NativeFunction: @convention(block) (String, [String: String]?, String?, String?, ObjCBool, String?, JSValue, JSValue) -> Void = { urlString, headers, method, body, redirect, encoding, resolve, reject in
let fetchV2NativeFunction: @convention(block) (String, Any?, String?, String?, ObjCBool, String?, JSValue, JSValue) -> Void = { urlString, headersAny, method, body, redirect, encoding, resolve, reject in
guard let url = URL(string: urlString) else {
Logger.shared.log("Invalid URL", type: "Error")
DispatchQueue.main.async {
@ -85,6 +85,21 @@ extension JSContext {
return
}
var headers: [String: String]? = nil
if let headersDict = headersAny as? [String: Any] {
var safeHeaders: [String: String] = [:]
for (key, value) in headersDict {
if let valueStr = value as? String {
safeHeaders[key] = valueStr
} else {
Logger.shared.log("Header value is not a String: \(key): \(value)", type: "Error")
}
}
headers = safeHeaders
} else if headersAny != nil {
Logger.shared.log("Headers argument is not a dictionary", type: "Error")
}
let httpMethod = method ?? "GET"
var request = URLRequest(url: url)
request.httpMethod = httpMethod