Home / Function/ callMiddleware() — astro Function Reference

callMiddleware() — astro Function Reference

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

Entity Profile

Dependency Diagram

graph TD
  043ddea3_c770_107e_ee46_680bc8cab580["callMiddleware()"]
  db77a309_a7b7_5c30_3a75_0733b8628466["callMiddleware.ts"]
  043ddea3_c770_107e_ee46_680bc8cab580 -->|defined in| db77a309_a7b7_5c30_3a75_0733b8628466
  style 043ddea3_c770_107e_ee46_680bc8cab580 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/astro/src/core/middleware/callMiddleware.ts lines 43–104

export async function callMiddleware(
	onRequest: MiddlewareHandler,
	apiContext: APIContext,
	responseFunction: (
		apiContext: APIContext,
		rewritePayload?: RewritePayload,
	) => Promise<Response> | Response,
): Promise<Response> {
	let nextCalled = false;
	let responseFunctionPromise: Promise<Response> | Response | undefined = undefined;
	const next: MiddlewareNext = async (payload) => {
		nextCalled = true;
		responseFunctionPromise = responseFunction(apiContext, payload);
		// We need to pass the APIContext pass to `callMiddleware` because it can be mutated across middleware functions
		return responseFunctionPromise;
	};

	const middlewarePromise = onRequest(apiContext, next);

	return await Promise.resolve(middlewarePromise).then(async (value) => {
		// first we check if `next` was called
		if (nextCalled) {
			/**
			 * Then we check if a value is returned. If so, we need to return the value returned by the
			 * middleware.
			 * e.g.
			 * ```js
			 * 	const response = await next();
			 * 	const new Response(null, { status: 500, headers: response.headers });
			 * ```
			 */
			if (typeof value !== 'undefined') {
				if (value instanceof Response === false) {
					throw new AstroError(AstroErrorData.MiddlewareNotAResponse);
				}
				return value;
			} else {
				/**
				 * Here we handle the case where `next` was called and returned nothing.
				 */
				if (responseFunctionPromise) {
					return responseFunctionPromise;
				} else {
					throw new AstroError(AstroErrorData.MiddlewareNotAResponse);
				}
			}
		} else if (typeof value === 'undefined') {
			/**
			 * There might be cases where `next` isn't called and the middleware **must** return
			 * something.
			 *
			 * If not thing is returned, then we raise an Astro error.
			 */
			throw new AstroError(AstroErrorData.MiddlewareNoDataOrNextCalled);
		} else if (value instanceof Response === false) {
			throw new AstroError(AstroErrorData.MiddlewareNotAResponse);
		} else {
			// Middleware did not call resolve and returned a value
			return value;
		}
	});
}

Domain

Subdomains

Frequently Asked Questions

What does callMiddleware() do?
callMiddleware() is a function in the astro codebase, defined in packages/astro/src/core/middleware/callMiddleware.ts.
Where is callMiddleware() defined?
callMiddleware() is defined in packages/astro/src/core/middleware/callMiddleware.ts at line 43.

Analyze Your Own Codebase

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

Try Supermodel Free