Skip to content

Commit

Permalink
Typecheck (#114)
Browse files Browse the repository at this point in the history
* refactor: Update Header component props to make url required

Make the `url` prop in the Header component required by changing its type to
`string | null`. This change ensures that a URL is always provided for the
Header component to function correctly.

* feat: Refactor type definitions and update injects in List and Project components

- Refactored ModuleOutput and FlowOutput to Module and Flow types respectively
- Updated injects in List.vue and Project.vue to use the new types for improved type safety.

* build: Enable typechecking in CI workflow.

* build: fix all ts error
  • Loading branch information
nexmoe authored May 1, 2024
1 parent a44bf27 commit ab15ad4
Show file tree
Hide file tree
Showing 14 changed files with 149 additions and 161 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:

# Run linting and typechecking
- run: pnpm run lint
# - run: pnpm run typecheck
- run: pnpm run typecheck

# # Run tests
# - run: pnpm run test
Expand Down
10 changes: 6 additions & 4 deletions components/module/List.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import type { AppRouter } from '@/server/trpc/routers'
type RouterOutput = inferRouterOutputs<AppRouter>
type ModuleOutput = RouterOutput['module']['get']
type FlowOutput = RouterOutput['flow']['get']
type Module = Exclude<ModuleOutput, null>
type Flow = Exclude<FlowOutput, null>
interface Props {
module: ModuleOutput
module: Module
}
const props = defineProps<Props>()
const flow = inject('flow') as FlowOutput
const flow = inject('flow') as Flow
const text = extractTextFromHTML(props.module.content)
</script>

Expand All @@ -31,15 +33,15 @@ const text = extractTextFromHTML(props.module.content)
/>

<h3
v-if="!flow.configNoTitle"
v-if="flow?.configNoTitle"
class="text-base font-bold tracking-tight text-black truncate"
>
{{ props.module.title }}
</h3>
</div>
<div
v-if="!flow.configNoContent"
v-if="flow?.configNoContent"
class="truncate"
>
<div v-html="text" />
Expand Down
9 changes: 4 additions & 5 deletions components/module/Project.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,23 @@ import type { AppRouter } from '@/server/trpc/routers'
type RouterOutput = inferRouterOutputs<AppRouter>
type ModuleOutput = RouterOutput['module']['get']
type Module = Exclude<ModuleOutput, null>
interface Props {
module: ModuleOutput
module: Module
}
const props = defineProps<Props>()
</script>

<template>
<div
class="module shu-card py-8"
>
<div class="module shu-card py-8">
<div class="px-10">
<div class="mt-1 mb-5 h-12 w-12 rounded-lg">
<NuxtImg
width="48px"
height="48px"
:src="props.module.image"
:src="props.module.image ?? undefined"
loading="lazy"
:alt="props.module.title"
/>
Expand Down
14 changes: 14 additions & 0 deletions env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,17 @@ declare module '*.svg' {
const component: DefineComponent
export default component
}

declare module 'probe-image-size' {
interface Props {
width: number
height: number
type: string
mime: string
wUnits: string
hUnits: string
url: string
}

export default function probe(url: string): Promise<Props>
}
2 changes: 0 additions & 2 deletions nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,6 @@ export default defineNuxtConfig({
},
},

extends: ['@sidebase/core'],

image: {
format: ['webp'],
quality: 85,
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
"@nuxt/image": "^1.5.0",
"@pinia/nuxt": "^0.5.1",
"@prisma/client": "^5.12.1",
"@sidebase/core": "^0.1.4",
"@trpc/client": "^10.45.2",
"@trpc/server": "^10.45.2",
"@vitest/coverage-v8": "^1.4.0",
Expand Down
2 changes: 1 addition & 1 deletion pages/module/[id].vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script setup lang="ts">
const id = useRoute().params.id
const id = useRoute().params.id as string
const useImg = useImage()
const { $client } = useNuxtApp()
Expand Down
2 changes: 1 addition & 1 deletion pages/space/_[id].vue
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<script setup lang="ts">
<script setup>
const id = useRoute().params.id
const { data: space, error } = useFetch(`/api/space/${id}`)
Expand Down
8 changes: 0 additions & 8 deletions pnpm-lock.yaml

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

48 changes: 19 additions & 29 deletions server/flowing.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { consola } from 'consola'
import { type API, type Flow, PrismaClient } from '@prisma/client'
import { type Prisma, type API, PrismaClient } from '@prisma/client'
import probe from 'probe-image-size'
import type { NModule } from '~/composables/adapter/types'
import useAdapter from '~/composables/adapter/useAdapter'
Expand Down Expand Up @@ -120,45 +120,34 @@ async function upsert(ele: NModule, flowId: string) {
// 如果找到已存在的模块,则打印信息并更新
if (res) {
consola.info(`Exists: ${ele.title}`)
let data = {
title: ele.title,
content: ele.content,
image: ele.image,
date: ele.date,
}
if (result?.width && result?.height) {
data = {
...data,
imageWidth: result.width,
imageHeight: result.height,
}
}

await prisma.module.update({
where: {
id: res.id,
},
data,
data: {
title: ele.title,
content: ele.content,
image: ele.image,
date: ele.date,
imageWidth: result?.width || null,
imageHeight: result?.height || null,
},
})
return
}

// 如果未找到已存在的模块,则打印成功消息并尝试插入新模块
consola.success(`Add: ${ele.title}`)
try {
let data = {
...ele,
date: ele.date || new Date().toISOString(),
flowId,
}
if (result?.width && result?.height) {
data = {
...data,
imageWidth: result.width,
imageHeight: result.height,
}
}
await prisma.module.create({
data,
data: {
...ele,
date: ele.date || new Date().toISOString(),
flowId,
imageWidth: result?.width || null,
imageHeight: result?.height || null,
},
})
}
catch (error) {
Expand All @@ -178,7 +167,7 @@ async function upsert(ele: NModule, flowId: string) {
*
* 对于所有成功抓取的数据适配器,将它们保存到数据库中的module表。
*/
async function flowingByFlow(flow: Flow) {
async function flowingByFlow(flow: Prisma.FlowGetPayload<{ include: { api: true } }>) {
const tasks = flow.api.map((api: API) => {
return (async () => {
let originIndex = 0
Expand Down Expand Up @@ -246,6 +235,7 @@ export async function allSize() {
},
})
for (const module of modules) {
if (!module.image) continue
const result = await probe(module.image)
if (result?.width && result?.height) {
consola.success(`Image size updated: ${module.title} `)
Expand Down
2 changes: 1 addition & 1 deletion server/trpc/routers/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { publicProcedure, router } from '../trpc'
import type { Props as SocialProps } from '~/components/Social.vue'
import type { Props as SocialProps } from '~/components/public/Social.vue'
import defaultData from '~/config/hero.json'

interface PrismaConfigItem {
Expand Down
9 changes: 2 additions & 7 deletions tests/app.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,11 @@ await setup({
})

describe('app', () => {
it('renders welcome page', async () => {
it('renders home page', async () => {
const html = await $fetch('/')

// Shows expected text
expect(html).toContain('is the productive Nuxt 3 stack')

// Contains data from healthcheck endpoint
expect(html).toContain('Server v0.0.1 initialized')
expect(html).toContain('Started at ')
expect(html).toContain('Last checked at ')
expect(html).toContain('Copyright')

await expectNoClientErrors('/')
})
Expand Down
Loading

0 comments on commit ab15ad4

Please sign in to comment.