Skip to content

Commit

Permalink
Merge branch 'feature/hn-api-schema' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
devsheva committed Jul 6, 2024
2 parents d914e96 + 565e99b commit d7ffad3
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 19 deletions.
10 changes: 10 additions & 0 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
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";
4 changes: 3 additions & 1 deletion src/api.ts
Original file line number Diff line number Diff line change
@@ -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<number[]> => {
const data: number[] = await fetch(`${HN_API}/topstories.json`).then((res) =>
Expand All @@ -14,5 +14,7 @@ export const getItem = async (id: number): Promise<Item | null> => {
(res) => res.json(),
)

itemSchema.parse(data)

return data
}
38 changes: 21 additions & 17 deletions src/types/item.ts
Original file line number Diff line number Diff line change
@@ -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<typeof itemSchema>

0 comments on commit d7ffad3

Please sign in to comment.