11import path from 'path' ;
22import fs from 'fs-extra' ;
33import { Client , ResourceServer } from 'auth0' ;
4- import { constants } from '../../../tools' ;
4+ import { constants , keywordReplace } from '../../../tools' ;
55
66import {
77 getFiles ,
@@ -16,6 +16,7 @@ import DirectoryContext from '..';
1616import { ParsedAsset } from '../../../types' ;
1717import { ClientGrant } from '../../../tools/auth0/handlers/clientGrants' ;
1818import { paginate } from '../../../tools/auth0/client' ;
19+ import { doesHaveKeywordMarker } from '../../../keywordPreservation' ;
1920
2021type ParsedClientGrants = ParsedAsset < 'clientGrants' , ClientGrant [ ] > ;
2122
@@ -71,28 +72,41 @@ async function dump(context: DirectoryContext): Promise<void> {
7172 }
7273
7374 const clientName = ( ( ) => {
74- const associatedClient = allClients . find ( ( client ) => {
75- return client . client_id === grant . client_id ;
76- } ) ;
75+ const associatedClient = allClients . find ( ( client ) => client . client_id === grant . client_id ) ;
7776
7877 if ( associatedClient === undefined ) return grant . client_id ;
7978
8079 return associatedClient . name ;
8180 } ) ( ) ;
8281
83- const apiName = ( ( ) => {
84- const associatedAPI = allResourceServers . find ( ( resourceServer ) => {
85- return resourceServer . identifier === grant . audience ;
86- } ) ;
82+ // Convert audience to the API name for readability
83+ const apiName = ( grantAudience : string ) => {
84+ const associatedAPI = allResourceServers . find (
85+ ( resourceServer ) => resourceServer . identifier === grantAudience
86+ ) ;
8787
88- if ( associatedAPI === undefined ) return grant . audience ;
88+ if ( associatedAPI === undefined ) return grantAudience ; // Use the audience if the API is not found
8989
90- return associatedAPI . name ;
91- } ) ( ) ;
90+ return associatedAPI . name ; // Use the name of the API
91+ } ;
9292
93- const name = sanitize ( `${ clientName } -${ apiName } ` ) ;
94- const grantFile = path . join ( grantsFolder , `${ name } .json` ) ;
93+ // Replace keyword markers if necessary
94+ const clientNameNonMarker = doesHaveKeywordMarker ( clientName , context . mappings )
95+ ? keywordReplace ( clientName , context . mappings )
96+ : clientName ;
97+ const apiAudienceNonMarker = doesHaveKeywordMarker ( grant . audience , context . mappings )
98+ ? keywordReplace ( grant . audience , context . mappings )
99+ : grant . audience ;
100+
101+ // Construct the name using non-marker names
102+ const name = sanitize ( `${ clientNameNonMarker } -${ apiName ( apiAudienceNonMarker ) } ` ) ;
95103
104+ // Ensure the name is not empty or invalid
105+ if ( ! name || name . trim ( ) . length === 0 ) {
106+ throw new Error ( `Invalid name generated for client grant: ${ JSON . stringify ( grant ) } ` ) ;
107+ }
108+
109+ const grantFile = path . join ( grantsFolder , `${ name } .json` ) ;
96110 dumpJSON ( grantFile , dumpGrant ) ;
97111 } ) ;
98112}
0 commit comments