fix fetch...

This commit is contained in:
Gabe Yuan
2023-08-04 17:56:47 +08:00
parent 7e47882d53
commit 1c8e745e29
3 changed files with 35 additions and 39 deletions

View File

@@ -1,5 +1,5 @@
import queryString from "query-string"; import queryString from "query-string";
import { fetchPolyfill } from "../libs/fetch"; import { fetchData } from "../libs/fetch";
import { import {
OPT_TRANS_GOOGLE, OPT_TRANS_GOOGLE,
OPT_TRANS_MICROSOFT, OPT_TRANS_MICROSOFT,
@@ -20,7 +20,7 @@ import { getSetting, detectLang } from "../libs";
* @returns * @returns
*/ */
export const apiSyncData = async (url, key, data) => export const apiSyncData = async (url, key, data) =>
fetchPolyfill(url, { fetchData(url, {
headers: { headers: {
"Content-type": "application/json", "Content-type": "application/json",
[KV_HEADER_KEY]: key, [KV_HEADER_KEY]: key,
@@ -48,7 +48,7 @@ const apiGoogleTranslate = async (translator, text, to, from) => {
}; };
const { googleUrl } = await getSetting(); const { googleUrl } = await getSetting();
const input = `${googleUrl}?${queryString.stringify(params)}`; const input = `${googleUrl}?${queryString.stringify(params)}`;
return fetchPolyfill( return fetchData(
input, input,
{ {
headers: { headers: {
@@ -73,7 +73,7 @@ const apiMicrosoftTranslate = (translator, text, to, from, token) => {
"api-version": "3.0", "api-version": "3.0",
}; };
const input = `${URL_MICROSOFT_TRANS}?${queryString.stringify(params)}`; const input = `${URL_MICROSOFT_TRANS}?${queryString.stringify(params)}`;
return fetchPolyfill( return fetchData(
input, input,
{ {
headers: { headers: {
@@ -100,7 +100,7 @@ const apiOpenaiTranslate = async (translator, text, to, from) => {
let prompt = openaiPrompt let prompt = openaiPrompt
.replaceAll(PROMPT_PLACE_FROM, from) .replaceAll(PROMPT_PLACE_FROM, from)
.replaceAll(PROMPT_PLACE_TO, to); .replaceAll(PROMPT_PLACE_TO, to);
return fetchPolyfill( return fetchData(
openaiUrl, openaiUrl,
{ {
headers: { headers: {

View File

@@ -9,7 +9,6 @@ import {
STOKEY_SYNC, STOKEY_SYNC,
CACHE_NAME, CACHE_NAME,
} from "./config"; } from "./config";
import { fetchData } from "./libs/fetch";
import storage from "./libs/storage"; import storage from "./libs/storage";
import { getSetting } from "./libs"; import { getSetting } from "./libs";
import { syncAll } from "./libs/sync"; import { syncAll } from "./libs/sync";
@@ -47,7 +46,10 @@ browser.runtime.onMessage.addListener(
({ action, args }, sender, sendResponse) => { ({ action, args }, sender, sendResponse) => {
switch (action) { switch (action) {
case MSG_FETCH: case MSG_FETCH:
fetchData(args.input, args.init, args.opts) fetch(args.input, args.init)
.then((res) => {
return res.json();
})
.then((data) => { .then((data) => {
sendResponse({ data }); sendResponse({ data });
}) })

View File

@@ -31,24 +31,40 @@ const newCacheReq = async (request, translator) => {
}; };
/** /**
* 调用fetch接口 * 兼容性封装
* @param {*} input * @param {*} input
* @param {*} init * @param {*} init
* @param {*} opts
* @returns * @returns
*/ */
export const fetchData = async ( export const fetchPolyfill = async (input, init) => {
input, if (browser?.runtime) {
init, // 插件调用
{ useCache = false, translator } = {} const res = await sendMsg(MSG_FETCH, { input, init });
) => { if (res.error) {
const req = new Request(input, init); throw new Error(res.error);
const cacheReq = await newCacheReq(req.clone(), translator); }
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); const cache = await caches.open(CACHE_NAME);
let res; let res;
// 查询缓存 // 查询缓存
if (useCache) { if (useCache) {
// console.log("usecache")
try { try {
res = await cache.match(cacheReq); res = await cache.match(cacheReq);
} catch (err) { } catch (err) {
@@ -58,8 +74,7 @@ export const fetchData = async (
// 发送请求 // 发送请求
if (!res) { if (!res) {
// console.log("usefetch") res = await fetchPolyfill(input, init);
res = await fetch(req);
} }
if (!res?.ok) { if (!res?.ok) {
@@ -81,24 +96,3 @@ export const fetchData = async (
} }
return await res.text(); 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);
};