chore(plugin-search): enable TypeScript strict mode (#11508)

This commit is contained in:
Germán Jabloñski
2025-03-03 15:31:26 -03:00
committed by GitHub
parent d57a78616a
commit efce1549d0
8 changed files with 34 additions and 18 deletions

View File

@@ -1,13 +1,13 @@
import type { CollectionConfig, Field } from 'payload'
import type { SearchPluginConfigWithLocales } from '../types.js'
import type { SanitizedSearchPluginConfig } from '../types.js'
import type { ReindexButtonServerProps } from './ui/ReindexButton/types.js'
import { generateReindexHandler } from '../utilities/generateReindexHandler.js'
// all settings can be overridden by the config
export const generateSearchCollection = (
pluginConfig: SearchPluginConfigWithLocales,
pluginConfig: SanitizedSearchPluginConfig,
): CollectionConfig => {
const searchSlug = pluginConfig?.searchOverrides?.slug || 'search'
const searchCollections = pluginConfig?.collections || []
@@ -55,6 +55,10 @@ export const generateSearchCollection = (
},
]
if (!collectionLabels) {
throw new Error('collectionLabels is required')
}
const newConfig: CollectionConfig = {
...(pluginConfig?.searchOverrides || {}),
slug: searchSlug,

View File

@@ -89,7 +89,7 @@ export const ReindexButtonClient: React.FC<ReindexButtonProps> = ({
if (typeof label === 'string') {
return label
} else {
return Object.hasOwn(label, locale.code) ? label[locale.code] : slug
return label && Object.hasOwn(label, locale.code) ? label[locale.code] : slug
}
},
[collectionLabels, locale.code],
@@ -97,7 +97,10 @@ export const ReindexButtonClient: React.FC<ReindexButtonProps> = ({
const pluralizedLabels = useMemo(() => {
return searchCollections.reduce<Record<string, string>>((acc, slug) => {
acc[slug] = getPluralizedLabel(slug)
const label = getPluralizedLabel(slug)
if (label) {
acc[slug] = label
}
return acc
}, {})
}, [searchCollections, getPluralizedLabel])
@@ -111,9 +114,6 @@ export const ReindexButtonClient: React.FC<ReindexButtonProps> = ({
const modalDescription = selectedAll
? t('general:confirmReindexDescriptionAll')
: t('general:confirmReindexDescription', { collections: selectedLabels })
const loadingText = selectedAll
? t('general:reindexingAll', { collections: t('general:collections') })
: t('general:reindexingAll', { collections: selectedLabels })
return (
<div>

View File

@@ -12,6 +12,7 @@ export const ReindexButton: SearchReindexButtonServerComponent = (props) => {
const pluralLabel = labels?.plural
if (typeof pluralLabel === 'function') {
// @ts-expect-error - I don't know why it gives an error. pluralLabel and i18n.t should both resolve to TFunction<DefaultTranslationKeys>
return [collection, pluralLabel({ t: i18n.t })]
}

View File

@@ -1,6 +1,6 @@
import type { CollectionAfterChangeHook, CollectionAfterDeleteHook, Config } from 'payload'
import type { SearchPluginConfig, SearchPluginConfigWithLocales } from './types.js'
import type { SanitizedSearchPluginConfig, SearchPluginConfig } from './types.js'
import { deleteFromSearch } from './Search/hooks/deleteFromSearch.js'
import { syncWithSearch } from './Search/hooks/syncWithSearch.js'
@@ -35,7 +35,7 @@ export const searchPlugin =
.map((collection) => [collection.slug, collection.labels]),
)
const pluginConfig: SearchPluginConfigWithLocales = {
const pluginConfig: SanitizedSearchPluginConfig = {
// write any config defaults here
deleteDrafts: true,
labels,

View File

@@ -77,6 +77,11 @@ export type SearchPluginConfigWithLocales = {
locales?: string[]
} & SearchPluginConfig
export type SanitizedSearchPluginConfig = {
reindexBatchSize: number
syncDrafts: boolean
} & SearchPluginConfigWithLocales
export type SyncWithSearchArgs = {
collection: string
pluginConfig: SearchPluginConfig

View File

@@ -9,7 +9,7 @@ import {
killTransaction,
} from 'payload'
import type { SearchPluginConfigWithLocales } from '../types.js'
import type { SanitizedSearchPluginConfig } from '../types.js'
import { syncDocAsSearchIndex } from './syncDocAsSearchIndex.js'
@@ -19,19 +19,29 @@ type ValidationResult = {
}
export const generateReindexHandler =
(pluginConfig: SearchPluginConfigWithLocales): PayloadHandler =>
(pluginConfig: SanitizedSearchPluginConfig): PayloadHandler =>
async (req) => {
addLocalesToRequestFromData(req)
if (!req.json) {
return new Response('Req.json is undefined', { status: 400 })
}
const { collections = [] } = (await req.json()) as { collections: string[] }
const t = req.t
const searchSlug = pluginConfig?.searchOverrides?.slug || 'search'
const searchCollections = pluginConfig?.collections || []
const reindexLocales = pluginConfig?.locales?.length ? pluginConfig.locales : [req.locale]
const reindexLocales = pluginConfig?.locales?.length
? pluginConfig.locales
: req.locale
? [req.locale]
: []
const validatePermissions = async (): Promise<ValidationResult> => {
const accessResults = await getAccessResults({ req })
const searchAccessResults = accessResults.collections[searchSlug]
const searchAccessResults = accessResults.collections?.[searchSlug]
if (!searchAccessResults) {
return { isValid: false, message: t('error:notAllowedToPerformAction') }
}
const permissions = [searchAccessResults.delete, searchAccessResults.update]
// plugin doesn't allow create by default:

View File

@@ -56,7 +56,7 @@ export const syncDocAsSearchIndex = async ({
`Error gathering default priority for ${searchSlug} documents related to ${collection}`,
)
}
} else {
} else if (priority !== undefined) {
defaultPriority = priority
}
}

View File

@@ -1,8 +1,4 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
/* TODO: remove the following lines */
"strict": false,
},
"references": [{ "path": "../payload" }, { "path": "../ui" }, { "path": "../next" }]
}