mirror of
https://github.com/cranci1/Sora.git
synced 2026-01-11 20:10:24 +00:00
Improve fetchV2 header handling in JSContext extension
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:
parent
22ba348959
commit
55dfa9cbf4
1 changed files with 16 additions and 1 deletions
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in a new issue