Skip to content

Commit

Permalink
refactor!: move types in a single declaration file
Browse files Browse the repository at this point in the history
  • Loading branch information
devsheva committed Jul 7, 2024
1 parent d7ffad3 commit abbd357
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 52 deletions.
12 changes: 7 additions & 5 deletions src/api.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { HN_API } from '@/config.ts'
import { Item, itemSchema } from '@/types/item.ts'
import { Item, itemSchema, storiesSchema, TopStories } from '@/types.ts'

export const getTopStories = async (): Promise<number[]> => {
const data: number[] = await fetch(`${HN_API}/topstories.json`).then((res) =>
res.json()
)
export const getTopStories = async (): Promise<TopStories> => {
const data: TopStories = await fetch(`${HN_API}/topstories.json`).then((
res,
) => res.json())

storiesSchema.parse(data)

return data
}
Expand Down
2 changes: 1 addition & 1 deletion src/preference/setup.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Composer, createConversation, R } from '@deps'
import { ConversationContext, PreferencesContext } from '@/types/sessionData.ts'
import { ConversationContext, PreferencesContext } from '@/types.ts'

const composer = new Composer<PreferencesContext>()

Expand Down
14 changes: 3 additions & 11 deletions src/tests/bot/setup.spec.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
import setup from '@/preference/setup.ts'
import { MemorySessionStorage, R } from '@deps'
import {
afterAll,
afterEach,
assertEquals,
beforeEach,
describe,
it,
} from '@dev_deps'
import { SessionData } from '@/types/sessionData.ts'
import { afterAll, assertEquals, beforeEach, describe, it } from '@dev_deps'
import { SessionData } from '@/types.ts'
import {
chat,
from,
Expand All @@ -31,8 +25,6 @@ afterAll(async () => {
await new Promise((r) => setTimeout(r, 0))
})

import setup from '@/preference/setup.ts'

describe('setup', () => {
it('saves preferences', async () => {
await testSetupConversation(
Expand Down
44 changes: 44 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import {
Context,
Conversation,
ConversationFlavor,
SessionFlavor,
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>

const storySchema = z.number()

export const storiesSchema = z.array(storySchema)

export type TopStories = z.infer<typeof storiesSchema>

export interface SessionData {
preferences: string[]
}

export type PreferencesContext =
& Context
& SessionFlavor<SessionData>
& ConversationFlavor

export type ConversationContext = Conversation<PreferencesContext>
21 changes: 0 additions & 21 deletions src/types/item.ts

This file was deleted.

12 changes: 0 additions & 12 deletions src/types/sessionData.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { SessionData } from '@/types/sessionData.ts'
import { freeStorage, MemorySessionStorage, R } from '@deps'
import { APP_ENV } from '@/config.ts'
import { SessionData } from '@/types.ts'

export const getSessionAdapter = (token?: string) =>
R.ifElse(
Expand Down
2 changes: 1 addition & 1 deletion src/utils/testHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
User,
} from '@deps'
import { faker } from '@dev_deps'
import { PreferencesContext, SessionData } from '@/types/sessionData.ts'
import { PreferencesContext, SessionData } from '@/types.ts'

export interface ApiCall<M extends keyof RawApi = keyof RawApi> {
method: M
Expand Down

1 comment on commit abbd357

@deno-deploy
Copy link

@deno-deploy deno-deploy bot commented on abbd357 Jul 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Failed to deploy:

Module not found "file:///src/src/types/sessionData.ts".

Please sign in to comment.