/*! * ======================================================================== * @rzl-zone/next-kit * ------------------------------------------------------------------------ * Version: `0.10.4` * Author: `Rizalvin Dwiky ` * Repository: `https://github.com/rzl-zone/rzl-zone/tree/main/packages/next-kit` * ======================================================================== */ import { n as ActionErrorJson, t as ActionError } from "../errors-Bcgq-OaR.cjs"; import { SafeReturn } from "p-safe"; import { RequestCookies, ResponseCookies } from "@edge-runtime/cookies"; type AnyFn = (this: This, ...args: readonly any[]) => unknown; type ActionFunc = T extends ((...args: infer Args) => infer Return) ? (this: any, ...args: Args) => Promise, ActionError>> : never; interface ActionContext { resolve: (result: Return) => never; reject: (error: ActionErrorJson | ActionError) => never; } declare class ActionClass>> { #private; private fn; constructor(fn: AnyFn>); resolve(result: Return): never; reject(reason: ActionErrorJson | ActionError): never; } type Action>> = InstanceType>; /** ------------------------------------------------------------------- * * ***A helper to simplify creating Next.js Server Actions.*** * ------------------------------------------------------------------- * * ***`⚠️ Warning: Currently is not support with turbopack flag mode !!!`*** * ------------------------------------------------------------------- * @example * * ***`actions.ts:`*** * ```tsx * "use server"; * * import { actionError, createAction } from "@rzl-zone/next-kit/extra/action"; * * export const hello = createAction(async (name: string) => { * if (!name) { * actionError("NAME_REQUIRED", "Name is required"); * } * return `Hello, ${name}!`; * }); * ``` * --- * * ***`page.tsx (server-component):`*** * ```tsx * import { hello } from "./actions"; * * export default async function Page() { * const { data, error } = await hello("John"); * if (error) return

ERROR: {error.message}

; * return

{data}

; * } * ``` */ declare function createAction>>(fn: T): ActionFunc; /** ------------------------------------------------------------------- * * ***This function is for throw error from `createAction`.*** * ------------------------------------------------------------------- * * ***`⚠️ Warning: Currently is not support with turbopack flag mode !!!`*** * ------------------------------------------------------------------- * @example * * ***`actions.ts:`*** * ```ts * "use server"; * * import { actionError, createAction } from "@rzl-zone/next-kit/extra/action"; * * export const hello = createAction(async (name: string) => { * if (!name) { * actionError("NAME_REQUIRED", "Name is required"); * } * return `Hello, ${name}!`; * }); * ``` * --- * * ***`page.tsx (server-component):`*** * ```tsx * import { hello } from "./actions"; * * export default async function Page() { * const { data, error } = await hello("John"); * if (error) return

ERROR: {error.message}

; * return

{data}

; * } * ``` */ declare function actionError(code: string, message: string): never; /** ------------------------------------------------------------------- * * ***Handles HTTP cookies for server-side components and actions.*** * ------------------------------------------------------------------- * * ***`⚠️ Warning: Currently is not support with turbopack flag mode !!!`*** * ------------------------------------------------------------------- * **This function leverages a shared request storage mechanism to access or modify the cookies.** * - ***This function serves two primary purposes:*** * 1. Reading cookies from an incoming HTTP request when used in a Server Component. * 2. Writing cookies to an outgoing HTTP response when used in a Server Component, Server Action, or Route Handler. * * @returns An object representing the mutable cookies for the current HTTP request context. * * @throws {Error} Throws an error if the request storage is not available, indicating an internal consistency issue. * * @example * // Reading cookies in a Server Component * import { cookies } from "@rzl-zone/next-kit/extra/action"; * * export default function Page() { * const requestCookies = cookies(); * console.log(requestCookies.get("sessionId")); * * return ( * //... * ); * } * * @example * // Writing cookies in a Server Action * import { cookies } from "@rzl-zone/next-kit/extra/action"; * * export function myAction() { * const responseCookies = cookies(); * responseCookies.set("sessionId", "abc123", { httpOnly: true }); * }; * * @example * // Modifying cookies in a Route Handler * import { cookies } from "@rzl-zone/next-kit/extra/action"; * * export default async function handler(req, res) { * const responseCookies = cookies(); * responseCookies.delete("sessionId"); * res.end("Cookie deleted"); * }; */ declare function cookies(): ResponseCookies; /** ------------------------------------------------------------------- * * ***Retrieves the client's IP address from request headers.*** * ------------------------------------------------------------------- * * ***`⚠️ Warning: Currently is not support with turbopack flag mode !!!`*** * ------------------------------------------------------------------- * ***The function checks various headers commonly used by different cloud providers and proxies to find the client's IP address.*** * ***It prioritizes the `x-forwarded-for` header, which may contain multiple IP addresses, and extracts the first one.*** * * ***If no valid IP is found, it return null.*** * * @returns The client's IP address. */ declare function clientIP(): Promise; export { Action, RequestCookies, ResponseCookies, actionError, clientIP, cookies, createAction };