mirror of
https://github.com/cranci1/Sora.git
synced 2026-05-03 08:49:05 +00:00
Add Headers,Status to Response Object (#76)
Some checks are pending
Build and Release IPA / Build IPA (push) Waiting to run
Some checks are pending
Build and Release IPA / Build IPA (push) Waiting to run
This commit is contained in:
parent
6d05282916
commit
df3a9e8f74
1 changed files with 18 additions and 25 deletions
|
|
@ -128,7 +128,13 @@ extension JSContext {
|
||||||
}
|
}
|
||||||
|
|
||||||
if let text = String(data: data, encoding: .utf8) {
|
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 {
|
} else {
|
||||||
Logger.shared.log("Unable to decode data to text", type: "Error")
|
Logger.shared.log("Unable to decode data to text", type: "Error")
|
||||||
reject.call(withArguments: ["Unable to decode data"])
|
reject.call(withArguments: ["Unable to decode data"])
|
||||||
|
|
@ -147,34 +153,21 @@ extension JSContext {
|
||||||
|
|
||||||
let fetchv2Definition = """
|
let fetchv2Definition = """
|
||||||
function fetchv2(url, headers = {}, method = "GET", body = null) {
|
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
|
// Ensure body is properly serialized
|
||||||
const processedBody = body ? JSON.stringify(body) : null;
|
processedBody = body ? JSON.stringify(body) : null
|
||||||
|
}
|
||||||
|
|
||||||
return new Promise(function(resolve, reject) {
|
return new Promise(function(resolve, reject) {
|
||||||
fetchV2Native(url, headers, method, processedBody, function(rawText) {
|
fetchV2Native(url, headers, method, processedBody, function(rawText) {
|
||||||
const responseObj = {
|
const responseObj = {
|
||||||
_data: rawText,
|
headers: rawText.headers,
|
||||||
|
status: rawText.status,
|
||||||
|
_data: rawText.body,
|
||||||
text: function() {
|
text: function() {
|
||||||
return Promise.resolve(this._data);
|
return Promise.resolve(this._data);
|
||||||
},
|
},
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue