-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new fields to API route context (#4986)
* Add new fields to API route context * Add props and redirect * Pass custom redirect status code * Correctly pass props in api routes * Add better docs * Add test * Fix build * Add constants file * Add links to jsdoc * Workaround lint issue * Thanks Chris Co-authored-by: Chris Swithinbank <swithinbank@gmail.com> * Missed one doc change Co-authored-by: Chris Swithinbank <swithinbank@gmail.com> * Add more detail to the changesets * Strict redirect status type Co-authored-by: Chris Swithinbank <swithinbank@gmail.com> Co-authored-by: Matthew Phillips <matthew@skypack.dev>
- Loading branch information
1 parent
07d16ff
commit ebd364e
Showing
17 changed files
with
257 additions
and
37 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,19 @@ | ||
--- | ||
'astro': minor | ||
--- | ||
|
||
## New properties for API routes | ||
|
||
In API routes, you can now get the `site`, `generator`, `url`, `clientAddress`, `props`, and `redirect` fields on the APIContext, which is the first parameter passed to an API route. This was done to make the APIContext more closely align with the `Astro` global in .astro pages. | ||
|
||
For example, here's how you might use the `clientAddress`, which is the user's IP address, to selectively allow users. | ||
|
||
```js | ||
export function post({ clientAddress, request, redirect }) { | ||
if(!allowList.has(clientAddress)) { | ||
return redirect('/not-allowed'); | ||
} | ||
} | ||
``` | ||
|
||
Check out the docs for more information on the newly available fields: https://docs.astro.build/en/core-concepts/endpoints/#server-endpoints-api-routes |
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,14 @@ | ||
--- | ||
'astro': minor | ||
--- | ||
|
||
## Support passing a custom status code for Astro.redirect | ||
|
||
New in this minor is the ability to pass a status code to `Astro.redirect`. By default it uses `302` but now you can pass another code as the second argument: | ||
|
||
```astro | ||
--- | ||
// This page was moved | ||
return Astro.redirect('/posts/new-post-name', 301); | ||
--- | ||
``` |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// process.env.PACKAGE_VERSION is injected when we build and publish the astro package. | ||
export const ASTRO_VERSION = process.env.PACKAGE_VERSION ?? 'development'; |
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
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
Oops, something went wrong.