From ea2a5f46329f3b3bb50acc5a47a04960582dcc08 Mon Sep 17 00:00:00 2001 From: devsheva Date: Sun, 7 Jul 2024 00:38:54 +0200 Subject: [PATCH 1/2] chore: add zod package --- deno.lock | 10 ++++++++++ deps.ts | 3 ++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/deno.lock b/deno.lock index c4c472a..ea8baea 100644 --- a/deno.lock +++ b/deno.lock @@ -122,6 +122,16 @@ "https://deno.land/x/oson@1.0.1/constructors.ts": "2b77dcdc8d8db5ece2860d1657f4dcef37dd761684f1d4b9535c7e56a0fbfcf6", "https://deno.land/x/oson@1.0.1/mod.ts": "54e494dc517ce0de6c727c25d9731ccc748e8646c883e922dc5d656f523a4e5b", "https://deno.land/x/oson@1.0.1/oson.ts": "ec3908ae5c9ceff7bfd869d95a2183b929b9d96fbff44b57d28d3b742d06a4a1", + "https://deno.land/x/zod@v3.16.1/ZodError.ts": "6e56e6416bbbad3539b577b7636dc198e5d87cf59f1ceb4e7b58d1d38190c2f7", + "https://deno.land/x/zod@v3.16.1/external.ts": "433e13ff774fef22d416027308db33cacb719774898372ec5faa32bb572f11dc", + "https://deno.land/x/zod@v3.16.1/helpers/errorUtil.ts": "7a77328240be7b847af6de9189963bd9f79cab32bbc61502a9db4fe6683e2ea7", + "https://deno.land/x/zod@v3.16.1/helpers/parseUtil.ts": "2611b41b579f551fcf0799415f9793709637bc7c7778bf53aab73cea92e859db", + "https://deno.land/x/zod@v3.16.1/helpers/partialUtil.ts": "8dc921a02b47384cf52217c7e539268daf619f89319b75bdf13ea178815725df", + "https://deno.land/x/zod@v3.16.1/helpers/typeAliases.ts": "a1a8d039eb98925f242f5ea1e21e6d3cabd7f05e9747680165c914695c979b4f", + "https://deno.land/x/zod@v3.16.1/helpers/util.ts": "1d28cc4dfde641f20d8544a1a747cf745d5eda0c5cdeaca8a29837991254a67d", + "https://deno.land/x/zod@v3.16.1/index.ts": "035a7422d9f2be54daa0fe464254b69225b443000673e4794095d672471e8792", + "https://deno.land/x/zod@v3.16.1/mod.ts": "64e55237cb4410e17d968cd08975566059f27638ebb0b86048031b987ba251c4", + "https://deno.land/x/zod@v3.16.1/types.ts": "fff4c7909b1919989c4cc1abe6218dfbdb53e197cacb2dfad2831dbd71dc56d2", "https://esm.sh/@faker-js/faker@v8.4.0": "3b580236f3f8c85a7b2b589e685a265367d4d548add82207dfc9fba6a50df32e", "https://esm.sh/v135/@faker-js/faker@8.4.0": "3b580236f3f8c85a7b2b589e685a265367d4d548add82207dfc9fba6a50df32e", "https://esm.sh/v135/@faker-js/faker@8.4.0/denonext/faker.mjs": "6d41425710d5f98197ac4842005814ca5375aaa0f57a504b3e138a4a5e1efa02", diff --git a/deps.ts b/deps.ts index 8e0f517..03c637f 100644 --- a/deps.ts +++ b/deps.ts @@ -2,4 +2,5 @@ export * as R from "https://x.nest.land/ramda@0.27.2/mod.ts" export * from "https://deno.land/x/grammy@v1.26.0/mod.ts"; export * from "https://deno.land/x/grammy_conversations@v1.2.0/mod.ts"; export * from "https://deno.land/x/grammy_storages@v2.4.2/free/src/mod.ts" -export * from "https://deno.land/x/grammy_types@v3.10.0/mod.ts" \ No newline at end of file +export * from "https://deno.land/x/grammy_types@v3.10.0/mod.ts" +export { z } from "https://deno.land/x/zod@v3.16.1/mod.ts"; From 565e99b12f9751a531719acfc85e9b87ffc3c14e Mon Sep 17 00:00:00 2001 From: devsheva Date: Sun, 7 Jul 2024 00:50:09 +0200 Subject: [PATCH 2/2] feat: validate Item schema --- src/api.ts | 4 +++- src/types/item.ts | 38 +++++++++++++++++++++----------------- 2 files changed, 24 insertions(+), 18 deletions(-) diff --git a/src/api.ts b/src/api.ts index fde261d..3e375fe 100644 --- a/src/api.ts +++ b/src/api.ts @@ -1,5 +1,5 @@ import { HN_API } from '@/config.ts' -import { Item } from '@/types/item.ts' +import { Item, itemSchema } from '@/types/item.ts' export const getTopStories = async (): Promise => { const data: number[] = await fetch(`${HN_API}/topstories.json`).then((res) => @@ -14,5 +14,7 @@ export const getItem = async (id: number): Promise => { (res) => res.json(), ) + itemSchema.parse(data) + return data } diff --git a/src/types/item.ts b/src/types/item.ts index 5c03c96..7d77d7d 100644 --- a/src/types/item.ts +++ b/src/types/item.ts @@ -1,17 +1,21 @@ -export type Item = { - id: number - deleted: boolean - type: string - by: string - time: number - text: string - dead: boolean - parent: number - poll: any // fix this - kids: any // fix this - url: string - score: number - title: string - parts: any // fix this - descendants: number -} +import { z } from '@deps' + +export const itemSchema = z.object({ + id: z.number(), + deleted: z.optional(z.boolean()), + type: z.optional(z.string()), + by: z.optional(z.string()), + time: z.optional(z.number()), + text: z.optional(z.string()), + dead: z.optional(z.boolean()), + parent: z.optional(z.number()), + poll: z.optional(z.unknown()), + kids: z.optional(z.unknown()), + url: z.optional(z.string()), + score: z.optional(z.number()), + title: z.optional(z.string()), + parts: z.optional(z.unknown()), + descendants: z.optional(z.number()), +}).nullable() + +export type Item = z.infer