forked from withastro/astro
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(actions): getActionPath() (withastro#12721)
* feat(actions): getActionPath() * feat: take trailing slash into account * fix * fix * Update wise-boxes-develop.md * Apply suggestions from code review Co-authored-by: Sarah Rainsberger <[email protected]> * Update .changeset/wise-boxes-develop.md Co-authored-by: Sarah Rainsberger <[email protected]> --------- Co-authored-by: Sarah Rainsberger <[email protected]> Co-authored-by: Emanuele Stoppa <[email protected]>
- Loading branch information
1 parent
36c1e06
commit c9d5110
Showing
6 changed files
with
101 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: | ||
|
||
```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 | ||
}) | ||
</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> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
packages/astro/test/fixtures/actions/src/pages/get-action-path.astro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |