0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-02-03 22:29:08 -05:00

feat: take trailing slash into account

This commit is contained in:
Florian Lefebvre 2024-12-12 14:49:59 +01:00
parent 89b340ad73
commit b79a6b0505
3 changed files with 15 additions and 18 deletions

View file

@ -10,7 +10,6 @@ import type {
MaybePromise,
ActionAPIContext as _ActionAPIContext,
} from '../utils.js';
import type { ActionClient } from './server.js';
export type ActionAPIContext = _ActionAPIContext;
export const ACTION_QUERY_PARAMS = _ACTION_QUERY_PARAMS;
@ -184,10 +183,6 @@ export function getActionQueryString(name: string) {
return `?${searchParams.toString()}`;
}
export function getActionPath(action: ActionClient<any, any, any>) {
return `${import.meta.env.BASE_URL.replace(/\/$/, '')}/_actions/${new URLSearchParams(action.toString()).get(ACTION_QUERY_PARAMS.actionName)}`;
}
export type SerializedActionResult =
| {
type: 'data';

View file

@ -3,7 +3,6 @@ import {
deserializeActionResult,
getActionQueryString,
appendForwardSlash,
getActionPath,
} from 'astro:actions';
const ENCODED_DOT = '%2E';
@ -53,6 +52,17 @@ function toActionProxy(actionCallback = {}, aggregatedPath = '') {
});
}
const SHOULD_APPEND_TRAILING_SLASH = '/** @TRAILING_SLASH@ **/';
/** @param {import('astro:actions').ActionClient<any, any, any>} */
export function getActionPath(action) {
let path = `${import.meta.env.BASE_URL.replace(/\/$/, '')}/_actions/${new URLSearchParams(action.toString()).get(ACTION_QUERY_PARAMS.actionName)}`;
if (SHOULD_APPEND_TRAILING_SLASH) {
path = appendForwardSlash(path);
}
return path;
}
/**
* @param {*} param argument passed to the action when called server or client-side.
* @param {string} path Built path to call action by path name.
@ -102,18 +112,6 @@ async function handleAction(param, path, context) {
},
);
const shouldAppendTrailingSlash = '/** @TRAILING_SLASH@ **/';
let actionPath = import.meta.env.BASE_URL.replace(/\/$/, '') + '/_actions/' + path;
if (shouldAppendTrailingSlash) {
actionPath = appendForwardSlash(actionPath);
}
const rawResult = await fetch(actionPath, {
method: 'POST',
body,
headers,
});
if (rawResult.status === 204) {
return deserializeActionResult({ type: 'empty', status: 204 });
}

View file

@ -1,3 +1,7 @@
declare module 'astro:actions' {
export * from 'astro/actions/runtime/virtual/server.js';
export function getActionPath(
action: import('astro/actions/runtime/virtual/server.js').ActionClient<any, any, any>,
): string;
}