@@ -4,49 +4,64 @@ import { errors } from '@strapi/utils';
44import { isContentTypeEnabled } from '../util/enabledContentTypes' ;
55import { 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 */
1024export 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 = {
0 commit comments