Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 | /** * API Request Collections Endpoints * * Manages collections of saved API requests for grouping and organization */ export const dynamic = "force-dynamic"; import { NextRequest, NextResponse } from 'next/server'; import { Session } from "next-auth"; import { prisma } from "@/lib/prisma"; import { withUser, withErrorHandling, successResponse, createdResponse, ApiError, ApiSuccessResponse, ApiErrorResponse } from "@/lib/api"; import { RouteContext } from "@/lib/api/middleware"; import type { AuthenticatedUser } from '@/lib/api/middleware/types'; // GET - Retrieve collections async function handleGet( request: NextRequest, _context: RouteContext | undefined, _session: Session, user: AuthenticatedUser ): Promise<NextResponse<ApiSuccessResponse<unknown> | ApiErrorResponse>> { const userId = user.id; const { searchParams } = new URL(request.url); const includeRequests = searchParams.get("includeRequests") === "true"; const collections = await prisma.apiRequestCollection.findMany({ where: { OR: [ { userId }, { isPublic: true }, ]}, orderBy: { name: "asc" }, include: { requests: includeRequests ? { orderBy: { order: "asc" }, select: { id: true, name: true, endpointId: true, method: true, path: true, pathParams: true, queryParams: true, headers: true, body: true, useAuth: true, order: true, createdAt: true, updatedAt: true}} : false, user: { select: { id: true, name: true}}, _count: { select: { requests: true }}}}); return successResponse({ data: collections }); } // POST - Create a collection async function handlePost( request: NextRequest, _context: RouteContext | undefined, _session: Session, user: AuthenticatedUser ): Promise<NextResponse<ApiSuccessResponse<unknown> | ApiErrorResponse>> { const userId = user.id; const body = await request.json(); const { name, description, isPublic } = body; if (!name) { throw ApiError.badRequest("Collection name is required"); } const collection = await prisma.apiRequestCollection.create({ data: { userId, name, description: description || null, isPublic: isPublic || false}, include: { _count: { select: { requests: true } }}}); return createdResponse(collection); } // PUT - Update a collection async function handlePut( request: NextRequest, _context: RouteContext | undefined, _session: Session, user: AuthenticatedUser ): Promise<NextResponse<ApiSuccessResponse<unknown> | ApiErrorResponse>> { const userId = user.id; const body = await request.json(); const { id, name, description, isPublic } = body; if (!id) { throw ApiError.badRequest("Collection ID is required"); } // Verify ownership const existingCollection = await prisma.apiRequestCollection.findFirst({ where: { id, userId }}); if (!existingCollection) { throw ApiError.notFound("Collection"); } const collection = await prisma.apiRequestCollection.update({ where: { id }, data: { name: name !== undefined ? name : existingCollection.name, description: description !== undefined ? description : existingCollection.description, isPublic: isPublic !== undefined ? isPublic : existingCollection.isPublic}, include: { _count: { select: { requests: true } }}}); return successResponse({ data: collection }); } // DELETE - Delete a collection async function handleDelete( request: NextRequest, _context: RouteContext | undefined, _session: Session, user: AuthenticatedUser ): Promise<NextResponse<ApiSuccessResponse<unknown> | ApiErrorResponse>> { const userId = user.id; const { searchParams } = new URL(request.url); const id = searchParams.get("id"); const deleteRequests = searchParams.get("deleteRequests") === "true"; if (!id) { throw ApiError.badRequest("Collection ID is required"); } // Verify ownership const existingCollection = await prisma.apiRequestCollection.findFirst({ where: { id: parseInt(id), userId }}); if (!existingCollection) { throw ApiError.notFound("Collection"); } if (deleteRequests) { // Delete all requests in the collection first await prisma.apiSavedRequest.deleteMany({ where: { collectionId: parseInt(id) }}); } else { // Move requests to uncategorized (set collectionId to null) await prisma.apiSavedRequest.updateMany({ where: { collectionId: parseInt(id) }, data: { collectionId: null }}); } await prisma.apiRequestCollection.delete({ where: { id: parseInt(id) }}); return successResponse({ success: true }); } export const GET = withErrorHandling(withUser(handleGet)); export const POST = withErrorHandling(withUser(handlePost)); export const PUT = withErrorHandling(withUser(handlePut)); export const DELETE = withErrorHandling(withUser(handleDelete)); |