1- import {
2- ACL_ALL_ABILITIES ,
3- ACL_APP_ABILITIES ,
4- CASL_ABILITY_BINDING ,
5- CASL_ABILITY_IMPORT ,
6- } from "src/generators/const/acl.const" ;
7- import { SchemaResolver } from "src/generators/core/SchemaResolver.class" ;
1+ import { ACL_APP_ABILITIES , CASL_ABILITY_BINDING , CASL_ABILITY_IMPORT } from "src/generators/const/acl.const" ;
82import { GenerateType , GenerateTypeParams , Import } from "src/generators/types/generate" ;
9- import { getUniqueArray } from "src/generators/utils/array.utils" ;
10- import { getTagAllAbilitiesName } from "src/generators/utils/generate/generate.acl.utils" ;
11- import { getEntityImports , getModelsImports } from "src/generators/utils/generate/generate.imports.utils" ;
3+ import { getAclData , getAppAbilitiesType } from "src/generators/utils/generate/generate.acl.utils" ;
124import { getNamespaceName } from "src/generators/utils/generate/generate.utils" ;
135import { getHbsTemplateDelegate } from "src/generators/utils/hbs/hbs-template.utils" ;
146
157export function generateAcl ( { resolver, data, tag = "" } : GenerateTypeParams ) {
16- const endpoints = data . get ( tag ) ?. endpoints . filter ( ( { acl } ) => acl && acl . length > 0 ) ;
17- if ( ! endpoints || endpoints . length === 0 ) {
8+ const aclData = getAclData ( { resolver , data , tag } ) ;
9+ if ( ! aclData ) {
1810 return ;
1911 }
2012
21- const hasAdditionalAbilityImports = endpoints . some (
22- ( { acl } ) => acl ?. [ 0 ] . conditions && Object . keys ( acl [ 0 ] . conditions ) . length > 0 ,
23- ) ;
13+ const { hasAdditionalAbilityImports, modelsImports, endpoints } = aclData ;
14+
2415 const caslAbilityTupleImport : Import = {
2516 bindings : [
2617 CASL_ABILITY_BINDING . abilityTuple ,
@@ -29,20 +20,6 @@ export function generateAcl({ resolver, data, tag = "" }: GenerateTypeParams) {
2920 from : CASL_ABILITY_IMPORT . from ,
3021 } ;
3122
32- const aclZodSchemas = endpoints . reduce ( ( acc , endpoint ) => {
33- const zodSchemas = endpoint . acl ?. [ 0 ] . conditionsTypes ?. reduce (
34- ( acc , propertyType ) => [ ...acc , ...( propertyType ?. zodSchemaName ? [ propertyType . zodSchemaName ] : [ ] ) ] ,
35- [ ] as string [ ] ,
36- ) ;
37- return [ ...acc , ...( zodSchemas ?? [ ] ) ] ;
38- } , [ ] as string [ ] ) ;
39-
40- const modelsImports = getModelsImports ( {
41- resolver,
42- tag,
43- zodSchemasAsTypes : getUniqueArray ( aclZodSchemas ) ,
44- } ) ;
45-
4623 const hbsTemplate = getHbsTemplateDelegate ( resolver , "acl" ) ;
4724
4825 return hbsTemplate ( {
@@ -54,35 +31,25 @@ export function generateAcl({ resolver, data, tag = "" }: GenerateTypeParams) {
5431 } ) ;
5532}
5633
57- export function generateAppAcl ( resolver : SchemaResolver , tags : string [ ] ) {
34+ export function generateAppAcl ( { resolver, data } : Omit < GenerateTypeParams , "tag" > ) {
35+ const { appAbilitiesType, hasAdditionalAbilityImports, modelsImports } = getAppAbilitiesType ( { resolver, data } ) ;
36+
5837 const caslAbilityTupleImport : Import = {
5938 bindings : [
6039 CASL_ABILITY_BINDING . pureAbility ,
61- ...( tags . length === 0 ? [ CASL_ABILITY_BINDING . subjectType , CASL_ABILITY_BINDING . abilityTuple ] : [ ] ) ,
40+ CASL_ABILITY_BINDING . abilityTuple ,
41+ ...( hasAdditionalAbilityImports ? [ CASL_ABILITY_BINDING . forcedSubject ] : [ ] ) ,
6242 ] ,
6343 from : CASL_ABILITY_IMPORT . from ,
6444 } ;
6545
66- const imports = getEntityImports ( {
67- tags,
68- entityName : ACL_ALL_ABILITIES ,
69- getAliasEntityName : getTagAllAbilitiesName ,
70- type : GenerateType . Acl ,
71- options : resolver . options ,
72- } ) ;
73-
74- const namespaces = tags . map ( ( tag ) => getNamespaceName ( { type : GenerateType . Acl , tag, options : resolver . options } ) ) ;
75-
7646 const hbsTemplate = getHbsTemplateDelegate ( resolver , "app-acl" ) ;
7747
7848 return hbsTemplate ( {
7949 caslAbilityTupleImport,
80- imports ,
81- allAbilities : ACL_ALL_ABILITIES ,
50+ modelsImports ,
51+ appAbilitiesType ,
8252 appAbilities : ACL_APP_ABILITIES ,
83- includeNamespace : resolver . options . tsNamespaces ,
84- tags,
85- namespaces,
8653 abilityTuple : CASL_ABILITY_BINDING . abilityTuple ,
8754 subjectType : CASL_ABILITY_BINDING . subjectType ,
8855 } ) ;
0 commit comments