astro/.changeset/fifty-ads-march.md
Bjorn Lu ebd364e392
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>
2022-10-12 17:03:49 -04:00

764 B

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.

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