Skip to content

Commit efce154

Browse files
authored
chore(plugin-search): enable TypeScript strict mode (#11508)
1 parent d57a786 commit efce154

File tree

8 files changed

+34
-18
lines changed

8 files changed

+34
-18
lines changed

packages/plugin-search/src/Search/index.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import type { CollectionConfig, Field } from 'payload'
22

3-
import type { SearchPluginConfigWithLocales } from '../types.js'
3+
import type { SanitizedSearchPluginConfig } from '../types.js'
44
import type { ReindexButtonServerProps } from './ui/ReindexButton/types.js'
55

66
import { generateReindexHandler } from '../utilities/generateReindexHandler.js'
77

88
// all settings can be overridden by the config
99
export const generateSearchCollection = (
10-
pluginConfig: SearchPluginConfigWithLocales,
10+
pluginConfig: SanitizedSearchPluginConfig,
1111
): CollectionConfig => {
1212
const searchSlug = pluginConfig?.searchOverrides?.slug || 'search'
1313
const searchCollections = pluginConfig?.collections || []
@@ -55,6 +55,10 @@ export const generateSearchCollection = (
5555
},
5656
]
5757

58+
if (!collectionLabels) {
59+
throw new Error('collectionLabels is required')
60+
}
61+
5862
const newConfig: CollectionConfig = {
5963
...(pluginConfig?.searchOverrides || {}),
6064
slug: searchSlug,

packages/plugin-search/src/Search/ui/ReindexButton/index.client.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,15 +89,18 @@ export const ReindexButtonClient: React.FC<ReindexButtonProps> = ({
8989
if (typeof label === 'string') {
9090
return label
9191
} else {
92-
return Object.hasOwn(label, locale.code) ? label[locale.code] : slug
92+
return label && Object.hasOwn(label, locale.code) ? label[locale.code] : slug
9393
}
9494
},
9595
[collectionLabels, locale.code],
9696
)
9797

9898
const pluralizedLabels = useMemo(() => {
9999
return searchCollections.reduce<Record<string, string>>((acc, slug) => {
100-
acc[slug] = getPluralizedLabel(slug)
100+
const label = getPluralizedLabel(slug)
101+
if (label) {
102+
acc[slug] = label
103+
}
101104
return acc
102105
}, {})
103106
}, [searchCollections, getPluralizedLabel])
@@ -111,9 +114,6 @@ export const ReindexButtonClient: React.FC<ReindexButtonProps> = ({
111114
const modalDescription = selectedAll
112115
? t('general:confirmReindexDescriptionAll')
113116
: t('general:confirmReindexDescription', { collections: selectedLabels })
114-
const loadingText = selectedAll
115-
? t('general:reindexingAll', { collections: t('general:collections') })
116-
: t('general:reindexingAll', { collections: selectedLabels })
117117

118118
return (
119119
<div>

packages/plugin-search/src/Search/ui/ReindexButton/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export const ReindexButton: SearchReindexButtonServerComponent = (props) => {
1212
const pluralLabel = labels?.plural
1313

1414
if (typeof pluralLabel === 'function') {
15+
// @ts-expect-error - I don't know why it gives an error. pluralLabel and i18n.t should both resolve to TFunction<DefaultTranslationKeys>
1516
return [collection, pluralLabel({ t: i18n.t })]
1617
}
1718

packages/plugin-search/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { CollectionAfterChangeHook, CollectionAfterDeleteHook, Config } from 'payload'
22

3-
import type { SearchPluginConfig, SearchPluginConfigWithLocales } from './types.js'
3+
import type { SanitizedSearchPluginConfig, SearchPluginConfig } from './types.js'
44

55
import { deleteFromSearch } from './Search/hooks/deleteFromSearch.js'
66
import { syncWithSearch } from './Search/hooks/syncWithSearch.js'
@@ -35,7 +35,7 @@ export const searchPlugin =
3535
.map((collection) => [collection.slug, collection.labels]),
3636
)
3737

38-
const pluginConfig: SearchPluginConfigWithLocales = {
38+
const pluginConfig: SanitizedSearchPluginConfig = {
3939
// write any config defaults here
4040
deleteDrafts: true,
4141
labels,

packages/plugin-search/src/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ export type SearchPluginConfigWithLocales = {
7777
locales?: string[]
7878
} & SearchPluginConfig
7979

80+
export type SanitizedSearchPluginConfig = {
81+
reindexBatchSize: number
82+
syncDrafts: boolean
83+
} & SearchPluginConfigWithLocales
84+
8085
export type SyncWithSearchArgs = {
8186
collection: string
8287
pluginConfig: SearchPluginConfig

packages/plugin-search/src/utilities/generateReindexHandler.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
killTransaction,
1010
} from 'payload'
1111

12-
import type { SearchPluginConfigWithLocales } from '../types.js'
12+
import type { SanitizedSearchPluginConfig } from '../types.js'
1313

1414
import { syncDocAsSearchIndex } from './syncDocAsSearchIndex.js'
1515

@@ -19,19 +19,29 @@ type ValidationResult = {
1919
}
2020

2121
export const generateReindexHandler =
22-
(pluginConfig: SearchPluginConfigWithLocales): PayloadHandler =>
22+
(pluginConfig: SanitizedSearchPluginConfig): PayloadHandler =>
2323
async (req) => {
2424
addLocalesToRequestFromData(req)
25+
if (!req.json) {
26+
return new Response('Req.json is undefined', { status: 400 })
27+
}
2528
const { collections = [] } = (await req.json()) as { collections: string[] }
2629
const t = req.t
2730

2831
const searchSlug = pluginConfig?.searchOverrides?.slug || 'search'
2932
const searchCollections = pluginConfig?.collections || []
30-
const reindexLocales = pluginConfig?.locales?.length ? pluginConfig.locales : [req.locale]
33+
const reindexLocales = pluginConfig?.locales?.length
34+
? pluginConfig.locales
35+
: req.locale
36+
? [req.locale]
37+
: []
3138

3239
const validatePermissions = async (): Promise<ValidationResult> => {
3340
const accessResults = await getAccessResults({ req })
34-
const searchAccessResults = accessResults.collections[searchSlug]
41+
const searchAccessResults = accessResults.collections?.[searchSlug]
42+
if (!searchAccessResults) {
43+
return { isValid: false, message: t('error:notAllowedToPerformAction') }
44+
}
3545

3646
const permissions = [searchAccessResults.delete, searchAccessResults.update]
3747
// plugin doesn't allow create by default:

packages/plugin-search/src/utilities/syncDocAsSearchIndex.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export const syncDocAsSearchIndex = async ({
5656
`Error gathering default priority for ${searchSlug} documents related to ${collection}`,
5757
)
5858
}
59-
} else {
59+
} else if (priority !== undefined) {
6060
defaultPriority = priority
6161
}
6262
}

packages/plugin-search/tsconfig.json

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
{
22
"extends": "../../tsconfig.base.json",
3-
"compilerOptions": {
4-
/* TODO: remove the following lines */
5-
"strict": false,
6-
},
73
"references": [{ "path": "../payload" }, { "path": "../ui" }, { "path": "../next" }]
84
}

0 commit comments

Comments
 (0)