GET - /tasks?limit={number}


server/types/index.ts
export type Task = {
id: number
label: string
done: boolean
}
server/api/tasks/index.ts
import { Task } from '$/types' // path alias $ -> server
export type Methods = {
get: {
query?: {
limit: number
}
resBody: Task[]
}
}
server/api/tasks/controller.ts
import { defineController } from './$relay' // '$relay.ts' is automatically generated by frourio
import { getTasks } from '$/service/tasks'
export default defineController(() => ({
get: async ({ query }) => ({
status: 200,
body: (await getTasks()).slice(0, query?.limit)
})
}))

Prisma schema

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

Prisma#findMany

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 getTasks = async (limit?: number) =>
(await prisma.task.findMany()).slice(0, limit)

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 { data, error, mutate } = useAspidaSWR(apiClient.tasks, { query: { limit: 10 } })