diff --git a/.changeset/friendly-rockets-exercise.md b/.changeset/friendly-rockets-exercise.md new file mode 100644 index 00000000..df447f44 --- /dev/null +++ b/.changeset/friendly-rockets-exercise.md @@ -0,0 +1,5 @@ +--- +"@pluginpal/webtools-core": patch +--- + +Added default value for url_pattern diff --git a/.changeset/grumpy-grapes-smile.md b/.changeset/grumpy-grapes-smile.md new file mode 100644 index 00000000..c6fb6745 --- /dev/null +++ b/.changeset/grumpy-grapes-smile.md @@ -0,0 +1,5 @@ +--- +"@pluginpal/webtools-core": patch +--- + +Make pluralName allowed in url pattern diff --git a/.nvmrc b/.nvmrc new file mode 100644 index 00000000..87ec8842 --- /dev/null +++ b/.nvmrc @@ -0,0 +1 @@ +18.18.2 diff --git a/README.md b/README.md index 6adb81a2..7e34b3aa 100644 --- a/README.md +++ b/README.md @@ -98,6 +98,7 @@ The following field types are allowed in a pattern: - `id` - `uid` - `string` +- `pluralName` See below a screenshot of the URL pattern creation form in Strapi. @@ -114,6 +115,21 @@ await fetch(`${API_URL}/api/webtools/router?path=/about-page`); // GET /api/webtools/router?path=/about-page ``` +## 🔧 Config +Config can be changed in the `config/plugins.js` file in your Strapi project. +You can overwrite the config like so: + +``` +module.exports = ({ env }) => ({ + // ... + 'webtools': { + config: { + website_url: string; + default_pattern: string, + }, + }, +}); +``` ## ✨ #StrapiPluginWeek This plugin was initially made as an entry to the Strapi Plugin Week hackathon. Back then it went under the name "URL alias". Since then a lot has changed and the plugin has been renamed to Webtools where the URL alias feature is just a part of it. diff --git a/packages/core/server/admin-api/config.ts b/packages/core/server/admin-api/config.ts index dd6920ec..8f450532 100644 --- a/packages/core/server/admin-api/config.ts +++ b/packages/core/server/admin-api/config.ts @@ -1,5 +1,6 @@ export interface Config { website_url: string; + default_pattern: string, } const config: { @@ -8,6 +9,7 @@ const config: { } = { default: { website_url: null, + default_pattern: '/[pluralName]/[id]', }, validator() {}, }; diff --git a/packages/core/server/admin-api/controllers/url-pattern.ts b/packages/core/server/admin-api/controllers/url-pattern.ts index 126aa8b9..ac2cfc49 100644 --- a/packages/core/server/admin-api/controllers/url-pattern.ts +++ b/packages/core/server/admin-api/controllers/url-pattern.ts @@ -61,7 +61,7 @@ const controller = () => ({ const fields = getPluginService('urlPatternService').getAllowedFields( contentType, - ['string', 'uid', 'id'], + ['pluralName', 'string', 'uid', 'id'], ); formattedFields[contentType.uid] = fields; }); @@ -76,6 +76,7 @@ const controller = () => ({ const contentType = strapi.contentTypes[modelName]; const fields = urlPatternService.getAllowedFields(contentType, [ + 'pluralName', 'string', 'uid', 'id', diff --git a/packages/core/server/admin-api/services/url-pattern.ts b/packages/core/server/admin-api/services/url-pattern.ts index cf0af8a6..fe954b32 100644 --- a/packages/core/server/admin-api/services/url-pattern.ts +++ b/packages/core/server/admin-api/services/url-pattern.ts @@ -2,6 +2,7 @@ import _ from 'lodash'; import { EntityService, Schema } from '@strapi/strapi'; +import { Common } from '@strapi/types'; import { getPluginService } from '../../util/getPluginService'; @@ -123,6 +124,10 @@ export default () => ({ fields.push('id'); } + if (allowedFields.includes('pluralName')) { + fields.push('pluralName'); + } + return fields; }, @@ -150,7 +155,10 @@ export default () => ({ * @returns {string} The path. */ - resolvePattern: async (uid: string, entity: { [key: string]: string | number }) => { + resolvePattern: async ( + uid: Common.UID.ContentType, + entity: { [key: string]: string | number }, + ): Promise => { const resolve = (pattern: string) => { let resolvedPattern: string = pattern; const fields = getPluginService('urlPatternService').getFieldsFromPattern(pattern); @@ -159,7 +167,15 @@ export default () => ({ const relationalField = field.split('.').length > 1 ? field.split('.') : null; // TODO: Relation fields. - if (!relationalField) { + if (field === 'pluralName') { + const fieldValue = strapi.contentTypes[uid].info.pluralName; + + if (!fieldValue) { + return; + } + + resolvedPattern = resolvedPattern.replace(`[${field}]`, fieldValue || ''); + } else if (!relationalField) { // Slugify. const fieldValue = _.kebabCase(_.deburr(_.toLower(String(entity[field])))); resolvedPattern = resolvedPattern.replace(`[${field}]`, fieldValue || ''); @@ -183,11 +199,10 @@ export default () => ({ }); if (!patterns[0]) { - return ''; + return resolve(strapi.config.get('plugin.webtools.default_pattern')); } const path = resolve(patterns[0].pattern); - return path; },