From 9ca6444bc96622c61cc1d5e198a5ed858c73da00 Mon Sep 17 00:00:00 2001 From: static Date: Sun, 5 Jan 2025 18:23:34 +0900 Subject: [PATCH] =?UTF-8?q?=EB=94=94=EB=A0=89=ED=84=B0=EB=A6=AC=20?= =?UTF-8?q?=ED=83=90=EC=83=89=20=EC=A4=91=20=EC=95=A1=EC=84=B8=EC=8A=A4=20?= =?UTF-8?q?=ED=86=A0=ED=81=B0=EC=9D=B4=20=EB=A7=8C=EB=A3=8C=EB=90=90?= =?UTF-8?q?=EC=9D=84=20=EB=95=8C=20=EB=A1=9C=EA=B7=B8=EC=9D=B8=20=ED=8E=98?= =?UTF-8?q?=EC=9D=B4=EC=A7=80=EB=A1=9C=20=EB=A6=AC=EB=8B=A4=EC=9D=B4?= =?UTF-8?q?=EB=A0=89=EC=85=98=EB=90=98=EB=8D=98=20=EB=AC=B8=EC=A0=9C=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/lib/hooks/callApi.ts | 55 ++++++++++++------- .../[[id]]/{+page.server.ts => +page.ts} | 11 ++-- 2 files changed, 40 insertions(+), 26 deletions(-) rename src/routes/(main)/directory/[[id]]/{+page.server.ts => +page.ts} (76%) diff --git a/src/lib/hooks/callApi.ts b/src/lib/hooks/callApi.ts index c36b59a..933bbb0 100644 --- a/src/lib/hooks/callApi.ts +++ b/src/lib/hooks/callApi.ts @@ -1,41 +1,54 @@ import { signRequestBody } from "$lib/modules/crypto"; -export const refreshToken = async () => { - return await fetch("/api/auth/refreshToken", { method: "POST" }); +export const refreshToken = async (fetchInternal = fetch) => { + return await fetchInternal("/api/auth/refreshToken", { method: "POST" }); }; -const callApi = async (input: RequestInfo, init?: RequestInit) => { - let res = await fetch(input, init); +const callApi = async (input: RequestInfo, init?: RequestInit, fetchInternal = fetch) => { + let res = await fetchInternal(input, init); if (res.status === 401) { res = await refreshToken(); if (!res.ok) { return res; } - res = await fetch(input, init); + res = await fetchInternal(input, init); } return res; }; -export const callGetApi = async (input: RequestInfo) => { - return await callApi(input); +export const callGetApi = async (input: RequestInfo, fetchInternal?: typeof fetch) => { + return await callApi(input, undefined, fetchInternal); }; -export const callPostApi = async (input: RequestInfo, payload: T) => { - return await callApi(input, { - method: "POST", - headers: { - "Content-Type": "application/json", +export const callPostApi = async ( + input: RequestInfo, + payload: T, + fetchInternal?: typeof fetch, +) => { + return await callApi( + input, + { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify(payload), }, - body: JSON.stringify(payload), - }); + fetchInternal, + ); }; -export const callSignedPostApi = async (input: RequestInfo, payload: T, signKey: CryptoKey) => { - return await callApi(input, { - method: "POST", - headers: { - "Content-Type": "application/json", +export const callSignedPostApi = async ( + input: RequestInfo, + payload: T, + signKey: CryptoKey, + fetchInternal?: typeof fetch, +) => { + return await callApi( + input, + { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: await signRequestBody(payload, signKey), }, - body: await signRequestBody(payload, signKey), - }); + fetchInternal, + ); }; diff --git a/src/routes/(main)/directory/[[id]]/+page.server.ts b/src/routes/(main)/directory/[[id]]/+page.ts similarity index 76% rename from src/routes/(main)/directory/[[id]]/+page.server.ts rename to src/routes/(main)/directory/[[id]]/+page.ts index d71982e..d80e3e9 100644 --- a/src/routes/(main)/directory/[[id]]/+page.server.ts +++ b/src/routes/(main)/directory/[[id]]/+page.ts @@ -1,9 +1,10 @@ import { error } from "@sveltejs/kit"; import { z } from "zod"; +import { callGetApi } from "$lib/hooks"; import type { DirectroyInfoResponse, FileInfoResponse } from "$lib/server/schemas"; -import type { PageServerLoad } from "./$types"; +import type { PageLoad } from "./$types"; -export const load: PageServerLoad = async ({ params, fetch }) => { +export const load: PageLoad = async ({ params, fetch }) => { const zodRes = z .object({ id: z.coerce.number().int().positive().optional(), @@ -13,13 +14,13 @@ export const load: PageServerLoad = async ({ params, fetch }) => { const { id } = zodRes.data; const directoryId = id ? id : ("root" as const); - const res = await fetch(`/api/directory/${directoryId}`); + const res = await callGetApi(`/api/directory/${directoryId}`, fetch); if (!res.ok) error(404, "Not found"); const directoryInfo: DirectroyInfoResponse = await res.json(); const subDirectoryInfos = await Promise.all( directoryInfo.subDirectories.map(async (subDirectoryId) => { - const res = await fetch(`/api/directory/${subDirectoryId}`); + const res = await callGetApi(`/api/directory/${subDirectoryId}`, fetch); if (!res.ok) error(500, "Internal server error"); return { ...((await res.json()) as DirectroyInfoResponse), @@ -29,7 +30,7 @@ export const load: PageServerLoad = async ({ params, fetch }) => { ); const fileInfos = await Promise.all( directoryInfo.files.map(async (fileId) => { - const res = await fetch(`/api/file/${fileId}`); + const res = await callGetApi(`/api/file/${fileId}`, fetch); if (!res.ok) error(500, "Internal server error"); return { ...((await res.json()) as FileInfoResponse),