Skip to content

Commit 9ce2ba6

Browse files
authored
fix: custom endpoints with method: 'put' (#9037)
### What? Fixes support for custom endpoints with `method: 'put'`. Previously, this didn't work: ```ts export default buildConfigWithDefaults({ collections: [ ], endpoints: [ { method: 'put', handler: () => new Response(), path: '/put', }, ], }) ``` ### Why? We supported this in 2.0 and docs are saying that we can use `'put'` as `method` https://payloadcms.com/docs/beta/rest-api/overview#custom-endpoints ### How? Implements the `REST_PUT` export for `@payloadcms/next/routes`, updates all templates. Additionally, adds tests to ensure root/collection level custom endpoints with all necessary methods execute properly. Fixes #8807 -->
1 parent f52b7c4 commit 9ce2ba6

File tree

13 files changed

+218
-9
lines changed

13 files changed

+218
-9
lines changed

packages/next/src/exports/routes.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ export {
66
OPTIONS as REST_OPTIONS,
77
PATCH as REST_PATCH,
88
POST as REST_POST,
9+
PUT as REST_PUT,
910
} from '../routes/rest/index.js'

packages/next/src/routes/rest/index.ts

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -821,3 +821,87 @@ export const PATCH =
821821
})
822822
}
823823
}
824+
825+
export const PUT =
826+
(config: Promise<SanitizedConfig> | SanitizedConfig) =>
827+
async (request: Request, { params: paramsPromise }: { params: Promise<{ slug: string[] }> }) => {
828+
const { slug } = await paramsPromise
829+
const [slug1] = slug
830+
let req: PayloadRequest
831+
let res: Response
832+
let collection: Collection
833+
834+
try {
835+
req = await createPayloadRequest({
836+
config,
837+
request,
838+
})
839+
collection = req.payload.collections?.[slug1]
840+
841+
const disableEndpoints = endpointsAreDisabled({
842+
endpoints: req.payload.config.endpoints,
843+
request,
844+
})
845+
if (disableEndpoints) {
846+
return disableEndpoints
847+
}
848+
849+
if (collection) {
850+
req.routeParams.collection = slug1
851+
852+
const disableEndpoints = endpointsAreDisabled({
853+
endpoints: collection.config.endpoints,
854+
request,
855+
})
856+
if (disableEndpoints) {
857+
return disableEndpoints
858+
}
859+
860+
const customEndpointResponse = await handleCustomEndpoints({
861+
endpoints: collection.config.endpoints,
862+
entitySlug: slug1,
863+
req,
864+
})
865+
866+
if (customEndpointResponse) {
867+
return customEndpointResponse
868+
}
869+
}
870+
871+
if (res instanceof Response) {
872+
if (req.responseHeaders) {
873+
const mergedResponse = new Response(res.body, {
874+
headers: mergeHeaders(req.responseHeaders, res.headers),
875+
status: res.status,
876+
statusText: res.statusText,
877+
})
878+
879+
return mergedResponse
880+
}
881+
882+
return res
883+
}
884+
885+
// root routes
886+
const customEndpointResponse = await handleCustomEndpoints({
887+
endpoints: req.payload.config.endpoints,
888+
req,
889+
})
890+
891+
if (customEndpointResponse) {
892+
return customEndpointResponse
893+
}
894+
895+
return RouteNotFoundResponse({
896+
slug,
897+
req,
898+
})
899+
} catch (error) {
900+
return routeError({
901+
collection,
902+
config,
903+
err: error,
904+
req: req || request,
905+
})
906+
}
907+
}
Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
/* THIS FILE WAS GENERATED AUTOMATICALLY BY PAYLOAD. */
22
/* DO NOT MODIFY IT BECAUSE IT COULD BE REWRITTEN AT ANY TIME. */
33
import config from '@payload-config'
4-
import { REST_DELETE, REST_GET, REST_OPTIONS, REST_PATCH, REST_POST } from '@payloadcms/next/routes'
4+
import {
5+
REST_DELETE,
6+
REST_GET,
7+
REST_OPTIONS,
8+
REST_PATCH,
9+
REST_POST,
10+
REST_PUT,
11+
} from '@payloadcms/next/routes'
512

613
export const GET = REST_GET(config)
714
export const POST = REST_POST(config)
815
export const DELETE = REST_DELETE(config)
916
export const PATCH = REST_PATCH(config)
17+
export const PUT = REST_PUT(config)
1018
export const OPTIONS = REST_OPTIONS(config)
Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
/* THIS FILE WAS GENERATED AUTOMATICALLY BY PAYLOAD. */
22
/* DO NOT MODIFY IT BECAUSE IT COULD BE REWRITTEN AT ANY TIME. */
33
import config from '@payload-config'
4-
import { REST_DELETE, REST_GET, REST_OPTIONS, REST_PATCH, REST_POST } from '@payloadcms/next/routes'
4+
import {
5+
REST_DELETE,
6+
REST_GET,
7+
REST_OPTIONS,
8+
REST_PATCH,
9+
REST_POST,
10+
REST_PUT,
11+
} from '@payloadcms/next/routes'
512

613
export const GET = REST_GET(config)
714
export const POST = REST_POST(config)
815
export const DELETE = REST_DELETE(config)
916
export const PATCH = REST_PATCH(config)
17+
export const PUT = REST_PUT(config)
1018
export const OPTIONS = REST_OPTIONS(config)
Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
/* THIS FILE WAS GENERATED AUTOMATICALLY BY PAYLOAD. */
22
/* DO NOT MODIFY IT BECAUSE IT COULD BE REWRITTEN AT ANY TIME. */
33
import config from '@payload-config'
4-
import { REST_DELETE, REST_GET, REST_OPTIONS, REST_PATCH, REST_POST } from '@payloadcms/next/routes'
4+
import {
5+
REST_DELETE,
6+
REST_GET,
7+
REST_OPTIONS,
8+
REST_PATCH,
9+
REST_POST,
10+
REST_PUT,
11+
} from '@payloadcms/next/routes'
512

613
export const GET = REST_GET(config)
714
export const POST = REST_POST(config)
815
export const DELETE = REST_DELETE(config)
916
export const PATCH = REST_PATCH(config)
17+
export const PUT = REST_PUT(config)
1018
export const OPTIONS = REST_OPTIONS(config)
Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
/* THIS FILE WAS GENERATED AUTOMATICALLY BY PAYLOAD. */
22
/* DO NOT MODIFY IT BECAUSE IT COULD BE REWRITTEN AT ANY TIME. */
33
import config from '@payload-config'
4-
import { REST_DELETE, REST_GET, REST_OPTIONS, REST_PATCH, REST_POST } from '@payloadcms/next/routes'
4+
import {
5+
REST_DELETE,
6+
REST_GET,
7+
REST_OPTIONS,
8+
REST_PATCH,
9+
REST_POST,
10+
REST_PUT,
11+
} from '@payloadcms/next/routes'
512

613
export const GET = REST_GET(config)
714
export const POST = REST_POST(config)
815
export const DELETE = REST_DELETE(config)
916
export const PATCH = REST_PATCH(config)
17+
18+
export const PUT = REST_PUT(config)
1019
export const OPTIONS = REST_OPTIONS(config)
Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
/* THIS FILE WAS GENERATED AUTOMATICALLY BY PAYLOAD. */
22
/* DO NOT MODIFY IT BECAUSE IT COULD BE REWRITTEN AT ANY TIME. */
33
import config from '@payload-config'
4-
import { REST_DELETE, REST_GET, REST_OPTIONS, REST_PATCH, REST_POST } from '@payloadcms/next/routes'
4+
import {
5+
REST_DELETE,
6+
REST_GET,
7+
REST_OPTIONS,
8+
REST_PATCH,
9+
REST_POST,
10+
REST_PUT,
11+
} from '@payloadcms/next/routes'
512

613
export const GET = REST_GET(config)
714
export const POST = REST_POST(config)
815
export const DELETE = REST_DELETE(config)
916
export const PATCH = REST_PATCH(config)
17+
export const PUT = REST_PUT(config)
1018
export const OPTIONS = REST_OPTIONS(config)
Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
/* THIS FILE WAS GENERATED AUTOMATICALLY BY PAYLOAD. */
22
/* DO NOT MODIFY IT BECAUSE IT COULD BE REWRITTEN AT ANY TIME. */
33
import config from '@payload-config'
4-
import { REST_DELETE, REST_GET, REST_OPTIONS, REST_PATCH, REST_POST } from '@payloadcms/next/routes'
4+
import {
5+
REST_DELETE,
6+
REST_GET,
7+
REST_OPTIONS,
8+
REST_PATCH,
9+
REST_POST,
10+
REST_PUT,
11+
} from '@payloadcms/next/routes'
512

613
export const GET = REST_GET(config)
714
export const POST = REST_POST(config)
815
export const DELETE = REST_DELETE(config)
916
export const PATCH = REST_PATCH(config)
17+
export const PUT = REST_PUT(config)
1018
export const OPTIONS = REST_OPTIONS(config)
Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
/* THIS FILE WAS GENERATED AUTOMATICALLY BY PAYLOAD. */
22
/* DO NOT MODIFY IT BECAUSE IT COULD BE REWRITTEN AT ANY TIME. */
33
import config from '@payload-config'
4-
import { REST_DELETE, REST_GET, REST_OPTIONS, REST_PATCH, REST_POST } from '@payloadcms/next/routes'
4+
import {
5+
REST_DELETE,
6+
REST_GET,
7+
REST_OPTIONS,
8+
REST_PATCH,
9+
REST_POST,
10+
REST_PUT,
11+
} from '@payloadcms/next/routes'
512

613
export const GET = REST_GET(config)
714
export const POST = REST_POST(config)
815
export const DELETE = REST_DELETE(config)
916
export const PATCH = REST_PATCH(config)
17+
export const PUT = REST_PUT(config)
1018
export const OPTIONS = REST_OPTIONS(config)
Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
/* THIS FILE WAS GENERATED AUTOMATICALLY BY PAYLOAD. */
22
/* DO NOT MODIFY IT BECAUSE IT COULD BE REWRITTEN AT ANY TIME. */
33
import config from '@payload-config'
4-
import { REST_DELETE, REST_GET, REST_OPTIONS, REST_PATCH, REST_POST } from '@payloadcms/next/routes'
4+
import {
5+
REST_DELETE,
6+
REST_GET,
7+
REST_OPTIONS,
8+
REST_PATCH,
9+
REST_POST,
10+
REST_PUT,
11+
} from '@payloadcms/next/routes'
512

613
export const GET = REST_GET(config)
714
export const POST = REST_POST(config)
815
export const DELETE = REST_DELETE(config)
916
export const PATCH = REST_PATCH(config)
17+
export const PUT = REST_PUT(config)
1018
export const OPTIONS = REST_OPTIONS(config)

0 commit comments

Comments
 (0)