fix fetch...
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import queryString from "query-string";
|
||||
import { fetchPolyfill } from "../libs/fetch";
|
||||
import { fetchData } from "../libs/fetch";
|
||||
import {
|
||||
OPT_TRANS_GOOGLE,
|
||||
OPT_TRANS_MICROSOFT,
|
||||
@@ -20,7 +20,7 @@ import { getSetting, detectLang } from "../libs";
|
||||
* @returns
|
||||
*/
|
||||
export const apiSyncData = async (url, key, data) =>
|
||||
fetchPolyfill(url, {
|
||||
fetchData(url, {
|
||||
headers: {
|
||||
"Content-type": "application/json",
|
||||
[KV_HEADER_KEY]: key,
|
||||
@@ -48,7 +48,7 @@ const apiGoogleTranslate = async (translator, text, to, from) => {
|
||||
};
|
||||
const { googleUrl } = await getSetting();
|
||||
const input = `${googleUrl}?${queryString.stringify(params)}`;
|
||||
return fetchPolyfill(
|
||||
return fetchData(
|
||||
input,
|
||||
{
|
||||
headers: {
|
||||
@@ -73,7 +73,7 @@ const apiMicrosoftTranslate = (translator, text, to, from, token) => {
|
||||
"api-version": "3.0",
|
||||
};
|
||||
const input = `${URL_MICROSOFT_TRANS}?${queryString.stringify(params)}`;
|
||||
return fetchPolyfill(
|
||||
return fetchData(
|
||||
input,
|
||||
{
|
||||
headers: {
|
||||
@@ -100,7 +100,7 @@ const apiOpenaiTranslate = async (translator, text, to, from) => {
|
||||
let prompt = openaiPrompt
|
||||
.replaceAll(PROMPT_PLACE_FROM, from)
|
||||
.replaceAll(PROMPT_PLACE_TO, to);
|
||||
return fetchPolyfill(
|
||||
return fetchData(
|
||||
openaiUrl,
|
||||
{
|
||||
headers: {
|
||||
|
||||
@@ -9,7 +9,6 @@ import {
|
||||
STOKEY_SYNC,
|
||||
CACHE_NAME,
|
||||
} from "./config";
|
||||
import { fetchData } from "./libs/fetch";
|
||||
import storage from "./libs/storage";
|
||||
import { getSetting } from "./libs";
|
||||
import { syncAll } from "./libs/sync";
|
||||
@@ -47,7 +46,10 @@ browser.runtime.onMessage.addListener(
|
||||
({ action, args }, sender, sendResponse) => {
|
||||
switch (action) {
|
||||
case MSG_FETCH:
|
||||
fetchData(args.input, args.init, args.opts)
|
||||
fetch(args.input, args.init)
|
||||
.then((res) => {
|
||||
return res.json();
|
||||
})
|
||||
.then((data) => {
|
||||
sendResponse({ data });
|
||||
})
|
||||
|
||||
@@ -31,24 +31,40 @@ const newCacheReq = async (request, translator) => {
|
||||
};
|
||||
|
||||
/**
|
||||
* 调用fetch接口
|
||||
* 兼容性封装
|
||||
* @param {*} input
|
||||
* @param {*} init
|
||||
* @param {*} opts
|
||||
* @returns
|
||||
*/
|
||||
export const fetchData = async (
|
||||
input,
|
||||
init,
|
||||
{ useCache = false, translator } = {}
|
||||
) => {
|
||||
const req = new Request(input, init);
|
||||
const cacheReq = await newCacheReq(req.clone(), translator);
|
||||
export const fetchPolyfill = async (input, init) => {
|
||||
if (browser?.runtime) {
|
||||
// 插件调用
|
||||
const res = await sendMsg(MSG_FETCH, { input, init });
|
||||
if (res.error) {
|
||||
throw new Error(res.error);
|
||||
}
|
||||
return res.data;
|
||||
}
|
||||
|
||||
// 网页直接调用
|
||||
return await fetch(input, init);
|
||||
};
|
||||
|
||||
/**
|
||||
* 请求数据
|
||||
* @param {*} input
|
||||
* @param {*} init
|
||||
* @param {*} opts
|
||||
* @returns
|
||||
*/
|
||||
export const fetchData = async (input, init, { useCache, translator } = {}) => {
|
||||
const cacheReq = await newCacheReq(new Request(input, init), translator);
|
||||
const cache = await caches.open(CACHE_NAME);
|
||||
let res;
|
||||
|
||||
// 查询缓存
|
||||
if (useCache) {
|
||||
// console.log("usecache")
|
||||
try {
|
||||
res = await cache.match(cacheReq);
|
||||
} catch (err) {
|
||||
@@ -58,8 +74,7 @@ export const fetchData = async (
|
||||
|
||||
// 发送请求
|
||||
if (!res) {
|
||||
// console.log("usefetch")
|
||||
res = await fetch(req);
|
||||
res = await fetchPolyfill(input, init);
|
||||
}
|
||||
|
||||
if (!res?.ok) {
|
||||
@@ -81,24 +96,3 @@ export const fetchData = async (
|
||||
}
|
||||
return await res.text();
|
||||
};
|
||||
|
||||
/**
|
||||
* 兼容性封装
|
||||
* @param {*} input
|
||||
* @param {*} init
|
||||
* @param {*} opts
|
||||
* @returns
|
||||
*/
|
||||
export const fetchPolyfill = async (input, init, opts) => {
|
||||
if (browser?.runtime) {
|
||||
// 插件调用
|
||||
const res = await sendMsg(MSG_FETCH, { input, init, opts });
|
||||
if (res.error) {
|
||||
throw new Error(res.error);
|
||||
}
|
||||
return res.data;
|
||||
}
|
||||
|
||||
// 网页直接调用
|
||||
return await fetchData(input, init, opts);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user