Skip to content

Commit

Permalink
Merge pull request #118 from pluginpal/feature/fallback-pattern
Browse files Browse the repository at this point in the history
Feature/fallback pattern
  • Loading branch information
boazpoolman authored Feb 4, 2024
2 parents ea48dad + bc0d321 commit 43f79b9
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/friendly-rockets-exercise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@pluginpal/webtools-core": patch
---

Added default value for url_pattern
5 changes: 5 additions & 0 deletions .changeset/grumpy-grapes-smile.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@pluginpal/webtools-core": patch
---

Make pluralName allowed in url pattern
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
18.18.2
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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 <a href="https://lu.ma/strapihacks">Strapi Plugin Week</a> 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.
Expand Down
2 changes: 2 additions & 0 deletions packages/core/server/admin-api/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export interface Config {
website_url: string;
default_pattern: string,
}

const config: {
Expand All @@ -8,6 +9,7 @@ const config: {
} = {
default: {
website_url: null,
default_pattern: '/[pluralName]/[id]',
},
validator() {},
};
Expand Down
3 changes: 2 additions & 1 deletion packages/core/server/admin-api/controllers/url-pattern.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ const controller = () => ({

const fields = getPluginService('urlPatternService').getAllowedFields(
contentType,
['string', 'uid', 'id'],
['pluralName', 'string', 'uid', 'id'],
);
formattedFields[contentType.uid] = fields;
});
Expand All @@ -76,6 +76,7 @@ const controller = () => ({
const contentType = strapi.contentTypes[modelName];

const fields = urlPatternService.getAllowedFields(contentType, [
'pluralName',
'string',
'uid',
'id',
Expand Down
23 changes: 19 additions & 4 deletions packages/core/server/admin-api/services/url-pattern.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import _ from 'lodash';
import { EntityService, Schema } from '@strapi/strapi';
import { Common } from '@strapi/types';

import { getPluginService } from '../../util/getPluginService';

Expand Down Expand Up @@ -123,6 +124,10 @@ export default () => ({
fields.push('id');
}

if (allowedFields.includes('pluralName')) {
fields.push('pluralName');
}

return fields;
},

Expand Down Expand Up @@ -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<string> => {
const resolve = (pattern: string) => {
let resolvedPattern: string = pattern;
const fields = getPluginService('urlPatternService').getFieldsFromPattern(pattern);
Expand All @@ -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 || '');
Expand All @@ -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;
},

Expand Down

0 comments on commit 43f79b9

Please sign in to comment.