From 1c8e745e2980b5d134d1b56c43bfd0d4993143c5 Mon Sep 17 00:00:00 2001 From: Gabe Yuan Date: Fri, 4 Aug 2023 17:56:47 +0800 Subject: [PATCH] fix fetch... --- src/apis/index.js | 10 ++++---- src/background.js | 6 +++-- src/libs/fetch.js | 58 +++++++++++++++++++++-------------------------- 3 files changed, 35 insertions(+), 39 deletions(-) diff --git a/src/apis/index.js b/src/apis/index.js index 7207b26..f31eceb 100644 --- a/src/apis/index.js +++ b/src/apis/index.js @@ -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: { diff --git a/src/background.js b/src/background.js index 73f893e..cd51699 100644 --- a/src/background.js +++ b/src/background.js @@ -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 }); }) diff --git a/src/libs/fetch.js b/src/libs/fetch.js index a40f197..d721f8a 100644 --- a/src/libs/fetch.js +++ b/src/libs/fetch.js @@ -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); -};