middleware.ts — astro Source File
Architecture documentation for middleware.ts, a typescript file in the astro codebase. 2 imports, 0 dependents.
Entity Profile
Dependency Diagram
graph LR 947df197_61f5_6433_3704_d6055d393fa8["middleware.ts"] 3a7a7ff7_6c64_95f9_af85_4deb518834f1["astro:middleware"] 947df197_61f5_6433_3704_d6055d393fa8 --> 3a7a7ff7_6c64_95f9_af85_4deb518834f1 58a2600d_f5df_9651_e0d8_9010ddeef24d["astro:actions"] 947df197_61f5_6433_3704_d6055d393fa8 --> 58a2600d_f5df_9651_e0d8_9010ddeef24d style 947df197_61f5_6433_3704_d6055d393fa8 fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
import { defineMiddleware, sequence } from 'astro:middleware';
import { getActionContext } from 'astro:actions';
const actionCookieForwarding = defineMiddleware(async (ctx, next) => {
if (ctx.isPrerendered) return next();
const { action, setActionResult, serializeActionResult } = getActionContext(ctx);
const payload = ctx.cookies.get('ACTION_PAYLOAD');
if (payload) {
const { actionName, actionResult } = payload.json();
setActionResult(actionName, actionResult);
ctx.cookies.delete('ACTION_PAYLOAD');
return next();
}
if (
action?.calledFrom === 'rpc' &&
action.name === 'locked' &&
!ctx.cookies.has('actionCookie')
) {
return new Response('Unauthorized', { status: 401 });
}
if (action?.calledFrom === 'form' && ctx.url.searchParams.has('actionCookieForwarding')) {
const actionResult = await action.handler();
ctx.cookies.set('ACTION_PAYLOAD', {
actionName: action.name,
actionResult: serializeActionResult(actionResult),
});
if (actionResult.error) {
const referer = ctx.request.headers.get('Referer');
if (!referer) {
throw new Error('Internal: Referer unexpectedly missing from Action POST request.');
}
return ctx.redirect(referer);
}
return ctx.redirect(ctx.originPathname);
}
return next();
});
export const onRequest = sequence(
defineMiddleware((ctx, next) => {
ctx.locals.user = {
name: 'Houston',
};
return next();
}),
actionCookieForwarding,
);
Domain
Subdomains
Functions
Dependencies
- astro:actions
- astro:middleware
Source
Frequently Asked Questions
What does middleware.ts do?
middleware.ts is a source file in the astro codebase, written in typescript. It belongs to the IntegrationAdapters domain, ReactIntegration subdomain.
What functions are defined in middleware.ts?
middleware.ts defines 2 function(s): actionCookieForwarding, onRequest.
What does middleware.ts depend on?
middleware.ts imports 2 module(s): astro:actions, astro:middleware.
Where is middleware.ts in the architecture?
middleware.ts is located at packages/astro/test/fixtures/actions/src/middleware.ts (domain: IntegrationAdapters, subdomain: ReactIntegration, directory: packages/astro/test/fixtures/actions/src).
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free