pulkitgangwar / next.js-snippets

Nextjs snippets extension
https://marketplace.visualstudio.com/items?itemName=PulkitGangwar.nextjs-snippets
MIT License
25 stars 6 forks source link

App Router support #12

Closed elie222 closed 4 months ago

elie222 commented 1 year ago

Wondering if App Router support is planned as many of the snippets work differently there.

pulkitgangwar commented 1 year ago

Can you please provide a example snippet.

elie222 commented 1 year ago

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"
  },
pulkitgangwar commented 12 months ago

Sorry for the late reply and thank you for the example. Working on the changes, will add new snippets soon.

zineanteoh commented 8 months ago

Love the convenience of this extension. Would be even better if it supports next >13 app router

sharma-shanu commented 6 months ago

This would be very helpful for the new nextjs app router (>13).

shaswatsaxena commented 4 months ago

Added support for Next.js app routes with the latest release.