Comments (6)
Added support for Next.js app routes with the latest release.
from next.js-snippets.
Sorry for the late reply and thank you for the example. Working on the changes, will add new snippets soon.
from next.js-snippets.
Can you please provide a example snippet.
from next.js-snippets.
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.
Love the convenience of this extension. Would be even better if it supports next >13 app router
from next.js-snippets.
This would be very helpful for the new nextjs app router (>13).
from next.js-snippets.
Related Issues (7)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from next.js-snippets.