Skip to content

Commit f88e414

Browse files
authored
Merge branch 'v3-upgrade-guide' into astro-namespace
2 parents 289021e + 6a32051 commit f88e414

23 files changed

Lines changed: 674 additions & 474 deletions

File tree

src/content/docs/en/core-concepts/endpoints.mdx

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Endpoints export a `get` function (optionally `async`) that receives a [context
1717
```ts
1818
// Example: src/pages/builtwith.json.ts
1919
// Outputs: /builtwith.json
20-
export async function get({params, request}) {
20+
export async function GET({params, request}) {
2121
return {
2222
body: JSON.stringify({
2323
name: 'Astro',
@@ -30,7 +30,7 @@ export async function get({params, request}) {
3030
The return object can also have an `encoding` property. It can be any valid [`BufferEncoding`](https://github.com/DefinitelyTyped/DefinitelyTyped/blob/bdd02508ddb5eebcf701fdb8ffd6e84eabf47885/types/node/buffer.d.ts#L169) accepted by Node.js' `fs.writeFile` method. For example, to produce a binary png image:
3131

3232
```ts title="src/pages/astro-logo.png.ts" {6}
33-
export async function get({ params, request }) {
33+
export async function GET({ params, request }) {
3434
const response = await fetch("https://docs.astro.build/assets/full-logo-light.png");
3535
const buffer = Buffer.from(await response.arrayBuffer());
3636
return {
@@ -45,7 +45,7 @@ You can also type your endpoint functions using the `APIRoute` type:
4545
```ts
4646
import type { APIRoute } from 'astro';
4747

48-
export const get: APIRoute = async ({ params, request }) => {
48+
export const GET: APIRoute = async ({ params, request }) => {
4949
...
5050
```
5151
@@ -58,7 +58,7 @@ import type { APIRoute } from 'astro';
5858

5959
const usernames = ["Sarah", "Chris", "Dan"]
6060

61-
export const get: APIRoute = ({ params, request }) => {
61+
export const GET: APIRoute = ({ params, request }) => {
6262
const id = params.id;
6363
return {
6464
body: JSON.stringify({
@@ -84,7 +84,7 @@ All endpoints receive a `request` property, but in static mode, you only have ac
8484
```ts title="src/pages/request-path.json.ts"
8585
import type { APIRoute } from 'astro';
8686

87-
export const get: APIRoute = ({ params, request }) => {
87+
export const GET: APIRoute = ({ params, request }) => {
8888
return {
8989
body: JSON.stringify({
9090
path: new URL(request.url).pathname
@@ -109,7 +109,7 @@ Server endpoints can access `params` without exporting `getStaticPaths`, and the
109109
```js title="src/pages/[id].json.js"
110110
import { getProduct } from '../db';
111111

112-
export async function get({ params }) {
112+
export async function GET({ params }) {
113113
const id = params.id;
114114
const product = await getProduct(id);
115115

@@ -134,7 +134,7 @@ This will respond to any request that matches the dynamic route. For example, if
134134
In SSR mode, certain providers require the `Content-Type` header to return an image. In this case, use a `Response` object to specify a `headers` property. For example, to produce a binary `.png` image:
135135
136136
```ts title="src/pages/astro-logo.png.ts"
137-
export async function get({ params, request }) {
137+
export async function GET({ params, request }) {
138138
const response = await fetch("https://docs.astro.build/assets/full-logo-light.png");
139139
const buffer = Buffer.from(await response.arrayBuffer());
140140
return new Response(buffer, {
@@ -144,40 +144,36 @@ export async function get({ params, request }) {
144144
```
145145
146146
### HTTP methods
147-
In addition to the `get` function, you can export a function with the name of any [HTTP method](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods). When a request comes in, Astro will check the method and call the corresponding function.
147+
In addition to the `GET` function, you can export a function with the name of any [HTTP method](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods). When a request comes in, Astro will check the method and call the corresponding function.
148148
149-
You can also export an `all` function to match any method that doesn't have a corresponding exported function. If there is a request with no matching method, it will redirect to your site's [404 page](/en/core-concepts/astro-pages/#custom-404-error-page).
150-
151-
:::note
152-
Since `delete` is a reserved word in JavaScript, export a `del` function to match the delete method.
153-
:::
149+
You can also export an `ALL` function to match any method that doesn't have a corresponding exported function. If there is a request with no matching method, it will redirect to your site's [404 page](/en/core-concepts/astro-pages/#custom-404-error-page).
154150
155151
```ts title="src/pages/methods.json.ts"
156-
export const get: APIRoute = ({ params, request }) => {
152+
export const GET: APIRoute = ({ params, request }) => {
157153
return {
158154
body: JSON.stringify({
159155
message: "This was a GET!"
160156
})
161157
}
162158
};
163159

164-
export const post: APIRoute = ({ request }) => {
160+
export const POST: APIRoute = ({ request }) => {
165161
return {
166162
body: JSON.stringify({
167163
message: "This was a POST!"
168164
})
169165
}
170166
}
171167

172-
export const del: APIRoute = ({ request }) => {
168+
export const DELETE: APIRoute = ({ request }) => {
173169
return {
174170
body: JSON.stringify({
175171
message: "This was a DELETE!"
176172
})
177173
}
178174
}
179175

180-
export const all: APIRoute = ({ request }) => {
176+
export const ALL: APIRoute = ({ request }) => {
181177
return {
182178
body: JSON.stringify({
183179
message: `This was a ${request.method}!`
@@ -192,7 +188,7 @@ export const all: APIRoute = ({ request }) => {
192188
In SSR mode, the `request` property returns a fully usable [`Request`](https://developer.mozilla.org/en-US/docs/Web/API/Request) object that refers to the current request. This allows you to accept data and check headers:
193189
194190
```ts title="src/pages/test-post.json.ts"
195-
export const post: APIRoute = async ({ request }) => {
191+
export const POST: APIRoute = async ({ request }) => {
196192
if (request.headers.get("Content-Type") === "application/json") {
197193
const body = await request.json();
198194
const name = body.name;
@@ -212,7 +208,7 @@ The endpoint context exports a `redirect()` utility similar to `Astro.redirect`:
212208
```js title="src/pages/links/[id].js" {14}
213209
import { getLinkUrl } from '../db';
214210

215-
export async function get({ params, redirect }) {
211+
export async function GET({ params, redirect }) {
216212
const { id } = params;
217213
const link = await getLinkUrl(id);
218214

0 commit comments

Comments
 (0)