Files
payloadcms/packages/payload/src/preferences/requestHandlers/findOne.ts
Alessio Gravili 67fb29b2a4 fix: reduce global DOM/Node type conflicts in server-only packages (#12737)
Currently, we globally enable both DOM and Node.js types. While this
mostly works, it can cause conflicts - particularly with `fetch`. For
example, TypeScript may incorrectly allow browser-only properties (like
`cache`) and reject valid Node.js ones like `dispatcher`.

This PR disables DOM types for server-only packages like payload,
ensuring Node-specific typings are applied. This caught a few instances
of incorrect fetch usage that were previously masked by overlapping DOM
types.

This is not a perfect solution - packages that contain both server and
client code (like richtext-lexical or next) will still suffer from this
issue. However, it's an improvement in cases where we can cleanly
separate server and client types, like for the `payload` package which
is server-only.

## Use-case

This change enables https://github.com/payloadcms/payload/pull/12622 to
explore using node-native fetch + `dispatcher`, instead of `node-fetch`
+ `agent`.

Currently, it will incorrectly report that `dispatcher` is not a valid
property for node-native fetch
2025-06-11 20:59:19 +00:00

47 lines
1.1 KiB
TypeScript

import { status as httpStatus } from 'http-status'
import type { PayloadHandler } from '../../config/types.js'
import type { PayloadRequest } from '../../types/index.js'
import { findOne } from '../operations/findOne.js'
export const findByIDHandler: PayloadHandler = async (incomingReq): Promise<Response> => {
// We cannot import the addDataAndFileToRequest utility here from the 'next' package because of dependency issues
// However that utility should be used where possible instead of manually appending the data
let data
try {
data = await incomingReq.json?.()
} catch (ignore) {
data = {}
}
const reqWithData: PayloadRequest = incomingReq
if (data) {
reqWithData.data = data
// @ts-expect-error
reqWithData.json = () => Promise.resolve(data)
}
const result = await findOne({
key: reqWithData.routeParams?.key as string,
req: reqWithData,
user: reqWithData.user,
})
return Response.json(
{
...(result
? result
: {
message: reqWithData.t('general:notFound'),
value: null,
}),
},
{
status: httpStatus.OK,
},
)
}