Skip to content

Commit 0711f88

Browse files
chore!: simplify api handler (#6910)
Removes PayloadRequestWithData in favour of just PayloadRequest with optional types for `data` and `locale` `addDataAndFileToRequest` and `addLocalesToRequestFromData` now takes in a single argument instead of an object ```ts // before await addDataAndFileToRequest({ request: req }) addLocalesToRequestFromData({ request: req }) // current await addDataAndFileToRequest(req) addLocalesToRequestFromData(req) ``` --------- Co-authored-by: Paul Popus <paul@nouance.io>
1 parent fd7d500 commit 0711f88

File tree

231 files changed

+738
-816
lines changed

Some content is hidden

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

231 files changed

+738
-816
lines changed

docs/rest-api/overview.mdx

Lines changed: 77 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -598,15 +598,37 @@ export const Orders: CollectionConfig = {
598598
{
599599
path: '/:id/tracking',
600600
method: 'get',
601-
handler: async (req, res, next) => {
601+
handler: (req) => {
602602
const tracking = await getTrackingInfo(req.params.id)
603-
if (tracking) {
604-
res.status(200).send({ tracking })
605-
} else {
606-
res.status(404).send({ error: 'not found' })
603+
604+
if (!tracking) {
605+
return Response.json({ error: 'not found' }, { status: 404})
607606
}
607+
608+
return Response.json({
609+
message: `Hello ${req.routeParams.name as string} @ ${req.routeParams.group as string}`,
610+
})
608611
},
609612
},
613+
{
614+
path: '/:id/tracking',
615+
method: 'post',
616+
handler: (req) => {
617+
// `data` is not automatically appended to the request
618+
// if you would like to read the body of the request
619+
// you can use `data = await req.json()`
620+
const data = await req.json()
621+
await req.payload.update({
622+
collection: 'tracking',
623+
data: {
624+
// data to update the document with
625+
}
626+
})
627+
return Response.json({
628+
message: 'successfully updated tracking info'
629+
})
630+
}
631+
}
610632
],
611633
// highlight-end
612634
}
@@ -619,6 +641,56 @@ export const Orders: CollectionConfig = {
619641
calls like req.payload.find() that will make use of access control and hooks.
620642
</Banner>
621643

644+
#### Helpful tips
645+
`req.data`
646+
647+
Data is not automatically appended to the request. You can read the body data by calling `await req.json()`.
648+
649+
Or you could use our helper function that mutates the request and appends data and file if found.
650+
651+
```ts
652+
import { addDataAndFileToRequest } from '@payloadcms/next/utilities'
653+
654+
// custom endpoint example
655+
{
656+
path: '/:id/tracking',
657+
method: 'post',
658+
handler: (req) => {
659+
await addDataAndFileToRequest(req)
660+
await req.payload.update({
661+
collection: 'tracking',
662+
data: {
663+
// data to update the document with
664+
}
665+
})
666+
return Response.json({
667+
message: 'successfully updated tracking info'
668+
})
669+
}
670+
}
671+
```
672+
673+
`req.locale` & `req.fallbackLocale`
674+
675+
The locale and the fallback locale are not automatically appended to custom endpoint requests. If you would like to add them you can use this helper function.
676+
677+
```ts
678+
import { addLocalesToRequestFromData } from '@payloadcms/next/utilities'
679+
680+
// custom endpoint example
681+
{
682+
path: '/:id/tracking',
683+
method: 'post',
684+
handler: (req) => {
685+
await addLocalesToRequestFromData(req)
686+
// you now can access req.locale & req.fallbackLocale
687+
return Response.json({ message: 'success' })
688+
}
689+
}
690+
```
691+
692+
693+
622694
## Method Override for GET Requests
623695

624696
Payload supports a method override feature that allows you to send GET requests using the HTTP POST method. This can be particularly useful in scenarios when the query string in a regular GET request is too long.

examples/auth/payload/src/app/(app)/_components/HydrateClientUser/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
'use client'
22

33
import type { Permissions } from 'payload/auth'
4-
import type { PayloadRequestWithData } from 'payload/types'
4+
import type { PayloadRequest } from 'payload/types'
55

66
import { useEffect } from 'react'
77

88
import { useAuth } from '../../_providers/Auth'
99

1010
export const HydrateClientUser: React.FC<{
1111
permissions: Permissions
12-
user: PayloadRequestWithData['user']
12+
user: PayloadRequest['user']
1313
}> = ({ permissions, user }) => {
1414
const { setPermissions, setUser } = useAuth()
1515

packages/db-mongodb/src/count.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { QueryOptions } from 'mongoose'
2-
import type { Count, PayloadRequestWithData } from 'payload'
2+
import type { Count, PayloadRequest } from 'payload'
33

44
import { flattenWhereToOperators } from 'payload'
55

@@ -9,7 +9,7 @@ import { withSession } from './withSession.js'
99

1010
export const count: Count = async function count(
1111
this: MongooseAdapter,
12-
{ collection, locale, req = {} as PayloadRequestWithData, where },
12+
{ collection, locale, req = {} as PayloadRequest, where },
1313
) {
1414
const Model = this.collections[collection]
1515
const options: QueryOptions = withSession(this, req.transactionID)

packages/db-mongodb/src/create.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Create, Document, PayloadRequestWithData } from 'payload'
1+
import type { Create, Document, PayloadRequest } from 'payload'
22

33
import type { MongooseAdapter } from './index.js'
44

@@ -7,7 +7,7 @@ import { withSession } from './withSession.js'
77

88
export const create: Create = async function create(
99
this: MongooseAdapter,
10-
{ collection, data, req = {} as PayloadRequestWithData },
10+
{ collection, data, req = {} as PayloadRequest },
1111
) {
1212
const Model = this.collections[collection]
1313
const options = withSession(this, req.transactionID)

packages/db-mongodb/src/createGlobal.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { CreateGlobal, PayloadRequestWithData } from 'payload'
1+
import type { CreateGlobal, PayloadRequest } from 'payload'
22

33
import type { MongooseAdapter } from './index.js'
44

@@ -7,7 +7,7 @@ import { withSession } from './withSession.js'
77

88
export const createGlobal: CreateGlobal = async function createGlobal(
99
this: MongooseAdapter,
10-
{ slug, data, req = {} as PayloadRequestWithData },
10+
{ slug, data, req = {} as PayloadRequest },
1111
) {
1212
const Model = this.globals
1313
const global = {

packages/db-mongodb/src/createGlobalVersion.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,12 @@
1-
import type { CreateGlobalVersion, Document, PayloadRequestWithData } from 'payload'
1+
import type { CreateGlobalVersion, Document, PayloadRequest } from 'payload'
22

33
import type { MongooseAdapter } from './index.js'
44

55
import { withSession } from './withSession.js'
66

77
export const createGlobalVersion: CreateGlobalVersion = async function createGlobalVersion(
88
this: MongooseAdapter,
9-
{
10-
autosave,
11-
createdAt,
12-
globalSlug,
13-
parent,
14-
req = {} as PayloadRequestWithData,
15-
updatedAt,
16-
versionData,
17-
},
9+
{ autosave, createdAt, globalSlug, parent, req = {} as PayloadRequest, updatedAt, versionData },
1810
) {
1911
const VersionModel = this.versions[globalSlug]
2012
const options = withSession(this, req.transactionID)

packages/db-mongodb/src/createVersion.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { CreateVersion, Document, PayloadRequestWithData } from 'payload'
1+
import type { CreateVersion, Document, PayloadRequest } from 'payload'
22

33
import type { MongooseAdapter } from './index.js'
44

@@ -11,7 +11,7 @@ export const createVersion: CreateVersion = async function createVersion(
1111
collectionSlug,
1212
createdAt,
1313
parent,
14-
req = {} as PayloadRequestWithData,
14+
req = {} as PayloadRequest,
1515
updatedAt,
1616
versionData,
1717
},

packages/db-mongodb/src/deleteMany.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import type { DeleteMany, PayloadRequestWithData } from 'payload'
1+
import type { DeleteMany, PayloadRequest } from 'payload'
22

33
import type { MongooseAdapter } from './index.js'
44

55
import { withSession } from './withSession.js'
66

77
export const deleteMany: DeleteMany = async function deleteMany(
88
this: MongooseAdapter,
9-
{ collection, req = {} as PayloadRequestWithData, where },
9+
{ collection, req = {} as PayloadRequest, where },
1010
) {
1111
const Model = this.collections[collection]
1212
const options = {

packages/db-mongodb/src/deleteOne.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { DeleteOne, Document, PayloadRequestWithData } from 'payload'
1+
import type { DeleteOne, Document, PayloadRequest } from 'payload'
22

33
import type { MongooseAdapter } from './index.js'
44

@@ -7,7 +7,7 @@ import { withSession } from './withSession.js'
77

88
export const deleteOne: DeleteOne = async function deleteOne(
99
this: MongooseAdapter,
10-
{ collection, req = {} as PayloadRequestWithData, where },
10+
{ collection, req = {} as PayloadRequest, where },
1111
) {
1212
const Model = this.collections[collection]
1313
const options = withSession(this, req.transactionID)

packages/db-mongodb/src/deleteVersions.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import type { DeleteVersions, PayloadRequestWithData } from 'payload'
1+
import type { DeleteVersions, PayloadRequest } from 'payload'
22

33
import type { MongooseAdapter } from './index.js'
44

55
import { withSession } from './withSession.js'
66

77
export const deleteVersions: DeleteVersions = async function deleteVersions(
88
this: MongooseAdapter,
9-
{ collection, locale, req = {} as PayloadRequestWithData, where },
9+
{ collection, locale, req = {} as PayloadRequest, where },
1010
) {
1111
const VersionsModel = this.versions[collection]
1212
const options = {

0 commit comments

Comments
 (0)