0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-16 21:46:22 -05:00

fix(actions): internal symbol check (#12424)

This commit is contained in:
Emanuele Stoppa 2024-11-14 12:41:16 +00:00 committed by GitHub
parent b745e382f1
commit 4364bff273
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 13 additions and 3 deletions

View file

@ -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

View file

@ -4,11 +4,13 @@ import { AstroError } from '../../../core/errors/errors.js';
import type { APIContext } from '../../../types/public/index.js'; import type { APIContext } from '../../../types/public/index.js';
import { ACTION_RPC_ROUTE_PATTERN } from '../../consts.js'; import { ACTION_RPC_ROUTE_PATTERN } from '../../consts.js';
import { import {
ACTION_API_CONTEXT_SYMBOL,
type ActionAPIContext, type ActionAPIContext,
type ErrorInferenceObject, type ErrorInferenceObject,
type MaybePromise, type MaybePromise,
formContentTypes, formContentTypes,
hasContentType, hasContentType,
isActionAPIContext,
} from '../utils.js'; } from '../utils.js';
import type { Locals } from '../utils.js'; import type { Locals } from '../utils.js';
import { getAction } from './get-action.js'; import { getAction } from './get-action.js';
@ -79,7 +81,8 @@ export function defineAction<
: getJsonServerHandler(handler, inputSchema); : getJsonServerHandler(handler, inputSchema);
async function safeServerHandler(this: ActionAPIContext, unparsedInput: unknown) { 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); throw new AstroError(ActionCalledFromServerError);
} }
return callSafely(() => serverHandler(unparsedInput, this)); return callSafely(() => serverHandler(unparsedInput, this));
@ -293,6 +296,7 @@ export function getActionContext(context: APIContext): ActionMiddlewareContext {
redirect: _redirect, redirect: _redirect,
...actionAPIContext ...actionAPIContext
} = context; } = context;
Reflect.set(actionAPIContext, ACTION_API_CONTEXT_SYMBOL, true);
const handler = baseAction.bind(actionAPIContext satisfies ActionAPIContext); const handler = baseAction.bind(actionAPIContext satisfies ActionAPIContext);
return handler(input); return handler(input);
}, },

View file

@ -1,7 +1,7 @@
import type fsMod from 'node:fs'; import type fsMod from 'node:fs';
import * as eslexer from 'es-module-lexer'; import * as eslexer from 'es-module-lexer';
import type { APIContext } from '../types/public/context.js'; 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'; import { deserializeActionResult, getActionQueryString } from './runtime/virtual/shared.js';
export function hasActionPayload(locals: APIContext['locals']): locals is Locals { 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'] { export function createCallAction(context: ActionAPIContext): APIContext['callAction'] {
return (baseAction, input) => { return (baseAction, input) => {
Reflect.set(context, ACTION_API_CONTEXT_SYMBOL, true);
const action = baseAction.bind(context); const action = baseAction.bind(context);
return action(input) as any; return action(input) as any;
}; };

View file

@ -2,5 +2,5 @@
import { actions } from "astro:actions"; import { actions } from "astro:actions";
// this is invalid, it should fail // this is invalid, it should fail
const result = await actions.imageUploadInChunks(); const result = await actions.subscribe({ channel: "hey" });
--- ---