Skip to content

Commit

Permalink
Merge branch 'main' into feat/get-action-path
Browse files Browse the repository at this point in the history
  • Loading branch information
florian-lefebvre committed Dec 12, 2024
2 parents 4fa0dc6 + 358eae8 commit 89b340a
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/hip-kids-ring.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/db': patch
---

Fixes the publishing of the package
5 changes: 5 additions & 0 deletions .changeset/spicy-guests-protect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Trailing slash support for actions
7 changes: 7 additions & 0 deletions packages/astro/src/actions/plugins.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type fsMod from 'node:fs';
import type { Plugin as VitePlugin } from 'vite';
import { shouldAppendForwardSlash } from '../core/build/util.js';
import type { AstroSettings } from '../types/astro.js';
import {
NOOP_ACTIONS,
Expand Down Expand Up @@ -84,6 +85,12 @@ export function vitePluginActions({
code += `\nexport * from 'astro/actions/runtime/virtual/server.js';`;
} else {
code += `\nexport * from 'astro/actions/runtime/virtual/client.js';`;
code = code.replace(
"'/** @TRAILING_SLASH@ **/'",
JSON.stringify(
shouldAppendForwardSlash(settings.config.trailingSlash, settings.config.build.format),
),
);
}
return code;
},
Expand Down
14 changes: 13 additions & 1 deletion packages/astro/src/actions/runtime/virtual/server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { z } from 'zod';
import type { Pipeline } from '../../../core/base-pipeline.js';
import { shouldAppendForwardSlash } from '../../../core/build/util.js';
import { ActionCalledFromServerError } from '../../../core/errors/errors-data.js';
import { AstroError } from '../../../core/errors/errors.js';
import { removeTrailingForwardSlash } from '../../../core/path.js';
import { apiContextRoutesSymbol } from '../../../core/render-context.js';
import type { APIContext } from '../../../types/public/index.js';
import { ACTION_RPC_ROUTE_PATTERN } from '../../consts.js';
import {
Expand Down Expand Up @@ -279,7 +283,15 @@ export function getActionContext(context: APIContext): ActionMiddlewareContext {
calledFrom: callerInfo.from,
name: callerInfo.name,
handler: async () => {
const baseAction = await getAction(callerInfo.name);
const pipeline: Pipeline = Reflect.get(context, apiContextRoutesSymbol);
const callerInfoName = shouldAppendForwardSlash(
pipeline.manifest.trailingSlash,
pipeline.manifest.buildFormat,
)
? removeTrailingForwardSlash(callerInfo.name)
: callerInfo.name;

const baseAction = await getAction(callerInfoName);
let input;
try {
input = await parseRequestBody(context.request);
Expand Down
3 changes: 3 additions & 0 deletions packages/astro/src/actions/runtime/virtual/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { z } from 'zod';
import { REDIRECT_STATUS_CODES } from '../../../core/constants.js';
import { ActionsReturnedInvalidDataError } from '../../../core/errors/errors-data.js';
import { AstroError } from '../../../core/errors/errors.js';
import { appendForwardSlash as _appendForwardSlash } from '../../../core/path.js';
import { ACTION_QUERY_PARAMS as _ACTION_QUERY_PARAMS } from '../../consts.js';
import type {
ErrorInferenceObject,
Expand All @@ -14,6 +15,8 @@ import type { ActionClient } from './server.js';
export type ActionAPIContext = _ActionAPIContext;
export const ACTION_QUERY_PARAMS = _ACTION_QUERY_PARAMS;

export const appendForwardSlash = _appendForwardSlash;

export const ACTION_ERROR_CODES = [
'BAD_REQUEST',
'UNAUTHORIZED',
Expand Down
14 changes: 14 additions & 0 deletions packages/astro/templates/actions.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
ActionError,
deserializeActionResult,
getActionQueryString,
appendForwardSlash,
getActionPath,
} from 'astro:actions';

Expand Down Expand Up @@ -100,6 +101,19 @@ async function handleAction(param, path, context) {
headers,
},
);

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 });
}
Expand Down
24 changes: 24 additions & 0 deletions packages/astro/test/actions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,30 @@ it('Base path should be used', async () => {
await devServer.stop();
});

it('Should support trailing slash', async () => {
const fixture = await loadFixture({
root: './fixtures/actions/',
adapter: testAdapter(),
trailingSlash: 'always',
});
const devServer = await fixture.startDevServer();
const formData = new FormData();
formData.append('channel', 'bholmesdev');
formData.append('comment', 'Hello, World!');
const res = await fixture.fetch('/_actions/comment/', {
method: 'POST',
body: formData,
});

assert.equal(res.ok, true);
assert.equal(res.headers.get('Content-Type'), 'application/json+devalue');

const data = devalue.parse(await res.text());
assert.equal(data.channel, 'bholmesdev');
assert.equal(data.comment, 'Hello, World!');
await devServer.stop();
});

it('getActionPath() should return the right path', async () => {
const fixture = await loadFixture({
root: './fixtures/actions/',
Expand Down
2 changes: 1 addition & 1 deletion packages/db/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@astrojs/db",
"version": "0.14.1",
"version": "0.14.3",
"description": "Add libSQL and Astro Studio support to your Astro site",
"license": "MIT",
"repository": {
Expand Down

0 comments on commit 89b340a

Please sign in to comment.