Home / Function/ createContext() — astro Function Reference

createContext() — astro Function Reference

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

Entity Profile

Dependency Diagram

graph TD
  06a60128_70e8_5ed7_e584_a837b37d2b5f["createContext()"]
  539f677c_93c9_1e05_dcf8_dd470043a1a3["index.ts"]
  06a60128_70e8_5ed7_e584_a837b37d2b5f -->|defined in| 539f677c_93c9_1e05_dcf8_dd470043a1a3
  style 06a60128_70e8_5ed7_e584_a837b37d2b5f fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/astro/src/core/middleware/index.ts lines 48–124

function createContext({
	request,
	params = {},
	userDefinedLocales = [],
	defaultLocale = '',
	locals = {},
}: CreateContext): APIContext {
	let preferredLocale: string | undefined = undefined;
	let preferredLocaleList: string[] | undefined = undefined;
	let currentLocale: string | undefined = undefined;
	let clientIpAddress: string | undefined;
	const url = new URL(request.url);
	const route = url.pathname;

	// TODO verify that this function works in an edge middleware environment
	const rewrite = (_reroutePayload: RewritePayload) => {
		// return dummy response
		return Promise.resolve(new Response(null));
	};
	const context: Omit<APIContext, 'getActionResult' | 'callAction'> = {
		cookies: new AstroCookies(request),
		request,
		params,
		site: undefined,
		generator: ASTRO_GENERATOR,
		props: {},
		rewrite,
		routePattern: '',
		redirect(path, status) {
			return new Response(null, {
				status: status || 302,
				headers: {
					Location: path,
				},
			});
		},
		isPrerendered: false,
		get preferredLocale(): string | undefined {
			return (preferredLocale ??= computePreferredLocale(request, userDefinedLocales));
		},
		get preferredLocaleList(): string[] | undefined {
			return (preferredLocaleList ??= computePreferredLocaleList(request, userDefinedLocales));
		},
		get currentLocale(): string | undefined {
			return (currentLocale ??= computeCurrentLocale(route, userDefinedLocales, defaultLocale));
		},
		url,
		get originPathname() {
			return getOriginPathname(request);
		},
		get clientAddress() {
			if (clientIpAddress) {
				return clientIpAddress;
			}
			clientIpAddress = getClientIpAddress(request);
			if (!clientIpAddress) {
				throw new AstroError(AstroErrorData.StaticClientAddressNotAvailable);
			}
			return clientIpAddress;
		},
		get locals() {
			if (typeof locals !== 'object') {
				throw new AstroError(AstroErrorData.LocalsNotAnObject);
			}
			return locals;
		},
		set locals(_) {
			throw new AstroError(AstroErrorData.LocalsReassigned);
		},
		session: undefined,
		csp: undefined,
	};
	return Object.assign(context, {
		getActionResult: createGetActionResult(context.locals),
		callAction: createCallAction(context),
	});
}

Domain

Subdomains

Frequently Asked Questions

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

Analyze Your Own Codebase

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

Try Supermodel Free