Skip to content

Commit 0f06255

Browse files
ematipicobluwyyanthomasdevdelucis
committed
feat: upper case the name of the endpoints (#7783)
Co-authored-by: Bjorn Lu <bjornlu.dev@gmail.com> Co-authored-by: Yan Thomas <61414485+Yan-Thomas@users.noreply.github.com> Co-authored-by: Chris Swithinbank <swithinbank@gmail.com>
1 parent 42fc57e commit 0f06255

File tree

45 files changed

+116
-53
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+116
-53
lines changed

.changeset/twelve-coats-rush.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
---
2+
'astro': major
3+
---
4+
5+
Lowercase names for endpoint functions are now deprecated.
6+
7+
Rename functions to their uppercase equivalent:
8+
9+
```diff
10+
- export function get() {
11+
+ export function GET() {
12+
return new Response(JSON.stringify({ "title": "Bob's blog" }));
13+
}
14+
15+
- export function post() {
16+
+ export function POST() {
17+
return new Response(JSON.stringify({ "title": "Bob's blog" }));
18+
}
19+
20+
- export function put() {
21+
+ export function PUT() {
22+
return new Response(JSON.stringify({ "title": "Bob's blog" }));
23+
}
24+
25+
- export function all() {
26+
+ export function ALL() {
27+
return new Response(JSON.stringify({ "title": "Bob's blog" }));
28+
}
29+
30+
// you can use the whole word "DELETE"
31+
- export function del() {
32+
+ export function DELETE() {
33+
return new Response(JSON.stringify({ "title": "Bob's blog" }));
34+
}
35+
```

packages/astro/src/assets/image-endpoint.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ async function loadRemoteImage(src: URL) {
2424
/**
2525
* Endpoint used in dev and SSR to serve optimized images by the base image services
2626
*/
27-
export const get: APIRoute = async ({ request }) => {
27+
export const GET: APIRoute = async ({ request }) => {
2828
try {
2929
const imageService = await getConfiguredImageService();
3030

packages/astro/src/core/endpoint/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ import type {
77
Params,
88
} from '../../@types/astro';
99
import type { Environment, RenderContext } from '../render/index';
10-
1110
import { renderEndpoint } from '../../runtime/server/index.js';
1211
import { ASTRO_VERSION } from '../constants.js';
1312
import { AstroCookies, attachToResponse } from '../cookies/index.js';
1413
import { AstroError, AstroErrorData } from '../errors/index.js';
1514
import { warn } from '../logger/core.js';
1615
import { callMiddleware } from '../middleware/callMiddleware.js';
16+
1717
const clientAddressSymbol = Symbol.for('astro.clientAddress');
1818
const clientLocalsSymbol = Symbol.for('astro.locals');
1919

@@ -119,11 +119,11 @@ export async function callEndpoint<MiddlewareResult = Response | EndpointOutput>
119119
onRequest as MiddlewareEndpointHandler,
120120
context,
121121
async () => {
122-
return await renderEndpoint(mod, context, env.ssr);
122+
return await renderEndpoint(mod, context, env.ssr, env.logging);
123123
}
124124
);
125125
} else {
126-
response = await renderEndpoint(mod, context, env.ssr);
126+
response = await renderEndpoint(mod, context, env.ssr, env.logging);
127127
}
128128

129129
if (response instanceof Response) {

packages/astro/src/runtime/server/endpoint.ts

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,56 @@
11
import type { APIContext, EndpointHandler, Params } from '../../@types/astro';
2+
import { type LogOptions, warn } from '../../core/logger/core.js';
23

3-
function getHandlerFromModule(mod: EndpointHandler, method: string) {
4+
function getHandlerFromModule(mod: EndpointHandler, method: string, logging: LogOptions) {
5+
const lowerCaseMethod = method.toLowerCase();
6+
7+
// TODO: remove in Astro 4.0
8+
if (mod[lowerCaseMethod]) {
9+
warn(
10+
logging,
11+
'astro',
12+
`Lower case endpoint names are deprecated and will not be supported in Astro 4.0. Rename the endpoint ${lowerCaseMethod} to ${method}.`
13+
);
14+
}
415
// If there was an exact match on `method`, return that function.
516
if (mod[method]) {
617
return mod[method];
718
}
19+
20+
// TODO: remove in Astro 4.0
21+
if (mod[lowerCaseMethod]) {
22+
return mod[lowerCaseMethod];
23+
}
24+
// TODO: remove in Astro 4.0
825
// Handle `del` instead of `delete`, since `delete` is a reserved word in JS.
926
if (method === 'delete' && mod['del']) {
1027
return mod['del'];
1128
}
29+
// TODO: remove in Astro 4.0
1230
// If a single `all` handler was used, return that function.
1331
if (mod['all']) {
1432
return mod['all'];
1533
}
34+
if (mod['ALL']) {
35+
return mod['ALL'];
36+
}
1637
// Otherwise, no handler found.
1738
return undefined;
1839
}
1940

2041
/** Renders an endpoint request to completion, returning the body. */
21-
export async function renderEndpoint(mod: EndpointHandler, context: APIContext, ssr: boolean) {
42+
export async function renderEndpoint(
43+
mod: EndpointHandler,
44+
context: APIContext,
45+
ssr: boolean,
46+
logging: LogOptions
47+
) {
2248
const { request, params } = context;
23-
const chosenMethod = request.method?.toLowerCase();
24-
const handler = getHandlerFromModule(mod, chosenMethod);
25-
if (!ssr && ssr === false && chosenMethod && chosenMethod !== 'get') {
49+
50+
const chosenMethod = request.method?.toUpperCase();
51+
const handler = getHandlerFromModule(mod, chosenMethod, logging);
52+
// TODO: remove the 'get' check in Astro 4.0
53+
if (!ssr && ssr === false && chosenMethod && chosenMethod !== 'GET' && chosenMethod !== 'get') {
2654
// eslint-disable-next-line no-console
2755
console.warn(`
2856
${chosenMethod} requests are not available when building a static site. Update your config to \`output: 'server'\` or \`output: 'hybrid'\` with an \`export const prerender = false\` to handle ${chosenMethod} requests.`);
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { APIRoute } from 'astro';
22

3-
export const get: APIRoute = async function () {
3+
export const GET: APIRoute = async function () {
44
return new Response(new Uint8Array([0xff]));
55
};

packages/astro/test/fixtures/api-routes/src/pages/context/data/[param].json.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export function getStaticPaths() {
1414
]
1515
}
1616

17-
export function get({ params, request }) {
17+
export function GET({ params, request }) {
1818
return {
1919
body: JSON.stringify({
2020
param: params.param,

packages/astro/test/fixtures/astro-cookies/src/pages/set-prefs.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
export function post({ cookies }) {
2+
export function POST({ cookies }) {
33
const mode = cookies.get('prefs').json().mode;
44

55
cookies.set('prefs', {

packages/astro/test/fixtures/astro-get-static-paths/src/pages/data/[slug].json.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export async function getStaticPaths() {
55
];
66
}
77

8-
export async function get() {
8+
export async function GET() {
99
return {
1010
body: JSON.stringify({
1111
title: '[slug]'

packages/astro/test/fixtures/astro-markdown-frontmatter-injection/src/pages/glob.json.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export async function get() {
1+
export async function GET() {
22
const docs = await import.meta.glob('./*.md', { eager: true });
33
return {
44
body: JSON.stringify(Object.values(docs).map(doc => doc.frontmatter)),

packages/astro/test/fixtures/astro-markdown/src/pages/headings-glob.json.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { getHeadings } from './with-layout.md';
22

3-
export async function get() {
3+
export async function GET() {
44
return {
55
body: JSON.stringify({
66
headings: getHeadings(),

0 commit comments

Comments
 (0)