recursive retry on GET /v1/packages (#313)

* #311 recursive retry on GET /v1/packages

* #308 add recursive retry on all GET requests

---------

Co-authored-by: neil molina <neil@neils-MacBook-Pro.local>
This commit is contained in:
Neil 2023-03-19 09:04:42 +08:00 committed by GitHub
parent cbe19fa138
commit c4e69ea89b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 16 deletions

View file

@ -25,7 +25,7 @@ const { ipcRenderer, shell } = window.require("electron");
export async function getPackages(): Promise<GUIPackage[]> {
const [packages, installedPackages] = await Promise.all([
apiGet<Package[]>("packages", { nocache: "true" }),
apiGet<Package[]>("packages"),
ipcRenderer.invoke("get-installed-packages") as { version: string; full_name: string }[]
]);

View file

@ -5,25 +5,37 @@ import { getSession } from "$libs/stores/auth";
export const baseUrl = "https://api.tea.xyz/v1";
export async function get<T>(urlPath: string, params?: { [key: string]: string }) {
console.log(`GET /v1/${urlPath}`);
export async function get<T>(
urlPath: string,
params?: { [key: string]: string }
): Promise<T | null> {
try {
console.log(`GET /v1/${urlPath}`);
const [session] = await Promise.all([getSession()]);
const [session] = await Promise.all([getSession()]);
const headers =
session?.device_id && session?.user
? await getHeaders(`GET/${urlPath}`, session)
: { Authorization: "public " };
const headers =
session?.device_id && session?.user
? await getHeaders(`GET/${urlPath}`, session)
: { Authorization: "public " };
const req = await axios.request({
method: "GET",
baseURL: "https://api.tea.xyz",
url: ["v1", ...urlPath.split("/")].filter((p) => p).join("/"),
headers,
params
});
const req = await axios.request({
method: "GET",
baseURL: "https://api.tea.xyz",
url: ["v1", ...urlPath.split("/")].filter((p) => p).join("/"),
headers,
params
});
return req.data as T;
if (req.status == 200) {
return req.data as T;
} else {
return await get<T>(urlPath, params || {});
}
} catch (error) {
console.error("ERROR", error);
return null;
}
}
async function getHeaders(path: string, session: Session) {