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:
@@ -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>) {
|
||||
|
||||
Reference in New Issue
Block a user