From 42a1a5be912e19a47d4b94468c7059b5157875d2 Mon Sep 17 00:00:00 2001 From: xylophonehero Date: Fri, 16 Sep 2022 22:52:11 +0100 Subject: [PATCH] feat: 14 possible solution --- src/14-generics.solution.2.ts | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 src/14-generics.solution.2.ts diff --git a/src/14-generics.solution.2.ts b/src/14-generics.solution.2.ts new file mode 100644 index 0000000..4400aaa --- /dev/null +++ b/src/14-generics.solution.2.ts @@ -0,0 +1,30 @@ +// CODE + +import { it } from "vitest"; +import { z } from "zod"; +import { Equal, Expect } from "./helpers/type-utils"; + +const genericFetch = ( + url: string, + schema: z.ZodSchema, +) => { + return fetch(url) + .then((res) => res.json()) + .then((result) => schema.parse(result)); +}; + +// TESTS + +it("Should fetch from the Star Wars API", async () => { + const result = await genericFetch( + "https://swapi.dev/api/people/1", + z.object({ + name: z.string(), + }), + ); + + type cases = [ + // Result should equal { name: string }, not any + Expect>, + ]; +});