Skip to content

Commit 225c24d

Browse files
authored
fix: collection access endpoint optional ID and use 404 for not found response (#10487)
The collection access endpoint, apparently, can be used without an ID as well and the correct status code in `notFoundResponse` was missing. Huge thanks to @akhrarovsaid
1 parent d8f4f06 commit 225c24d

File tree

4 files changed

+15
-4
lines changed

4 files changed

+15
-4
lines changed

packages/payload/src/collections/endpoints/docAccess.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { headersWithCors } from '../../utilities/headersWithCors.js'
77
import { docAccessOperation } from '../operations/docAccess.js'
88

99
export const docAccessHandler: PayloadHandler = async (req) => {
10-
const { id, collection } = getRequestCollectionWithID(req)
10+
const { id, collection } = getRequestCollectionWithID(req, { optionalID: true })
1111
const result = await docAccessOperation({
1212
id,
1313
collection,

packages/payload/src/collections/endpoints/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export const defaultCollectionEndpoints: Endpoint[] = [
4343
{
4444
handler: docAccessHandler,
4545
method: 'post',
46-
path: '/access/:id',
46+
path: '/access/:id?',
4747
},
4848
{
4949
handler: duplicateHandler,

packages/payload/src/utilities/getRequestEntity.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@ export const getRequestCollectionWithID = <T extends boolean>(
2424
req: PayloadRequest,
2525
{
2626
disableSanitize,
27+
optionalID,
2728
}: {
2829
disableSanitize?: T
30+
optionalID?: boolean
2931
} = {},
3032
): {
3133
collection: Collection
@@ -35,6 +37,13 @@ export const getRequestCollectionWithID = <T extends boolean>(
3537
const id = req.routeParams.id
3638

3739
if (typeof id !== 'string') {
40+
if (optionalID) {
41+
return {
42+
id: undefined,
43+
collection,
44+
}
45+
}
46+
3847
throw new APIError(`ID was not specified`, 400)
3948
}
4049

packages/payload/src/utilities/handleEndpoints.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@ import { routeError } from './routeError.js'
1212

1313
const notFoundResponse = (req: PayloadRequest) => {
1414
return Response.json(
15-
{},
15+
{
16+
message: `Route not found "${new URL(req.url).pathname}"`,
17+
},
1618
{
1719
headers: headersWithCors({
1820
headers: new Headers(),
1921
req,
2022
}),
21-
status: 200,
23+
status: httpStatus.NOT_FOUND,
2224
},
2325
)
2426
}

0 commit comments

Comments
 (0)