Home / Function/ defineAction() — astro Function Reference

defineAction() — astro Function Reference

Architecture documentation for the defineAction() function in server.ts from the astro codebase.

Entity Profile

Dependency Diagram

graph TD
  246d72b2_472d_5097_3c81_0588c08ec2c9["defineAction()"]
  12df90c3_b0fe_d858_b821_5011b6067fdb["server.ts"]
  246d72b2_472d_5097_3c81_0588c08ec2c9 -->|defined in| 12df90c3_b0fe_d858_b821_5011b6067fdb
  a0bc39ba_2938_607e_8820_271766974a1b["getFormServerHandler()"]
  246d72b2_472d_5097_3c81_0588c08ec2c9 -->|calls| a0bc39ba_2938_607e_8820_271766974a1b
  24188646_08f2_55ed_5b29_0b515184121a["getJsonServerHandler()"]
  246d72b2_472d_5097_3c81_0588c08ec2c9 -->|calls| 24188646_08f2_55ed_5b29_0b515184121a
  ad42f793_8b30_3230_ce44_8487dc7a4091["isActionAPIContext()"]
  246d72b2_472d_5097_3c81_0588c08ec2c9 -->|calls| ad42f793_8b30_3230_ce44_8487dc7a4091
  6bdb7540_3cf8_62a4_f943_abdf672f35db["callSafely()"]
  246d72b2_472d_5097_3c81_0588c08ec2c9 -->|calls| 6bdb7540_3cf8_62a4_f943_abdf672f35db
  style 246d72b2_472d_5097_3c81_0588c08ec2c9 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/astro/src/actions/runtime/server.ts lines 32–71

export function defineAction<
	TOutput,
	TAccept extends ActionAccept | undefined = undefined,
	TInputSchema extends z.$ZodType | undefined = TAccept extends 'form'
		? // If `input` is omitted, default to `FormData` for forms and `any` for JSON.
			z.$ZodType<FormData>
		: undefined,
>({
	accept,
	input: inputSchema,
	handler,
}: {
	input?: TInputSchema;
	accept?: TAccept;
	handler: ActionHandler<TInputSchema, TOutput>;
}): ActionClient<TOutput, TAccept, TInputSchema> & string {
	const serverHandler =
		accept === 'form'
			? getFormServerHandler(handler, inputSchema)
			: getJsonServerHandler(handler, inputSchema);

	async function safeServerHandler(this: ActionAPIContext, unparsedInput: unknown) {
		// The ActionAPIContext should always contain the `params` property
		if (typeof this === 'function' || !isActionAPIContext(this)) {
			throw new AstroError(ActionCalledFromServerError);
		}
		return callSafely(() => serverHandler(unparsedInput, this));
	}

	Object.assign(safeServerHandler, {
		orThrow(this: ActionAPIContext, unparsedInput: unknown) {
			if (typeof this === 'function') {
				throw new AstroError(ActionCalledFromServerError);
			}
			return serverHandler(unparsedInput, this);
		},
	});

	return safeServerHandler as ActionClient<TOutput, TAccept, TInputSchema> & string;
}

Domain

Subdomains

Frequently Asked Questions

What does defineAction() do?
defineAction() is a function in the astro codebase, defined in packages/astro/src/actions/runtime/server.ts.
Where is defineAction() defined?
defineAction() is defined in packages/astro/src/actions/runtime/server.ts at line 32.
What does defineAction() call?
defineAction() calls 4 function(s): callSafely, getFormServerHandler, getJsonServerHandler, isActionAPIContext.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free