Home / Function/ renderEndpoint() — astro Function Reference

renderEndpoint() — astro Function Reference

Architecture documentation for the renderEndpoint() function in endpoint.ts from the astro codebase.

Entity Profile

Dependency Diagram

graph TD
  c46dd6f6_32a9_fd06_a779_73ff2f6c738e["renderEndpoint()"]
  828cf199_ae8d_edfb_c3c2_135feadcf052["endpoint.ts"]
  c46dd6f6_32a9_fd06_a779_73ff2f6c738e -->|defined in| 828cf199_ae8d_edfb_c3c2_135feadcf052
  style c46dd6f6_32a9_fd06_a779_73ff2f6c738e fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/astro/src/runtime/server/endpoint.ts lines 10–91

export async function renderEndpoint(
	mod: {
		[method: string]: APIRoute;
	},
	context: APIContext,
	isPrerendered: boolean,
	logger: Logger,
) {
	const { request, url } = context;

	const method = request.method.toUpperCase();
	// use the exact match on `method`, fallback to ALL
	let handler = mod[method] ?? mod['ALL'];
	// use GET handler for HEAD requests
	if (!handler && method === 'HEAD' && mod['GET']) {
		handler = mod['GET'];
	}
	if (isPrerendered && !['GET', 'HEAD'].includes(method)) {
		logger.warn(
			'router',
			`${url.pathname} ${colors.bold(
				method,
			)} requests are not available in static endpoints. Mark this page as server-rendered (\`export const prerender = false;\`) or update your config to \`output: 'server'\` to make all your pages server-rendered by default.`,
		);
	}
	if (handler === undefined) {
		logger.warn(
			'router',
			`No API Route handler exists for the method "${method}" for the route "${url.pathname}".\n` +
				`Found handlers: ${Object.keys(mod)
					.map((exp) => JSON.stringify(exp))
					.join(', ')}\n` +
				('all' in mod
					? `One of the exported handlers is "all" (lowercase), did you mean to export 'ALL'?\n`
					: ''),
		);
		// No handler matching the verb found, so this should be a
		// 404. Should be handled by 404.astro route if possible.
		return new Response(null, { status: 404 });
	}
	if (typeof handler !== 'function') {
		logger.error(
			'router',
			`The route "${
				url.pathname
			}" exports a value for the method "${method}", but it is of the type ${typeof handler} instead of a function.`,
		);
		return new Response(null, { status: 500 });
	}

	let response = await handler.call(mod, context);

	if (!response || response instanceof Response === false) {
		throw new AstroError(EndpointDidNotReturnAResponse);
	}

	// Endpoints explicitly returning 404 or 500 response status should
	// NOT be subject to rerouting to 404.astro or 500.astro.
	if (REROUTABLE_STATUS_CODES.includes(response.status)) {
		try {
			response.headers.set(REROUTE_DIRECTIVE_HEADER, 'no');
		} catch (err) {
			// In some cases the response may have immutable headers
			// This is the case if, for example, the user directly returns a `fetch` response
			// There's no clean way to check if the headers are immutable, so we just catch the error
			// Note that response.clone() still has immutable headers!
			if ((err as Error).message?.includes('immutable')) {
				response = new Response(response.body, response);
				response.headers.set(REROUTE_DIRECTIVE_HEADER, 'no');
			} else {
				throw err;
			}
		}
	}

	if (method === 'HEAD') {
		// make sure HEAD responses doesnt have body
		return new Response(null, response);
	}

	return response;

Domain

Subdomains

Frequently Asked Questions

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

Analyze Your Own Codebase

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

Try Supermodel Free