Giter VIP home page Giter VIP logo

Comments (6)

shaswatsaxena avatar shaswatsaxena commented on July 16, 2024 3

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

from next.js-snippets.

pulkitgangwar avatar pulkitgangwar commented on July 16, 2024 1

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

from next.js-snippets.

pulkitgangwar avatar pulkitgangwar commented on July 16, 2024

Can you please provide a example snippet.

from next.js-snippets.

elie222 avatar elie222 commented on July 16, 2024

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"
  },

from next.js-snippets.

zineanteoh avatar zineanteoh commented on July 16, 2024

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

from next.js-snippets.

sharma-shanu avatar sharma-shanu commented on July 16, 2024

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

from next.js-snippets.

Related Issues (7)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.