chore(templates): add type detection on getGlobal utility

This commit is contained in:
pierrecabriere
2025-03-10 17:49:35 -04:00
parent c8f01e31a1
commit 79bbb0f275
4 changed files with 16 additions and 16 deletions

View File

@@ -1,13 +1,13 @@
import type { Config } from 'src/payload-types'
import { unstable_cache } from 'next/cache'
import { getPayload } from 'payload'
import { type DataFromGlobalSlug, getPayload } from 'payload'
import configPromise from '../payload.config'
type Global = keyof Config['globals']
async function getGlobal(slug: Global, depth = 0) {
async function getGlobal<T extends Global>(slug: T, depth = 0): Promise<DataFromGlobalSlug<T>> {
const payload = await getPayload({ config: configPromise })
const global = await payload.findGlobal({
@@ -21,7 +21,7 @@ async function getGlobal(slug: Global, depth = 0) {
/**
* Returns a unstable_cache function mapped with the cache tag for the slug
*/
export const getCachedGlobal = (slug: Global, depth = 0) =>
unstable_cache(async () => getGlobal(slug, depth), [slug], {
export const getCachedGlobal = <T extends Global>(slug: T, depth = 0) =>
unstable_cache(async () => getGlobal<T>(slug, depth), [slug], {
tags: [`global_${slug}`],
})

View File

@@ -1,13 +1,13 @@
import type { Config } from 'src/payload-types'
import configPromise from '@payload-config'
import { getPayload } from 'payload'
import { type DataFromGlobalSlug, getPayload } from 'payload'
import { unstable_cache } from 'next/cache'
import { TypedLocale } from 'payload'
type Global = keyof Config['globals']
async function getGlobal(slug: Global, depth = 0, locale: TypedLocale) {
async function getGlobal<T extends Global>(slug: T, depth = 0, locale: TypedLocale): Promise<DataFromGlobalSlug<T>> {
const payload = await getPayload({ config: configPromise })
const global = await payload.findGlobal({
@@ -22,7 +22,7 @@ async function getGlobal(slug: Global, depth = 0, locale: TypedLocale) {
/**
* Returns a unstable_cache function mapped with the cache tag for the slug and locale
*/
export const getCachedGlobal = (slug: Global, depth = 0, locale: TypedLocale) =>
unstable_cache(async () => getGlobal(slug, depth, locale), [slug, locale], {
export const getCachedGlobal = <T extends Global>(slug: T, depth = 0, locale: TypedLocale) =>
unstable_cache(async () => getGlobal<T>(slug, depth, locale), [slug, locale], {
tags: [`global_${slug}`],
})