Skip to content

Commit 28b5353

Browse files
committed
feat: new configuration option called 'router_use_controllers'
1 parent 377c519 commit 28b5353

File tree

5 files changed

+88
-3
lines changed

5 files changed

+88
-3
lines changed

.changeset/short-rivers-fly.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"strapi-plugin-webtools": minor
3+
"docs": minor
4+
---
5+
6+
feat: new configuration option called 'router_use_controllers'

packages/core/server/config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export interface Config {
66
website_url: string;
77
default_pattern: string,
88
unique_per_locale: boolean,
9+
router_use_controllers: boolean,
910
slugify: (fieldValue: string) => string,
1011
}
1112

@@ -14,6 +15,7 @@ const config: {
1415
validator: () => void
1516
} = {
1617
default: {
18+
router_use_controllers: false,
1719
website_url: null,
1820
default_pattern: '/[pluralName]/[documentId]',
1921
slugify: (fieldValue) => kebabCase(deburr(toLower(fieldValue))),

packages/core/server/controllers/core.ts

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,56 @@ import { Schema } from '@strapi/strapi';
55
import { getPluginService } from '../util/getPluginService';
66
import { sanitizeOutput } from '../util/sanitizeOutput';
77

8+
type EntityResponse = { data: {}, meta: {} };
9+
10+
const routerWithControllers = async (ctx: Context) => {
11+
const { path, ...searchQuery } = ctx.query;
12+
13+
// Find related entity by path.
14+
const { entity, contentType } = await getPluginService('url-alias').findRelatedEntity(path as string, {
15+
...searchQuery,
16+
fields: ['documentId'],
17+
});
18+
19+
const isSingleType = strapi.contentTypes[contentType].kind === 'singleType';
20+
let controllerEntity: EntityResponse = null;
21+
22+
// Query the full entity using the content type controller.
23+
if (isSingleType) {
24+
controllerEntity = await strapi.controllers[contentType].find(ctx, async () => {}) as
25+
EntityResponse;
26+
} else {
27+
controllerEntity = await strapi.controllers[contentType].findOne({
28+
...ctx,
29+
query: {
30+
...ctx.query,
31+
},
32+
params: {
33+
...ctx.params as {},
34+
id: entity.documentId,
35+
},
36+
}, async () => {}) as EntityResponse;
37+
}
38+
39+
if (!controllerEntity) {
40+
ctx.notFound();
41+
return null;
42+
}
43+
44+
// Add content type to response.
45+
const responseEntity = {
46+
data: {
47+
...controllerEntity.data,
48+
contentType,
49+
},
50+
meta: {
51+
...controllerEntity.meta,
52+
},
53+
};
54+
55+
return responseEntity;
56+
};
57+
858
/**
959
* Router controller
1060
*/
@@ -14,13 +64,17 @@ export default {
1464
const { path, ...searchQuery } = ctx.query;
1565
const { auth } = ctx.state;
1666

17-
const { entity, contentType } = await getPluginService('url-alias').findRelatedEntity(path as string, searchQuery);
67+
const routerUseControllers = strapi.config.get('plugin::webtools.router_use_controllers', false);
1868

19-
if (!entity) {
20-
ctx.notFound();
69+
if (routerUseControllers) {
70+
const entity = await routerWithControllers(ctx);
71+
ctx.body = entity;
2172
return;
2273
}
2374

75+
// Find related entity by path.
76+
const { entity, contentType } = await getPluginService('url-alias').findRelatedEntity(path as string, searchQuery);
77+
2478
// Check 'find' permissions for the content type we're querying.
2579
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
2680
await strapi.auth.verify(auth, { scope: [`${contentType}.find`] });
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
sidebar_label: 'Router use controllers'
3+
displayed_sidebar: webtoolsSidebar
4+
slug: /configuration/router-use-controllers
5+
---
6+
7+
# Router use controllers
8+
9+
The [Webtools Router](/api/rest#router) endpoint has an option to make use of the core controllers of your content types. That means that you can extend your controllers as you're used to and the result will be returned by the Router endpoint by of Webtools.
10+
11+
:::note
12+
To make use of this feature you will need to enable the `findOne` permission of the specific content type.
13+
:::
14+
15+
In the future this might become the default behavior but that will cause a breaking change in the current behavior.
16+
17+
| Name | Details |
18+
| ---- | ------- |
19+
| Key | `router_use_controllers` |
20+
| Required | false |
21+
| Type | boolean |
22+
| Default | false |

packages/docs/sidebars.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ const sidebars = {
8080
items: [
8181
"configuration/introduction",
8282
"configuration/default-pattern",
83+
"configuration/router-use-controllers",
8384
"configuration/website-url",
8485
"configuration/slugify",
8586
"configuration/unique-per-locale",

0 commit comments

Comments
 (0)