hook, store 리네이밍

This commit is contained in:
static
2024-12-27 21:50:04 +09:00
parent 400438c395
commit 1aafe126d6
11 changed files with 61 additions and 48 deletions

View File

@@ -1,29 +1,30 @@
import { accessToken } from "$lib/stores/auth";
import { get } from "svelte/store";
import { accessTokenStore } from "$lib/stores";
const refreshToken = async () => {
const res = await fetch("/api/auth/refreshtoken", {
const res = await fetch("/api/auth/refreshToken", {
method: "POST",
credentials: "same-origin",
});
if (!res.ok) {
accessToken.set(null);
accessTokenStore.set(null);
throw new Error("Failed to refresh token");
}
const data = await res.json();
const token = data.accessToken as string;
accessToken.set(token);
accessTokenStore.set(token);
return token;
};
const callAPIInternal = async (
input: RequestInfo,
init?: RequestInit,
token?: string | null,
init: RequestInit | undefined,
token: string | null,
retryIfUnauthorized = true,
): Promise<Response> => {
if (token === null) {
if (!token) {
token = await refreshToken();
retryIfUnauthorized = false;
}
@@ -35,13 +36,13 @@ const callAPIInternal = async (
Authorization: `Bearer ${token}`,
},
});
if (res.status === 401 && retryIfUnauthorized && token !== undefined) {
if (res.status === 401 && retryIfUnauthorized) {
return await callAPIInternal(input, init, null, false);
}
return res;
};
export default async (input: RequestInfo, init?: RequestInit, token?: string | null) => {
return await callAPIInternal(input, init, token);
export const callAPI = async (input: RequestInfo, init?: RequestInit) => {
return await callAPIInternal(input, init, get(accessTokenStore));
};

View File

@@ -1,21 +0,0 @@
import { writable } from "svelte/store";
import { goto as svelteGoto } from "$app/navigation";
type Path = "/key/export";
interface KeyExportState {
pubKeyBase64: string;
privKeyBase64: string;
}
export const keyExportState = writable<KeyExportState | null>(null);
export function goto(path: "/key/export", state: KeyExportState): Promise<void>;
export function goto(path: Path, state: unknown) {
switch (path) {
case "/key/export":
keyExportState.set(state as KeyExportState);
return svelteGoto(path);
}
}

View File

@@ -0,0 +1,33 @@
import { goto } from "$app/navigation";
type Path = "/key/export";
interface KeyExportState {
pubKeyBase64: string;
privKeyBase64: string;
}
const useAutoNull = <T>(value: T | null) => {
return {
get: () => {
const result = value;
value = null;
return result;
},
set: (newValue: T) => {
value = newValue;
},
};
};
export const keyExportState = useAutoNull<KeyExportState>(null);
export function gotoStateful(path: "/key/export", state: KeyExportState): Promise<void>;
export function gotoStateful(path: Path, state: unknown) {
switch (path) {
case "/key/export":
keyExportState.set(state as KeyExportState);
return goto(path);
}
}

View File

@@ -1,2 +1,2 @@
export { default as callAPI } from "./callAPI";
export { goto } from "./goto";
export { callAPI } from "./callAPI";
export { gotoStateful } from "./gotoStateful";

View File

@@ -1,3 +1,3 @@
import { writable } from "svelte/store";
export const accessToken = writable<string | null>(null);
export const accessTokenStore = writable<string | null>(null);

2
src/lib/stores/index.ts Normal file
View File

@@ -0,0 +1,2 @@
export * from "./auth";
export * from "./key";

View File

@@ -1,4 +1,4 @@
import { writable } from "svelte/store";
export const pubKey = writable<CryptoKey | null>(null);
export const privKey = writable<CryptoKey | null>(null);
export const pubKeyStore = writable<CryptoKey | null>(null);
export const privKeyStore = writable<CryptoKey | null>(null);