Closed elie222 closed 4 months ago
Can you please provide a example snippet.
eg. for a GET api request:
export function GET = (request: Request, params: {}) => {
return NextResponse.json({});
}
Or more detailed:
"Next API Route": {
"prefix": "napi",
"body": [
"import { z } from \"zod\";",
"import { NextResponse } from \"next/server\";",
"import { getAuthSession } from \"@/utils/auth\";",
"import prisma from \"@/utils/prisma\";",
"import { withError } from \"@/utils/middleware\";",
"",
"export type ${1:ApiName}Response = Awaited<",
" ReturnType<typeof get${1:ApiName}>",
">;",
"",
"async function get${1:ApiName}(options: { userId: string }) {",
" const result = await prisma.${2:table}.findMany({",
" where: {",
" userId: options.userId,",
" },",
" });",
" return { result };",
"}",
"",
"export async function GET() {",
" const session = await getAuthSession();",
" if (!session) return NextResponse.json({ error: \"Not authenticated\" });",
"",
" const result = await get${1:ApiName}({ userId: session.user.id });",
"",
" return NextResponse.json(result);",
"}",
"",
"const ${1:ApiName}Body = z.object({ message: z.string() });",
"export type ${1/(.*)/${1:/downcase}/}Body = z.infer<typeof ${1:ApiName}Body>;",
"export type update${1:ApiName}Response = Awaited<ReturnType<typeof ${1:ApiName}>>;",
"",
"export const POST = withError(async (request: Request) => {",
" const session = await getAuthSession();",
" if (!session) return NextResponse.json({ error: \"Not authenticated\" });",
"",
" const json = await request.json();",
" const body = ${1/(.*)/${1:/downcase}/}Body.parse(json);",
"",
" const result = await prisma.${2:table}.update({",
" where: {",
" id: params.id,",
" userId: session.user.id,",
" },",
" data: body",
" })",
"",
" return NextResponse.json(result);",
"});",
"",
"",
"export async function DELETE(_request: Request, params: { id: string }) {",
" const session = await getAuthSession();",
" if (!session) return NextResponse.json({ error: \"Not authenticated\" });",
"",
" const result = await prisma.${2:table}.delete({",
" where: {",
" id: params.id,",
" userId: session.user.id,",
" }",
" })",
"",
" return NextResponse.json(result);",
"}",
""
],
"description": "Next API Route"
},
Sorry for the late reply and thank you for the example. Working on the changes, will add new snippets soon.
Love the convenience of this extension. Would be even better if it supports next >13 app router
This would be very helpful for the new nextjs app router (>13).
Added support for Next.js app routes with the latest release.
Wondering if App Router support is planned as many of the snippets work differently there.