[feat] now localhost goes plain fetch
This commit is contained in:
@@ -288,7 +288,10 @@ export async function loadData() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const knownHostes = ["localhost","172.0.0.1"]
|
||||||
|
|
||||||
export async function globalFetch(url:string, arg:{body?:any,headers?:{[key:string]:string}, rawResponse?:boolean, method?:"POST"|"GET"}) {
|
export async function globalFetch(url:string, arg:{body?:any,headers?:{[key:string]:string}, rawResponse?:boolean, method?:"POST"|"GET"}) {
|
||||||
|
try {
|
||||||
const db = get(DataBase)
|
const db = get(DataBase)
|
||||||
const method = arg.method ?? "POST"
|
const method = arg.method ?? "POST"
|
||||||
|
|
||||||
@@ -315,6 +318,46 @@ export async function globalFetch(url:string, arg:{body?:any,headers?:{[key:stri
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const urlHost = (new URL(url)).hostname
|
||||||
|
let forcePlainFetch = knownHostes.includes(urlHost)
|
||||||
|
|
||||||
|
if(db.requestmet === 'plain' || forcePlainFetch){
|
||||||
|
try {
|
||||||
|
let headers = arg.headers ?? {}
|
||||||
|
if(!headers["Content-Type"]){
|
||||||
|
headers["Content-Type"] = `application/json`
|
||||||
|
}
|
||||||
|
const furl = new URL(url)
|
||||||
|
|
||||||
|
const da = await fetch(furl, {
|
||||||
|
body: JSON.stringify(arg.body),
|
||||||
|
headers: arg.headers,
|
||||||
|
method: method
|
||||||
|
})
|
||||||
|
|
||||||
|
if(arg.rawResponse){
|
||||||
|
addFetchLog("Uint8Array Response", da.ok)
|
||||||
|
return {
|
||||||
|
ok: da.ok,
|
||||||
|
data: new Uint8Array(await da.arrayBuffer())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
const dat = await da.json()
|
||||||
|
addFetchLog(dat, da.ok)
|
||||||
|
return {
|
||||||
|
ok: da.ok,
|
||||||
|
data: dat
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (error) {
|
||||||
|
return {
|
||||||
|
ok: false,
|
||||||
|
data: `${error}`,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
if(db.requestmet === 'proxy'){
|
if(db.requestmet === 'proxy'){
|
||||||
try {
|
try {
|
||||||
let headers = arg.headers ?? {}
|
let headers = arg.headers ?? {}
|
||||||
@@ -353,45 +396,6 @@ export async function globalFetch(url:string, arg:{body?:any,headers?:{[key:stri
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(db.requestmet === 'plain'){
|
|
||||||
try {
|
|
||||||
let headers = arg.headers ?? {}
|
|
||||||
if(!headers["Content-Type"]){
|
|
||||||
headers["Content-Type"] = `application/json`
|
|
||||||
}
|
|
||||||
const furl = new URL(url)
|
|
||||||
|
|
||||||
const da = await fetch(furl, {
|
|
||||||
body: JSON.stringify(arg.body),
|
|
||||||
headers: arg.headers,
|
|
||||||
method: method
|
|
||||||
})
|
|
||||||
|
|
||||||
if(arg.rawResponse){
|
|
||||||
addFetchLog("Uint8Array Response", da.ok)
|
|
||||||
return {
|
|
||||||
ok: da.ok,
|
|
||||||
data: new Uint8Array(await da.arrayBuffer())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
const dat = await da.json()
|
|
||||||
addFetchLog(dat, da.ok)
|
|
||||||
return {
|
|
||||||
ok: da.ok,
|
|
||||||
data: dat
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
} catch (error) {
|
|
||||||
return {
|
|
||||||
ok: false,
|
|
||||||
data: `${error}`,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
if(isTauri){
|
if(isTauri){
|
||||||
if(db.requester === 'new'){
|
if(db.requester === 'new'){
|
||||||
try {
|
try {
|
||||||
@@ -508,6 +512,12 @@ export async function globalFetch(url:string, arg:{body?:any,headers?:{[key:stri
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} catch (error) {
|
||||||
|
return {
|
||||||
|
ok:false,
|
||||||
|
data: `${error}`
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const re = /\\/g
|
const re = /\\/g
|
||||||
|
|||||||
Reference in New Issue
Block a user