GET - /tasks/{taskId}


server/types/index.ts
export type Task = {
id: number
label: string
done: boolean
}
server/api/tasks/[email protected]/index.ts
import { Task } from '$/types' // path alias $ -> server
export type Methods = {
get: {
resBody: Task
}
}
server/api/tasks/[email protected]/controller.ts
import { defineController } from './$relay' // '$relay.ts' is automatically generated by frourio
import { findTask } from '$/service/tasks'
export default defineController(() => ({
get: async ({ params }) => {
const task = await findTask(params.taskId)
return task ? { status: 200, body: task } : { status: 404 }
}
}))

Prisma schema

server/prisma/schema.prisma
model Task {
id Int @id @default(autoincrement())
label String
done Boolean @default(false)
}

Prisma#findOne

server/service/tasks.ts
import { PrismaClient } from '@prisma/client'
import { Task } from '$prisma/client' // path alias $prisma -> ./node_modules/.prisma/*
const prisma = new PrismaClient()
export const findTask = (id: Task['id']) => prisma.task.findOne({ where: { id }})

Use aspida and @aspida/swr for the frontend HTTP client.
(Frourio and aspida are maintained by the same developer)

pages/index.tsx
import useAspidaSWR from '@aspida/swr'
import { apiClient } from '~/utils/apiClient'
const taskId = 1
const { data, error, mutate } = useAspidaSWR(apiClient.tasks._taskId(taskId))