import type * as z from 'zod/v4/core'; import type { APIContext } from '../../types/public/index.js'; import type { ActionError, codeToStatusMap } from './client.js'; export type ActionErrorCode = keyof typeof codeToStatusMap; export type ActionAccept = 'form' | 'json'; export type ActionHandler = TInputSchema extends z.$ZodType ? (input: z.infer, context: ActionAPIContext) => MaybePromise : (input: any, context: ActionAPIContext) => MaybePromise; export type ActionReturnType> = Awaited>; type InferKey = '__internalInfer'; /** * Infers the type of an action's input based on its Zod schema * * @see https://docs.astro.build/en/reference/modules/astro-actions/#actioninputschema */ export type ActionInputSchema> = T extends { [key in InferKey]: any; } ? T[InferKey] : never; export type ActionClient = TInputSchema extends z.$ZodType ? ((input: TAccept extends 'form' ? FormData : z.input) => Promise extends ErrorInferenceObject ? z.input : ErrorInferenceObject, Awaited>>) & { queryString: string; orThrow: (input: TAccept extends 'form' ? FormData : z.input) => Promise>; } & { [key in InferKey]: TInputSchema; } : ((input?: any) => Promise>>) & { orThrow: (input?: any) => Promise>; }; export type SafeResult = { data: TOutput; error: undefined; } | { data: undefined; error: ActionError; }; export type SerializedActionResult = { type: 'data'; contentType: 'application/json+devalue'; status: 200; body: string; } | { type: 'error'; contentType: 'application/json'; status: number; body: string; } | { type: 'empty'; status: 204; }; export interface ActionsLocals { _actionPayload: { actionResult: SerializedActionResult; actionName: string; }; } export type ActionAPIContext = Pick; export type MaybePromise = T | Promise; /** * Used to preserve the input schema type in the error object. * This allows for type inference on the `fields` property * when type narrowed to an `ActionInputError`. * * Example: Action has an input schema of `{ name: z.string() }`. * When calling the action and checking `isInputError(result.error)`, * `result.error.fields` will be typed with the `name` field. */ export type ErrorInferenceObject = Record; export {};