Home / Function/ getActionContext() — astro Function Reference

getActionContext() — astro Function Reference

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

Entity Profile

Dependency Diagram

graph TD
  87610e32_7608_ae90_0908_65538303d4a6["getActionContext()"]
  12df90c3_b0fe_d858_b821_5011b6067fdb["server.ts"]
  87610e32_7608_ae90_0908_65538303d4a6 -->|defined in| 12df90c3_b0fe_d858_b821_5011b6067fdb
  d05f7b3a_0f53_ab9b_0fc7_7983cac82bdb["getCallerInfo()"]
  87610e32_7608_ae90_0908_65538303d4a6 -->|calls| d05f7b3a_0f53_ab9b_0fc7_7983cac82bdb
  d2a22336_a5ee_9bc2_8056_2770db2de333["parseRequestBody()"]
  87610e32_7608_ae90_0908_65538303d4a6 -->|calls| d2a22336_a5ee_9bc2_8056_2770db2de333
  style 87610e32_7608_ae90_0908_65538303d4a6 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/astro/src/actions/runtime/server.ts lines 157–240

export function getActionContext(context: APIContext): AstroActionContext {
	const callerInfo = getCallerInfo(context);

	// Prevents action results from being handled on a rewrite.
	// Also prevents our *own* fallback middleware from running
	// if the user's middleware has already handled the result.
	const actionResultAlreadySet = Boolean((context.locals as ActionsLocals)._actionPayload);

	let action: AstroActionContext['action'] = undefined;

	if (callerInfo && context.request.method === 'POST' && !actionResultAlreadySet) {
		action = {
			calledFrom: callerInfo.from,
			name: callerInfo.name,
			handler: async () => {
				const pipeline: Pipeline = Reflect.get(context, pipelineSymbol);
				const callerInfoName = shouldAppendForwardSlash(
					pipeline.manifest.trailingSlash,
					pipeline.manifest.buildFormat,
				)
					? removeTrailingForwardSlash(callerInfo.name)
					: callerInfo.name;

				let baseAction;
				try {
					baseAction = await pipeline.getAction(callerInfoName);
				} catch (error) {
					// Check if this is an ActionNotFoundError by comparing the name property
					// We use this approach instead of instanceof because the error might be
					// a different instance of the AstroError class depending on the environment
					if (
						error instanceof Error &&
						'name' in error &&
						typeof error.name === 'string' &&
						error.name === ActionNotFoundError.name
					) {
						return { data: undefined, error: new ActionError({ code: 'NOT_FOUND' }) };
					}
					throw error;
				}

				let input;
				try {
					input = await parseRequestBody(context.request);
				} catch (e) {
					if (e instanceof TypeError) {
						return { data: undefined, error: new ActionError({ code: 'UNSUPPORTED_MEDIA_TYPE' }) };
					}
					throw e;
				}

				const omitKeys = ['props', 'getActionResult', 'callAction', 'redirect'];

				// Clones the context, preserving accessors and methods but omitting
				// the properties that are not needed in the action handler.
				const actionAPIContext = Object.create(
					Object.getPrototypeOf(context),
					Object.fromEntries(
						Object.entries(Object.getOwnPropertyDescriptors(context)).filter(
							([key]) => !omitKeys.includes(key),
						),
					),
				);

				Reflect.set(actionAPIContext, ACTION_API_CONTEXT_SYMBOL, true);
				const handler = baseAction.bind(actionAPIContext satisfies ActionAPIContext);
				return handler(input);
			},
		};
	}

	function setActionResult(actionName: string, actionResult: SerializedActionResult) {
		(context.locals as ActionsLocals)._actionPayload = {
			actionResult,
			actionName,
		};
	}
	return {
		action,
		setActionResult,
		serializeActionResult,

Domain

Subdomains

Frequently Asked Questions

What does getActionContext() do?
getActionContext() is a function in the astro codebase, defined in packages/astro/src/actions/runtime/server.ts.
Where is getActionContext() defined?
getActionContext() is defined in packages/astro/src/actions/runtime/server.ts at line 157.
What does getActionContext() call?
getActionContext() calls 2 function(s): getCallerInfo, parseRequestBody.

Analyze Your Own Codebase

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

Try Supermodel Free