@@ -2,7 +2,8 @@ import { Modules, Data } from '@strapi/strapi';
22import { isContentTypeEnabled } from '../util/enabledContentTypes' ;
33import { getPluginService } from '../util/getPluginService' ;
44
5- const createMiddleware : Modules . Documents . Middleware . Middleware = async ( context , next ) => {
5+ // eslint-disable-next-line max-len
6+ const generateUrlAliasMiddleware : Modules . Documents . Middleware . Middleware = async ( context , next ) => {
67 const { uid, action } = context ;
78 const hasWT = isContentTypeEnabled ( uid ) ;
89
@@ -11,12 +12,12 @@ const createMiddleware: Modules.Documents.Middleware.Middleware = async (context
1112 return next ( ) ;
1213 }
1314
14- // Run this middleware only for the create action.
15- if ( action !== 'create' ) {
15+ // Run this middleware only for the create, update and clone action.
16+ if ( ! [ 'clone' , 'create' , 'update' ] . includes ( action ) ) {
1617 return next ( ) ;
1718 }
1819
19- const params = context . params as Modules . Documents . ServiceParams < 'api::test.test' > [ 'create' ] ;
20+ const params = context . params as Modules . Documents . ServiceParams < 'api::test.test' > [ 'create' | 'update' | 'clone' ] ;
2021
2122 // Fetch the URL pattern for this content type.
2223 let relations : string [ ] = [ ] ;
@@ -38,37 +39,41 @@ const createMiddleware: Modules.Documents.Middleware.Middleware = async (context
3839 } ) ;
3940 } ) ) ;
4041
41- // If a URL alias was created, fetch it.
42- if ( params . data . url_alias ?. [ 0 ] ) {
43- urlAliasEntity = await strapi . documents ( 'plugin::webtools.url-alias' ) . findOne ( {
44- documentId : params . data . url_alias [ 0 ] ,
45- } ) ;
46- }
47-
48- // If a URL alias was created and 'generated' is set to false, do nothing.
49- if ( urlAliasEntity ?. generated === false ) {
50- return next ( ) ;
51- }
52-
53- // Ideally here we would create the URL alias an directly fire
54- // the `service.create.call` function with the new URL alias id.
55- // Though it is possible that the `id` field is used in the URL.
56- // In that case we have to create the entity first. Then when we know
57- // the id, can we create the URL alias entity and can we update
58- // the previously created entity.
59- const newEntity = await next ( ) as Modules . Documents . AnyDocument ;
42+ // Fire the action.
43+ const entity = await next ( ) as Modules . Documents . AnyDocument ;
6044
45+ // Fetch the full entity.
6146 const fullEntity = await strapi . documents ( uid as 'api::test.test' ) . findOne ( {
62- documentId : newEntity . documentId ,
47+ documentId : entity . documentId ,
6348 populate : {
49+ ...relations . reduce ( ( obj , key ) => ( { ...obj , [ key ] : { } } ) , { } ) ,
50+ url_alias : {
51+ fields : [ 'id' , 'generated' ] ,
52+ } ,
6453 localizations : {
6554 populate : {
66- url_alias : true ,
55+ url_alias : {
56+ fields : [ 'id' ] ,
57+ } ,
6758 } ,
6859 } ,
6960 } ,
7061 } ) ;
7162
63+ // If the document already has an URL alias, fetch it.
64+ if ( params . data . url_alias ?. [ 0 ] ) {
65+ urlAliasEntity = await strapi . documents ( 'plugin::webtools.url-alias' ) . findOne ( {
66+ documentId : params . data . url_alias [ 0 ] ,
67+ } ) ;
68+ } else if ( fullEntity . url_alias [ 0 ] ) {
69+ [ urlAliasEntity ] = fullEntity . url_alias ;
70+ }
71+
72+ // If the URL alias has 'generated' set to false, do nothing.
73+ if ( urlAliasEntity ?. generated === false ) {
74+ return next ( ) ;
75+ }
76+
7277 // Fetch the URL alias localizations.
7378 const urlAliasLocalizations = fullEntity . localizations
7479 // @todo check all url aliases, not just the first.
@@ -129,14 +134,9 @@ const createMiddleware: Modules.Documents.Middleware.Middleware = async (context
129134 } ) ) ;
130135
131136 // Eventually update the entity to include the URL alias.
132- const updatedEntity = await strapi . documents ( uid as 'api::test.test' ) . update ( {
133- documentId : fullEntity . documentId ,
134- data : {
135- url_alias : [ urlAliasEntity . documentId ] ,
136- } ,
137- } ) ;
137+ params . data . url_alias = [ urlAliasEntity ?. documentId ] ;
138138
139- return updatedEntity ;
139+ return next ( ) ;
140140} ;
141141
142- export default createMiddleware ;
142+ export default generateUrlAliasMiddleware ;
0 commit comments