Skip to content

Commit

Permalink
Fix: allow locals access from actions (#11006)
Browse files Browse the repository at this point in the history
* fix: move actions middleware to post

* fix(test): user middleware

* chore: changeset
  • Loading branch information
bholmesdev authored May 13, 2024
1 parent cb2586f commit 7418bb0
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/popular-spiders-cough.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"astro": patch
---

Fix `locals` access from action handlers
2 changes: 1 addition & 1 deletion packages/astro/src/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export default function astroActions(): AstroIntegration {

params.addMiddleware({
entrypoint: 'astro/actions/runtime/middleware.js',
order: 'pre',
order: 'post',
});

await typegen({
Expand Down
16 changes: 16 additions & 0 deletions packages/astro/test/actions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import assert from 'node:assert/strict';
import { after, before, describe, it } from 'node:test';
import testAdapter from './test-adapter.js';
import { loadFixture } from './test-utils.js';
import * as cheerio from 'cheerio';

describe('Astro Actions', () => {
let fixture;
Expand Down Expand Up @@ -169,5 +170,20 @@ describe('Astro Actions', () => {
assert.equal(json.success, true);
assert.equal(json.isFormData, true, 'Should receive plain FormData');
});

it('Respects user middleware', async () => {
const formData = new FormData();
formData.append('_astroAction', '/_actions/getUser');
const req = new Request('http://example.com/middleware', {
method: 'POST',
body: formData,
});
const res = await app.render(req);
assert.equal(res.ok, true);

const html = await res.text();
let $ = cheerio.load(html);
assert.equal($('#user').text(), 'Houston');
})
});
});
9 changes: 8 additions & 1 deletion packages/astro/test/fixtures/actions/src/actions/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defineAction, z } from 'astro:actions';
import { defineAction, getApiContext, z } from 'astro:actions';

export const server = {
subscribe: defineAction({
Expand Down Expand Up @@ -29,4 +29,11 @@ export const server = {
};
},
}),
getUser: defineAction({
accept: 'form',
handler: async () => {
const { locals } = getApiContext();
return locals.user;
}
})
};
8 changes: 8 additions & 0 deletions packages/astro/test/fixtures/actions/src/middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { defineMiddleware } from 'astro:middleware';

export const onRequest = defineMiddleware((ctx, next) => {
ctx.locals.user = {
name: 'Houston',
};
return next();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
import { actions } from 'astro:actions';
const res = Astro.getActionResult(actions.getUser);
---

<h1 id="user">{res?.data?.name}</h1>

0 comments on commit 7418bb0

Please sign in to comment.