Function controller

  • Wrapper function to handle API route logic with error handling and context preparation.

    Type Parameters

    • T extends NextResponse | HandlerResponse

    Parameters

    • handler: (ctx: ControllerContext<T>) => Promise<NextResponse | HandlerResponse>

      The function that handles the request logic.

    Returns {
        authorize(
            authorizationCheck: (token: string) => boolean | Promise<boolean>,
        ): (
            request: NextRequest,
            context: { params?: Record<string, string> },
        ) => Promise<Response>;
        (
            request: NextRequest,
            context: { params?: Record<string, string> },
        ): Promise<Response>;
    }

    A handler for Next.js API routes.

      • (
            request: NextRequest,
            context: { params?: Record<string, string> },
        ): Promise<Response>
      • Main request handler.

        Parameters

        • request: NextRequest

          The incoming NextRequest object.

        • context: { params?: Record<string, string> }

          Additional context, such as route parameters.

        Returns Promise<Response>

        A Next.js Response object.

    • authorize:function
      • Adds authorization logic to the request handler.

        Parameters

        • authorizationCheck: (token: string) => boolean | Promise<boolean>

          A function to check the token's validity.

        Returns (
            request: NextRequest,
            context: { params?: Record<string, string> },
        ) => Promise<Response>

        A handler with authorization.