diff options
| author | Fuwn <[email protected]> | 2026-03-03 09:04:44 -0800 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2026-03-03 09:08:43 -0800 |
| commit | 6a44eac70c41bb1343a20ddf3ce775e416214d75 (patch) | |
| tree | c5128228834222a1a6f0cc93869215a806bd9a0a /src/lib/Effect | |
| parent | refactor(effect): migrate api auth cookie decoding (diff) | |
| download | due.moe-6a44eac70c41bb1343a20ddf3ce775e416214d75.tar.xz due.moe-6a44eac70c41bb1343a20ddf3ce775e416214d75.zip | |
refactor(effect): add request body schema decoders to api routes
Diffstat (limited to 'src/lib/Effect')
| -rw-r--r-- | src/lib/Effect/requestBody.test.ts | 56 | ||||
| -rw-r--r-- | src/lib/Effect/requestBody.ts | 15 |
2 files changed, 71 insertions, 0 deletions
diff --git a/src/lib/Effect/requestBody.test.ts b/src/lib/Effect/requestBody.test.ts new file mode 100644 index 00000000..8ca31036 --- /dev/null +++ b/src/lib/Effect/requestBody.test.ts @@ -0,0 +1,56 @@ +import { describe, expect, it } from "vitest"; +import { Schema } from "effect"; +import { + decodeRequestJsonOrThrow, + decodeUnknownOrThrow, +} from "$lib/Effect/requestBody"; + +describe("request body effect decoders", () => { + it("decodes unknown payload with a schema", () => { + const decoded = decodeUnknownOrThrow(Schema.Array(Schema.String), [ + "a", + "b", + "c", + ]); + + expect(decoded).toEqual(["a", "b", "c"]); + }); + + it("throws when payload does not match schema", () => { + expect(() => + decodeUnknownOrThrow(Schema.Array(Schema.String), ["a", 2]), + ).toThrowError(); + }); + + it("decodes request.json body with schema", async () => { + const request = new Request("https://due.moe/api/preferences", { + method: "PUT", + headers: { + "content-type": "application/json", + }, + body: JSON.stringify(["seasonal", "ongoing"]), + }); + const decoded = await decodeRequestJsonOrThrow( + request, + Schema.Array(Schema.String), + ); + + expect(decoded).toEqual(["seasonal", "ongoing"]); + }); + + it("decodes request.json object body with record schema", async () => { + const request = new Request("https://due.moe/api/configuration", { + method: "PUT", + headers: { + "content-type": "application/json", + }, + body: JSON.stringify({ theme: "dark", notifications: true }), + }); + const decoded = await decodeRequestJsonOrThrow( + request, + Schema.Record(Schema.String, Schema.Unknown), + ); + + expect(decoded).toEqual({ theme: "dark", notifications: true }); + }); +}); diff --git a/src/lib/Effect/requestBody.ts b/src/lib/Effect/requestBody.ts new file mode 100644 index 00000000..c43c30b2 --- /dev/null +++ b/src/lib/Effect/requestBody.ts @@ -0,0 +1,15 @@ +import { Schema } from "effect"; + +type SyncDecodingSchema = Schema.Top & { + readonly DecodingServices: never; +}; + +export const decodeUnknownOrThrow = <S extends SyncDecodingSchema>( + schema: S, + input: unknown, +): S["Type"] => Schema.decodeUnknownSync(schema)(input); + +export const decodeRequestJsonOrThrow = async <S extends SyncDecodingSchema>( + request: Request, + schema: S, +): Promise<S["Type"]> => decodeUnknownOrThrow(schema, await request.json()); |