aboutsummaryrefslogtreecommitdiff
path: root/src/lib
diff options
context:
space:
mode:
authorFuwn <[email protected]>2026-03-03 09:10:14 -0800
committerFuwn <[email protected]>2026-03-03 09:10:18 -0800
commitb3ac58a43e5c604a460e7cfcd6100a6d644f15c6 (patch)
treeac7cdcf7f68d623eeb56d05b9f63cbfb5722ca41 /src/lib
parentrefactor(effect): add request body schema decoders to api routes (diff)
downloaddue.moe-b3ac58a43e5c604a460e7cfcd6100a6d644f15c6.tar.xz
due.moe-b3ac58a43e5c604a460e7cfcd6100a6d644f15c6.zip
refactor(effect): harden settings and media cache json parsing
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/Data/AniList/media.ts3
-rw-r--r--src/lib/Effect/json.test.ts24
-rw-r--r--src/lib/Effect/json.ts28
3 files changed, 54 insertions, 1 deletions
diff --git a/src/lib/Data/AniList/media.ts b/src/lib/Data/AniList/media.ts
index 7a9fe810..f2a4aad6 100644
--- a/src/lib/Data/AniList/media.ts
+++ b/src/lib/Data/AniList/media.ts
@@ -1,4 +1,5 @@
import type { AniListAuthorisation } from "$lib/Data/AniList/identity";
+import { parseJsonStringOrDefault } from "$lib/Effect/json";
import type { UserIdentity } from "./identity";
import anime from "$stores/anime";
import manga from "$stores/manga";
@@ -271,7 +272,7 @@ export const mediaListCollection = async (
}
if (mediaCache !== undefined && mediaCache !== "")
- return JSON.parse(mediaCache);
+ return parseJsonStringOrDefault<Media[]>(mediaCache, []);
const userIdResponse = await (
await fetch("https://graphql.anilist.co", {
diff --git a/src/lib/Effect/json.test.ts b/src/lib/Effect/json.test.ts
new file mode 100644
index 00000000..b315fe61
--- /dev/null
+++ b/src/lib/Effect/json.test.ts
@@ -0,0 +1,24 @@
+import { describe, expect, it } from "vitest";
+import {
+ parseJsonStringOrDefault,
+ parseJsonStringOrThrow,
+} from "$lib/Effect/json";
+
+describe("effect json parsing", () => {
+ it("parses valid json strings", () => {
+ expect(parseJsonStringOrThrow(`{"ok":true,"value":3}`)).toEqual({
+ ok: true,
+ value: 3,
+ });
+ });
+
+ it("throws for invalid json strings", () => {
+ expect(() => parseJsonStringOrThrow("{nope}")).toThrowError();
+ });
+
+ it("returns fallback for invalid json", () => {
+ expect(parseJsonStringOrDefault("{nope}", { ok: false })).toEqual({
+ ok: false,
+ });
+ });
+});
diff --git a/src/lib/Effect/json.ts b/src/lib/Effect/json.ts
new file mode 100644
index 00000000..b905cc41
--- /dev/null
+++ b/src/lib/Effect/json.ts
@@ -0,0 +1,28 @@
+import { Effect, Result } from "effect";
+
+export const parseJsonStringEffect = (value: string) =>
+ Effect.try({
+ try: () => JSON.parse(value) as unknown,
+ catch: (cause) => new Error("Invalid JSON payload", { cause }),
+ });
+
+export const parseJsonStringEither = (value: string) =>
+ Result.try({
+ try: () => Effect.runSync(parseJsonStringEffect(value)),
+ catch: (cause) =>
+ cause instanceof Error
+ ? cause
+ : new Error("Failed to parse JSON payload", { cause }),
+ });
+
+export const parseJsonStringOrThrow = <T = unknown>(value: string): T =>
+ Result.getOrThrow(parseJsonStringEither(value)) as T;
+
+export const parseJsonStringOrDefault = <T>(value: string, fallback: T): T => {
+ const parsed = parseJsonStringEither(value);
+
+ return Result.match(parsed, {
+ onSuccess: (decoded) => decoded as T,
+ onFailure: () => fallback,
+ });
+};