Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/short-rivers-fly.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"strapi-plugin-webtools": minor
"docs": minor
---

feat: new configuration option called 'router_use_controllers'
2 changes: 2 additions & 0 deletions packages/core/server/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export interface Config {
website_url: string;
default_pattern: string,
unique_per_locale: boolean,
router_use_controllers: boolean,
slugify: (fieldValue: string) => string,
}

Expand All @@ -14,6 +15,7 @@ const config: {
validator: () => void
} = {
default: {
router_use_controllers: false,
website_url: null,
default_pattern: '/[pluralName]/[documentId]',
slugify: (fieldValue) => kebabCase(deburr(toLower(fieldValue))),
Expand Down
59 changes: 59 additions & 0 deletions packages/core/server/controllers/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,56 @@ import { Schema } from '@strapi/strapi';
import { getPluginService } from '../util/getPluginService';
import { sanitizeOutput } from '../util/sanitizeOutput';

type EntityResponse = { data: {}, meta: {} };

const routerWithControllers = async (ctx: Context) => {
const { path, ...searchQuery } = ctx.query;

// Find related entity by path.
const { entity, contentType } = await getPluginService('url-alias').findRelatedEntity(path as string, {
...searchQuery,
fields: ['documentId'],
});

const isSingleType = strapi.contentTypes[contentType].kind === 'singleType';
let controllerEntity: EntityResponse = null;

// Query the full entity using the content type controller.
if (isSingleType) {
controllerEntity = await strapi.controllers[contentType].find(ctx, async () => {}) as
EntityResponse;
} else {
controllerEntity = await strapi.controllers[contentType].findOne({
...ctx,
query: {
...ctx.query,
},
params: {
...ctx.params as {},
id: entity.documentId,
},
}, async () => {}) as EntityResponse;
}

if (!controllerEntity) {
ctx.notFound();
return null;
}

// Add content type to response.
const responseEntity = {
data: {
...controllerEntity.data,
contentType,
},
meta: {
...controllerEntity.meta,
},
};

return responseEntity;
};

/**
* Router controller
*/
Expand All @@ -14,6 +64,15 @@ export default {
const { path, ...searchQuery } = ctx.query;
const { auth } = ctx.state;

const routerUseControllers = strapi.config.get('plugin::webtools.router_use_controllers', false);

if (routerUseControllers) {
const entity = await routerWithControllers(ctx);
ctx.body = entity;
return;
}

// Find related entity by path.
const { entity, contentType } = await getPluginService('url-alias').findRelatedEntity(path as string, searchQuery);

if (!entity) {
Expand Down
22 changes: 22 additions & 0 deletions packages/docs/docs/configuration/router-use-controllers.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
sidebar_label: 'Router use controllers'
displayed_sidebar: webtoolsSidebar
slug: /configuration/router-use-controllers
---

# Router use controllers

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.

:::note
To make use of this feature you will need to enable the `findOne` permission of the specific content type.
:::

In the future this might become the default behavior but that will cause a breaking change in the current behavior.

| Name | Details |
| ---- | ------- |
| Key | `router_use_controllers` |
| Required | false |
| Type | boolean |
| Default | false |
1 change: 1 addition & 0 deletions packages/docs/sidebars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ const sidebars = {
items: [
"configuration/introduction",
"configuration/default-pattern",
"configuration/router-use-controllers",
"configuration/website-url",
"configuration/slugify",
"configuration/unique-per-locale",
Expand Down