chore(plugin-search): enable TypeScript strict mode (#11508)
This commit is contained in:
@@ -1,13 +1,13 @@
|
|||||||
import type { CollectionConfig, Field } from 'payload'
|
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 type { ReindexButtonServerProps } from './ui/ReindexButton/types.js'
|
||||||
|
|
||||||
import { generateReindexHandler } from '../utilities/generateReindexHandler.js'
|
import { generateReindexHandler } from '../utilities/generateReindexHandler.js'
|
||||||
|
|
||||||
// all settings can be overridden by the config
|
// all settings can be overridden by the config
|
||||||
export const generateSearchCollection = (
|
export const generateSearchCollection = (
|
||||||
pluginConfig: SearchPluginConfigWithLocales,
|
pluginConfig: SanitizedSearchPluginConfig,
|
||||||
): CollectionConfig => {
|
): CollectionConfig => {
|
||||||
const searchSlug = pluginConfig?.searchOverrides?.slug || 'search'
|
const searchSlug = pluginConfig?.searchOverrides?.slug || 'search'
|
||||||
const searchCollections = pluginConfig?.collections || []
|
const searchCollections = pluginConfig?.collections || []
|
||||||
@@ -55,6 +55,10 @@ export const generateSearchCollection = (
|
|||||||
},
|
},
|
||||||
]
|
]
|
||||||
|
|
||||||
|
if (!collectionLabels) {
|
||||||
|
throw new Error('collectionLabels is required')
|
||||||
|
}
|
||||||
|
|
||||||
const newConfig: CollectionConfig = {
|
const newConfig: CollectionConfig = {
|
||||||
...(pluginConfig?.searchOverrides || {}),
|
...(pluginConfig?.searchOverrides || {}),
|
||||||
slug: searchSlug,
|
slug: searchSlug,
|
||||||
|
|||||||
@@ -89,7 +89,7 @@ export const ReindexButtonClient: React.FC<ReindexButtonProps> = ({
|
|||||||
if (typeof label === 'string') {
|
if (typeof label === 'string') {
|
||||||
return label
|
return label
|
||||||
} else {
|
} 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],
|
[collectionLabels, locale.code],
|
||||||
@@ -97,7 +97,10 @@ export const ReindexButtonClient: React.FC<ReindexButtonProps> = ({
|
|||||||
|
|
||||||
const pluralizedLabels = useMemo(() => {
|
const pluralizedLabels = useMemo(() => {
|
||||||
return searchCollections.reduce<Record<string, string>>((acc, slug) => {
|
return searchCollections.reduce<Record<string, string>>((acc, slug) => {
|
||||||
acc[slug] = getPluralizedLabel(slug)
|
const label = getPluralizedLabel(slug)
|
||||||
|
if (label) {
|
||||||
|
acc[slug] = label
|
||||||
|
}
|
||||||
return acc
|
return acc
|
||||||
}, {})
|
}, {})
|
||||||
}, [searchCollections, getPluralizedLabel])
|
}, [searchCollections, getPluralizedLabel])
|
||||||
@@ -111,9 +114,6 @@ export const ReindexButtonClient: React.FC<ReindexButtonProps> = ({
|
|||||||
const modalDescription = selectedAll
|
const modalDescription = selectedAll
|
||||||
? t('general:confirmReindexDescriptionAll')
|
? t('general:confirmReindexDescriptionAll')
|
||||||
: t('general:confirmReindexDescription', { collections: selectedLabels })
|
: t('general:confirmReindexDescription', { collections: selectedLabels })
|
||||||
const loadingText = selectedAll
|
|
||||||
? t('general:reindexingAll', { collections: t('general:collections') })
|
|
||||||
: t('general:reindexingAll', { collections: selectedLabels })
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ export const ReindexButton: SearchReindexButtonServerComponent = (props) => {
|
|||||||
const pluralLabel = labels?.plural
|
const pluralLabel = labels?.plural
|
||||||
|
|
||||||
if (typeof pluralLabel === 'function') {
|
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 })]
|
return [collection, pluralLabel({ t: i18n.t })]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import type { CollectionAfterChangeHook, CollectionAfterDeleteHook, Config } from 'payload'
|
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 { deleteFromSearch } from './Search/hooks/deleteFromSearch.js'
|
||||||
import { syncWithSearch } from './Search/hooks/syncWithSearch.js'
|
import { syncWithSearch } from './Search/hooks/syncWithSearch.js'
|
||||||
@@ -35,7 +35,7 @@ export const searchPlugin =
|
|||||||
.map((collection) => [collection.slug, collection.labels]),
|
.map((collection) => [collection.slug, collection.labels]),
|
||||||
)
|
)
|
||||||
|
|
||||||
const pluginConfig: SearchPluginConfigWithLocales = {
|
const pluginConfig: SanitizedSearchPluginConfig = {
|
||||||
// write any config defaults here
|
// write any config defaults here
|
||||||
deleteDrafts: true,
|
deleteDrafts: true,
|
||||||
labels,
|
labels,
|
||||||
|
|||||||
@@ -77,6 +77,11 @@ export type SearchPluginConfigWithLocales = {
|
|||||||
locales?: string[]
|
locales?: string[]
|
||||||
} & SearchPluginConfig
|
} & SearchPluginConfig
|
||||||
|
|
||||||
|
export type SanitizedSearchPluginConfig = {
|
||||||
|
reindexBatchSize: number
|
||||||
|
syncDrafts: boolean
|
||||||
|
} & SearchPluginConfigWithLocales
|
||||||
|
|
||||||
export type SyncWithSearchArgs = {
|
export type SyncWithSearchArgs = {
|
||||||
collection: string
|
collection: string
|
||||||
pluginConfig: SearchPluginConfig
|
pluginConfig: SearchPluginConfig
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ import {
|
|||||||
killTransaction,
|
killTransaction,
|
||||||
} from 'payload'
|
} from 'payload'
|
||||||
|
|
||||||
import type { SearchPluginConfigWithLocales } from '../types.js'
|
import type { SanitizedSearchPluginConfig } from '../types.js'
|
||||||
|
|
||||||
import { syncDocAsSearchIndex } from './syncDocAsSearchIndex.js'
|
import { syncDocAsSearchIndex } from './syncDocAsSearchIndex.js'
|
||||||
|
|
||||||
@@ -19,19 +19,29 @@ type ValidationResult = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const generateReindexHandler =
|
export const generateReindexHandler =
|
||||||
(pluginConfig: SearchPluginConfigWithLocales): PayloadHandler =>
|
(pluginConfig: SanitizedSearchPluginConfig): PayloadHandler =>
|
||||||
async (req) => {
|
async (req) => {
|
||||||
addLocalesToRequestFromData(req)
|
addLocalesToRequestFromData(req)
|
||||||
|
if (!req.json) {
|
||||||
|
return new Response('Req.json is undefined', { status: 400 })
|
||||||
|
}
|
||||||
const { collections = [] } = (await req.json()) as { collections: string[] }
|
const { collections = [] } = (await req.json()) as { collections: string[] }
|
||||||
const t = req.t
|
const t = req.t
|
||||||
|
|
||||||
const searchSlug = pluginConfig?.searchOverrides?.slug || 'search'
|
const searchSlug = pluginConfig?.searchOverrides?.slug || 'search'
|
||||||
const searchCollections = pluginConfig?.collections || []
|
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 validatePermissions = async (): Promise<ValidationResult> => {
|
||||||
const accessResults = await getAccessResults({ req })
|
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]
|
const permissions = [searchAccessResults.delete, searchAccessResults.update]
|
||||||
// plugin doesn't allow create by default:
|
// plugin doesn't allow create by default:
|
||||||
|
|||||||
@@ -56,7 +56,7 @@ export const syncDocAsSearchIndex = async ({
|
|||||||
`Error gathering default priority for ${searchSlug} documents related to ${collection}`,
|
`Error gathering default priority for ${searchSlug} documents related to ${collection}`,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
} else {
|
} else if (priority !== undefined) {
|
||||||
defaultPriority = priority
|
defaultPriority = priority
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,4 @@
|
|||||||
{
|
{
|
||||||
"extends": "../../tsconfig.base.json",
|
"extends": "../../tsconfig.base.json",
|
||||||
"compilerOptions": {
|
|
||||||
/* TODO: remove the following lines */
|
|
||||||
"strict": false,
|
|
||||||
},
|
|
||||||
"references": [{ "path": "../payload" }, { "path": "../ui" }, { "path": "../next" }]
|
"references": [{ "path": "../payload" }, { "path": "../ui" }, { "path": "../next" }]
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user