Skip to content

Commit d450867

Browse files
committed
feat: fix linting issues and add interfaces
1 parent cbe297d commit d450867

File tree

2 files changed

+58
-33
lines changed

2 files changed

+58
-33
lines changed

packages/core/server/controllers/search.ts

Lines changed: 49 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,49 +4,64 @@ import { errors } from '@strapi/utils';
44
import { isContentTypeEnabled } from '../util/enabledContentTypes';
55
import { getPluginService } from '../util/getPluginService';
66

7+
interface ContentTypeConfig {
8+
[key: string]: any; // We only pass this to isContentTypeEnabled, so any is fine here
9+
}
10+
11+
interface DocumentEntry {
12+
id: number;
13+
documentId: string;
14+
[key: string]: unknown; // Dynamic fields like title, etc.
15+
}
16+
17+
interface SearchResult extends DocumentEntry {
18+
contentType: string;
19+
}
20+
721
/**
822
* Search controller
923
*/
1024
export default {
1125
search: async (ctx: Context & { params: { id: number } }) => {
1226
const { q } = ctx.query;
13-
const results = [];
27+
const results: SearchResult[] = [];
1428

1529
const qStr = typeof q === 'string' ? q.trim() : '';
1630
if (!qStr) {
1731
throw new errors.ValidationError('Missing or invalid query parameter "?q=" (must be a non-empty string)');
1832
}
1933

2034
await Promise.all(
21-
Object.entries(strapi.contentTypes).map(async ([uid, config]: [UID.CollectionType, any]) => {
22-
const hasWT = isContentTypeEnabled(config);
23-
if (!hasWT) return;
24-
25-
const mainField = await getPluginService('get-main-field').getMainField(uid);
26-
if (!mainField) return;
27-
28-
const entries = await (strapi as any).documents(uid).findMany({
29-
filters: {
30-
[mainField]: { $containsi: qStr },
31-
},
32-
fields: [mainField, 'documentId'],
33-
populate: {
34-
url_alias: { fields: ['id'] },
35-
},
36-
});
37-
38-
if (!entries || entries.length === 0) return;
39-
40-
const entriesWithContentType = entries.map((entry: any) => ({
41-
...entry,
42-
contentType: uid,
43-
}));
44-
45-
results.push(...entriesWithContentType);
46-
}),
35+
Object.entries(strapi.contentTypes).map(
36+
async ([uid, config]: [UID.CollectionType, ContentTypeConfig]) => {
37+
const hasWT = isContentTypeEnabled(config);
38+
if (!hasWT) return;
39+
40+
const mainField = await getPluginService('get-main-field').getMainField(uid);
41+
if (!mainField) return;
42+
43+
const entries: DocumentEntry[] = await strapi.documents(uid).findMany({
44+
filters: {
45+
[mainField]: { $containsi: qStr },
46+
},
47+
fields: [mainField, 'documentId'],
48+
populate: {
49+
url_alias: { fields: ['id'] },
50+
},
51+
});
52+
53+
if (!entries || entries.length === 0) return;
54+
55+
const entriesWithContentType: SearchResult[] = entries.map((entry: DocumentEntry) => ({
56+
...entry,
57+
contentType: uid,
58+
}));
59+
60+
results.push(...entriesWithContentType);
61+
},
62+
),
4763
);
4864

49-
// @ts-ignore
5065
ctx.body = results;
5166
},
5267
reverseSearch: async (ctx: Context & { params: { contentType: string; documentId: string } }) => {
@@ -56,14 +71,17 @@ export default {
5671
throw new errors.ValidationError(`Unknown or invalid content type: ${contentType}`);
5772
}
5873

59-
const mainField = await getPluginService('get-main-field').getMainField(contentType as UID.CollectionType);
60-
const entry = await (strapi as any).documents(contentType as UID.CollectionType).findOne({
74+
const mainField = await getPluginService('get-main-field').getMainField(
75+
contentType as UID.CollectionType,
76+
);
77+
// eslint-disable-next-line max-len
78+
const entry: DocumentEntry | null = await strapi.documents(contentType as UID.CollectionType).findOne({
6179
documentId,
6280
fields: ['id', 'documentId', ...(mainField ? [mainField] : [])],
6381
});
6482

6583
if (!entry) {
66-
throw new errors.ValidationError('Entry not found');
84+
throw new errors.NotFoundError('Entry not found');
6785
}
6886

6987
ctx.body = {

packages/core/server/services/get-main-field.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
import { UID } from '@strapi/strapi';
22

3+
interface ContentManagerConfig {
4+
settings?: {
5+
mainField?: string;
6+
};
7+
}
8+
39
export const getMainField = async (uid: UID.CollectionType): Promise<string | null> => {
410
const coreStoreSettings = await strapi.query('strapi::core-store').findMany({
511
where: { key: `plugin_content_manager_configuration_content_types::${uid}` },
6-
});
12+
}) as Array<{ value: string }>;
13+
714
if (!coreStoreSettings?.[0]) return null;
815

9-
const value = JSON.parse(coreStoreSettings[0].value);
16+
const value = JSON.parse(coreStoreSettings[0].value) as ContentManagerConfig;
1017

1118
return value?.settings?.mainField ?? null;
1219
};

0 commit comments

Comments
 (0)