POST - /tasks


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 = {
post: {
reqBody: Pick<Task, 'label'>
status: 201 // optional
resBody: Task
}
}
server/api/tasks/controller.ts
import { defineController } from './$relay' // '$relay.ts' is automatically generated by frourio
import { createTask } from '$/service/tasks'
export default defineController(() => ({
post: async ({ body }) => {
const task = await createTask(body.label)
return { status: 201, body: task }
}
}))

Prisma schema

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

Prisma#create

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 createTask = (label: Task['label']) =>
prisma.task.create({ data: { label } })

Use aspida 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'
await apiClient.tasks.post({ body: { label: 'new label' } })