diff --git a/.changeset/brown-bulldogs-share.md b/.changeset/brown-bulldogs-share.md new file mode 100644 index 000000000000..c8cadb11b28c --- /dev/null +++ b/.changeset/brown-bulldogs-share.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fixes an issue where an incorrect usage of Astro actions was lost when porting the fix from v4 to v5 diff --git a/packages/astro/src/actions/runtime/virtual/server.ts b/packages/astro/src/actions/runtime/virtual/server.ts index f28b3c2393b9..47decf18e5d4 100644 --- a/packages/astro/src/actions/runtime/virtual/server.ts +++ b/packages/astro/src/actions/runtime/virtual/server.ts @@ -4,11 +4,13 @@ import { AstroError } from '../../../core/errors/errors.js'; import type { APIContext } from '../../../types/public/index.js'; import { ACTION_RPC_ROUTE_PATTERN } from '../../consts.js'; import { + ACTION_API_CONTEXT_SYMBOL, type ActionAPIContext, type ErrorInferenceObject, type MaybePromise, formContentTypes, hasContentType, + isActionAPIContext, } from '../utils.js'; import type { Locals } from '../utils.js'; import { getAction } from './get-action.js'; @@ -79,7 +81,8 @@ export function defineAction< : getJsonServerHandler(handler, inputSchema); async function safeServerHandler(this: ActionAPIContext, unparsedInput: unknown) { - if (typeof this === 'function') { + // The ActionAPIContext should always contain the `params` property + if (typeof this === 'function' || !isActionAPIContext(this)) { throw new AstroError(ActionCalledFromServerError); } return callSafely(() => serverHandler(unparsedInput, this)); @@ -293,6 +296,7 @@ export function getActionContext(context: APIContext): ActionMiddlewareContext { redirect: _redirect, ...actionAPIContext } = context; + Reflect.set(actionAPIContext, ACTION_API_CONTEXT_SYMBOL, true); const handler = baseAction.bind(actionAPIContext satisfies ActionAPIContext); return handler(input); }, diff --git a/packages/astro/src/actions/utils.ts b/packages/astro/src/actions/utils.ts index dc0fa4b1486e..b6fe63f6caca 100644 --- a/packages/astro/src/actions/utils.ts +++ b/packages/astro/src/actions/utils.ts @@ -1,7 +1,7 @@ import type fsMod from 'node:fs'; import * as eslexer from 'es-module-lexer'; import type { APIContext } from '../types/public/context.js'; -import type { ActionAPIContext, Locals } from './runtime/utils.js'; +import { ACTION_API_CONTEXT_SYMBOL, type ActionAPIContext, type Locals } from './runtime/utils.js'; import { deserializeActionResult, getActionQueryString } from './runtime/virtual/shared.js'; export function hasActionPayload(locals: APIContext['locals']): locals is Locals { @@ -22,6 +22,7 @@ export function createGetActionResult(locals: APIContext['locals']): APIContext[ export function createCallAction(context: ActionAPIContext): APIContext['callAction'] { return (baseAction, input) => { + Reflect.set(context, ACTION_API_CONTEXT_SYMBOL, true); const action = baseAction.bind(context); return action(input) as any; }; diff --git a/packages/astro/test/fixtures/actions/src/pages/invalid.astro b/packages/astro/test/fixtures/actions/src/pages/invalid.astro index 908eee853bd4..fe152dbba0a7 100644 --- a/packages/astro/test/fixtures/actions/src/pages/invalid.astro +++ b/packages/astro/test/fixtures/actions/src/pages/invalid.astro @@ -2,5 +2,5 @@ import { actions } from "astro:actions"; // this is invalid, it should fail -const result = await actions.imageUploadInChunks(); +const result = await actions.subscribe({ channel: "hey" }); ---