mirror of
https://github.com/withastro/astro.git
synced 2025-02-10 22:38:53 -05:00
refactor: actionsinternal -> actionpayload
This commit is contained in:
parent
ffe80acb2e
commit
40cbc3da43
3 changed files with 16 additions and 16 deletions
|
@ -12,13 +12,13 @@ import {
|
|||
} from './virtual/shared.js';
|
||||
import { ACTION_QUERY_PARAMS } from '../consts.js';
|
||||
|
||||
type ActionPayload = {
|
||||
export type ActionPayload = {
|
||||
actionResult: SerializedActionResult;
|
||||
actionName: string;
|
||||
};
|
||||
|
||||
export type Locals = {
|
||||
_actionsInternal: ActionPayload;
|
||||
_actionPayload: ActionPayload;
|
||||
};
|
||||
|
||||
export const onRequest = defineMiddleware(async (context, next) => {
|
||||
|
@ -26,9 +26,9 @@ export const onRequest = defineMiddleware(async (context, next) => {
|
|||
const { request } = context;
|
||||
// Actions middleware may have run already after a path rewrite.
|
||||
// See https://github.com/withastro/roadmap/blob/feat/reroute/proposals/0047-rerouting.md#ctxrewrite
|
||||
// `_actionsInternal` is the same for every page,
|
||||
// `_actionPayload` is the same for every page,
|
||||
// so short circuit if already defined.
|
||||
if (locals._actionsInternal) return next();
|
||||
if (locals._actionPayload) return next();
|
||||
|
||||
const actionPayload = context.cookies.get(ACTION_QUERY_PARAMS.actionPayload)?.json();
|
||||
if (actionPayload) {
|
||||
|
@ -74,14 +74,14 @@ async function renderResult({
|
|||
}) {
|
||||
const locals = context.locals as Locals;
|
||||
|
||||
locals._actionsInternal = { actionResult, actionName };
|
||||
locals._actionPayload = { actionResult, actionName };
|
||||
const response = await next();
|
||||
context.cookies.delete(ACTION_QUERY_PARAMS.actionPayload);
|
||||
|
||||
if (locals._actionsInternal.actionResult.type === 'error') {
|
||||
if (locals._actionPayload.actionResult.type === 'error') {
|
||||
return new Response(response.body, {
|
||||
status: locals._actionsInternal.actionResult.status,
|
||||
statusText: locals._actionsInternal.actionResult.type,
|
||||
status: locals._actionPayload.actionResult.status,
|
||||
statusText: locals._actionPayload.actionResult.type,
|
||||
headers: response.headers,
|
||||
});
|
||||
}
|
||||
|
|
|
@ -3,19 +3,19 @@ import type { Locals } from './runtime/middleware.js';
|
|||
import { type ActionAPIContext } from './runtime/utils.js';
|
||||
import { deserializeActionResult, getActionQueryString } from './runtime/virtual/shared.js';
|
||||
|
||||
export function hasActionsInternal(locals: APIContext['locals']): locals is Locals {
|
||||
return '_actionsInternal' in locals;
|
||||
export function hasActionPayload(locals: APIContext['locals']): locals is Locals {
|
||||
return '_actionPayload' in locals;
|
||||
}
|
||||
|
||||
export function createGetActionResult(locals: APIContext['locals']): APIContext['getActionResult'] {
|
||||
return (actionFn): any => {
|
||||
if (
|
||||
!hasActionsInternal(locals) ||
|
||||
actionFn.toString() !== getActionQueryString(locals._actionsInternal.actionName)
|
||||
!hasActionPayload(locals) ||
|
||||
actionFn.toString() !== getActionQueryString(locals._actionPayload.actionName)
|
||||
) {
|
||||
return undefined;
|
||||
}
|
||||
return deserializeActionResult(locals._actionsInternal.actionResult);
|
||||
return deserializeActionResult(locals._actionPayload.actionResult);
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
@ -11,7 +11,7 @@ import type {
|
|||
} from '../@types/astro.js';
|
||||
import type { ActionAPIContext } from '../actions/runtime/utils.js';
|
||||
import { deserializeActionResult } from '../actions/runtime/virtual/shared.js';
|
||||
import { createCallAction, createGetActionResult, hasActionsInternal } from '../actions/utils.js';
|
||||
import { createCallAction, createGetActionResult, hasActionPayload } from '../actions/utils.js';
|
||||
import {
|
||||
computeCurrentLocale,
|
||||
computePreferredLocale,
|
||||
|
@ -314,8 +314,8 @@ export class RenderContext {
|
|||
},
|
||||
} satisfies AstroGlobal['response'];
|
||||
|
||||
const actionResult = hasActionsInternal(this.locals)
|
||||
? deserializeActionResult(this.locals._actionsInternal.actionResult)
|
||||
const actionResult = hasActionPayload(this.locals)
|
||||
? deserializeActionResult(this.locals._actionPayload.actionResult)
|
||||
: undefined;
|
||||
|
||||
// Create the result object that will be passed into the renderPage function.
|
||||
|
|
Loading…
Add table
Reference in a new issue