fix: findByID adjust type to null if disableErrors: true is passed (#8282)

Fixes https://github.com/payloadcms/payload/issues/8280

Now, the result type of this operation:
```ts
const post = await payload.findByID({
  collection: "posts",
  id,
  disableErrors: true
})
```
is `Post | null` instead of `Post` when `disableErrors: true` is passed

Adds test for the `disableErrors` property and docs.
This commit is contained in:
Sasha
2024-09-18 19:39:58 +03:00
committed by GitHub
parent 9821aeb67a
commit 37e1adfa5c
4 changed files with 42 additions and 23 deletions

View File

@@ -1,7 +1,6 @@
import type { Payload } from 'payload'
import { randomBytes } from 'crypto'
import { randomBytes, randomUUID } from 'crypto'
import path from 'path'
import { NotFound, type Payload } from 'payload'
import { fileURLToPath } from 'url'
import type { NextRESTClient } from '../helpers/NextRESTClient.js'
@@ -1519,6 +1518,17 @@ describe('collections-rest', () => {
expect(result.errors[0].message).toStrictEqual('Something went wrong.')
})
})
describe('Local', () => {
it('findByID should throw NotFound if the doc was not found, if disableErrors: true then return null', async () => {
const post = await createPost()
const id = typeof post.id === 'string' ? randomUUID() : 999
await expect(payload.findByID({ collection: 'posts', id })).rejects.toBeInstanceOf(NotFound)
await expect(
payload.findByID({ collection: 'posts', id, disableErrors: true }),
).resolves.toBeNull()
})
})
})
async function createPost(overrides?: Partial<Post>) {