@@ -9,37 +9,47 @@ import { sanitizeOutput } from '../util/sanitizeOutput';
99 * Router controller
1010 */
1111
12+ const routerController = async ( ctx : Context ) => {
13+ const { path, ...searchQuery } = ctx . query ;
14+ const { auth } = ctx . state ;
15+
16+ const { entity, contentType } = await getPluginService ( 'url-alias' ) . findRelatedEntity ( path as string , searchQuery ) ;
17+
18+ if ( ! entity ) {
19+ ctx . notFound ( ) ;
20+ return ;
21+ }
22+
23+ // Check 'find' permissions for the content type we're querying.
24+ // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
25+ await strapi . auth . verify ( auth , { scope : [ `${ contentType } .find` ] } ) ;
26+
27+ // Add content type to response.
28+ const responseEntity = {
29+ ...entity ,
30+ contentType,
31+ } ;
32+
33+ const contentTypeObj = strapi . contentTypes [ contentType ] as Schema . ContentType ;
34+
35+ // Format response.
36+ const sanitizedEntity = await sanitizeOutput ( responseEntity , contentTypeObj , auth ) ;
37+ ctx . body = await strapi . controller ( contentType as UID . Controller )
38+ // @ts -expect-error
39+ // The strapi object is typed in a way that the following is expected to be a controller.
40+ // In fact that is not true, as this also exposes the helper functions of the controller.
41+ // That is the reason we put a ts-expect-error here.
42+ . transformResponse ( sanitizedEntity , { } ) ;
43+ } ;
44+
1245export default {
13- router : async ( ctx : Context ) => {
14- const { path, ...searchQuery } = ctx . query ;
15- const { auth } = ctx . state ;
16-
17- const { entity, contentType } = await getPluginService ( 'url-alias' ) . findRelatedEntity ( path as string , searchQuery ) ;
18-
19- if ( ! entity ) {
20- ctx . notFound ( ) ;
21- return ;
22- }
23-
24- // Check 'find' permissions for the content type we're querying.
25- // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
26- await strapi . auth . verify ( auth , { scope : [ `${ contentType } .find` ] } ) ;
27-
28- // Add content type to response.
29- const responseEntity = {
30- ...entity ,
31- contentType,
32- } ;
33-
34- const contentTypeObj = strapi . contentTypes [ contentType ] as Schema . ContentType ;
35-
36- // Format response.
37- const sanitizedEntity = await sanitizeOutput ( responseEntity , contentTypeObj , auth ) ;
38- ctx . body = await strapi . controller ( contentType as UID . Controller )
39- // @ts -expect-error
40- // The strapi object is typed in a way that the following is expected to be a controller.
41- // In fact that is not true, as this also exposes the helper functions of the controller.
42- // That is the reason we put a ts-expect-error here.
43- . transformResponse ( sanitizedEntity , { } ) ;
44- } ,
46+ /**
47+ * @description Name the controller 'find' to allow it to be fetched by a read-only API token.
48+ */
49+ find : routerController ,
50+ /**
51+ * @deprecated Use 'find' instead.
52+ * @description Will be removed in future versions.
53+ */
54+ router : routerController ,
4555} ;
0 commit comments