diff --git a/src/worker.mjs b/src/worker.mjs index abf5b61..09df11a 100644 --- a/src/worker.mjs +++ b/src/worker.mjs @@ -1,5 +1,5 @@ export default { - async fetch (request, env) { + async fetch (request) { if (request.method === "OPTIONS") { return handleOPTIONS(request); } @@ -11,33 +11,9 @@ export default { status: 401 }); } - const tokenCache = env.KV; - let token, endpoint; - try { - token = await tokenCache.get(authKey); - if (token) { - endpoint = await tokenCache.get(authKey + "-ep"); - } - } catch (e) { - console.error(e); - } - if (!token) { - let expires_at, endpoints, errResponse; - ({ token, expires_at, endpoints, errResponse } = await getToken(authKey)); - if (errResponse) { return errResponse; } - endpoint = endpoints?.api; - if (expires_at) { - try { - await tokenCache.put(authKey, token, { expiration: expires_at }); - if (endpoint) { - await tokenCache.put(authKey + "-ep", endpoint, { expiration: expires_at }); - } - } catch (e) { - console.error(e); - } - } - } - return makeRequest(request, url.pathname, token, endpoint || "https://api.githubcopilot.com"); + const { token, endpoints, errResponse } = await getToken(authKey); + if (errResponse) { return errResponse; } + return makeRequest(request, url.pathname, token, endpoints?.api || "https://api.githubcopilot.com"); } }; diff --git a/wrangler.toml b/wrangler.toml index 960a85f..1ddf89b 100644 --- a/wrangler.toml +++ b/wrangler.toml @@ -3,6 +3,8 @@ main = "src/worker.mjs" compatibility_date = "2024-01-17" no_bundle = true -[[kv_namespaces]] -binding = "KV" -id = "4e595a4637f048ed8c92bab1821c6e54" +# Bind a KV Namespace. Use KV as persistent storage for small key-value pairs. +# Docs: https://developers.cloudflare.com/workers/runtime-apis/kv +# [[kv_namespaces]] +# binding = "MY_KV_NAMESPACE" +# id = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"