# theedoran/next-safe-action architecture

> Architecture diagram of the GitHub repository theedoran/next-safe-action, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-28). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/theedoran/next-safe-action
- Repository: https://github.com/theedoran/next-safe-action

## Overview

##### Project Type and Purpose
- Recognize that this is a library intended for Next.js projects to perform safe server actions with full end-to-end type safety.
- It leverages modern Next.js, React, and TypeScript features while integrating with various validation libraries (like Typebox, Yup, Zod, Valibot) and supporting middleware patterns.
- The presence of a playground app and a documentation website shows that there’s an ecosystem around this library meant for demos, testing, and user support.

##### Analyzing the File Structure
- The top-level folders such as “apps” (which contains the playground demo), “packages” (which holds the core library code), and “website” (for documentation) indicate a modular monorepo.
- In the “packages/next-safe-action” directory, you see subdirectories and files that point to:
- Adapters for different validation libraries (files under “src/adapters/…”).
- Middleware components and error handling (see “src/middleware.ts” and “src/next/errors/…”).
- Hooks, stateful and stateless action functions, and utilities (files such as “hooks.ts”, “hooks-utils.ts”, “safe-action-client.ts”, etc.).
- The playground app (in “apps/playground”) is structured as a Next.js app with various example implementations which demonstrate different usage patterns (async operations, optimistic updates, file upload, etc.).
- The website directory shows a typical documentation site setup, with components, pages, docs folders, and configuration files for Docusaurus.

a. Core Library (next-safe-action):
- Adapters: These support multiple validation libraries, ensuring flexible input/output validation.
- Middleware: Responsible for error handling, server error responses, and integration points within Next.js.
- Hooks and Client-Side Utilities: Provide the mechanisms for type-safe interaction from React components.
- Utility Modules & Error Management: Handling of validation errors and metadata propagation.
b. Playground App:
- A Next.js application that demonstrates how the library is integrated (show sample pages, actions, examples, and custom hooks usage).
c. Documentation Website:
- Highlights the user guide, API reference, and examples.
- The Playground App relies on the Core Library to execute safe server actions.
- The website communicates documentation and guides that explain how to integrate the library.
- Internal modules within the core library (adapters, middleware, hooks) interact with each other for full-stack type safety.
- Directional data flow: From the client-side (React components in the Playground) through the safe action hooks to the server and then back with type-checked responses.

##### Architectural Patterns and Design Principles
- The architecture follows a modular design, evident from a separation of library concerns (action builders, adapters, middleware) and separate applications (playground for examples and website for docs).
- It uses middleware patterns to intercept and manage server actions which is central to ensuring safety and proper error handling.
- There is clear decoupling:
- The library is independent and reusable across Next.js projects.
- The playground and website serve distinct use cases (demonstration vs. documentation).
- The project also leverages best practices like automated CI/CD (GitHub workflows) and commit hooks (see .husky folder).

##### Elements
- Clear labels for each major component (Core Library, Playground App, Documentation Website).
- Sub-component labels within the Core Library:
- Adapters (with individual labels for Typebox, Yup, Zod, etc.)
- Middleware and Error Handling (including the segments under “src/next/errors”)
- Action Builder & Hooks (indicating functions like safe-action-client, hooks.ts, etc.).
- React components trigger safe actions via hooks.
- Safe actions are processed through the middleware and adapters.
- Results/validation errors are sent back to the React components.

## Diagram (Mermaid)

```mermaid
graph TD
    %% Monorepo Container
    root["Monorepo"]

    %% Core Library Subgraph
    subgraph "Core Library"
        cl["Core Library"]:::core
        va["Validation Adapters"]:::core
        me["Middleware & Error Handling"]:::core
        h["Hooks"]:::core
        sa["Safe Action Client"]:::core
        ut["Utility Modules"]:::core

        %% Internal relationships in Core Library
        cl --> h
        cl --> sa
        h --> me
        sa --> me
        me --> va
        va --> h
        h --> ut
        sa --> ut
    end

    %% Playground App Subgraph
    subgraph "Playground App"
        pa["Playground App"]:::playground
    end

    %% Documentation Website Subgraph
    subgraph "Documentation Website"
        dw["Documentation Website"]:::docs
    end

    %% External Interactions
    pa -->|"triggers safe actions"| h
    pa -->|"triggers safe actions"| sa
    dw ---|"documents"| cl

    %% Click Events for Core Library Components
    click cl "https://github.com/theedoran/next-safe-action/tree/main/packages/next-safe-action"
    click va "https://github.com/theedoran/next-safe-action/tree/main/packages/next-safe-action/src/adapters"
    click me "https://github.com/theedoran/next-safe-action/blob/main/packages/next-safe-action/src/middleware.ts"
    click h "https://github.com/theedoran/next-safe-action/blob/main/packages/next-safe-action/src/hooks.ts"
    click sa "https://github.com/theedoran/next-safe-action/blob/main/packages/next-safe-action/src/safe-action-client.ts"

    %% Click Events for Other Components
    click pa "https://github.com/theedoran/next-safe-action/tree/main/apps/playground"
    click dw "https://github.com/theedoran/next-safe-action/tree/main/website"

    %% Styles
    classDef core fill:#FFCCCC,stroke:#990000,stroke-width:2px;
    classDef playground fill:#CCFFCC,stroke:#006600,stroke-width:2px;
    classDef docs fill:#CCCCFF,stroke:#000099,stroke-width:2px;
    class cl,va,me,h,sa,ut core
    class pa playground
    class dw docs
```
