next-action-forge 0.5.1 → 0.5.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -99,6 +99,9 @@ function isErrorResponse(response) {
99
99
  return !response.success;
100
100
  }
101
101
 
102
+ // src/core/server-action-client.ts
103
+ var import_zod2 = require("zod");
104
+
102
105
  // src/next/errors/redirect.ts
103
106
  var RedirectStatusCode = /* @__PURE__ */ ((RedirectStatusCode2) => {
104
107
  RedirectStatusCode2[RedirectStatusCode2["SeeOther"] = 303] = "SeeOther";
@@ -239,7 +242,7 @@ function createServerAction(config) {
239
242
  try {
240
243
  let parsedInput = void 0;
241
244
  if (hasInputSchema && config.inputSchema) {
242
- const parseResult = config.inputSchema.safeParse(input);
245
+ const parseResult = import_zod2.z.safeParse(config.inputSchema, input);
243
246
  if (!parseResult.success) {
244
247
  return handleServerActionError(parseResult.error, config.onError ? (err) => config.onError(err, { parsedInput: input }) : void 0);
245
248
  }
@@ -261,7 +264,7 @@ function createServerAction(config) {
261
264
  const result = hasInputSchema ? await config.serverCodeFn(parsedInput, middlewareContext) : await config.serverCodeFn(middlewareContext);
262
265
  let validatedOutput = result;
263
266
  if (config.outputSchema) {
264
- const parseResult = config.outputSchema.safeParse(result);
267
+ const parseResult = import_zod2.z.safeParse(config.outputSchema, result);
265
268
  if (!parseResult.success) {
266
269
  return handleServerActionError(parseResult.error);
267
270
  }
@@ -296,18 +299,49 @@ function createServerAction(config) {
296
299
  actionFn.__hasInputSchema = config.inputSchema !== void 0 && config.inputSchema !== null;
297
300
  return actionFn;
298
301
  }
302
+ var ISO_DATE_REGEX = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d{1,3})?(?:Z|[+-]\d{2}:?\d{2})$/;
303
+ var BIGINT_REGEX = /^__bigint__(-?\d+)$/;
304
+ function restoreSpecialTypes(parsed) {
305
+ if (typeof parsed !== "object" || parsed === null) return parsed;
306
+ if ("__type__" in parsed && parsed.__type__ === "Map") {
307
+ const entries = parsed.entries;
308
+ if (Array.isArray(entries)) {
309
+ return new Map(entries);
310
+ }
311
+ }
312
+ if ("__type__" in parsed && parsed.__type__ === "Set") {
313
+ const values = parsed.values;
314
+ if (Array.isArray(values)) {
315
+ return new Set(values);
316
+ }
317
+ }
318
+ return parsed;
319
+ }
299
320
  function tryParseJSON(value) {
300
321
  if (typeof value !== "string") return value;
301
322
  const trimmed = value.trim();
302
- if (!trimmed) return value;
323
+ if (!trimmed) return null;
303
324
  if (trimmed === "true") return true;
304
325
  if (trimmed === "false") return false;
305
326
  if (trimmed === "null") return null;
327
+ const bigintMatch = trimmed.match(BIGINT_REGEX);
328
+ if (bigintMatch) {
329
+ try {
330
+ return BigInt(bigintMatch[1]);
331
+ } catch {
332
+ }
333
+ }
334
+ if (ISO_DATE_REGEX.test(trimmed)) {
335
+ const date = new Date(trimmed);
336
+ if (!isNaN(date.getTime())) {
337
+ return date;
338
+ }
339
+ }
306
340
  if (trimmed.startsWith("{") && trimmed.endsWith("}") || trimmed.startsWith("[") && trimmed.endsWith("]")) {
307
341
  try {
308
342
  const parsed = JSON.parse(trimmed);
309
343
  if (typeof parsed === "object" && parsed !== null) {
310
- return parsed;
344
+ return restoreSpecialTypes(parsed);
311
345
  }
312
346
  } catch {
313
347
  }
@@ -340,7 +374,7 @@ function createFormServerAction(config) {
340
374
  try {
341
375
  let parsedInput = parseFormData(formData);
342
376
  if (config.inputSchema) {
343
- const parseResult = config.inputSchema.safeParse(parsedInput);
377
+ const parseResult = import_zod2.z.safeParse(config.inputSchema, parsedInput);
344
378
  if (!parseResult.success) {
345
379
  return handleServerActionError(parseResult.error, config.onError ? (err) => config.onError(err, { parsedInput }) : void 0);
346
380
  }
@@ -362,7 +396,7 @@ function createFormServerAction(config) {
362
396
  const result = hasInputSchema ? await config.serverCodeFn(parsedInput, middlewareContext) : await config.serverCodeFn(middlewareContext);
363
397
  let validatedOutput = result;
364
398
  if (config.outputSchema) {
365
- const parseResult = config.outputSchema.safeParse(result);
399
+ const parseResult = import_zod2.z.safeParse(config.outputSchema, result);
366
400
  if (!parseResult.success) {
367
401
  return handleServerActionError(parseResult.error);
368
402
  }
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/core/index.ts","../../src/core/error-handler.ts","../../src/next/errors/redirect.ts","../../src/next/errors/http-access-fallback.ts","../../src/next/errors/index.ts","../../src/core/server-action-client.ts","../../src/core/middleware-helpers.ts"],"sourcesContent":["export * from \"./error-handler\";\nexport * from \"./server-action-client\";\nexport * from \"./middleware-helpers\";","import { z, ZodError } from \"zod\";\nimport type { ServerActionError, ServerActionResponse } from \"../types\";\n\nfunction convertZodError(error: ZodError): ServerActionError {\n const { fieldErrors, formErrors } = z.flattenError(error);\n const [field] = Object.keys(fieldErrors) as (keyof typeof fieldErrors)[];\n const message = (field && fieldErrors[field]?.[0]) ?? formErrors[0] ?? \"Validation failed\";\n return {\n message,\n code: \"VALIDATION_ERROR\",\n field,\n fields: fieldErrors,\n };\n}\n\nfunction convertGenericError(error: unknown): ServerActionError {\n const message = error instanceof Error ? error.message : \"An unexpected error occurred\";\n\n return {\n message: process.env.NODE_ENV === \"production\" ? \"An unexpected error occurred\" : message,\n code: \"INTERNAL_ERROR\",\n statusCode: 500,\n };\n}\n\nexport function handleServerActionError(\n error: unknown,\n customHandler?: (error: unknown) => ServerActionError | undefined\n): { success: false; error: ServerActionError } {\n // Debug logging only in development\n if (process.env.NODE_ENV === 'development') {\n console.error(\"[Server Action Error]\", error?.constructor?.name || 'Unknown', error instanceof Error ? error.message : error);\n }\n\n // Try custom handler first\n if (customHandler) {\n try {\n const customError = customHandler(error);\n if (customError) {\n return {\n success: false,\n error: customError,\n };\n }\n } catch (handlerError) {\n console.error(\"[Server Action Error] Custom handler threw an error:\", handlerError);\n }\n }\n\n if (error instanceof ZodError) {\n return {\n success: false,\n error: convertZodError(error),\n };\n }\n\n if (error instanceof Error && \"toServerActionError\" in error && typeof error.toServerActionError === \"function\") {\n const serverError = error.toServerActionError() as ServerActionError;\n \n // Check if this is an auth error based on the error type/code\n if (serverError.code === \"AUTHENTICATION_ERROR\" || (error as any).type === \"AUTHENTICATION_ERROR\") {\n serverError.shouldRedirect = true;\n serverError.redirectTo = \"/login\";\n }\n \n return {\n success: false,\n error: serverError,\n };\n }\n\n return {\n success: false,\n error: convertGenericError(error),\n };\n}\n\nexport function isErrorResponse<T>(response: ServerActionResponse<T>): response is { success: false; error: ServerActionError } {\n return !response.success;\n}","// Comes from: https://github.com/vercel/next.js/blob/canary/packages/next/src/client/components/redirect-error.ts\n\nenum RedirectStatusCode {\n\tSeeOther = 303,\n\tTemporaryRedirect = 307,\n\tPermanentRedirect = 308,\n}\n\nconst REDIRECT_ERROR_CODE = \"NEXT_REDIRECT\";\n\nenum RedirectType {\n\tpush = \"push\",\n\treplace = \"replace\",\n}\n\nexport type RedirectError = Error & {\n\tdigest: `${typeof REDIRECT_ERROR_CODE};${RedirectType};${string};${RedirectStatusCode};`;\n};\n\n/**\n * Checks an error to determine if it's an error generated by the\n * `redirect(url)` helper.\n *\n * @param error the error that may reference a redirect error\n * @returns true if the error is a redirect error\n */\nexport function isRedirectError(error: unknown): error is RedirectError {\n\tif (typeof error !== \"object\" || error === null || !(\"digest\" in error) || typeof error.digest !== \"string\") {\n\t\treturn false;\n\t}\n\n\tconst digest = error.digest.split(\";\");\n\tconst [errorCode, type] = digest;\n\tconst destination = digest.slice(2, -2).join(\";\");\n\tconst status = digest.at(-2);\n\n\tconst statusCode = Number(status);\n\n\treturn (\n\t\terrorCode === REDIRECT_ERROR_CODE &&\n\t\t(type === \"replace\" || type === \"push\") &&\n\t\ttypeof destination === \"string\" &&\n\t\t!isNaN(statusCode) &&\n\t\tstatusCode in RedirectStatusCode\n\t);\n}","// Comes from https://github.com/vercel/next.js/blob/canary/packages/next/src/client/components/http-access-fallback/http-access-fallback.ts\n\nconst HTTPAccessErrorStatus = {\n\tNOT_FOUND: 404,\n\tFORBIDDEN: 403,\n\tUNAUTHORIZED: 401,\n};\n\nconst ALLOWED_CODES = new Set(Object.values(HTTPAccessErrorStatus));\n\nconst HTTP_ERROR_FALLBACK_ERROR_CODE = \"NEXT_HTTP_ERROR_FALLBACK\";\n\nexport type HTTPAccessFallbackError = Error & {\n\tdigest: `${typeof HTTP_ERROR_FALLBACK_ERROR_CODE};${string}`;\n};\n\n/**\n * Checks an error to determine if it's an error generated by\n * the HTTP navigation APIs `notFound()`, `forbidden()` or `unauthorized()`.\n *\n * @param error the error that may reference a HTTP access error\n * @returns true if the error is a HTTP access error\n */\nexport function isHTTPAccessFallbackError(error: unknown): error is HTTPAccessFallbackError {\n\tif (typeof error !== \"object\" || error === null || !(\"digest\" in error) || typeof error.digest !== \"string\") {\n\t\treturn false;\n\t}\n\tconst [prefix, httpStatus] = error.digest.split(\";\");\n\n\treturn prefix === HTTP_ERROR_FALLBACK_ERROR_CODE && ALLOWED_CODES.has(Number(httpStatus));\n}\n\nexport function getAccessFallbackHTTPStatus(error: HTTPAccessFallbackError): number {\n\tconst httpStatus = error.digest.split(\";\")[1];\n\treturn Number(httpStatus);\n}","import { isRedirectError } from \"./redirect\";\nimport { isHTTPAccessFallbackError, getAccessFallbackHTTPStatus } from \"./http-access-fallback\";\n\nexport { isRedirectError } from \"./redirect\";\nexport type { RedirectError } from \"./redirect\";\n\nexport { isHTTPAccessFallbackError, getAccessFallbackHTTPStatus } from \"./http-access-fallback\";\nexport type { HTTPAccessFallbackError } from \"./http-access-fallback\";\n\n/**\n * Checks if the error is a navigation error that should be re-thrown\n * This includes redirect errors and HTTP access errors (notFound, forbidden, unauthorized)\n */\nexport function isNextNavigationError(error: unknown): boolean {\n\treturn isRedirectError(error) || isHTTPAccessFallbackError(error);\n}\n\n/**\n * Checks if the error is a notFound error\n * Note: Next.js implements notFound() using HTTP_ERROR_FALLBACK with status 404,\n * not as a separate error type like NEXT_REDIRECT\n */\nexport function isNotFoundError(error: unknown): boolean {\n\treturn isHTTPAccessFallbackError(error) && getAccessFallbackHTTPStatus(error as any) === 404;\n}","import { z } from \"zod\";\nimport type { ServerActionError, ServerActionResponse, ServerAction } from \"../types\";\nimport { handleServerActionError } from \"./error-handler\";\nimport { isNextNavigationError } from \"../next/errors\";\n\ntype InferZodOutput<T> = T extends z.ZodTypeAny ? z.output<T> : never;\ntype InferZodInput<T> = T extends z.ZodTypeAny ? z.input<T> : never;\n\ntype ServerActionMiddleware<Context, Input = unknown> = (params: { context: Context; input: Input }) => Promise<{ context: Context } | { error: ServerActionError }>;\n\nimport type { RedirectConfig } from \"../types\";\n\ntype SafeActionClientArgs<Context extends object, InputSchema extends z.ZodTypeAny | undefined, OutputSchema extends z.ZodTypeAny | undefined> = {\n middlewareFns: ServerActionMiddleware<any, any>[];\n inputSchema?: InputSchema;\n outputSchema?: OutputSchema;\n onError?: (error: unknown, context: { parsedInput?: unknown }) => ServerActionError | undefined;\n ctxType?: Context;\n redirectConfig?: string | RedirectConfig | ((result: any) => string | RedirectConfig | undefined);\n};\n\nexport class ServerActionClient<Context extends object = {}, InputSchema extends z.ZodTypeAny | undefined = undefined, OutputSchema extends z.ZodTypeAny | undefined = undefined> {\n readonly #args: SafeActionClientArgs<Context, InputSchema, OutputSchema>;\n\n constructor(\n args: SafeActionClientArgs<Context, InputSchema, OutputSchema> = {\n middlewareFns: [],\n }\n ) {\n this.#args = args;\n }\n\n /**\n * Use a middleware function.\n * @param middlewareFn Middleware function\n */\n use<NextContext extends object>(\n middlewareFn: ServerActionMiddleware<\n Context & NextContext, \n InputSchema extends z.ZodTypeAny ? z.output<InputSchema> : undefined\n >\n ) {\n return new ServerActionClient<Context & NextContext, InputSchema, OutputSchema>({\n ...this.#args,\n middlewareFns: [...this.#args.middlewareFns, middlewareFn] as any,\n ctxType: {} as Context & NextContext,\n });\n }\n\n /**\n * Define the input validation schema for the action.\n * @param schema Input validation schema\n */\n inputSchema<IS extends z.ZodTypeAny>(schema: IS) {\n return new ServerActionClient<Context, IS, OutputSchema>({\n ...this.#args,\n inputSchema: schema,\n });\n }\n\n /**\n * Define the output validation schema for the action.\n * @param schema Output validation schema\n */\n outputSchema<OS extends z.ZodTypeAny>(schema: OS) {\n return new ServerActionClient<Context, InputSchema, OS>({\n ...this.#args,\n outputSchema: schema,\n });\n }\n\n /**\n * Define a custom error handler for the action.\n * @param handler Error handler function\n */\n onError(handler: (error: unknown, context: { parsedInput?: unknown }) => ServerActionError | undefined) {\n return new ServerActionClient<Context, InputSchema, OutputSchema>({\n ...this.#args,\n onError: handler,\n });\n }\n\n /**\n * Define a redirect configuration for successful actions.\n * @param config Redirect URL, config object, or function that returns redirect config based on result\n */\n redirect<Data = any>(\n config: string | RedirectConfig | ((result: Data) => string | RedirectConfig | undefined)\n ) {\n return new ServerActionClient<Context, InputSchema, OutputSchema>({\n ...this.#args,\n redirectConfig: config,\n });\n }\n\n\n /**\n * Define the action.\n * @param serverCodeFn Code that will be executed on the server side\n */\n action<Data>(\n serverCodeFn: InputSchema extends undefined\n ? (context: Context) => Promise<Data>\n : InputSchema extends z.ZodTypeAny \n ? (input: InferZodOutput<InputSchema>, context: Context) => Promise<Data>\n : (context: Context) => Promise<Data>\n ): ServerAction<\n InputSchema extends undefined \n ? void \n : InputSchema extends z.ZodTypeAny \n ? InferZodInput<InputSchema> \n : void, \n OutputSchema extends z.ZodTypeAny ? InferZodOutput<OutputSchema> : Data\n > {\n return createServerAction<\n InputSchema extends undefined \n ? void \n : InputSchema extends z.ZodTypeAny \n ? InferZodInput<InputSchema> \n : void,\n OutputSchema extends z.ZodTypeAny ? InferZodOutput<OutputSchema> : Data\n >({\n inputSchema: this.#args.inputSchema,\n outputSchema: this.#args.outputSchema,\n middlewareFns: this.#args.middlewareFns,\n onError: this.#args.onError,\n serverCodeFn: serverCodeFn as any,\n redirectConfig: this.#args.redirectConfig,\n });\n }\n\n /**\n * Define a form action that accepts FormData.\n * @param serverCodeFn Code that will be executed on the server side\n */\n formAction<Data>(\n serverCodeFn: InputSchema extends undefined\n ? (context: Context) => Promise<Data>\n : InputSchema extends z.ZodTypeAny \n ? (input: InferZodOutput<InputSchema>, context: Context) => Promise<Data>\n : (context: Context) => Promise<Data>\n ): (prev: ServerActionResponse<OutputSchema extends z.ZodTypeAny ? InferZodOutput<OutputSchema> : Data>, formData: FormData) => Promise<ServerActionResponse<OutputSchema extends z.ZodTypeAny ? InferZodOutput<OutputSchema> : Data>> {\n return createFormServerAction({\n inputSchema: this.#args.inputSchema,\n outputSchema: this.#args.outputSchema,\n middlewareFns: this.#args.middlewareFns,\n onError: this.#args.onError,\n serverCodeFn,\n redirectConfig: this.#args.redirectConfig,\n });\n }\n}\n\n// Internal function to create the actual server action\nfunction createServerAction<TInput, TOutput>(config: {\n inputSchema?: z.ZodTypeAny;\n outputSchema?: z.ZodTypeAny;\n middlewareFns: ServerActionMiddleware<any, any>[];\n onError?: (error: unknown, context: { parsedInput?: unknown }) => ServerActionError | undefined;\n serverCodeFn: (...args: any[]) => Promise<any>;\n redirectConfig?: string | RedirectConfig | ((result: any) => string | RedirectConfig | undefined);\n}): ServerAction<TInput, TOutput> {\n const actionFn = async (...args: TInput extends void ? [] : [input: TInput]): Promise<ServerActionResponse<TOutput>> => {\n const context: { parsedInput?: unknown } = {};\n \n // Type guard: check if we have an input schema\n const hasInputSchema = config.inputSchema !== undefined && config.inputSchema !== null;\n const input = hasInputSchema ? args[0] as TInput : undefined;\n\n try {\n let parsedInput: any = undefined;\n\n // Input validation\n if (hasInputSchema && config.inputSchema) {\n const parseResult = config.inputSchema.safeParse(input);\n if (!parseResult.success) {\n return handleServerActionError(parseResult.error, config.onError ? (err) => config.onError!(err, { parsedInput: input }) : undefined);\n }\n parsedInput = parseResult.data;\n context.parsedInput = parsedInput;\n }\n\n // Execute middleware chain\n let middlewareContext = {};\n for (const middleware of config.middlewareFns) {\n const middlewareInput = hasInputSchema ? parsedInput : undefined;\n const result = await middleware({ context: middlewareContext, input: middlewareInput });\n if (\"error\" in result) {\n return {\n success: false,\n error: result.error,\n };\n }\n middlewareContext = { ...middlewareContext, ...result.context };\n }\n\n // Execute the server code\n const result = hasInputSchema \n ? await config.serverCodeFn(parsedInput, middlewareContext)\n : await config.serverCodeFn(middlewareContext);\n\n // Output validation\n let validatedOutput: any = result;\n if (config.outputSchema) {\n const parseResult = config.outputSchema.safeParse(result);\n if (!parseResult.success) {\n return handleServerActionError(parseResult.error);\n }\n validatedOutput = parseResult.data;\n }\n\n // Handle redirect config if present\n let redirectValue: string | RedirectConfig | undefined;\n if (config.redirectConfig) {\n if (typeof config.redirectConfig === 'function') {\n redirectValue = config.redirectConfig(validatedOutput);\n } else {\n redirectValue = config.redirectConfig;\n }\n }\n\n return {\n success: true,\n data: validatedOutput,\n ...(redirectValue && { redirect: redirectValue }),\n };\n } catch (error) {\n // Preserve Next.js navigation errors (redirect, notFound, etc.)\n if (isNextNavigationError(error)) {\n throw error;\n }\n \n // Debug logging only in development\n if (process.env.NODE_ENV === 'development') {\n console.log(\"[executeAction] Error:\", error?.constructor?.name, error instanceof Error ? error.message : error);\n }\n \n // Use the handleServerActionError function with custom handler\n const errorHandler = config.onError ? (err: unknown) => {\n return config.onError!(err, { parsedInput: context.parsedInput });\n } : undefined;\n \n return handleServerActionError(error, errorHandler);\n }\n };\n\n // Add metadata for runtime type checking\n (actionFn as any).__hasInputSchema = config.inputSchema !== undefined && config.inputSchema !== null;\n \n // Return the function with correct typing based on whether input is void\n return actionFn as ServerAction<TInput, TOutput>;\n}\n\n/**\n * Safely parses JSON strings back to objects/arrays and handles FormData boolean conventions.\n * \n * DESIGN PRINCIPLES:\n * - Parse objects and arrays (start with { or [)\n * - Parse \"true\"/\"false\" to booleans (FormData convention for checkboxes)\n * - DO NOT parse number strings (\"123\") to avoid ambiguity\n * - Fail gracefully on malformed JSON\n * - Maintain perfect symmetry with client-side objectToFormData\n * \n * WHY WE PARSE BOOLEANS:\n * - HTML forms cannot send real JavaScript booleans, only strings\n * - Checkboxes commonly send \"true\"/\"false\" strings\n * - React Hook Form uses this convention\n * - This is the expected behavior in FormData context\n */\nfunction tryParseJSON(value: unknown): unknown {\n // Non-strings pass through unchanged\n if (typeof value !== 'string') return value;\n \n const trimmed = value.trim();\n \n // Empty strings remain empty\n if (!trimmed) return value;\n \n // Parse boolean strings (common for checkboxes and React Hook Form)\n // This is safe in FormData context since HTML can't send real booleans\n if (trimmed === 'true') return true;\n if (trimmed === 'false') return false;\n \n // Parse null (less common but sometimes used)\n if (trimmed === 'null') return null;\n \n // Only attempt parsing for obvious JSON structures\n if ((trimmed.startsWith('{') && trimmed.endsWith('}')) ||\n (trimmed.startsWith('[') && trimmed.endsWith(']'))) {\n try {\n const parsed = JSON.parse(trimmed);\n // Extra safety: verify we got an object/array\n if (typeof parsed === 'object' && parsed !== null) {\n return parsed;\n }\n } catch {\n // Malformed JSON - return original string\n }\n }\n \n // All other strings (including number strings like \"123\") remain as strings\n // This avoids ambiguity with actual string values that happen to be numeric\n return value;\n}\n\n// Form data parser with automatic JSON deserialization\nfunction parseFormData(formData: FormData): Record<string, any> {\n const result: Record<string, any> = {};\n const keys = new Set<string>();\n\n for (const [key] of formData.entries()) {\n keys.add(key);\n }\n\n for (const key of keys) {\n const values = formData.getAll(key);\n\n if (key.endsWith(\"[]\")) {\n const cleanKey = key.slice(0, -2);\n // Parse each array item (handles nested objects in arrays)\n result[cleanKey] = values.map(tryParseJSON);\n } else if (values.length === 1) {\n // Parse single value (handles JSON stringified objects)\n result[key] = tryParseJSON(values[0]);\n } else {\n // Parse multiple values\n result[key] = values.map(tryParseJSON);\n }\n }\n\n return result;\n}\n\n// Internal function to create form server action\nfunction createFormServerAction<TOutput>(config: {\n inputSchema?: z.ZodTypeAny;\n outputSchema?: z.ZodTypeAny;\n middlewareFns: ServerActionMiddleware<any>[];\n onError?: (error: unknown, context: { parsedInput?: unknown }) => ServerActionError | undefined;\n serverCodeFn: (...args: any[]) => Promise<any>;\n redirectConfig?: string | RedirectConfig | ((result: any) => string | RedirectConfig | undefined);\n}): (prev: ServerActionResponse<TOutput>, formData: FormData) => Promise<ServerActionResponse<TOutput>> {\n return async (_prev: ServerActionResponse<TOutput>, formData: FormData): Promise<ServerActionResponse<TOutput>> => {\n const context: { parsedInput?: unknown } = {};\n const hasInputSchema = config.inputSchema !== undefined && config.inputSchema !== null;\n\n try {\n let parsedInput: any = parseFormData(formData);\n\n // Input validation\n if (config.inputSchema) {\n const parseResult = config.inputSchema.safeParse(parsedInput);\n if (!parseResult.success) {\n return handleServerActionError(parseResult.error, config.onError ? (err) => config.onError!(err, { parsedInput }) : undefined);\n }\n parsedInput = parseResult.data;\n context.parsedInput = parsedInput;\n }\n\n // Execute middleware chain\n let middlewareContext = {};\n for (const middleware of config.middlewareFns) {\n const middlewareInput = hasInputSchema ? parsedInput : undefined;\n const result = await middleware({ context: middlewareContext, input: middlewareInput });\n if (\"error\" in result) {\n return {\n success: false,\n error: result.error,\n };\n }\n middlewareContext = { ...middlewareContext, ...result.context };\n }\n\n // Execute the server code\n const result = hasInputSchema \n ? await config.serverCodeFn(parsedInput, middlewareContext)\n : await config.serverCodeFn(middlewareContext);\n\n // Output validation\n let validatedOutput: any = result;\n if (config.outputSchema) {\n const parseResult = config.outputSchema.safeParse(result);\n if (!parseResult.success) {\n return handleServerActionError(parseResult.error);\n }\n validatedOutput = parseResult.data;\n }\n\n // Handle redirect config if present\n let redirectValue: string | RedirectConfig | undefined;\n if (config.redirectConfig) {\n if (typeof config.redirectConfig === 'function') {\n redirectValue = config.redirectConfig(validatedOutput);\n } else {\n redirectValue = config.redirectConfig;\n }\n }\n\n return {\n success: true,\n data: validatedOutput,\n ...(redirectValue && { redirect: redirectValue }),\n };\n } catch (error) {\n // Preserve Next.js navigation errors (redirect, notFound, etc.)\n if (isNextNavigationError(error)) {\n throw error;\n }\n // Debug logging only in development\n if (process.env.NODE_ENV === 'development') {\n console.log(\"[ServerActionClient] Error:\", error?.constructor?.name, error instanceof Error ? error.message : error);\n }\n const errorHandler = config.onError ? (err: unknown) => {\n return config.onError!(err, { parsedInput: context.parsedInput });\n } : undefined;\n return handleServerActionError(error, errorHandler);\n }\n };\n}\n\n// Export factory function for creating a new client\nexport function createActionClient() {\n return new ServerActionClient();\n}\n","import type { ServerActionError } from '../types';\n\n/**\n * Type definition for Server Action Middleware\n *\n * A middleware can either:\n * - Return an updated context that gets merged with the existing context\n * - Return an error that stops the action execution\n */\nexport type ServerActionMiddleware<Context, Input = unknown> = (params: {\n context: Context;\n input: Input;\n}) => Promise<{ context: Context } | { error: ServerActionError }>;\n\n/**\n * Helper for creating middleware with correct type inference for the output context.\n *\n * This helper solves the TypeScript type inference issue where factory functions\n * that return middleware lose the context type information. By wrapping your\n * middleware with this helper, you explicitly declare what context type the\n * middleware will output, enabling proper type inference through the `.use()` chain.\n *\n * @template TOutputContext - The type of context this middleware will output\n * @template TInput - The type of input data (defaults to unknown)\n *\n * @param middlewareFn - The middleware function that transforms context\n * @returns A typed middleware function with proper type inference\n *\n * @example\n * ```typescript\n * interface AuthContext {\n * userId: string;\n * role: string;\n * }\n *\n * const createAuthMiddleware = () =>\n * createTypedMiddleware<AuthContext>(async ({ context, input }) => {\n * const session = await getSession();\n *\n * if (!session) {\n * return { error: { message: 'Unauthorized', code: 'AUTH_ERROR' } };\n * }\n *\n * return {\n * context: {\n * userId: session.userId,\n * role: session.role,\n * },\n * };\n * });\n *\n * // Now TypeScript correctly infers authClient has AuthContext\n * const authClient = baseClient.use(createAuthMiddleware());\n *\n * // And in your actions, context is properly typed\n * authClient.action(async (context) => {\n * const userId = context.userId; // ✅ Type-safe!\n * });\n * ```\n */\nexport function createTypedMiddleware<TOutputContext extends object, TInput = unknown>(\n middlewareFn: (params: {\n context: any;\n input: TInput;\n }) => Promise<{ context: TOutputContext } | { error: ServerActionError }>\n): ServerActionMiddleware<TOutputContext, TInput> {\n return middlewareFn as any;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,iBAA4B;AAG5B,SAAS,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;AACJ,aAAO,wBAAwB,OAAO,YAAY;AAAA,IACpD;AAAA,EACF;AACF;AAGO,SAAS,qBAAqB;AACnC,SAAO,IAAI,mBAAmB;AAChC;;;AC3WO,SAAS,sBACd,cAIgD;AAChD,SAAO;AACT;","names":["RedirectStatusCode","result"]}
1
+ {"version":3,"sources":["../../src/core/index.ts","../../src/core/error-handler.ts","../../src/core/server-action-client.ts","../../src/next/errors/redirect.ts","../../src/next/errors/http-access-fallback.ts","../../src/next/errors/index.ts","../../src/core/middleware-helpers.ts"],"sourcesContent":["export * from \"./error-handler\";\nexport * from \"./server-action-client\";\nexport * from \"./middleware-helpers\";","import { z, ZodError } from \"zod\";\nimport type { ServerActionError, ServerActionResponse } from \"../types\";\n\nfunction convertZodError(error: ZodError): ServerActionError {\n const { fieldErrors, formErrors } = z.flattenError(error);\n const [field] = Object.keys(fieldErrors) as (keyof typeof fieldErrors)[];\n const message = (field && fieldErrors[field]?.[0]) ?? formErrors[0] ?? \"Validation failed\";\n return {\n message,\n code: \"VALIDATION_ERROR\",\n field,\n fields: fieldErrors,\n };\n}\n\nfunction convertGenericError(error: unknown): ServerActionError {\n const message = error instanceof Error ? error.message : \"An unexpected error occurred\";\n\n return {\n message: process.env.NODE_ENV === \"production\" ? \"An unexpected error occurred\" : message,\n code: \"INTERNAL_ERROR\",\n statusCode: 500,\n };\n}\n\nexport function handleServerActionError(\n error: unknown,\n customHandler?: (error: unknown) => ServerActionError | undefined\n): { success: false; error: ServerActionError } {\n // Debug logging only in development\n if (process.env.NODE_ENV === 'development') {\n console.error(\"[Server Action Error]\", error?.constructor?.name || 'Unknown', error instanceof Error ? error.message : error);\n }\n\n // Try custom handler first\n if (customHandler) {\n try {\n const customError = customHandler(error);\n if (customError) {\n return {\n success: false,\n error: customError,\n };\n }\n } catch (handlerError) {\n console.error(\"[Server Action Error] Custom handler threw an error:\", handlerError);\n }\n }\n\n if (error instanceof ZodError) {\n return {\n success: false,\n error: convertZodError(error),\n };\n }\n\n if (error instanceof Error && \"toServerActionError\" in error && typeof error.toServerActionError === \"function\") {\n const serverError = error.toServerActionError() as ServerActionError;\n \n // Check if this is an auth error based on the error type/code\n if (serverError.code === \"AUTHENTICATION_ERROR\" || (error as any).type === \"AUTHENTICATION_ERROR\") {\n serverError.shouldRedirect = true;\n serverError.redirectTo = \"/login\";\n }\n \n return {\n success: false,\n error: serverError,\n };\n }\n\n return {\n success: false,\n error: convertGenericError(error),\n };\n}\n\nexport function isErrorResponse<T>(response: ServerActionResponse<T>): response is { success: false; error: ServerActionError } {\n return !response.success;\n}","import { z } from \"zod\";\nimport type { ServerActionError, ServerActionResponse, ServerAction } from \"../types\";\nimport { handleServerActionError } from \"./error-handler\";\nimport { isNextNavigationError } from \"../next/errors\";\n\ntype InferZodOutput<T> = T extends z.ZodTypeAny ? z.output<T> : never;\ntype InferZodInput<T> = T extends z.ZodTypeAny ? z.input<T> : never;\n\ntype ServerActionMiddleware<Context, Input = unknown> = (params: { context: Context; input: Input }) => Promise<{ context: Context } | { error: ServerActionError }>;\n\nimport type { RedirectConfig } from \"../types\";\n\ntype SafeActionClientArgs<Context extends object, InputSchema extends z.ZodTypeAny | undefined, OutputSchema extends z.ZodTypeAny | undefined> = {\n middlewareFns: ServerActionMiddleware<any, any>[];\n inputSchema?: InputSchema;\n outputSchema?: OutputSchema;\n onError?: (error: unknown, context: { parsedInput?: unknown }) => ServerActionError | undefined;\n ctxType?: Context;\n redirectConfig?: string | RedirectConfig | ((result: any) => string | RedirectConfig | undefined);\n};\n\nexport class ServerActionClient<Context extends object = {}, InputSchema extends z.ZodTypeAny | undefined = undefined, OutputSchema extends z.ZodTypeAny | undefined = undefined> {\n readonly #args: SafeActionClientArgs<Context, InputSchema, OutputSchema>;\n\n constructor(\n args: SafeActionClientArgs<Context, InputSchema, OutputSchema> = {\n middlewareFns: [],\n }\n ) {\n this.#args = args;\n }\n\n /**\n * Use a middleware function.\n * @param middlewareFn Middleware function\n */\n use<NextContext extends object>(\n middlewareFn: ServerActionMiddleware<\n Context & NextContext, \n InputSchema extends z.ZodTypeAny ? z.output<InputSchema> : undefined\n >\n ) {\n return new ServerActionClient<Context & NextContext, InputSchema, OutputSchema>({\n ...this.#args,\n middlewareFns: [...this.#args.middlewareFns, middlewareFn] as any,\n ctxType: {} as Context & NextContext,\n });\n }\n\n /**\n * Define the input validation schema for the action.\n * @param schema Input validation schema\n */\n inputSchema<IS extends z.ZodTypeAny>(schema: IS) {\n return new ServerActionClient<Context, IS, OutputSchema>({\n ...this.#args,\n inputSchema: schema,\n });\n }\n\n /**\n * Define the output validation schema for the action.\n * @param schema Output validation schema\n */\n outputSchema<OS extends z.ZodTypeAny>(schema: OS) {\n return new ServerActionClient<Context, InputSchema, OS>({\n ...this.#args,\n outputSchema: schema,\n });\n }\n\n /**\n * Define a custom error handler for the action.\n * @param handler Error handler function\n */\n onError(handler: (error: unknown, context: { parsedInput?: unknown }) => ServerActionError | undefined) {\n return new ServerActionClient<Context, InputSchema, OutputSchema>({\n ...this.#args,\n onError: handler,\n });\n }\n\n /**\n * Define a redirect configuration for successful actions.\n * @param config Redirect URL, config object, or function that returns redirect config based on result\n */\n redirect<Data = any>(\n config: string | RedirectConfig | ((result: Data) => string | RedirectConfig | undefined)\n ) {\n return new ServerActionClient<Context, InputSchema, OutputSchema>({\n ...this.#args,\n redirectConfig: config,\n });\n }\n\n\n /**\n * Define the action.\n * @param serverCodeFn Code that will be executed on the server side\n */\n action<Data>(\n serverCodeFn: InputSchema extends undefined\n ? (context: Context) => Promise<Data>\n : InputSchema extends z.ZodTypeAny \n ? (input: InferZodOutput<InputSchema>, context: Context) => Promise<Data>\n : (context: Context) => Promise<Data>\n ): ServerAction<\n InputSchema extends undefined \n ? void \n : InputSchema extends z.ZodTypeAny \n ? InferZodInput<InputSchema> \n : void, \n OutputSchema extends z.ZodTypeAny ? InferZodOutput<OutputSchema> : Data\n > {\n return createServerAction<\n InputSchema extends undefined \n ? void \n : InputSchema extends z.ZodTypeAny \n ? InferZodInput<InputSchema> \n : void,\n OutputSchema extends z.ZodTypeAny ? InferZodOutput<OutputSchema> : Data\n >({\n inputSchema: this.#args.inputSchema,\n outputSchema: this.#args.outputSchema,\n middlewareFns: this.#args.middlewareFns,\n onError: this.#args.onError,\n serverCodeFn: serverCodeFn as any,\n redirectConfig: this.#args.redirectConfig,\n });\n }\n\n /**\n * Define a form action that accepts FormData.\n * @param serverCodeFn Code that will be executed on the server side\n */\n formAction<Data>(\n serverCodeFn: InputSchema extends undefined\n ? (context: Context) => Promise<Data>\n : InputSchema extends z.ZodTypeAny \n ? (input: InferZodOutput<InputSchema>, context: Context) => Promise<Data>\n : (context: Context) => Promise<Data>\n ): (prev: ServerActionResponse<OutputSchema extends z.ZodTypeAny ? InferZodOutput<OutputSchema> : Data>, formData: FormData) => Promise<ServerActionResponse<OutputSchema extends z.ZodTypeAny ? InferZodOutput<OutputSchema> : Data>> {\n return createFormServerAction({\n inputSchema: this.#args.inputSchema,\n outputSchema: this.#args.outputSchema,\n middlewareFns: this.#args.middlewareFns,\n onError: this.#args.onError,\n serverCodeFn,\n redirectConfig: this.#args.redirectConfig,\n });\n }\n}\n\n// Internal function to create the actual server action\nfunction createServerAction<TInput, TOutput>(config: {\n inputSchema?: z.ZodTypeAny;\n outputSchema?: z.ZodTypeAny;\n middlewareFns: ServerActionMiddleware<any, any>[];\n onError?: (error: unknown, context: { parsedInput?: unknown }) => ServerActionError | undefined;\n serverCodeFn: (...args: any[]) => Promise<any>;\n redirectConfig?: string | RedirectConfig | ((result: any) => string | RedirectConfig | undefined);\n}): ServerAction<TInput, TOutput> {\n const actionFn = async (...args: TInput extends void ? [] : [input: TInput]): Promise<ServerActionResponse<TOutput>> => {\n const context: { parsedInput?: unknown } = {};\n \n // Type guard: check if we have an input schema\n const hasInputSchema = config.inputSchema !== undefined && config.inputSchema !== null;\n const input = hasInputSchema ? args[0] as TInput : undefined;\n\n try {\n let parsedInput: any = undefined;\n\n // Input validation\n if (hasInputSchema && config.inputSchema) {\n const parseResult = z.safeParse(config.inputSchema, input);\n if (!parseResult.success) {\n return handleServerActionError(parseResult.error, config.onError ? (err) => config.onError!(err, { parsedInput: input }) : undefined);\n }\n parsedInput = parseResult.data;\n context.parsedInput = parsedInput;\n }\n\n // Execute middleware chain\n let middlewareContext = {};\n for (const middleware of config.middlewareFns) {\n const middlewareInput = hasInputSchema ? parsedInput : undefined;\n const result = await middleware({ context: middlewareContext, input: middlewareInput });\n if (\"error\" in result) {\n return {\n success: false,\n error: result.error,\n };\n }\n middlewareContext = { ...middlewareContext, ...result.context };\n }\n\n // Execute the server code\n const result = hasInputSchema \n ? await config.serverCodeFn(parsedInput, middlewareContext)\n : await config.serverCodeFn(middlewareContext);\n\n // Output validation\n let validatedOutput: any = result;\n if (config.outputSchema) {\n const parseResult = z.safeParse(config.outputSchema, result);\n if (!parseResult.success) {\n return handleServerActionError(parseResult.error);\n }\n validatedOutput = parseResult.data;\n }\n\n // Handle redirect config if present\n let redirectValue: string | RedirectConfig | undefined;\n if (config.redirectConfig) {\n if (typeof config.redirectConfig === 'function') {\n redirectValue = config.redirectConfig(validatedOutput);\n } else {\n redirectValue = config.redirectConfig;\n }\n }\n\n return {\n success: true,\n data: validatedOutput,\n ...(redirectValue && { redirect: redirectValue }),\n };\n } catch (error) {\n // Preserve Next.js navigation errors (redirect, notFound, etc.)\n if (isNextNavigationError(error)) {\n throw error;\n }\n\n // Debug logging only in development\n if (process.env.NODE_ENV === 'development') {\n console.log(\"[executeAction] Error:\", error?.constructor?.name, error instanceof Error ? error.message : error);\n }\n \n // Use the handleServerActionError function with custom handler\n const errorHandler = config.onError ? (err: unknown) => {\n return config.onError!(err, { parsedInput: context.parsedInput });\n } : undefined;\n \n return handleServerActionError(error, errorHandler);\n }\n };\n\n // Add metadata for runtime type checking\n (actionFn as any).__hasInputSchema = config.inputSchema !== undefined && config.inputSchema !== null;\n \n // Return the function with correct typing based on whether input is void\n return actionFn as ServerAction<TInput, TOutput>;\n}\n\n// ISO 8601 date string regex pattern\nconst ISO_DATE_REGEX = /^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(?:\\.\\d{1,3})?(?:Z|[+-]\\d{2}:?\\d{2})$/;\n\n// BigInt marker pattern (from client-side serialization)\nconst BIGINT_REGEX = /^__bigint__(-?\\d+)$/;\n\n/**\n * Restores special types (Map, Set) from their serialized form.\n */\nfunction restoreSpecialTypes(parsed: unknown): unknown {\n if (typeof parsed !== 'object' || parsed === null) return parsed;\n\n // Check for Map marker\n if ('__type__' in parsed && (parsed as Record<string, unknown>).__type__ === 'Map') {\n const entries = (parsed as Record<string, unknown>).entries;\n if (Array.isArray(entries)) {\n return new Map(entries as [unknown, unknown][]);\n }\n }\n\n // Check for Set marker\n if ('__type__' in parsed && (parsed as Record<string, unknown>).__type__ === 'Set') {\n const values = (parsed as Record<string, unknown>).values;\n if (Array.isArray(values)) {\n return new Set(values);\n }\n }\n\n return parsed;\n}\n\n/**\n * Safely parses JSON strings back to objects/arrays and handles FormData boolean conventions.\n *\n * DESIGN PRINCIPLES:\n * - Parse objects and arrays (start with { or [)\n * - Parse \"true\"/\"false\" to booleans (FormData convention for checkboxes)\n * - Parse ISO 8601 date strings to Date objects\n * - Parse BigInt strings (prefixed with __bigint__)\n * - Restore Map and Set from serialized form\n * - DO NOT parse number strings (\"123\") to avoid ambiguity\n * - Fail gracefully on malformed JSON\n * - Maintain perfect symmetry with client-side objectToFormData\n *\n * WHY WE PARSE BOOLEANS:\n * - HTML forms cannot send real JavaScript booleans, only strings\n * - Checkboxes commonly send \"true\"/\"false\" strings\n * - React Hook Form uses this convention\n * - This is the expected behavior in FormData context\n *\n * WHY WE PARSE DATES:\n * - Date objects are serialized to ISO strings via toISOString() on the client\n * - We need to reconstruct them as Date objects for Zod z.date() validation\n */\nfunction tryParseJSON(value: unknown): unknown {\n // Non-strings pass through unchanged\n if (typeof value !== 'string') return value;\n\n const trimmed = value.trim();\n\n // Empty strings become null (useful for nullable date fields)\n if (!trimmed) return null;\n\n // Parse boolean strings (common for checkboxes and React Hook Form)\n // This is safe in FormData context since HTML can't send real booleans\n if (trimmed === 'true') return true;\n if (trimmed === 'false') return false;\n\n // Parse null (less common but sometimes used)\n if (trimmed === 'null') return null;\n\n // Parse BigInt strings (from client-side serialization)\n const bigintMatch = trimmed.match(BIGINT_REGEX);\n if (bigintMatch) {\n try {\n return BigInt(bigintMatch[1]);\n } catch {\n // Invalid BigInt - fall through to return original string\n }\n }\n\n // Parse ISO 8601 date strings to Date objects\n if (ISO_DATE_REGEX.test(trimmed)) {\n const date = new Date(trimmed);\n // Only return Date if parsing was successful (not Invalid Date)\n if (!isNaN(date.getTime())) {\n return date;\n }\n }\n\n // Only attempt parsing for obvious JSON structures\n if ((trimmed.startsWith('{') && trimmed.endsWith('}')) ||\n (trimmed.startsWith('[') && trimmed.endsWith(']'))) {\n try {\n const parsed = JSON.parse(trimmed);\n // Extra safety: verify we got an object/array\n if (typeof parsed === 'object' && parsed !== null) {\n // Restore special types (Map, Set)\n return restoreSpecialTypes(parsed);\n }\n } catch {\n // Malformed JSON - return original string\n }\n }\n\n // All other strings (including number strings like \"123\") remain as strings\n // This avoids ambiguity with actual string values that happen to be numeric\n return value;\n}\n\n// Form data parser with automatic JSON deserialization\nfunction parseFormData(formData: FormData): Record<string, any> {\n const result: Record<string, any> = {};\n const keys = new Set<string>();\n\n for (const [key] of formData.entries()) {\n keys.add(key);\n }\n\n for (const key of keys) {\n const values = formData.getAll(key);\n\n if (key.endsWith(\"[]\")) {\n const cleanKey = key.slice(0, -2);\n // Parse each array item (handles nested objects in arrays)\n result[cleanKey] = values.map(tryParseJSON);\n } else if (values.length === 1) {\n // Parse single value (handles JSON stringified objects)\n result[key] = tryParseJSON(values[0]);\n } else {\n // Parse multiple values\n result[key] = values.map(tryParseJSON);\n }\n }\n\n return result;\n}\n\n// Internal function to create form server action\nfunction createFormServerAction<TOutput>(config: {\n inputSchema?: z.ZodTypeAny;\n outputSchema?: z.ZodTypeAny;\n middlewareFns: ServerActionMiddleware<any>[];\n onError?: (error: unknown, context: { parsedInput?: unknown }) => ServerActionError | undefined;\n serverCodeFn: (...args: any[]) => Promise<any>;\n redirectConfig?: string | RedirectConfig | ((result: any) => string | RedirectConfig | undefined);\n}): (prev: ServerActionResponse<TOutput>, formData: FormData) => Promise<ServerActionResponse<TOutput>> {\n return async (_prev: ServerActionResponse<TOutput>, formData: FormData): Promise<ServerActionResponse<TOutput>> => {\n const context: { parsedInput?: unknown } = {};\n const hasInputSchema = config.inputSchema !== undefined && config.inputSchema !== null;\n\n try {\n let parsedInput: any = parseFormData(formData);\n\n // Input validation\n if (config.inputSchema) {\n const parseResult = z.safeParse(config.inputSchema, parsedInput);\n if (!parseResult.success) {\n return handleServerActionError(parseResult.error, config.onError ? (err) => config.onError!(err, { parsedInput }) : undefined);\n }\n parsedInput = parseResult.data;\n context.parsedInput = parsedInput;\n }\n\n // Execute middleware chain\n let middlewareContext = {};\n for (const middleware of config.middlewareFns) {\n const middlewareInput = hasInputSchema ? parsedInput : undefined;\n const result = await middleware({ context: middlewareContext, input: middlewareInput });\n if (\"error\" in result) {\n return {\n success: false,\n error: result.error,\n };\n }\n middlewareContext = { ...middlewareContext, ...result.context };\n }\n\n // Execute the server code\n const result = hasInputSchema \n ? await config.serverCodeFn(parsedInput, middlewareContext)\n : await config.serverCodeFn(middlewareContext);\n\n // Output validation\n let validatedOutput: any = result;\n if (config.outputSchema) {\n const parseResult = z.safeParse(config.outputSchema, result);\n if (!parseResult.success) {\n return handleServerActionError(parseResult.error);\n }\n validatedOutput = parseResult.data;\n }\n\n // Handle redirect config if present\n let redirectValue: string | RedirectConfig | undefined;\n if (config.redirectConfig) {\n if (typeof config.redirectConfig === 'function') {\n redirectValue = config.redirectConfig(validatedOutput);\n } else {\n redirectValue = config.redirectConfig;\n }\n }\n\n return {\n success: true,\n data: validatedOutput,\n ...(redirectValue && { redirect: redirectValue }),\n };\n } catch (error) {\n // Preserve Next.js navigation errors (redirect, notFound, etc.)\n if (isNextNavigationError(error)) {\n throw error;\n }\n // Debug logging only in development\n if (process.env.NODE_ENV === 'development') {\n console.log(\"[ServerActionClient] Error:\", error?.constructor?.name, error instanceof Error ? error.message : error);\n }\n const errorHandler = config.onError ? (err: unknown) => {\n return config.onError!(err, { parsedInput: context.parsedInput });\n } : undefined;\n return handleServerActionError(error, errorHandler);\n }\n };\n}\n\n// Export factory function for creating a new client\nexport function createActionClient() {\n return new ServerActionClient();\n}\n","// Comes from: https://github.com/vercel/next.js/blob/canary/packages/next/src/client/components/redirect-error.ts\n\nenum RedirectStatusCode {\n\tSeeOther = 303,\n\tTemporaryRedirect = 307,\n\tPermanentRedirect = 308,\n}\n\nconst REDIRECT_ERROR_CODE = \"NEXT_REDIRECT\";\n\nenum RedirectType {\n\tpush = \"push\",\n\treplace = \"replace\",\n}\n\nexport type RedirectError = Error & {\n\tdigest: `${typeof REDIRECT_ERROR_CODE};${RedirectType};${string};${RedirectStatusCode};`;\n};\n\n/**\n * Checks an error to determine if it's an error generated by the\n * `redirect(url)` helper.\n *\n * @param error the error that may reference a redirect error\n * @returns true if the error is a redirect error\n */\nexport function isRedirectError(error: unknown): error is RedirectError {\n\tif (typeof error !== \"object\" || error === null || !(\"digest\" in error) || typeof error.digest !== \"string\") {\n\t\treturn false;\n\t}\n\n\tconst digest = error.digest.split(\";\");\n\tconst [errorCode, type] = digest;\n\tconst destination = digest.slice(2, -2).join(\";\");\n\tconst status = digest.at(-2);\n\n\tconst statusCode = Number(status);\n\n\treturn (\n\t\terrorCode === REDIRECT_ERROR_CODE &&\n\t\t(type === \"replace\" || type === \"push\") &&\n\t\ttypeof destination === \"string\" &&\n\t\t!isNaN(statusCode) &&\n\t\tstatusCode in RedirectStatusCode\n\t);\n}","// Comes from https://github.com/vercel/next.js/blob/canary/packages/next/src/client/components/http-access-fallback/http-access-fallback.ts\n\nconst HTTPAccessErrorStatus = {\n\tNOT_FOUND: 404,\n\tFORBIDDEN: 403,\n\tUNAUTHORIZED: 401,\n};\n\nconst ALLOWED_CODES = new Set(Object.values(HTTPAccessErrorStatus));\n\nconst HTTP_ERROR_FALLBACK_ERROR_CODE = \"NEXT_HTTP_ERROR_FALLBACK\";\n\nexport type HTTPAccessFallbackError = Error & {\n\tdigest: `${typeof HTTP_ERROR_FALLBACK_ERROR_CODE};${string}`;\n};\n\n/**\n * Checks an error to determine if it's an error generated by\n * the HTTP navigation APIs `notFound()`, `forbidden()` or `unauthorized()`.\n *\n * @param error the error that may reference a HTTP access error\n * @returns true if the error is a HTTP access error\n */\nexport function isHTTPAccessFallbackError(error: unknown): error is HTTPAccessFallbackError {\n\tif (typeof error !== \"object\" || error === null || !(\"digest\" in error) || typeof error.digest !== \"string\") {\n\t\treturn false;\n\t}\n\tconst [prefix, httpStatus] = error.digest.split(\";\");\n\n\treturn prefix === HTTP_ERROR_FALLBACK_ERROR_CODE && ALLOWED_CODES.has(Number(httpStatus));\n}\n\nexport function getAccessFallbackHTTPStatus(error: HTTPAccessFallbackError): number {\n\tconst httpStatus = error.digest.split(\";\")[1];\n\treturn Number(httpStatus);\n}","import { isRedirectError } from \"./redirect\";\nimport { isHTTPAccessFallbackError, getAccessFallbackHTTPStatus } from \"./http-access-fallback\";\n\nexport { isRedirectError } from \"./redirect\";\nexport type { RedirectError } from \"./redirect\";\n\nexport { isHTTPAccessFallbackError, getAccessFallbackHTTPStatus } from \"./http-access-fallback\";\nexport type { HTTPAccessFallbackError } from \"./http-access-fallback\";\n\n/**\n * Checks if the error is a navigation error that should be re-thrown\n * This includes redirect errors and HTTP access errors (notFound, forbidden, unauthorized)\n */\nexport function isNextNavigationError(error: unknown): boolean {\n\treturn isRedirectError(error) || isHTTPAccessFallbackError(error);\n}\n\n/**\n * Checks if the error is a notFound error\n * Note: Next.js implements notFound() using HTTP_ERROR_FALLBACK with status 404,\n * not as a separate error type like NEXT_REDIRECT\n */\nexport function isNotFoundError(error: unknown): boolean {\n\treturn isHTTPAccessFallbackError(error) && getAccessFallbackHTTPStatus(error as any) === 404;\n}","import type { ServerActionError } from '../types';\n\n/**\n * Type definition for Server Action Middleware\n *\n * A middleware can either:\n * - Return an updated context that gets merged with the existing context\n * - Return an error that stops the action execution\n */\nexport type ServerActionMiddleware<Context, Input = unknown> = (params: {\n context: Context;\n input: Input;\n}) => Promise<{ context: Context } | { error: ServerActionError }>;\n\n/**\n * Helper for creating middleware with correct type inference for the output context.\n *\n * This helper solves the TypeScript type inference issue where factory functions\n * that return middleware lose the context type information. By wrapping your\n * middleware with this helper, you explicitly declare what context type the\n * middleware will output, enabling proper type inference through the `.use()` chain.\n *\n * @template TOutputContext - The type of context this middleware will output\n * @template TInput - The type of input data (defaults to unknown)\n *\n * @param middlewareFn - The middleware function that transforms context\n * @returns A typed middleware function with proper type inference\n *\n * @example\n * ```typescript\n * interface AuthContext {\n * userId: string;\n * role: string;\n * }\n *\n * const createAuthMiddleware = () =>\n * createTypedMiddleware<AuthContext>(async ({ context, input }) => {\n * const session = await getSession();\n *\n * if (!session) {\n * return { error: { message: 'Unauthorized', code: 'AUTH_ERROR' } };\n * }\n *\n * return {\n * context: {\n * userId: session.userId,\n * role: session.role,\n * },\n * };\n * });\n *\n * // Now TypeScript correctly infers authClient has AuthContext\n * const authClient = baseClient.use(createAuthMiddleware());\n *\n * // And in your actions, context is properly typed\n * authClient.action(async (context) => {\n * const userId = context.userId; // ✅ Type-safe!\n * });\n * ```\n */\nexport function createTypedMiddleware<TOutputContext extends object, TInput = unknown>(\n middlewareFn: (params: {\n context: any;\n input: TInput;\n }) => Promise<{ context: TOutputContext } | { error: ServerActionError }>\n): ServerActionMiddleware<TOutputContext, TInput> {\n return middlewareFn as any;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,iBAA4B;AAG5B,SAAS,gBAAgB,OAAoC;AAC3D,QAAM,EAAE,aAAa,WAAW,IAAI,aAAE,aAAa,KAAK;AACxD,QAA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c,cAAE,UAAU,OAAO,cAAc,MAAM;AAC3D,YAAI,CAAC,YAAY,SAAS;AACxB,iBAAO,wBAAwB,YAAY,KAAK;AAAA,QAClD;AACA,0BAAkB,YAAY;AAAA,MAChC;AAGA,UAAI;AACJ,UAAI,OAAO,gBAAgB;AACzB,YAAI,OAAO,OAAO,mBAAmB,YAAY;AAC/C,0BAAgB,OAAO,eAAe,eAAe;AAAA,QACvD,OAAO;AACL,0BAAgB,OAAO;AAAA,QACzB;AAAA,MACF;AAEA,aAAO;AAAA,QACL,SAAS;AAAA,QACT,MAAM;AAAA,QACN,GAAI,iBAAiB,EAAE,UAAU,cAAc;AAAA,MACjD;AAAA,IACF,SAAS,OAAO;AAEd,UAAI,sBAAsB,KAAK,GAAG;AAChC,cAAM;AAAA,MACR;AAEA,UAAI,QAAQ,IAAI,aAAa,eAAe;AAC1C,gBAAQ,IAAI,+BAA+B,OAAO,aAAa,MAAM,iBAAiB,QAAQ,MAAM,UAAU,KAAK;AAAA,MACrH;AACA,YAAM,eAAe,OAAO,UAAU,CAAC,QAAiB;AACtD,eAAO,OAAO,QAAS,KAAK,EAAE,aAAa,QAAQ,YAAY,CAAC;AAAA,MAClE,IAAI;AACJ,aAAO,wBAAwB,OAAO,YAAY;AAAA,IACpD;AAAA,EACF;AACF;AAGO,SAAS,qBAAqB;AACnC,SAAO,IAAI,mBAAmB;AAChC;;;AIraO,SAAS,sBACd,cAIgD;AAChD,SAAO;AACT;","names":["import_zod","RedirectStatusCode","result"]}
@@ -70,6 +70,9 @@ function isErrorResponse(response) {
70
70
  return !response.success;
71
71
  }
72
72
 
73
+ // src/core/server-action-client.ts
74
+ import { z as z2 } from "zod";
75
+
73
76
  // src/next/errors/redirect.ts
74
77
  var RedirectStatusCode = /* @__PURE__ */ ((RedirectStatusCode2) => {
75
78
  RedirectStatusCode2[RedirectStatusCode2["SeeOther"] = 303] = "SeeOther";
@@ -210,7 +213,7 @@ function createServerAction(config) {
210
213
  try {
211
214
  let parsedInput = void 0;
212
215
  if (hasInputSchema && config.inputSchema) {
213
- const parseResult = config.inputSchema.safeParse(input);
216
+ const parseResult = z2.safeParse(config.inputSchema, input);
214
217
  if (!parseResult.success) {
215
218
  return handleServerActionError(parseResult.error, config.onError ? (err) => config.onError(err, { parsedInput: input }) : void 0);
216
219
  }
@@ -232,7 +235,7 @@ function createServerAction(config) {
232
235
  const result = hasInputSchema ? await config.serverCodeFn(parsedInput, middlewareContext) : await config.serverCodeFn(middlewareContext);
233
236
  let validatedOutput = result;
234
237
  if (config.outputSchema) {
235
- const parseResult = config.outputSchema.safeParse(result);
238
+ const parseResult = z2.safeParse(config.outputSchema, result);
236
239
  if (!parseResult.success) {
237
240
  return handleServerActionError(parseResult.error);
238
241
  }
@@ -267,18 +270,49 @@ function createServerAction(config) {
267
270
  actionFn.__hasInputSchema = config.inputSchema !== void 0 && config.inputSchema !== null;
268
271
  return actionFn;
269
272
  }
273
+ var ISO_DATE_REGEX = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d{1,3})?(?:Z|[+-]\d{2}:?\d{2})$/;
274
+ var BIGINT_REGEX = /^__bigint__(-?\d+)$/;
275
+ function restoreSpecialTypes(parsed) {
276
+ if (typeof parsed !== "object" || parsed === null) return parsed;
277
+ if ("__type__" in parsed && parsed.__type__ === "Map") {
278
+ const entries = parsed.entries;
279
+ if (Array.isArray(entries)) {
280
+ return new Map(entries);
281
+ }
282
+ }
283
+ if ("__type__" in parsed && parsed.__type__ === "Set") {
284
+ const values = parsed.values;
285
+ if (Array.isArray(values)) {
286
+ return new Set(values);
287
+ }
288
+ }
289
+ return parsed;
290
+ }
270
291
  function tryParseJSON(value) {
271
292
  if (typeof value !== "string") return value;
272
293
  const trimmed = value.trim();
273
- if (!trimmed) return value;
294
+ if (!trimmed) return null;
274
295
  if (trimmed === "true") return true;
275
296
  if (trimmed === "false") return false;
276
297
  if (trimmed === "null") return null;
298
+ const bigintMatch = trimmed.match(BIGINT_REGEX);
299
+ if (bigintMatch) {
300
+ try {
301
+ return BigInt(bigintMatch[1]);
302
+ } catch {
303
+ }
304
+ }
305
+ if (ISO_DATE_REGEX.test(trimmed)) {
306
+ const date = new Date(trimmed);
307
+ if (!isNaN(date.getTime())) {
308
+ return date;
309
+ }
310
+ }
277
311
  if (trimmed.startsWith("{") && trimmed.endsWith("}") || trimmed.startsWith("[") && trimmed.endsWith("]")) {
278
312
  try {
279
313
  const parsed = JSON.parse(trimmed);
280
314
  if (typeof parsed === "object" && parsed !== null) {
281
- return parsed;
315
+ return restoreSpecialTypes(parsed);
282
316
  }
283
317
  } catch {
284
318
  }
@@ -311,7 +345,7 @@ function createFormServerAction(config) {
311
345
  try {
312
346
  let parsedInput = parseFormData(formData);
313
347
  if (config.inputSchema) {
314
- const parseResult = config.inputSchema.safeParse(parsedInput);
348
+ const parseResult = z2.safeParse(config.inputSchema, parsedInput);
315
349
  if (!parseResult.success) {
316
350
  return handleServerActionError(parseResult.error, config.onError ? (err) => config.onError(err, { parsedInput }) : void 0);
317
351
  }
@@ -333,7 +367,7 @@ function createFormServerAction(config) {
333
367
  const result = hasInputSchema ? await config.serverCodeFn(parsedInput, middlewareContext) : await config.serverCodeFn(middlewareContext);
334
368
  let validatedOutput = result;
335
369
  if (config.outputSchema) {
336
- const parseResult = config.outputSchema.safeParse(result);
370
+ const parseResult = z2.safeParse(config.outputSchema, result);
337
371
  if (!parseResult.success) {
338
372
  return handleServerActionError(parseResult.error);
339
373
  }
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/core/error-handler.ts","../../src/next/errors/redirect.ts","../../src/next/errors/http-access-fallback.ts","../../src/next/errors/index.ts","../../src/core/server-action-client.ts","../../src/core/middleware-helpers.ts"],"sourcesContent":["import { z, ZodError } from \"zod\";\nimport type { ServerActionError, ServerActionResponse } from \"../types\";\n\nfunction convertZodError(error: ZodError): ServerActionError {\n const { fieldErrors, formErrors } = z.flattenError(error);\n const [field] = Object.keys(fieldErrors) as (keyof typeof fieldErrors)[];\n const message = (field && fieldErrors[field]?.[0]) ?? formErrors[0] ?? \"Validation failed\";\n return {\n message,\n code: \"VALIDATION_ERROR\",\n field,\n fields: fieldErrors,\n };\n}\n\nfunction convertGenericError(error: unknown): ServerActionError {\n const message = error instanceof Error ? error.message : \"An unexpected error occurred\";\n\n return {\n message: process.env.NODE_ENV === \"production\" ? \"An unexpected error occurred\" : message,\n code: \"INTERNAL_ERROR\",\n statusCode: 500,\n };\n}\n\nexport function handleServerActionError(\n error: unknown,\n customHandler?: (error: unknown) => ServerActionError | undefined\n): { success: false; error: ServerActionError } {\n // Debug logging only in development\n if (process.env.NODE_ENV === 'development') {\n console.error(\"[Server Action Error]\", error?.constructor?.name || 'Unknown', error instanceof Error ? error.message : error);\n }\n\n // Try custom handler first\n if (customHandler) {\n try {\n const customError = customHandler(error);\n if (customError) {\n return {\n success: false,\n error: customError,\n };\n }\n } catch (handlerError) {\n console.error(\"[Server Action Error] Custom handler threw an error:\", handlerError);\n }\n }\n\n if (error instanceof ZodError) {\n return {\n success: false,\n error: convertZodError(error),\n };\n }\n\n if (error instanceof Error && \"toServerActionError\" in error && typeof error.toServerActionError === \"function\") {\n const serverError = error.toServerActionError() as ServerActionError;\n \n // Check if this is an auth error based on the error type/code\n if (serverError.code === \"AUTHENTICATION_ERROR\" || (error as any).type === \"AUTHENTICATION_ERROR\") {\n serverError.shouldRedirect = true;\n serverError.redirectTo = \"/login\";\n }\n \n return {\n success: false,\n error: serverError,\n };\n }\n\n return {\n success: false,\n error: convertGenericError(error),\n };\n}\n\nexport function isErrorResponse<T>(response: ServerActionResponse<T>): response is { success: false; error: ServerActionError } {\n return !response.success;\n}","// Comes from: https://github.com/vercel/next.js/blob/canary/packages/next/src/client/components/redirect-error.ts\n\nenum RedirectStatusCode {\n\tSeeOther = 303,\n\tTemporaryRedirect = 307,\n\tPermanentRedirect = 308,\n}\n\nconst REDIRECT_ERROR_CODE = \"NEXT_REDIRECT\";\n\nenum RedirectType {\n\tpush = \"push\",\n\treplace = \"replace\",\n}\n\nexport type RedirectError = Error & {\n\tdigest: `${typeof REDIRECT_ERROR_CODE};${RedirectType};${string};${RedirectStatusCode};`;\n};\n\n/**\n * Checks an error to determine if it's an error generated by the\n * `redirect(url)` helper.\n *\n * @param error the error that may reference a redirect error\n * @returns true if the error is a redirect error\n */\nexport function isRedirectError(error: unknown): error is RedirectError {\n\tif (typeof error !== \"object\" || error === null || !(\"digest\" in error) || typeof error.digest !== \"string\") {\n\t\treturn false;\n\t}\n\n\tconst digest = error.digest.split(\";\");\n\tconst [errorCode, type] = digest;\n\tconst destination = digest.slice(2, -2).join(\";\");\n\tconst status = digest.at(-2);\n\n\tconst statusCode = Number(status);\n\n\treturn (\n\t\terrorCode === REDIRECT_ERROR_CODE &&\n\t\t(type === \"replace\" || type === \"push\") &&\n\t\ttypeof destination === \"string\" &&\n\t\t!isNaN(statusCode) &&\n\t\tstatusCode in RedirectStatusCode\n\t);\n}","// Comes from https://github.com/vercel/next.js/blob/canary/packages/next/src/client/components/http-access-fallback/http-access-fallback.ts\n\nconst HTTPAccessErrorStatus = {\n\tNOT_FOUND: 404,\n\tFORBIDDEN: 403,\n\tUNAUTHORIZED: 401,\n};\n\nconst ALLOWED_CODES = new Set(Object.values(HTTPAccessErrorStatus));\n\nconst HTTP_ERROR_FALLBACK_ERROR_CODE = \"NEXT_HTTP_ERROR_FALLBACK\";\n\nexport type HTTPAccessFallbackError = Error & {\n\tdigest: `${typeof HTTP_ERROR_FALLBACK_ERROR_CODE};${string}`;\n};\n\n/**\n * Checks an error to determine if it's an error generated by\n * the HTTP navigation APIs `notFound()`, `forbidden()` or `unauthorized()`.\n *\n * @param error the error that may reference a HTTP access error\n * @returns true if the error is a HTTP access error\n */\nexport function isHTTPAccessFallbackError(error: unknown): error is HTTPAccessFallbackError {\n\tif (typeof error !== \"object\" || error === null || !(\"digest\" in error) || typeof error.digest !== \"string\") {\n\t\treturn false;\n\t}\n\tconst [prefix, httpStatus] = error.digest.split(\";\");\n\n\treturn prefix === HTTP_ERROR_FALLBACK_ERROR_CODE && ALLOWED_CODES.has(Number(httpStatus));\n}\n\nexport function getAccessFallbackHTTPStatus(error: HTTPAccessFallbackError): number {\n\tconst httpStatus = error.digest.split(\";\")[1];\n\treturn Number(httpStatus);\n}","import { isRedirectError } from \"./redirect\";\nimport { isHTTPAccessFallbackError, getAccessFallbackHTTPStatus } from \"./http-access-fallback\";\n\nexport { isRedirectError } from \"./redirect\";\nexport type { RedirectError } from \"./redirect\";\n\nexport { isHTTPAccessFallbackError, getAccessFallbackHTTPStatus } from \"./http-access-fallback\";\nexport type { HTTPAccessFallbackError } from \"./http-access-fallback\";\n\n/**\n * Checks if the error is a navigation error that should be re-thrown\n * This includes redirect errors and HTTP access errors (notFound, forbidden, unauthorized)\n */\nexport function isNextNavigationError(error: unknown): boolean {\n\treturn isRedirectError(error) || isHTTPAccessFallbackError(error);\n}\n\n/**\n * Checks if the error is a notFound error\n * Note: Next.js implements notFound() using HTTP_ERROR_FALLBACK with status 404,\n * not as a separate error type like NEXT_REDIRECT\n */\nexport function isNotFoundError(error: unknown): boolean {\n\treturn isHTTPAccessFallbackError(error) && getAccessFallbackHTTPStatus(error as any) === 404;\n}","import { z } from \"zod\";\nimport type { ServerActionError, ServerActionResponse, ServerAction } from \"../types\";\nimport { handleServerActionError } from \"./error-handler\";\nimport { isNextNavigationError } from \"../next/errors\";\n\ntype InferZodOutput<T> = T extends z.ZodTypeAny ? z.output<T> : never;\ntype InferZodInput<T> = T extends z.ZodTypeAny ? z.input<T> : never;\n\ntype ServerActionMiddleware<Context, Input = unknown> = (params: { context: Context; input: Input }) => Promise<{ context: Context } | { error: ServerActionError }>;\n\nimport type { RedirectConfig } from \"../types\";\n\ntype SafeActionClientArgs<Context extends object, InputSchema extends z.ZodTypeAny | undefined, OutputSchema extends z.ZodTypeAny | undefined> = {\n middlewareFns: ServerActionMiddleware<any, any>[];\n inputSchema?: InputSchema;\n outputSchema?: OutputSchema;\n onError?: (error: unknown, context: { parsedInput?: unknown }) => ServerActionError | undefined;\n ctxType?: Context;\n redirectConfig?: string | RedirectConfig | ((result: any) => string | RedirectConfig | undefined);\n};\n\nexport class ServerActionClient<Context extends object = {}, InputSchema extends z.ZodTypeAny | undefined = undefined, OutputSchema extends z.ZodTypeAny | undefined = undefined> {\n readonly #args: SafeActionClientArgs<Context, InputSchema, OutputSchema>;\n\n constructor(\n args: SafeActionClientArgs<Context, InputSchema, OutputSchema> = {\n middlewareFns: [],\n }\n ) {\n this.#args = args;\n }\n\n /**\n * Use a middleware function.\n * @param middlewareFn Middleware function\n */\n use<NextContext extends object>(\n middlewareFn: ServerActionMiddleware<\n Context & NextContext, \n InputSchema extends z.ZodTypeAny ? z.output<InputSchema> : undefined\n >\n ) {\n return new ServerActionClient<Context & NextContext, InputSchema, OutputSchema>({\n ...this.#args,\n middlewareFns: [...this.#args.middlewareFns, middlewareFn] as any,\n ctxType: {} as Context & NextContext,\n });\n }\n\n /**\n * Define the input validation schema for the action.\n * @param schema Input validation schema\n */\n inputSchema<IS extends z.ZodTypeAny>(schema: IS) {\n return new ServerActionClient<Context, IS, OutputSchema>({\n ...this.#args,\n inputSchema: schema,\n });\n }\n\n /**\n * Define the output validation schema for the action.\n * @param schema Output validation schema\n */\n outputSchema<OS extends z.ZodTypeAny>(schema: OS) {\n return new ServerActionClient<Context, InputSchema, OS>({\n ...this.#args,\n outputSchema: schema,\n });\n }\n\n /**\n * Define a custom error handler for the action.\n * @param handler Error handler function\n */\n onError(handler: (error: unknown, context: { parsedInput?: unknown }) => ServerActionError | undefined) {\n return new ServerActionClient<Context, InputSchema, OutputSchema>({\n ...this.#args,\n onError: handler,\n });\n }\n\n /**\n * Define a redirect configuration for successful actions.\n * @param config Redirect URL, config object, or function that returns redirect config based on result\n */\n redirect<Data = any>(\n config: string | RedirectConfig | ((result: Data) => string | RedirectConfig | undefined)\n ) {\n return new ServerActionClient<Context, InputSchema, OutputSchema>({\n ...this.#args,\n redirectConfig: config,\n });\n }\n\n\n /**\n * Define the action.\n * @param serverCodeFn Code that will be executed on the server side\n */\n action<Data>(\n serverCodeFn: InputSchema extends undefined\n ? (context: Context) => Promise<Data>\n : InputSchema extends z.ZodTypeAny \n ? (input: InferZodOutput<InputSchema>, context: Context) => Promise<Data>\n : (context: Context) => Promise<Data>\n ): ServerAction<\n InputSchema extends undefined \n ? void \n : InputSchema extends z.ZodTypeAny \n ? InferZodInput<InputSchema> \n : void, \n OutputSchema extends z.ZodTypeAny ? InferZodOutput<OutputSchema> : Data\n > {\n return createServerAction<\n InputSchema extends undefined \n ? void \n : InputSchema extends z.ZodTypeAny \n ? InferZodInput<InputSchema> \n : void,\n OutputSchema extends z.ZodTypeAny ? InferZodOutput<OutputSchema> : Data\n >({\n inputSchema: this.#args.inputSchema,\n outputSchema: this.#args.outputSchema,\n middlewareFns: this.#args.middlewareFns,\n onError: this.#args.onError,\n serverCodeFn: serverCodeFn as any,\n redirectConfig: this.#args.redirectConfig,\n });\n }\n\n /**\n * Define a form action that accepts FormData.\n * @param serverCodeFn Code that will be executed on the server side\n */\n formAction<Data>(\n serverCodeFn: InputSchema extends undefined\n ? (context: Context) => Promise<Data>\n : InputSchema extends z.ZodTypeAny \n ? (input: InferZodOutput<InputSchema>, context: Context) => Promise<Data>\n : (context: Context) => Promise<Data>\n ): (prev: ServerActionResponse<OutputSchema extends z.ZodTypeAny ? InferZodOutput<OutputSchema> : Data>, formData: FormData) => Promise<ServerActionResponse<OutputSchema extends z.ZodTypeAny ? InferZodOutput<OutputSchema> : Data>> {\n return createFormServerAction({\n inputSchema: this.#args.inputSchema,\n outputSchema: this.#args.outputSchema,\n middlewareFns: this.#args.middlewareFns,\n onError: this.#args.onError,\n serverCodeFn,\n redirectConfig: this.#args.redirectConfig,\n });\n }\n}\n\n// Internal function to create the actual server action\nfunction createServerAction<TInput, TOutput>(config: {\n inputSchema?: z.ZodTypeAny;\n outputSchema?: z.ZodTypeAny;\n middlewareFns: ServerActionMiddleware<any, any>[];\n onError?: (error: unknown, context: { parsedInput?: unknown }) => ServerActionError | undefined;\n serverCodeFn: (...args: any[]) => Promise<any>;\n redirectConfig?: string | RedirectConfig | ((result: any) => string | RedirectConfig | undefined);\n}): ServerAction<TInput, TOutput> {\n const actionFn = async (...args: TInput extends void ? [] : [input: TInput]): Promise<ServerActionResponse<TOutput>> => {\n const context: { parsedInput?: unknown } = {};\n \n // Type guard: check if we have an input schema\n const hasInputSchema = config.inputSchema !== undefined && config.inputSchema !== null;\n const input = hasInputSchema ? args[0] as TInput : undefined;\n\n try {\n let parsedInput: any = undefined;\n\n // Input validation\n if (hasInputSchema && config.inputSchema) {\n const parseResult = config.inputSchema.safeParse(input);\n if (!parseResult.success) {\n return handleServerActionError(parseResult.error, config.onError ? (err) => config.onError!(err, { parsedInput: input }) : undefined);\n }\n parsedInput = parseResult.data;\n context.parsedInput = parsedInput;\n }\n\n // Execute middleware chain\n let middlewareContext = {};\n for (const middleware of config.middlewareFns) {\n const middlewareInput = hasInputSchema ? parsedInput : undefined;\n const result = await middleware({ context: middlewareContext, input: middlewareInput });\n if (\"error\" in result) {\n return {\n success: false,\n error: result.error,\n };\n }\n middlewareContext = { ...middlewareContext, ...result.context };\n }\n\n // Execute the server code\n const result = hasInputSchema \n ? await config.serverCodeFn(parsedInput, middlewareContext)\n : await config.serverCodeFn(middlewareContext);\n\n // Output validation\n let validatedOutput: any = result;\n if (config.outputSchema) {\n const parseResult = config.outputSchema.safeParse(result);\n if (!parseResult.success) {\n return handleServerActionError(parseResult.error);\n }\n validatedOutput = parseResult.data;\n }\n\n // Handle redirect config if present\n let redirectValue: string | RedirectConfig | undefined;\n if (config.redirectConfig) {\n if (typeof config.redirectConfig === 'function') {\n redirectValue = config.redirectConfig(validatedOutput);\n } else {\n redirectValue = config.redirectConfig;\n }\n }\n\n return {\n success: true,\n data: validatedOutput,\n ...(redirectValue && { redirect: redirectValue }),\n };\n } catch (error) {\n // Preserve Next.js navigation errors (redirect, notFound, etc.)\n if (isNextNavigationError(error)) {\n throw error;\n }\n \n // Debug logging only in development\n if (process.env.NODE_ENV === 'development') {\n console.log(\"[executeAction] Error:\", error?.constructor?.name, error instanceof Error ? error.message : error);\n }\n \n // Use the handleServerActionError function with custom handler\n const errorHandler = config.onError ? (err: unknown) => {\n return config.onError!(err, { parsedInput: context.parsedInput });\n } : undefined;\n \n return handleServerActionError(error, errorHandler);\n }\n };\n\n // Add metadata for runtime type checking\n (actionFn as any).__hasInputSchema = config.inputSchema !== undefined && config.inputSchema !== null;\n \n // Return the function with correct typing based on whether input is void\n return actionFn as ServerAction<TInput, TOutput>;\n}\n\n/**\n * Safely parses JSON strings back to objects/arrays and handles FormData boolean conventions.\n * \n * DESIGN PRINCIPLES:\n * - Parse objects and arrays (start with { or [)\n * - Parse \"true\"/\"false\" to booleans (FormData convention for checkboxes)\n * - DO NOT parse number strings (\"123\") to avoid ambiguity\n * - Fail gracefully on malformed JSON\n * - Maintain perfect symmetry with client-side objectToFormData\n * \n * WHY WE PARSE BOOLEANS:\n * - HTML forms cannot send real JavaScript booleans, only strings\n * - Checkboxes commonly send \"true\"/\"false\" strings\n * - React Hook Form uses this convention\n * - This is the expected behavior in FormData context\n */\nfunction tryParseJSON(value: unknown): unknown {\n // Non-strings pass through unchanged\n if (typeof value !== 'string') return value;\n \n const trimmed = value.trim();\n \n // Empty strings remain empty\n if (!trimmed) return value;\n \n // Parse boolean strings (common for checkboxes and React Hook Form)\n // This is safe in FormData context since HTML can't send real booleans\n if (trimmed === 'true') return true;\n if (trimmed === 'false') return false;\n \n // Parse null (less common but sometimes used)\n if (trimmed === 'null') return null;\n \n // Only attempt parsing for obvious JSON structures\n if ((trimmed.startsWith('{') && trimmed.endsWith('}')) ||\n (trimmed.startsWith('[') && trimmed.endsWith(']'))) {\n try {\n const parsed = JSON.parse(trimmed);\n // Extra safety: verify we got an object/array\n if (typeof parsed === 'object' && parsed !== null) {\n return parsed;\n }\n } catch {\n // Malformed JSON - return original string\n }\n }\n \n // All other strings (including number strings like \"123\") remain as strings\n // This avoids ambiguity with actual string values that happen to be numeric\n return value;\n}\n\n// Form data parser with automatic JSON deserialization\nfunction parseFormData(formData: FormData): Record<string, any> {\n const result: Record<string, any> = {};\n const keys = new Set<string>();\n\n for (const [key] of formData.entries()) {\n keys.add(key);\n }\n\n for (const key of keys) {\n const values = formData.getAll(key);\n\n if (key.endsWith(\"[]\")) {\n const cleanKey = key.slice(0, -2);\n // Parse each array item (handles nested objects in arrays)\n result[cleanKey] = values.map(tryParseJSON);\n } else if (values.length === 1) {\n // Parse single value (handles JSON stringified objects)\n result[key] = tryParseJSON(values[0]);\n } else {\n // Parse multiple values\n result[key] = values.map(tryParseJSON);\n }\n }\n\n return result;\n}\n\n// Internal function to create form server action\nfunction createFormServerAction<TOutput>(config: {\n inputSchema?: z.ZodTypeAny;\n outputSchema?: z.ZodTypeAny;\n middlewareFns: ServerActionMiddleware<any>[];\n onError?: (error: unknown, context: { parsedInput?: unknown }) => ServerActionError | undefined;\n serverCodeFn: (...args: any[]) => Promise<any>;\n redirectConfig?: string | RedirectConfig | ((result: any) => string | RedirectConfig | undefined);\n}): (prev: ServerActionResponse<TOutput>, formData: FormData) => Promise<ServerActionResponse<TOutput>> {\n return async (_prev: ServerActionResponse<TOutput>, formData: FormData): Promise<ServerActionResponse<TOutput>> => {\n const context: { parsedInput?: unknown } = {};\n const hasInputSchema = config.inputSchema !== undefined && config.inputSchema !== null;\n\n try {\n let parsedInput: any = parseFormData(formData);\n\n // Input validation\n if (config.inputSchema) {\n const parseResult = config.inputSchema.safeParse(parsedInput);\n if (!parseResult.success) {\n return handleServerActionError(parseResult.error, config.onError ? (err) => config.onError!(err, { parsedInput }) : undefined);\n }\n parsedInput = parseResult.data;\n context.parsedInput = parsedInput;\n }\n\n // Execute middleware chain\n let middlewareContext = {};\n for (const middleware of config.middlewareFns) {\n const middlewareInput = hasInputSchema ? parsedInput : undefined;\n const result = await middleware({ context: middlewareContext, input: middlewareInput });\n if (\"error\" in result) {\n return {\n success: false,\n error: result.error,\n };\n }\n middlewareContext = { ...middlewareContext, ...result.context };\n }\n\n // Execute the server code\n const result = hasInputSchema \n ? await config.serverCodeFn(parsedInput, middlewareContext)\n : await config.serverCodeFn(middlewareContext);\n\n // Output validation\n let validatedOutput: any = result;\n if (config.outputSchema) {\n const parseResult = config.outputSchema.safeParse(result);\n if (!parseResult.success) {\n return handleServerActionError(parseResult.error);\n }\n validatedOutput = parseResult.data;\n }\n\n // Handle redirect config if present\n let redirectValue: string | RedirectConfig | undefined;\n if (config.redirectConfig) {\n if (typeof config.redirectConfig === 'function') {\n redirectValue = config.redirectConfig(validatedOutput);\n } else {\n redirectValue = config.redirectConfig;\n }\n }\n\n return {\n success: true,\n data: validatedOutput,\n ...(redirectValue && { redirect: redirectValue }),\n };\n } catch (error) {\n // Preserve Next.js navigation errors (redirect, notFound, etc.)\n if (isNextNavigationError(error)) {\n throw error;\n }\n // Debug logging only in development\n if (process.env.NODE_ENV === 'development') {\n console.log(\"[ServerActionClient] Error:\", error?.constructor?.name, error instanceof Error ? error.message : error);\n }\n const errorHandler = config.onError ? (err: unknown) => {\n return config.onError!(err, { parsedInput: context.parsedInput });\n } : undefined;\n return handleServerActionError(error, errorHandler);\n }\n };\n}\n\n// Export factory function for creating a new client\nexport function createActionClient() {\n return new ServerActionClient();\n}\n","import type { ServerActionError } from '../types';\n\n/**\n * Type definition for Server Action Middleware\n *\n * A middleware can either:\n * - Return an updated context that gets merged with the existing context\n * - Return an error that stops the action execution\n */\nexport type ServerActionMiddleware<Context, Input = unknown> = (params: {\n context: Context;\n input: Input;\n}) => Promise<{ context: Context } | { error: ServerActionError }>;\n\n/**\n * Helper for creating middleware with correct type inference for the output context.\n *\n * This helper solves the TypeScript type inference issue where factory functions\n * that return middleware lose the context type information. By wrapping your\n * middleware with this helper, you explicitly declare what context type the\n * middleware will output, enabling proper type inference through the `.use()` chain.\n *\n * @template TOutputContext - The type of context this middleware will output\n * @template TInput - The type of input data (defaults to unknown)\n *\n * @param middlewareFn - The middleware function that transforms context\n * @returns A typed middleware function with proper type inference\n *\n * @example\n * ```typescript\n * interface AuthContext {\n * userId: string;\n * role: string;\n * }\n *\n * const createAuthMiddleware = () =>\n * createTypedMiddleware<AuthContext>(async ({ context, input }) => {\n * const session = await getSession();\n *\n * if (!session) {\n * return { error: { message: 'Unauthorized', code: 'AUTH_ERROR' } };\n * }\n *\n * return {\n * context: {\n * userId: session.userId,\n * role: session.role,\n * },\n * };\n * });\n *\n * // Now TypeScript correctly infers authClient has AuthContext\n * const authClient = baseClient.use(createAuthMiddleware());\n *\n * // And in your actions, context is properly typed\n * authClient.action(async (context) => {\n * const userId = context.userId; // ✅ Type-safe!\n * });\n * ```\n */\nexport function createTypedMiddleware<TOutputContext extends object, TInput = unknown>(\n middlewareFn: (params: {\n context: any;\n input: TInput;\n }) => Promise<{ context: TOutputContext } | { error: ServerActionError }>\n): ServerActionMiddleware<TOutputContext, TInput> {\n return middlewareFn as any;\n}\n"],"mappings":";;;;;;;;;AAAA,SAAS,GAAG,gBAAgB;AAG5B,SAAS,gBAAgB,OAAoC;AAC3D,QAAM,EAAE,aAAa,WAAW,IAAI,EAAE,aAAa,KAAK;AACxD,QAAM,CAAC,KAAK,IAAI,OAAO,KAAK,WAAW;AACvC,QAAM,WAAW,SAAS,YAAY,KAAK,IAAI,CAAC,MAAM,WAAW,CAAC,KAAK;AACvE,SAAO;AAAA,IACL;AAAA,IACA,MAAM;AAAA,IACN;AAAA,IACA,QAAQ;AAAA,EACV;AACF;AAEA,SAAS,oBAAoB,OAAmC;AAC9D,QAAM,UAAU,iBAAiB,QAAQ,MAAM,UAAU;AAEzD,SAAO;AAAA,IACL,SAAS,QAAQ,IAAI,aAAa,eAAe,iCAAiC;AAAA,IAClF,MAAM;AAAA,IACN,YAAY;AAAA,EACd;AACF;AAEO,SAAS,wBACd,OACA,eAC8C;AAE9C,MAAI,QAAQ,IAAI,aAAa,eAAe;AAC1C,YAAQ,MAAM,yBAAyB,OAAO,aA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wB,YAAY,KAAK;AAAA,QAClD;AACA,0BAAkB,YAAY;AAAA,MAChC;AAGA,UAAI;AACJ,UAAI,OAAO,gBAAgB;AACzB,YAAI,OAAO,OAAO,mBAAmB,YAAY;AAC/C,0BAAgB,OAAO,eAAe,eAAe;AAAA,QACvD,OAAO;AACL,0BAAgB,OAAO;AAAA,QACzB;AAAA,MACF;AAEA,aAAO;AAAA,QACL,SAAS;AAAA,QACT,MAAM;AAAA,QACN,GAAI,iBAAiB,EAAE,UAAU,cAAc;AAAA,MACjD;AAAA,IACF,SAAS,OAAO;AAEd,UAAI,sBAAsB,KAAK,GAAG;AAChC,cAAM;AAAA,MACR;AAEA,UAAI,QAAQ,IAAI,aAAa,eAAe;AAC1C,gBAAQ,IAAI,+BAA+B,OAAO,aAAa,MAAM,iBAAiB,QAAQ,MAAM,UAAU,KAAK;AAAA,MACrH;AACA,YAAM,eAAe,OAAO,UAAU,CAAC,QAAiB;AACtD,eAAO,OAAO,QAAS,KAAK,EAAE,aAAa,QAAQ,YAAY,CAAC;AAAA,MAClE,IAAI;AACJ,aAAO,wBAAwB,OAAO,YAAY;AAAA,IACpD;AAAA,EACF;AACF;AAGO,SAAS,qBAAqB;AACnC,SAAO,IAAI,mBAAmB;AAChC;;;AC3WO,SAAS,sBACd,cAIgD;AAChD,SAAO;AACT;","names":["RedirectStatusCode","result"]}
1
+ {"version":3,"sources":["../../src/core/error-handler.ts","../../src/core/server-action-client.ts","../../src/next/errors/redirect.ts","../../src/next/errors/http-access-fallback.ts","../../src/next/errors/index.ts","../../src/core/middleware-helpers.ts"],"sourcesContent":["import { z, ZodError } from \"zod\";\nimport type { ServerActionError, ServerActionResponse } from \"../types\";\n\nfunction convertZodError(error: ZodError): ServerActionError {\n const { fieldErrors, formErrors } = z.flattenError(error);\n const [field] = Object.keys(fieldErrors) as (keyof typeof fieldErrors)[];\n const message = (field && fieldErrors[field]?.[0]) ?? formErrors[0] ?? \"Validation failed\";\n return {\n message,\n code: \"VALIDATION_ERROR\",\n field,\n fields: fieldErrors,\n };\n}\n\nfunction convertGenericError(error: unknown): ServerActionError {\n const message = error instanceof Error ? error.message : \"An unexpected error occurred\";\n\n return {\n message: process.env.NODE_ENV === \"production\" ? \"An unexpected error occurred\" : message,\n code: \"INTERNAL_ERROR\",\n statusCode: 500,\n };\n}\n\nexport function handleServerActionError(\n error: unknown,\n customHandler?: (error: unknown) => ServerActionError | undefined\n): { success: false; error: ServerActionError } {\n // Debug logging only in development\n if (process.env.NODE_ENV === 'development') {\n console.error(\"[Server Action Error]\", error?.constructor?.name || 'Unknown', error instanceof Error ? error.message : error);\n }\n\n // Try custom handler first\n if (customHandler) {\n try {\n const customError = customHandler(error);\n if (customError) {\n return {\n success: false,\n error: customError,\n };\n }\n } catch (handlerError) {\n console.error(\"[Server Action Error] Custom handler threw an error:\", handlerError);\n }\n }\n\n if (error instanceof ZodError) {\n return {\n success: false,\n error: convertZodError(error),\n };\n }\n\n if (error instanceof Error && \"toServerActionError\" in error && typeof error.toServerActionError === \"function\") {\n const serverError = error.toServerActionError() as ServerActionError;\n \n // Check if this is an auth error based on the error type/code\n if (serverError.code === \"AUTHENTICATION_ERROR\" || (error as any).type === \"AUTHENTICATION_ERROR\") {\n serverError.shouldRedirect = true;\n serverError.redirectTo = \"/login\";\n }\n \n return {\n success: false,\n error: serverError,\n };\n }\n\n return {\n success: false,\n error: convertGenericError(error),\n };\n}\n\nexport function isErrorResponse<T>(response: ServerActionResponse<T>): response is { success: false; error: ServerActionError } {\n return !response.success;\n}","import { z } from \"zod\";\nimport type { ServerActionError, ServerActionResponse, ServerAction } from \"../types\";\nimport { handleServerActionError } from \"./error-handler\";\nimport { isNextNavigationError } from \"../next/errors\";\n\ntype InferZodOutput<T> = T extends z.ZodTypeAny ? z.output<T> : never;\ntype InferZodInput<T> = T extends z.ZodTypeAny ? z.input<T> : never;\n\ntype ServerActionMiddleware<Context, Input = unknown> = (params: { context: Context; input: Input }) => Promise<{ context: Context } | { error: ServerActionError }>;\n\nimport type { RedirectConfig } from \"../types\";\n\ntype SafeActionClientArgs<Context extends object, InputSchema extends z.ZodTypeAny | undefined, OutputSchema extends z.ZodTypeAny | undefined> = {\n middlewareFns: ServerActionMiddleware<any, any>[];\n inputSchema?: InputSchema;\n outputSchema?: OutputSchema;\n onError?: (error: unknown, context: { parsedInput?: unknown }) => ServerActionError | undefined;\n ctxType?: Context;\n redirectConfig?: string | RedirectConfig | ((result: any) => string | RedirectConfig | undefined);\n};\n\nexport class ServerActionClient<Context extends object = {}, InputSchema extends z.ZodTypeAny | undefined = undefined, OutputSchema extends z.ZodTypeAny | undefined = undefined> {\n readonly #args: SafeActionClientArgs<Context, InputSchema, OutputSchema>;\n\n constructor(\n args: SafeActionClientArgs<Context, InputSchema, OutputSchema> = {\n middlewareFns: [],\n }\n ) {\n this.#args = args;\n }\n\n /**\n * Use a middleware function.\n * @param middlewareFn Middleware function\n */\n use<NextContext extends object>(\n middlewareFn: ServerActionMiddleware<\n Context & NextContext, \n InputSchema extends z.ZodTypeAny ? z.output<InputSchema> : undefined\n >\n ) {\n return new ServerActionClient<Context & NextContext, InputSchema, OutputSchema>({\n ...this.#args,\n middlewareFns: [...this.#args.middlewareFns, middlewareFn] as any,\n ctxType: {} as Context & NextContext,\n });\n }\n\n /**\n * Define the input validation schema for the action.\n * @param schema Input validation schema\n */\n inputSchema<IS extends z.ZodTypeAny>(schema: IS) {\n return new ServerActionClient<Context, IS, OutputSchema>({\n ...this.#args,\n inputSchema: schema,\n });\n }\n\n /**\n * Define the output validation schema for the action.\n * @param schema Output validation schema\n */\n outputSchema<OS extends z.ZodTypeAny>(schema: OS) {\n return new ServerActionClient<Context, InputSchema, OS>({\n ...this.#args,\n outputSchema: schema,\n });\n }\n\n /**\n * Define a custom error handler for the action.\n * @param handler Error handler function\n */\n onError(handler: (error: unknown, context: { parsedInput?: unknown }) => ServerActionError | undefined) {\n return new ServerActionClient<Context, InputSchema, OutputSchema>({\n ...this.#args,\n onError: handler,\n });\n }\n\n /**\n * Define a redirect configuration for successful actions.\n * @param config Redirect URL, config object, or function that returns redirect config based on result\n */\n redirect<Data = any>(\n config: string | RedirectConfig | ((result: Data) => string | RedirectConfig | undefined)\n ) {\n return new ServerActionClient<Context, InputSchema, OutputSchema>({\n ...this.#args,\n redirectConfig: config,\n });\n }\n\n\n /**\n * Define the action.\n * @param serverCodeFn Code that will be executed on the server side\n */\n action<Data>(\n serverCodeFn: InputSchema extends undefined\n ? (context: Context) => Promise<Data>\n : InputSchema extends z.ZodTypeAny \n ? (input: InferZodOutput<InputSchema>, context: Context) => Promise<Data>\n : (context: Context) => Promise<Data>\n ): ServerAction<\n InputSchema extends undefined \n ? void \n : InputSchema extends z.ZodTypeAny \n ? InferZodInput<InputSchema> \n : void, \n OutputSchema extends z.ZodTypeAny ? InferZodOutput<OutputSchema> : Data\n > {\n return createServerAction<\n InputSchema extends undefined \n ? void \n : InputSchema extends z.ZodTypeAny \n ? InferZodInput<InputSchema> \n : void,\n OutputSchema extends z.ZodTypeAny ? InferZodOutput<OutputSchema> : Data\n >({\n inputSchema: this.#args.inputSchema,\n outputSchema: this.#args.outputSchema,\n middlewareFns: this.#args.middlewareFns,\n onError: this.#args.onError,\n serverCodeFn: serverCodeFn as any,\n redirectConfig: this.#args.redirectConfig,\n });\n }\n\n /**\n * Define a form action that accepts FormData.\n * @param serverCodeFn Code that will be executed on the server side\n */\n formAction<Data>(\n serverCodeFn: InputSchema extends undefined\n ? (context: Context) => Promise<Data>\n : InputSchema extends z.ZodTypeAny \n ? (input: InferZodOutput<InputSchema>, context: Context) => Promise<Data>\n : (context: Context) => Promise<Data>\n ): (prev: ServerActionResponse<OutputSchema extends z.ZodTypeAny ? InferZodOutput<OutputSchema> : Data>, formData: FormData) => Promise<ServerActionResponse<OutputSchema extends z.ZodTypeAny ? InferZodOutput<OutputSchema> : Data>> {\n return createFormServerAction({\n inputSchema: this.#args.inputSchema,\n outputSchema: this.#args.outputSchema,\n middlewareFns: this.#args.middlewareFns,\n onError: this.#args.onError,\n serverCodeFn,\n redirectConfig: this.#args.redirectConfig,\n });\n }\n}\n\n// Internal function to create the actual server action\nfunction createServerAction<TInput, TOutput>(config: {\n inputSchema?: z.ZodTypeAny;\n outputSchema?: z.ZodTypeAny;\n middlewareFns: ServerActionMiddleware<any, any>[];\n onError?: (error: unknown, context: { parsedInput?: unknown }) => ServerActionError | undefined;\n serverCodeFn: (...args: any[]) => Promise<any>;\n redirectConfig?: string | RedirectConfig | ((result: any) => string | RedirectConfig | undefined);\n}): ServerAction<TInput, TOutput> {\n const actionFn = async (...args: TInput extends void ? [] : [input: TInput]): Promise<ServerActionResponse<TOutput>> => {\n const context: { parsedInput?: unknown } = {};\n \n // Type guard: check if we have an input schema\n const hasInputSchema = config.inputSchema !== undefined && config.inputSchema !== null;\n const input = hasInputSchema ? args[0] as TInput : undefined;\n\n try {\n let parsedInput: any = undefined;\n\n // Input validation\n if (hasInputSchema && config.inputSchema) {\n const parseResult = z.safeParse(config.inputSchema, input);\n if (!parseResult.success) {\n return handleServerActionError(parseResult.error, config.onError ? (err) => config.onError!(err, { parsedInput: input }) : undefined);\n }\n parsedInput = parseResult.data;\n context.parsedInput = parsedInput;\n }\n\n // Execute middleware chain\n let middlewareContext = {};\n for (const middleware of config.middlewareFns) {\n const middlewareInput = hasInputSchema ? parsedInput : undefined;\n const result = await middleware({ context: middlewareContext, input: middlewareInput });\n if (\"error\" in result) {\n return {\n success: false,\n error: result.error,\n };\n }\n middlewareContext = { ...middlewareContext, ...result.context };\n }\n\n // Execute the server code\n const result = hasInputSchema \n ? await config.serverCodeFn(parsedInput, middlewareContext)\n : await config.serverCodeFn(middlewareContext);\n\n // Output validation\n let validatedOutput: any = result;\n if (config.outputSchema) {\n const parseResult = z.safeParse(config.outputSchema, result);\n if (!parseResult.success) {\n return handleServerActionError(parseResult.error);\n }\n validatedOutput = parseResult.data;\n }\n\n // Handle redirect config if present\n let redirectValue: string | RedirectConfig | undefined;\n if (config.redirectConfig) {\n if (typeof config.redirectConfig === 'function') {\n redirectValue = config.redirectConfig(validatedOutput);\n } else {\n redirectValue = config.redirectConfig;\n }\n }\n\n return {\n success: true,\n data: validatedOutput,\n ...(redirectValue && { redirect: redirectValue }),\n };\n } catch (error) {\n // Preserve Next.js navigation errors (redirect, notFound, etc.)\n if (isNextNavigationError(error)) {\n throw error;\n }\n\n // Debug logging only in development\n if (process.env.NODE_ENV === 'development') {\n console.log(\"[executeAction] Error:\", error?.constructor?.name, error instanceof Error ? error.message : error);\n }\n \n // Use the handleServerActionError function with custom handler\n const errorHandler = config.onError ? (err: unknown) => {\n return config.onError!(err, { parsedInput: context.parsedInput });\n } : undefined;\n \n return handleServerActionError(error, errorHandler);\n }\n };\n\n // Add metadata for runtime type checking\n (actionFn as any).__hasInputSchema = config.inputSchema !== undefined && config.inputSchema !== null;\n \n // Return the function with correct typing based on whether input is void\n return actionFn as ServerAction<TInput, TOutput>;\n}\n\n// ISO 8601 date string regex pattern\nconst ISO_DATE_REGEX = /^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(?:\\.\\d{1,3})?(?:Z|[+-]\\d{2}:?\\d{2})$/;\n\n// BigInt marker pattern (from client-side serialization)\nconst BIGINT_REGEX = /^__bigint__(-?\\d+)$/;\n\n/**\n * Restores special types (Map, Set) from their serialized form.\n */\nfunction restoreSpecialTypes(parsed: unknown): unknown {\n if (typeof parsed !== 'object' || parsed === null) return parsed;\n\n // Check for Map marker\n if ('__type__' in parsed && (parsed as Record<string, unknown>).__type__ === 'Map') {\n const entries = (parsed as Record<string, unknown>).entries;\n if (Array.isArray(entries)) {\n return new Map(entries as [unknown, unknown][]);\n }\n }\n\n // Check for Set marker\n if ('__type__' in parsed && (parsed as Record<string, unknown>).__type__ === 'Set') {\n const values = (parsed as Record<string, unknown>).values;\n if (Array.isArray(values)) {\n return new Set(values);\n }\n }\n\n return parsed;\n}\n\n/**\n * Safely parses JSON strings back to objects/arrays and handles FormData boolean conventions.\n *\n * DESIGN PRINCIPLES:\n * - Parse objects and arrays (start with { or [)\n * - Parse \"true\"/\"false\" to booleans (FormData convention for checkboxes)\n * - Parse ISO 8601 date strings to Date objects\n * - Parse BigInt strings (prefixed with __bigint__)\n * - Restore Map and Set from serialized form\n * - DO NOT parse number strings (\"123\") to avoid ambiguity\n * - Fail gracefully on malformed JSON\n * - Maintain perfect symmetry with client-side objectToFormData\n *\n * WHY WE PARSE BOOLEANS:\n * - HTML forms cannot send real JavaScript booleans, only strings\n * - Checkboxes commonly send \"true\"/\"false\" strings\n * - React Hook Form uses this convention\n * - This is the expected behavior in FormData context\n *\n * WHY WE PARSE DATES:\n * - Date objects are serialized to ISO strings via toISOString() on the client\n * - We need to reconstruct them as Date objects for Zod z.date() validation\n */\nfunction tryParseJSON(value: unknown): unknown {\n // Non-strings pass through unchanged\n if (typeof value !== 'string') return value;\n\n const trimmed = value.trim();\n\n // Empty strings become null (useful for nullable date fields)\n if (!trimmed) return null;\n\n // Parse boolean strings (common for checkboxes and React Hook Form)\n // This is safe in FormData context since HTML can't send real booleans\n if (trimmed === 'true') return true;\n if (trimmed === 'false') return false;\n\n // Parse null (less common but sometimes used)\n if (trimmed === 'null') return null;\n\n // Parse BigInt strings (from client-side serialization)\n const bigintMatch = trimmed.match(BIGINT_REGEX);\n if (bigintMatch) {\n try {\n return BigInt(bigintMatch[1]);\n } catch {\n // Invalid BigInt - fall through to return original string\n }\n }\n\n // Parse ISO 8601 date strings to Date objects\n if (ISO_DATE_REGEX.test(trimmed)) {\n const date = new Date(trimmed);\n // Only return Date if parsing was successful (not Invalid Date)\n if (!isNaN(date.getTime())) {\n return date;\n }\n }\n\n // Only attempt parsing for obvious JSON structures\n if ((trimmed.startsWith('{') && trimmed.endsWith('}')) ||\n (trimmed.startsWith('[') && trimmed.endsWith(']'))) {\n try {\n const parsed = JSON.parse(trimmed);\n // Extra safety: verify we got an object/array\n if (typeof parsed === 'object' && parsed !== null) {\n // Restore special types (Map, Set)\n return restoreSpecialTypes(parsed);\n }\n } catch {\n // Malformed JSON - return original string\n }\n }\n\n // All other strings (including number strings like \"123\") remain as strings\n // This avoids ambiguity with actual string values that happen to be numeric\n return value;\n}\n\n// Form data parser with automatic JSON deserialization\nfunction parseFormData(formData: FormData): Record<string, any> {\n const result: Record<string, any> = {};\n const keys = new Set<string>();\n\n for (const [key] of formData.entries()) {\n keys.add(key);\n }\n\n for (const key of keys) {\n const values = formData.getAll(key);\n\n if (key.endsWith(\"[]\")) {\n const cleanKey = key.slice(0, -2);\n // Parse each array item (handles nested objects in arrays)\n result[cleanKey] = values.map(tryParseJSON);\n } else if (values.length === 1) {\n // Parse single value (handles JSON stringified objects)\n result[key] = tryParseJSON(values[0]);\n } else {\n // Parse multiple values\n result[key] = values.map(tryParseJSON);\n }\n }\n\n return result;\n}\n\n// Internal function to create form server action\nfunction createFormServerAction<TOutput>(config: {\n inputSchema?: z.ZodTypeAny;\n outputSchema?: z.ZodTypeAny;\n middlewareFns: ServerActionMiddleware<any>[];\n onError?: (error: unknown, context: { parsedInput?: unknown }) => ServerActionError | undefined;\n serverCodeFn: (...args: any[]) => Promise<any>;\n redirectConfig?: string | RedirectConfig | ((result: any) => string | RedirectConfig | undefined);\n}): (prev: ServerActionResponse<TOutput>, formData: FormData) => Promise<ServerActionResponse<TOutput>> {\n return async (_prev: ServerActionResponse<TOutput>, formData: FormData): Promise<ServerActionResponse<TOutput>> => {\n const context: { parsedInput?: unknown } = {};\n const hasInputSchema = config.inputSchema !== undefined && config.inputSchema !== null;\n\n try {\n let parsedInput: any = parseFormData(formData);\n\n // Input validation\n if (config.inputSchema) {\n const parseResult = z.safeParse(config.inputSchema, parsedInput);\n if (!parseResult.success) {\n return handleServerActionError(parseResult.error, config.onError ? (err) => config.onError!(err, { parsedInput }) : undefined);\n }\n parsedInput = parseResult.data;\n context.parsedInput = parsedInput;\n }\n\n // Execute middleware chain\n let middlewareContext = {};\n for (const middleware of config.middlewareFns) {\n const middlewareInput = hasInputSchema ? parsedInput : undefined;\n const result = await middleware({ context: middlewareContext, input: middlewareInput });\n if (\"error\" in result) {\n return {\n success: false,\n error: result.error,\n };\n }\n middlewareContext = { ...middlewareContext, ...result.context };\n }\n\n // Execute the server code\n const result = hasInputSchema \n ? await config.serverCodeFn(parsedInput, middlewareContext)\n : await config.serverCodeFn(middlewareContext);\n\n // Output validation\n let validatedOutput: any = result;\n if (config.outputSchema) {\n const parseResult = z.safeParse(config.outputSchema, result);\n if (!parseResult.success) {\n return handleServerActionError(parseResult.error);\n }\n validatedOutput = parseResult.data;\n }\n\n // Handle redirect config if present\n let redirectValue: string | RedirectConfig | undefined;\n if (config.redirectConfig) {\n if (typeof config.redirectConfig === 'function') {\n redirectValue = config.redirectConfig(validatedOutput);\n } else {\n redirectValue = config.redirectConfig;\n }\n }\n\n return {\n success: true,\n data: validatedOutput,\n ...(redirectValue && { redirect: redirectValue }),\n };\n } catch (error) {\n // Preserve Next.js navigation errors (redirect, notFound, etc.)\n if (isNextNavigationError(error)) {\n throw error;\n }\n // Debug logging only in development\n if (process.env.NODE_ENV === 'development') {\n console.log(\"[ServerActionClient] Error:\", error?.constructor?.name, error instanceof Error ? error.message : error);\n }\n const errorHandler = config.onError ? (err: unknown) => {\n return config.onError!(err, { parsedInput: context.parsedInput });\n } : undefined;\n return handleServerActionError(error, errorHandler);\n }\n };\n}\n\n// Export factory function for creating a new client\nexport function createActionClient() {\n return new ServerActionClient();\n}\n","// Comes from: https://github.com/vercel/next.js/blob/canary/packages/next/src/client/components/redirect-error.ts\n\nenum RedirectStatusCode {\n\tSeeOther = 303,\n\tTemporaryRedirect = 307,\n\tPermanentRedirect = 308,\n}\n\nconst REDIRECT_ERROR_CODE = \"NEXT_REDIRECT\";\n\nenum RedirectType {\n\tpush = \"push\",\n\treplace = \"replace\",\n}\n\nexport type RedirectError = Error & {\n\tdigest: `${typeof REDIRECT_ERROR_CODE};${RedirectType};${string};${RedirectStatusCode};`;\n};\n\n/**\n * Checks an error to determine if it's an error generated by the\n * `redirect(url)` helper.\n *\n * @param error the error that may reference a redirect error\n * @returns true if the error is a redirect error\n */\nexport function isRedirectError(error: unknown): error is RedirectError {\n\tif (typeof error !== \"object\" || error === null || !(\"digest\" in error) || typeof error.digest !== \"string\") {\n\t\treturn false;\n\t}\n\n\tconst digest = error.digest.split(\";\");\n\tconst [errorCode, type] = digest;\n\tconst destination = digest.slice(2, -2).join(\";\");\n\tconst status = digest.at(-2);\n\n\tconst statusCode = Number(status);\n\n\treturn (\n\t\terrorCode === REDIRECT_ERROR_CODE &&\n\t\t(type === \"replace\" || type === \"push\") &&\n\t\ttypeof destination === \"string\" &&\n\t\t!isNaN(statusCode) &&\n\t\tstatusCode in RedirectStatusCode\n\t);\n}","// Comes from https://github.com/vercel/next.js/blob/canary/packages/next/src/client/components/http-access-fallback/http-access-fallback.ts\n\nconst HTTPAccessErrorStatus = {\n\tNOT_FOUND: 404,\n\tFORBIDDEN: 403,\n\tUNAUTHORIZED: 401,\n};\n\nconst ALLOWED_CODES = new Set(Object.values(HTTPAccessErrorStatus));\n\nconst HTTP_ERROR_FALLBACK_ERROR_CODE = \"NEXT_HTTP_ERROR_FALLBACK\";\n\nexport type HTTPAccessFallbackError = Error & {\n\tdigest: `${typeof HTTP_ERROR_FALLBACK_ERROR_CODE};${string}`;\n};\n\n/**\n * Checks an error to determine if it's an error generated by\n * the HTTP navigation APIs `notFound()`, `forbidden()` or `unauthorized()`.\n *\n * @param error the error that may reference a HTTP access error\n * @returns true if the error is a HTTP access error\n */\nexport function isHTTPAccessFallbackError(error: unknown): error is HTTPAccessFallbackError {\n\tif (typeof error !== \"object\" || error === null || !(\"digest\" in error) || typeof error.digest !== \"string\") {\n\t\treturn false;\n\t}\n\tconst [prefix, httpStatus] = error.digest.split(\";\");\n\n\treturn prefix === HTTP_ERROR_FALLBACK_ERROR_CODE && ALLOWED_CODES.has(Number(httpStatus));\n}\n\nexport function getAccessFallbackHTTPStatus(error: HTTPAccessFallbackError): number {\n\tconst httpStatus = error.digest.split(\";\")[1];\n\treturn Number(httpStatus);\n}","import { isRedirectError } from \"./redirect\";\nimport { isHTTPAccessFallbackError, getAccessFallbackHTTPStatus } from \"./http-access-fallback\";\n\nexport { isRedirectError } from \"./redirect\";\nexport type { RedirectError } from \"./redirect\";\n\nexport { isHTTPAccessFallbackError, getAccessFallbackHTTPStatus } from \"./http-access-fallback\";\nexport type { HTTPAccessFallbackError } from \"./http-access-fallback\";\n\n/**\n * Checks if the error is a navigation error that should be re-thrown\n * This includes redirect errors and HTTP access errors (notFound, forbidden, unauthorized)\n */\nexport function isNextNavigationError(error: unknown): boolean {\n\treturn isRedirectError(error) || isHTTPAccessFallbackError(error);\n}\n\n/**\n * Checks if the error is a notFound error\n * Note: Next.js implements notFound() using HTTP_ERROR_FALLBACK with status 404,\n * not as a separate error type like NEXT_REDIRECT\n */\nexport function isNotFoundError(error: unknown): boolean {\n\treturn isHTTPAccessFallbackError(error) && getAccessFallbackHTTPStatus(error as any) === 404;\n}","import type { ServerActionError } from '../types';\n\n/**\n * Type definition for Server Action Middleware\n *\n * A middleware can either:\n * - Return an updated context that gets merged with the existing context\n * - Return an error that stops the action execution\n */\nexport type ServerActionMiddleware<Context, Input = unknown> = (params: {\n context: Context;\n input: Input;\n}) => Promise<{ context: Context } | { error: ServerActionError }>;\n\n/**\n * Helper for creating middleware with correct type inference for the output context.\n *\n * This helper solves the TypeScript type inference issue where factory functions\n * that return middleware lose the context type information. By wrapping your\n * middleware with this helper, you explicitly declare what context type the\n * middleware will output, enabling proper type inference through the `.use()` chain.\n *\n * @template TOutputContext - The type of context this middleware will output\n * @template TInput - The type of input data (defaults to unknown)\n *\n * @param middlewareFn - The middleware function that transforms context\n * @returns A typed middleware function with proper type inference\n *\n * @example\n * ```typescript\n * interface AuthContext {\n * userId: string;\n * role: string;\n * }\n *\n * const createAuthMiddleware = () =>\n * createTypedMiddleware<AuthContext>(async ({ context, input }) => {\n * const session = await getSession();\n *\n * if (!session) {\n * return { error: { message: 'Unauthorized', code: 'AUTH_ERROR' } };\n * }\n *\n * return {\n * context: {\n * userId: session.userId,\n * role: session.role,\n * },\n * };\n * });\n *\n * // Now TypeScript correctly infers authClient has AuthContext\n * const authClient = baseClient.use(createAuthMiddleware());\n *\n * // And in your actions, context is properly typed\n * authClient.action(async (context) => {\n * const userId = context.userId; // ✅ Type-safe!\n * });\n * ```\n */\nexport function createTypedMiddleware<TOutputContext extends object, TInput = unknown>(\n middlewareFn: (params: {\n context: any;\n input: TInput;\n }) => Promise<{ context: TOutputContext } | { error: ServerActionError }>\n): ServerActionMiddleware<TOutputContext, TInput> {\n return middlewareFn as any;\n}\n"],"mappings":";;;;;;;;;AAAA,SAAS,GAAG,gBAAgB;AAG5B,SAAS,gBAAgB,OAAoC;AAC3D,QAAM,EAAE,aAAa,WAAW,IAAI,EAAE,aAAa,KAAK;AACxD,QAAM,CAAC,KAAK,IAAI,OAAO,KAAK,WAAW;AACvC,QAAM,WAAW,SAAS,YAAY,KAAK,IAAI,CAAC,MAAM,WAAW,CAAC,KAAK;AACvE,SAAO;AAAA,IACL;AAAA,IACA,MAAM;AAAA,IACN;AAAA,IACA,QAAQ;AAAA,EACV;AACF;AAEA,SAAS,oBAAoB,OAAmC;AAC9D,QAAM,UAAU,iBAAiB,QAAQ,MAAM,UAAU;AAEzD,SAAO;AAAA,IACL,SAAS,QAAQ,IAAI,aAAa,eAAe,iCAAiC;AAAA,IAClF,MAAM;AAAA,IACN,YAAY;AAAA,EACd;AACF;AAEO,SAAS,wBACd,OACA,eAC8C;AAE9C,MAAI,QAAQ,IAAI,aAAa,eAAe;AAC1C,YAAQ,MAAM,yBAAyB,OAAO,aAAa,QAAQ,WAAW,iBAAiB,QAAQ,MAAM,UAAU,KAAK;AAAA,EAC9H;AAGA,MAAI,eAAe;AACjB,QAAI;AACF,YAAM,cAAc,cAAc,KAAK;AACvC,UAAI,aAAa;AACf,eAAO;AAAA,UACL,SAAS;AAAA,UACT,OAAO;AAAA,QACT;AAAA,MACF;AAAA,IACF,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