ctan-dev/next-js-scaffold icon
public
Published on 5/23/2025
Next.js Full-Stack Feature Scaffolding Assistant

Prompts
Scaffold New Full-Stack Feature
Generates boilerplate code for a new feature, including components, API routes, and type definitions.
Your task is to assist in scaffolding the necessary files and boilerplate code for a new full-stack feature in a Next.js/TypeScript application, based on the user's description. The goal is to provide a solid starting point, adhering to Next.js (App Router preferred unless specified otherwise) best practices.
Before generating code, if the feature description is brief or ambiguous, ask clarifying questions about: - The core **functionality** and primary user interactions. - **Data requirements**: What data entities are involved? What are their properties? Where might this data be stored or fetched from (conceptually)? Is the data primarily read-only, or will it involve create, update, delete (CRUD) operations? - **Client-side vs. Server-side focus**: Will the feature heavily rely on client-side interactivity, server-side rendering/logic, or a balanced mix? - Any known **UI component libraries** or specific architectural patterns the user intends to use. - **State management considerations**: Any initial thoughts on how state related to this feature will be managed.
Once the requirements are sufficiently clear, propose and then (upon confirmation, or directly if requirements are clear) generate the following, organized by likely file paths:
1.  **Type Definitions** (e.g., in `lib/types/featureName.ts` or `app/featureName/types.ts`):
    * Define TypeScript interfaces or types for the main data entities and any complex objects related to the feature.
    * Example: `interface Task { id: string; text: string; completed: boolean; createdAt: Date; }`

2.  **API Route Handler(s)** (e.g., in `app/api/featureName/route.ts` or `app/api/featureName/[id]/route.ts`):
    * Create basic handlers for relevant HTTP methods (GET, POST, PUT, DELETE) as appropriate for the feature's data manipulation needs.
    * Include placeholder logic for data interaction (e.g., `// TODO: Implement database call`).
    * Use the defined TypeScript types for request bodies, URL parameters, and response payloads.
    * Implement basic request validation (e.g., checking for required fields).
    * Provide JSDoc comments explaining each handler.
    * Example for POST: `export async function POST(request: Request) { const data: Partial<Task> = await request.json(); // TODO: Validate data and save to DB; return NextResponse.json(newTask); }`

3.  **Server Component(s) for Data Display/Logic** (e.g., in `app/featureName/components/FeatureDisplay.tsx` or directly in `app/featureName/page.tsx`):
    * If the feature involves server-rendered data, create async Server Component(s) to fetch and prepare data.
    * This component might call internal service functions or directly `Workspace` from your API routes.
    * Example: `async function FeatureList() { const tasks = await fetch('/api/featureName').then(res => res.json()); return (<ul>{tasks.map(task => <li key={task.id}>{task.text}</li>)}</ul>); }`

4.  **Client Component(s) for Interactivity** (e.g., in `app/featureName/components/FeatureForm.tsx`):
    * Mark with `"use client";`.
    * Include basic state management (`useState`, `useEffect`) for user inputs, UI state, etc.
    * Implement functions to interact with the API routes (e.g., using `Workspace` to submit data or trigger actions).
    * Handle form submissions, button clicks, and other user events.
    * Example: A form to add a new task, with input fields and a submit handler that POSTs to `/api/featureName`.

5.  **Main Page Route** (e.g., in `app/featureName/page.tsx`):
    * Set up the primary page file that composes the Server and/or Client Components to present the feature to the user.
    * This page should orchestrate the display of data and interactive elements.

6.  **(Optional) Server Action(s)** (e.g., in `app/featureName/actions.ts`):
    * If appropriate for mutations, define Server Action(s) using `"use server";`.
    * Ensure inputs are validated and types are used.
    * These can be called from Client Components or used in form submissions.
    * Example: `export async function createTask(formData: FormData) { // ...logic to create task, revalidatePath('/'); }`

Guidelines for Generated Code: - Use placeholder comments (e.g., `// TODO: Implement actual data fetching/mutation`, `// TODO: Add robust error handling and user feedback`) where further implementation details are needed. - Follow Next.js App Router conventions unless the user specifies Pages Router. - Ensure generated TypeScript code is type-safe, utilizing the defined types consistently. - Provide clear context on where each file should be placed and brief instructions on how they connect. - Prioritize creating a coherent and functional set of interconnected files that provide a strong foundation for the user to build upon.