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>
45 lines
1.0 KiB
TypeScript
45 lines
1.0 KiB
TypeScript
import type { Endpoint, PayloadHandler } from 'payload'
|
|
|
|
import { addDataAndFileToRequest } from '@payloadcms/next/utilities'
|
|
import httpStatus from 'http-status'
|
|
|
|
export const handler: PayloadHandler = async (req) => {
|
|
await addDataAndFileToRequest(req)
|
|
|
|
const { data, payload, user } = req
|
|
const operation = data?.operation ? String(data.operation) : undefined
|
|
|
|
if (data?.operation && typeof payload[operation] === 'function') {
|
|
try {
|
|
const result = await payload[operation]({
|
|
...(typeof data.args === 'object' ? data.args : {}),
|
|
user,
|
|
})
|
|
|
|
return Response.json(result, {
|
|
status: httpStatus.OK,
|
|
})
|
|
} catch (err) {
|
|
payload.logger.error(err)
|
|
return Response.json(err, {
|
|
status: httpStatus.BAD_REQUEST,
|
|
})
|
|
}
|
|
}
|
|
|
|
return Response.json(
|
|
{
|
|
message: 'Payload Local API method not found.',
|
|
},
|
|
{
|
|
status: httpStatus.BAD_REQUEST,
|
|
},
|
|
)
|
|
}
|
|
|
|
export const localAPIEndpoint: Endpoint = {
|
|
path: '/local-api',
|
|
method: 'post',
|
|
handler,
|
|
}
|