Home / File/ endpoint.ts — astro Source File

endpoint.ts — astro Source File

Architecture documentation for endpoint.ts, a typescript file in the astro codebase. 7 imports, 0 dependents.

File typescript CoreAstro RenderingEngine 7 imports 1 functions

Entity Profile

Dependency Diagram

graph LR
  828cf199_ae8d_edfb_c3c2_135feadcf052["endpoint.ts"]
  7216d952_4e4a_2d18_a85b_74b4ace79e2b["../core/constants.js"]
  828cf199_ae8d_edfb_c3c2_135feadcf052 --> 7216d952_4e4a_2d18_a85b_74b4ace79e2b
  dd6187d6_53c4_ce90_a1d1_3a0b5e7e7d3f["../../core/errors/errors.js"]
  828cf199_ae8d_edfb_c3c2_135feadcf052 --> dd6187d6_53c4_ce90_a1d1_3a0b5e7e7d3f
  8df634da_0f30_1e1f_1314_2439b0c9baab["../core/errors/errors-data.js"]
  828cf199_ae8d_edfb_c3c2_135feadcf052 --> 8df634da_0f30_1e1f_1314_2439b0c9baab
  d3861967_b647_84d2_ff48_15013353bd56["../core/logger/core.js"]
  828cf199_ae8d_edfb_c3c2_135feadcf052 --> d3861967_b647_84d2_ff48_15013353bd56
  135a8084_d596_67c2_9209_cca6693604e6["../types/public/common.js"]
  828cf199_ae8d_edfb_c3c2_135feadcf052 --> 135a8084_d596_67c2_9209_cca6693604e6
  28857b9f_4720_3f29_4abb_a7eec34dcca5["../types/public/context.js"]
  828cf199_ae8d_edfb_c3c2_135feadcf052 --> 28857b9f_4720_3f29_4abb_a7eec34dcca5
  10250468_0e83_bd69_43e9_3bcef2294a91["piccolore"]
  828cf199_ae8d_edfb_c3c2_135feadcf052 --> 10250468_0e83_bd69_43e9_3bcef2294a91
  style 828cf199_ae8d_edfb_c3c2_135feadcf052 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import colors from 'piccolore';
import { REROUTABLE_STATUS_CODES, REROUTE_DIRECTIVE_HEADER } from '../../core/constants.js';
import { AstroError } from '../../core/errors/errors.js';
import { EndpointDidNotReturnAResponse } from '../../core/errors/errors-data.js';
import type { Logger } from '../../core/logger/core.js';
import type { APIRoute } from '../../types/public/common.js';
import type { APIContext } from '../../types/public/context.js';

/** Renders an endpoint request to completion, returning the body. */
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

Functions

Dependencies

  • ../../core/errors/errors.js
  • ../core/constants.js
  • ../core/errors/errors-data.js
  • ../core/logger/core.js
  • ../types/public/common.js
  • ../types/public/context.js
  • piccolore

Frequently Asked Questions

What does endpoint.ts do?
endpoint.ts is a source file in the astro codebase, written in typescript. It belongs to the CoreAstro domain, RenderingEngine subdomain.
What functions are defined in endpoint.ts?
endpoint.ts defines 1 function(s): renderEndpoint.
What does endpoint.ts depend on?
endpoint.ts imports 7 module(s): ../../core/errors/errors.js, ../core/constants.js, ../core/errors/errors-data.js, ../core/logger/core.js, ../types/public/common.js, ../types/public/context.js, piccolore.
Where is endpoint.ts in the architecture?
endpoint.ts is located at packages/astro/src/runtime/server/endpoint.ts (domain: CoreAstro, subdomain: RenderingEngine, directory: packages/astro/src/runtime/server).

Analyze Your Own Codebase

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

Try Supermodel Free