Skip to content

Commit 427746d

Browse files
committed
refactor: rename the router controller to 'find' for fetching with a read-only API token
1 parent 0625149 commit 427746d

File tree

3 files changed

+48
-33
lines changed

3 files changed

+48
-33
lines changed

.changeset/hungry-planes-return.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"strapi-plugin-webtools": patch
3+
---
4+
5+
refactor: rename the router controller to 'find' for fetching with a read-only API token

packages/core/server/controllers/core.ts

Lines changed: 42 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
1245
export 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
};

packages/core/server/routes/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export default {
4545
{
4646
method: 'GET',
4747
path: '/router',
48-
handler: 'core.router',
48+
handler: 'core.find',
4949
config: {
5050
policies: [],
5151
},

0 commit comments

Comments
 (0)