|
1 | 1 | import { Context } from 'koa'; |
2 | 2 | import { UID } from '@strapi/strapi'; |
| 3 | +import { errors } from '@strapi/utils'; |
3 | 4 | import { isContentTypeEnabled } from '../util/enabledContentTypes'; |
4 | | -import { getMainField } from '../services/get-main-field'; |
| 5 | +import { getPluginService } from '../util/getPluginService'; |
5 | 6 |
|
6 | 7 | /** |
7 | 8 | * Search controller |
8 | 9 | */ |
9 | 10 | export default { |
10 | 11 | search: async (ctx: Context & { params: { id: number } }) => { |
11 | 12 | const { q } = ctx.query; |
12 | | - const { id } = ctx.params; |
13 | | - let results = []; |
| 13 | + const results = []; |
14 | 14 |
|
15 | 15 | const qStr = typeof q === 'string' ? q.trim() : ''; |
16 | 16 | if (!qStr) { |
17 | | - ctx.throw(400, 'Missing or invalid query parameter "?q=" (must be a non-empty strin4g)'); |
18 | | - return; |
| 17 | + throw new errors.ValidationError('Missing or invalid query parameter "?q=" (must be a non-empty string)'); |
19 | 18 | } |
20 | 19 |
|
21 | 20 | await Promise.all( |
22 | 21 | Object.entries(strapi.contentTypes).map(async ([uid, config]: [UID.CollectionType, any]) => { |
23 | 22 | const hasWT = isContentTypeEnabled(config); |
24 | 23 | if (!hasWT) return; |
25 | 24 |
|
26 | | - const mainField = await getMainField(uid); |
| 25 | + const mainField = await getPluginService('get-main-field').getMainField(uid); |
27 | 26 | if (!mainField) return; |
28 | 27 |
|
29 | 28 | const entries = await (strapi as any).documents(uid).findMany({ |
@@ -54,20 +53,17 @@ export default { |
54 | 53 | const { contentType, documentId } = ctx.params; |
55 | 54 |
|
56 | 55 | if (typeof contentType !== 'string' || !(contentType in strapi.contentTypes)) { |
57 | | - ctx.throw(400, `Unknown or invalid content type: ${contentType}`); |
58 | | - return; |
| 56 | + throw new errors.ValidationError(`Unknown or invalid content type: ${contentType}`); |
59 | 57 | } |
60 | 58 |
|
61 | | - const mainField = await getMainField(contentType as UID.CollectionType); |
62 | | - |
| 59 | + const mainField = await getPluginService('get-main-field').getMainField(contentType as UID.CollectionType); |
63 | 60 | const entry = await (strapi as any).documents(contentType as UID.CollectionType).findOne({ |
64 | 61 | documentId, |
65 | 62 | fields: ['id', 'documentId', ...(mainField ? [mainField] : [])], |
66 | 63 | }); |
67 | 64 |
|
68 | 65 | if (!entry) { |
69 | | - ctx.throw(404, 'Entry not found'); |
70 | | - return; |
| 66 | + throw new errors.ValidationError('Entry not found'); |
71 | 67 | } |
72 | 68 |
|
73 | 69 | ctx.body = { |
|
0 commit comments