Home / Function/ serializeActionResult() — astro Function Reference

serializeActionResult() — astro Function Reference

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

Entity Profile

Dependency Diagram

graph TD
  6d680ac3_3f25_41c7_0c0a_eb21a0b2d9c6["serializeActionResult()"]
  12df90c3_b0fe_d858_b821_5011b6067fdb["server.ts"]
  6d680ac3_3f25_41c7_0c0a_eb21a0b2d9c6 -->|defined in| 12df90c3_b0fe_d858_b821_5011b6067fdb
  style 6d680ac3_3f25_41c7_0c0a_eb21a0b2d9c6 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/astro/src/actions/runtime/server.ts lines 388–446

export function serializeActionResult(res: SafeResult<any, any>): SerializedActionResult {
	if (res.error) {
		if (import.meta.env?.DEV) {
			actionResultErrorStack.set(res.error.stack);
		}

		let body: Record<string, any>;
		if (res.error instanceof ActionInputError) {
			body = {
				type: res.error.type,
				issues: res.error.issues,
				fields: res.error.fields,
			};
		} else {
			body = {
				...res.error,
				message: res.error.message,
			};
		}

		return {
			type: 'error',
			status: res.error.status,
			contentType: 'application/json',
			body: JSON.stringify(body),
		};
	}
	if (res.data === undefined) {
		return {
			type: 'empty',
			status: 204,
		};
	}
	let body;
	try {
		body = devalueStringify(res.data, {
			// Add support for URL objects
			URL: (value) => value instanceof URL && value.href,
		});
	} catch (e) {
		let hint = ActionsReturnedInvalidDataError.hint;
		if (res.data instanceof Response) {
			hint = REDIRECT_STATUS_CODES.includes(res.data.status as any)
				? 'If you need to redirect when the action succeeds, trigger a redirect where the action is called. See the Actions guide for server and client redirect examples: https://docs.astro.build/en/guides/actions.'
				: 'If you need to return a Response object, try using a server endpoint instead. See https://docs.astro.build/en/guides/endpoints/#server-endpoints-api-routes';
		}
		throw new AstroError({
			...ActionsReturnedInvalidDataError,
			message: ActionsReturnedInvalidDataError.message(String(e)),
			hint,
		});
	}
	return {
		type: 'data',
		status: 200,
		contentType: 'application/json+devalue',
		body,
	};
}

Domain

Subdomains

Frequently Asked Questions

What does serializeActionResult() do?
serializeActionResult() is a function in the astro codebase, defined in packages/astro/src/actions/runtime/server.ts.
Where is serializeActionResult() defined?
serializeActionResult() is defined in packages/astro/src/actions/runtime/server.ts at line 388.

Analyze Your Own Codebase

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

Try Supermodel Free