Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(actions): getActionPath() #12721

Merged
merged 9 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions .changeset/wise-boxes-develop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
---
'astro': minor
---

Adds a new `getActionPath()` helper available from `astro:actions`

Astro 5.1 introduces a new helper function, `getActionPath()` to give you more flexibility when calling your action.

Calling `getActionPath()` with your action returns its URL path so you can make a `fetch()` request with custom headers, or use your action with an API such as `navigator.sendBeacon()`. Then, you can [handle the custom-formatted returned data](https://docs.astro.build/en/guides/actions/#handling-returned-data) as needed, just as if you had called an action directly.

This example shows how to call a defined `like` action passing the `Authorization` header and the [`keepalive`](https://developer.mozilla.org/en-US/docs/Web/API/Request/keepalive) option:
sarah11918 marked this conversation as resolved.
Show resolved Hide resolved

```astro
<script>
// src/components/my-component.astro
import { actions, getActionPath } from 'astro:actions'

await fetch(getActionPath(actions.like), {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: 'Bearer YOUR_TOKEN'
},
body: JSON.stringify({ id: 'YOUR_ID' }),
keepalive: true
})
Comment on lines +18 to +26
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding this! I think it's definitely worth noting that actions do not return plain JSON. They return values that are parsable by the devalue library. I suggest using the deserializeActionResult() utility from astro:actions for this.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah good call thank you!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @bholmesdev -- is showing using deserializeActionResult() in this context concise? I realized we only have a short API reference sentence for this (no example usage), and no example using this anywhere in docs. (So, I'm not sure if this can be shown quickly or not.)

This would be a great addition to the actual docs (where I don't really care if it's concise or not), and if it's easy to do here, would also be a great place to put it!

</script>
```

This example shows how to call the same `like` action using the [`sendBeacon`](https://developer.mozilla.org/en-US/docs/Web/API/Navigator/sendBeacon) API:

```astro
<script>
// src/components/my-component.astro
import { actions, getActionPath } from 'astro:actions'

navigator.sendBeacon(
getActionPath(actions.like),
new Blob([JSON.stringify({ id: 'YOUR_ID' })], {
type: 'application/json'
})
)
</script>
```
12 changes: 6 additions & 6 deletions packages/astro/src/actions/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,13 @@ 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),
),
);
}
code = code.replace(
"'/** @TRAILING_SLASH@ **/'",
JSON.stringify(
shouldAppendForwardSlash(settings.config.trailingSlash, settings.config.build.format),
),
);
return code;
},
};
Expand Down
36 changes: 24 additions & 12 deletions packages/astro/templates/actions.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
ActionError,
ACTION_QUERY_PARAMS,
appendForwardSlash,
deserializeActionResult,
getActionQueryString,
Expand Down Expand Up @@ -52,6 +53,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.
Expand Down Expand Up @@ -88,19 +100,19 @@ async function handleAction(param, path, context) {
headers.set('Content-Length', '0');
}
}
const rawResult = await fetch(
getActionPath({
toString() {
return getActionQueryString(path);
},
}),
{
method: 'POST',
body,
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
17 changes: 17 additions & 0 deletions packages/astro/test/actions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,23 @@ it('Should support trailing slash', async () => {
await devServer.stop();
});

it('getActionPath() should return the right path', async () => {
const fixture = await loadFixture({
root: './fixtures/actions/',
adapter: testAdapter(),
base: '/base',
trailingSlash: 'always',
});
const devServer = await fixture.startDevServer();
const res = await fixture.fetch('/base/get-action-path/');

assert.equal(res.ok, true);
const html = await res.text();
let $ = cheerio.load(html);
assert.equal($('[data-path]').text(), '/base/_actions/transformFormInput/');
await devServer.stop();
});

/**
* Follow an expected redirect response.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
import { actions, getActionPath } from "astro:actions"

const path = getActionPath(actions.transformFormInput)
---
<p data-path>{path}</p>
4 changes: 4 additions & 0 deletions packages/astro/types/actions.d.ts
Original file line number Diff line number Diff line change
@@ -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;
}
Loading