1- import type { SelectType } from 'payload'
1+ import type { ApplyDisableErrors , SelectType } from 'payload'
22
33import type { PayloadSDK } from '../index.js'
44import 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}
0 commit comments