Skip to content

Commit 21d54f0

Browse files
committed
feat: add missing disableErrors to globals and sdk operations
1 parent 77f96a4 commit 21d54f0

11 files changed

Lines changed: 245 additions & 46 deletions

File tree

packages/payload/src/globals/operations/findOne.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export type GlobalFindOneArgs = {
2525
*/
2626
data?: Record<string, unknown>
2727
depth?: number
28+
disableErrors?: boolean
2829
draft?: boolean
2930
globalConfig: SanitizedGlobalConfig
3031
includeLockStatus?: boolean
@@ -42,6 +43,7 @@ export const findOneOperation = async <T extends Record<string, unknown>>(
4243
const {
4344
slug,
4445
depth,
46+
disableErrors,
4547
draft: replaceWithVersion = false,
4648
flattenLocales,
4749
globalConfig,
@@ -79,11 +81,14 @@ export const findOneOperation = async <T extends Record<string, unknown>>(
7981
let accessResult!: AccessResult
8082

8183
if (!overrideAccess) {
82-
accessResult = await executeAccess({ req }, globalConfig.access.read)
84+
accessResult = await executeAccess({ disableErrors, req }, globalConfig.access.read)
8385
}
8486

8587
if (accessResult === false) {
86-
throw new NotFound(req.t)
88+
if (!disableErrors) {
89+
throw new NotFound(req.t)
90+
}
91+
return null!
8792
}
8893

8994
const select = sanitizeSelect({

packages/payload/src/globals/operations/local/findOne.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ export type Options<TSlug extends GlobalSlug, TSelect extends SelectType> = {
3636
* [Control auto-population](https://payloadcms.com/docs/queries/depth) of nested relationship and upload fields.
3737
*/
3838
depth?: number
39+
/**
40+
* When set to `true`, errors will not be thrown.
41+
*/
42+
disableErrors?: boolean
3943
/**
4044
* Whether the document should be queried from the versions table/collection or not. [More](https://payloadcms.com/docs/versions/drafts#draft-api)
4145
*/
@@ -97,6 +101,7 @@ export async function findOneGlobalLocal<
97101
slug: globalSlug,
98102
data,
99103
depth,
104+
disableErrors,
100105
draft = false,
101106
flattenLocales,
102107
includeLockStatus,
@@ -116,6 +121,7 @@ export async function findOneGlobalLocal<
116121
slug: globalSlug as string,
117122
data,
118123
depth,
124+
disableErrors,
119125
draft,
120126
flattenLocales,
121127
globalConfig,

packages/sdk/src/collections/count.ts

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ export type CountOptions<T extends PayloadGeneratedTypes, TSlug extends Collecti
88
* the Collection slug to operate against.
99
*/
1010
collection: TSlug
11+
/**
12+
* When set to `true`, errors will not be thrown.
13+
*/
14+
disableErrors?: boolean
1115
/**
1216
* Specify [locale](https://payloadcms.com/docs/configuration/localization) for any returned documents.
1317
*/
@@ -23,12 +27,24 @@ export async function count<T extends PayloadGeneratedTypes, TSlug extends Colle
2327
options: CountOptions<T, TSlug>,
2428
init?: RequestInit,
2529
): Promise<{ totalDocs: number }> {
26-
const response = await sdk.request({
27-
args: options,
28-
init,
29-
method: 'GET',
30-
path: `/${options.collection}/count`,
31-
})
30+
try {
31+
const response = await sdk.request({
32+
args: options,
33+
init,
34+
method: 'GET',
35+
path: `/${options.collection}/count`,
36+
})
37+
38+
if (response.ok) {
39+
return response.json()
40+
} else {
41+
throw new Error()
42+
}
43+
} catch {
44+
if (options.disableErrors) {
45+
return { totalDocs: 0 }
46+
}
3247

33-
return response.json()
48+
throw new Error(`Error counting documents in ${options.collection}`)
49+
}
3450
}

packages/sdk/src/collections/find.ts

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ export type FindOptions<
2323
* [Control auto-population](https://payloadcms.com/docs/queries/depth) of nested relationship and upload fields.
2424
*/
2525
depth?: number
26+
/**
27+
* When set to `true`, errors will not be thrown.
28+
*/
29+
disableErrors?: boolean
2630
/**
2731
* Whether the documents should be queried from the versions table/collection or not. [More](https://payloadcms.com/docs/versions/drafts#draft-api)
2832
*/
@@ -94,12 +98,34 @@ export async function find<
9498
options: FindOptions<T, TSlug, TSelect>,
9599
init?: RequestInit,
96100
): Promise<PaginatedDocs<TransformCollectionWithSelect<T, TSlug, TSelect>>> {
97-
const response = await sdk.request({
98-
args: options,
99-
init,
100-
method: 'GET',
101-
path: `/${options.collection}`,
102-
})
101+
try {
102+
const response = await sdk.request({
103+
args: options,
104+
init,
105+
method: 'GET',
106+
path: `/${options.collection}`,
107+
})
108+
109+
if (response.ok) {
110+
return response.json()
111+
} else {
112+
throw new Error()
113+
}
114+
} catch {
115+
if (options.disableErrors) {
116+
return {
117+
docs: [],
118+
hasNextPage: false,
119+
hasPrevPage: false,
120+
limit: 0,
121+
page: 1,
122+
pagingCounter: 1,
123+
prevPage: null,
124+
totalDocs: 0,
125+
totalPages: 1,
126+
}
127+
}
103128

104-
return response.json()
129+
throw new Error(`Error retrieving documents from ${options.collection}`)
130+
}
105131
}

packages/sdk/src/collections/restoreVersion.ts

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import type { ApplyDisableErrors } from 'payload'
2+
13
import type { PayloadSDK } from '../index.js'
24
import type {
35
CollectionSlug,
@@ -10,6 +12,7 @@ import type {
1012
export type RestoreVersionByIDOptions<
1113
T extends PayloadGeneratedTypes,
1214
TSlug extends CollectionSlug<T>,
15+
TDisableErrors extends boolean,
1316
> = {
1417
/**
1518
* the Collection slug to operate against.
@@ -19,6 +22,10 @@ export type RestoreVersionByIDOptions<
1922
* [Control auto-population](https://payloadcms.com/docs/queries/depth) of nested relationship and upload fields.
2023
*/
2124
depth?: number
25+
/**
26+
* When set to `true`, errors will not be thrown.
27+
*/
28+
disableErrors?: TDisableErrors
2229
/**
2330
* Whether the document should be queried from the versions table/collection or not. [More](https://payloadcms.com/docs/versions/drafts#draft-api)
2431
*/
@@ -44,17 +51,31 @@ export type RestoreVersionByIDOptions<
4451
export async function restoreVersion<
4552
T extends PayloadGeneratedTypes,
4653
TSlug extends CollectionSlug<T>,
54+
TDisableErrors extends boolean,
4755
>(
4856
sdk: PayloadSDK<T>,
49-
options: RestoreVersionByIDOptions<T, TSlug>,
57+
options: RestoreVersionByIDOptions<T, TSlug, TDisableErrors>,
5058
init?: RequestInit,
51-
): Promise<DataFromCollectionSlug<T, TSlug>> {
52-
const response = await sdk.request({
53-
args: options,
54-
init,
55-
method: 'POST',
56-
path: `/${options.collection}/versions/${options.id}`,
57-
})
59+
): Promise<ApplyDisableErrors<DataFromCollectionSlug<T, TSlug>, TDisableErrors>> {
60+
try {
61+
const response = await sdk.request({
62+
args: options,
63+
init,
64+
method: 'POST',
65+
path: `/${options.collection}/versions/${options.id}`,
66+
})
67+
68+
if (response.ok) {
69+
return response.json()
70+
} else {
71+
throw new Error()
72+
}
73+
} catch {
74+
if (options.disableErrors) {
75+
// @ts-expect-error generic nullable
76+
return null
77+
}
5878

59-
return response.json()
79+
throw new Error(`Error restoring version ${options.id} in ${options.collection}`)
80+
}
6081
}

packages/sdk/src/globals/findOne.ts

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { SelectType } from 'payload'
1+
import type { ApplyDisableErrors, SelectType } from 'payload'
22

33
import type { PayloadSDK } from '../index.js'
44
import type {
@@ -14,11 +14,18 @@ export type FindGlobalOptions<
1414
T extends PayloadGeneratedTypes,
1515
TSlug extends GlobalSlug<T>,
1616
TSelect extends SelectType,
17+
// TODO 4.0: place TDisableErrors above TSelect to match with other operations
18+
TDisableErrors extends boolean,
1719
> = {
1820
/**
1921
* [Control auto-population](https://payloadcms.com/docs/queries/depth) of nested relationship and upload fields.
2022
*/
2123
depth?: number
24+
/**
25+
* When set to `true`, errors will not be thrown.
26+
* `null` will be returned instead, if the document was not found.
27+
*/
28+
disableErrors?: TDisableErrors
2229
/**
2330
* Whether the document should be queried from the versions table/collection or not. [More](https://payloadcms.com/docs/versions/drafts#draft-api)
2431
*/
@@ -49,17 +56,31 @@ export async function findGlobal<
4956
T extends PayloadGeneratedTypes,
5057
TSlug extends GlobalSlug<T>,
5158
TSelect extends SelectFromGlobalSlug<T, TSlug>,
59+
TDisableErrors extends boolean = boolean,
5260
>(
5361
sdk: PayloadSDK<T>,
54-
options: FindGlobalOptions<T, TSlug, TSelect>,
62+
options: FindGlobalOptions<T, TSlug, TSelect, TDisableErrors>,
5563
init?: RequestInit,
56-
): Promise<TransformGlobalWithSelect<T, TSlug, TSelect>> {
57-
const response = await sdk.request({
58-
args: options,
59-
init,
60-
method: 'GET',
61-
path: `/globals/${options.slug}`,
62-
})
64+
): Promise<ApplyDisableErrors<TransformGlobalWithSelect<T, TSlug, TSelect>, TDisableErrors>> {
65+
try {
66+
const response = await sdk.request({
67+
args: options,
68+
init,
69+
method: 'GET',
70+
path: `/globals/${options.slug}`,
71+
})
72+
73+
if (response.ok) {
74+
return response.json()
75+
} else {
76+
throw new Error()
77+
}
78+
} catch {
79+
if (options.disableErrors) {
80+
// @ts-expect-error generic nullable
81+
return null
82+
}
6383

64-
return response.json()
84+
throw new Error(`Error retrieving global ${options.slug}`)
85+
}
6586
}

packages/sdk/src/globals/restoreVersion.ts

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { TypeWithVersion } from 'payload'
1+
import type { ApplyDisableErrors, TypeWithVersion } from 'payload'
22

33
import type { PayloadSDK } from '../index.js'
44
import type {
@@ -12,11 +12,16 @@ import type {
1212
export type RestoreGlobalVersionByIDOptions<
1313
T extends PayloadGeneratedTypes,
1414
TSlug extends GlobalSlug<T>,
15+
TDisableErrors extends boolean,
1516
> = {
1617
/**
1718
* [Control auto-population](https://payloadcms.com/docs/queries/depth) of nested relationship and upload fields.
1819
*/
1920
depth?: number
21+
/**
22+
* When set to `true`, errors will not be thrown.
23+
*/
24+
disableErrors?: TDisableErrors
2025
draft?: boolean
2126
/**
2227
* Specify a [fallback locale](https://payloadcms.com/docs/configuration/localization) to use for any returned documents.
@@ -43,19 +48,29 @@ export type RestoreGlobalVersionByIDOptions<
4348
export async function restoreGlobalVersion<
4449
T extends PayloadGeneratedTypes,
4550
TSlug extends GlobalSlug<T>,
51+
TDisableErrors extends boolean = boolean,
4652
>(
4753
sdk: PayloadSDK<T>,
48-
options: RestoreGlobalVersionByIDOptions<T, TSlug>,
54+
options: RestoreGlobalVersionByIDOptions<T, TSlug, TDisableErrors>,
4955
init?: RequestInit,
50-
): Promise<TypeWithVersion<DataFromGlobalSlug<T, TSlug>>> {
51-
const response = await sdk.request({
52-
args: options,
53-
init,
54-
method: 'POST',
55-
path: `/globals/${options.slug}/versions/${options.id}`,
56-
})
56+
): Promise<ApplyDisableErrors<TypeWithVersion<DataFromGlobalSlug<T, TSlug>>, TDisableErrors>> {
57+
try {
58+
const response = await sdk.request({
59+
args: options,
60+
init,
61+
method: 'POST',
62+
path: `/globals/${options.slug}/versions/${options.id}`,
63+
})
64+
65+
const { doc } = await response.json()
5766

58-
const { doc } = await response.json()
67+
return doc
68+
} catch {
69+
if (options.disableErrors) {
70+
// @ts-expect-error generic nullable
71+
return null
72+
}
5973

60-
return doc
74+
throw new Error(`Error restoring version ${options.id} for global ${options.slug}`)
75+
}
6176
}

test/collections-rest/int.spec.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1754,6 +1754,19 @@ describe('collections-rest', () => {
17541754
payload.findByID({ collection: 'posts', id, disableErrors: true }),
17551755
).resolves.toBeNull()
17561756
})
1757+
1758+
it('find should support disableErrors: true', async () => {
1759+
const res = await payload.find({ collection: 'posts', disableErrors: true })
1760+
1761+
expect(res.docs).toHaveLength(0)
1762+
expect(res.totalDocs).toBe(0)
1763+
})
1764+
1765+
it('count should support disableErrors: true', async () => {
1766+
const res = await payload.count({ collection: 'posts', disableErrors: true })
1767+
1768+
expect(res.totalDocs).toEqual(0)
1769+
})
17571770
})
17581771

17591772
describe('Custom endpoints', () => {

test/globals/int.spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,15 @@ describe('globals', () => {
195195
expect(hasAccess.title).toBeDefined()
196196
})
197197

198+
it('should return null when finding non-existent global with disableErrors: true', async () => {
199+
const doc = await payload.findGlobal({
200+
slug,
201+
disableErrors: true,
202+
})
203+
204+
expect(doc).toBeNull()
205+
})
206+
198207
it('should get globals with defaultValues populated before first creation', async () => {
199208
const defaultValueGlobal = await payload.findGlobal({
200209
slug: defaultValueSlug,

0 commit comments

Comments
 (0)