Skip to content

Commit f80e102

Browse files
committed
Acl check imported functions fix
1 parent 062899f commit f80e102

13 files changed

Lines changed: 70 additions & 15 deletions

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@povio/openapi-codegen-cli",
3-
"version": "0.13.5",
3+
"version": "0.13.6",
44
"main": "./dist/index.js",
55
"bin": {
66
"openapi-codegen": "./dist/sh.js"

src/generators/generate/generateQueries.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { QUERIES_MODULE_NAME, QUERY_HOOKS, QUERY_IMPORT } from "../const/queries
55
import { EndpointParameter } from "../types/endpoint";
66
import { GenerateType, GenerateTypeParams, Import } from "../types/generate";
77
import { getUniqueArray } from "../utils/array.utils";
8-
import { getEndpointsImports, getModelsImports } from "../utils/generate/generate.imports.utils";
8+
import { getAclImports, getEndpointsImports, getModelsImports } from "../utils/generate/generate.imports.utils";
99
import {
1010
getAclCheckImportPath,
1111
getMutationEffectsImportPath,
@@ -57,7 +57,8 @@ export function generateQueries({ resolver, data, tag = "" }: GenerateTypeParams
5757
from: getMutationEffectsImportPath(resolver.options),
5858
};
5959

60-
const hasAclCheck = resolver.options.checkAcl && endpoints.some((endpoint) => endpoint.acl);
60+
const aclEndpoints = endpoints.filter((endpoint) => endpoint.acl);
61+
const hasAclCheck = resolver.options.checkAcl && aclEndpoints.length > 0;
6162
const aclCheckImport: Import = {
6263
bindings: [ACL_CHECK_HOOK],
6364
from: getAclCheckImportPath(resolver.options),
@@ -87,6 +88,12 @@ export function generateQueries({ resolver, data, tag = "" }: GenerateTypeParams
8788
options: resolver.options,
8889
});
8990

91+
const aclImports = getAclImports({
92+
tag,
93+
endpoints: aclEndpoints,
94+
options: resolver.options,
95+
});
96+
9097
const hbsTemplate = getHbsTemplateDelegate(resolver, "queries");
9198

9299
return hbsTemplate({
@@ -102,6 +109,7 @@ export function generateQueries({ resolver, data, tag = "" }: GenerateTypeParams
102109
queryTypesImport,
103110
modelsImports,
104111
endpointsImports,
112+
aclImports,
105113
includeNamespace: resolver.options.tsNamespaces,
106114
tag,
107115
namespace: getNamespaceName({ type: GenerateType.Queries, tag, options: resolver.options }),
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
checkAcl({{importedAbilityFunctionName endpoint}}({{#if generateAclCheckParams}}{ {{#each (abilityConditionsTypes endpoint) as | propertyType |}}{{propertyType.name}}, {{/each}} } {{/if}}));

src/generators/templates/partials/acl-check-params.hbs

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/generators/templates/partials/query-use-infinite-query.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export const {{infiniteQueryName endpoint}} = <TData>({{#if (endpointParams endp
88
return {{infiniteQueryHook}}({
99
queryKey: keys.{{endpointName endpoint}}Infinite({{#if (endpointParams endpoint)}}{{{endpointArgs endpoint excludePageParam=true}}}{{/if}}),
1010
queryFn: ({ pageParam }) => {{#if hasQueryFnBody}}{ {{/if}}
11-
{{#if hasAclCheck}}checkAcl({{genAclCheckParams endpoint}});{{/if}}
11+
{{#if hasAclCheck}}{{{genAclCheckCall endpoint}}}{{/if}}
1212
{{#if hasQueryFnBody}}return {{/if}}{{importedEndpointName endpoint}}({{{endpointArgs endpoint replacePageParam=true}}}{{#if hasAxiosRequestConfig}}, {{axiosRequestConfigName}}{{/if}})
1313
{{#if hasQueryFnBody}} }{{/if}},
1414
initialPageParam: 1,

src/generators/templates/partials/query-use-mutation.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export const {{queryName endpoint mutation=true}} = (options?: AppMutationOption
99

1010
return {{queryHook}}({
1111
mutationFn: {{#if endpoint.mediaUpload}}async {{/if}}({{#if (endpointParams endpoint includeFileParam=true)}} { {{{endpointArgs endpoint includeFileParam=true}}} } {{/if}}) => {{#if hasMutationFnBody}} { {{/if}}
12-
{{#if hasAclCheck}}checkAcl({{genAclCheckParams endpoint}});{{/if}}
12+
{{#if hasAclCheck}}{{{genAclCheckCall endpoint}}}{{/if}}
1313
{{#if endpoint.mediaUpload}}const uploadInstructions = await {{importedEndpointName endpoint}}({{{endpointArgs endpoint}}}{{#if hasAxiosRequestConfig}}{{#if (endpointArgs endpoint)}}, {{/if}}{{axiosRequestConfigName}}{{/if}});
1414

1515
if (file && uploadInstructions.url) {

src/generators/templates/partials/query-use-query.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export const {{queryName endpoint}} = <TData>({{#if (endpointParams endpoint)}}{
88
return {{queryHook}}({
99
queryKey: keys.{{endpointName endpoint}}({{#if (endpointParams endpoint)}}{{{endpointArgs endpoint}}}{{/if}}),
1010
queryFn: {{#if hasQueryFn}}() => {{#if hasQueryFnBody}}{ {{/if}}
11-
{{#if hasAclCheck}}checkAcl({{genAclCheckParams endpoint}});{{/if}}
11+
{{#if hasAclCheck}}{{{genAclCheckCall endpoint}}}{{/if}}
1212
{{#if hasQueryFnBody}}return {{/if}}{{importedEndpointName endpoint}}({{{endpointArgs endpoint}}}{{#if hasAxiosRequestConfig}}{{#if (endpointArgs endpoint)}}, {{/if}}{{axiosRequestConfigName}}{{/if}}){{else}}{{importedEndpointName endpoint}}{{/if}}
1313
{{#if hasQueryFnBody}} }{{/if}},
1414
...options,

src/generators/templates/queries.hbs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@
2626
{{#each endpointsImports as | endpointsImport |}}
2727
{{{genImport endpointsImport}}}
2828
{{/each}}
29+
{{! Acl import }}
30+
{{#each aclImports as | aclImport |}}
31+
{{{genImport aclImport}}}
32+
{{/each}}
2933

3034
{{#if includeNamespace}}
3135
export namespace {{namespace}} {

src/generators/utils/generate/generate.acl.utils.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
11
import { ACL_ALL_ABILITIES } from "src/generators/const/acl.const";
2+
import { GenerateType } from "src/generators/types/generate";
3+
import { GenerateOptions } from "src/generators/types/options";
24
import { Endpoint } from "../../types/endpoint";
35
import { capitalize, snakeToCamel } from "../string.utils";
6+
import { getEndpointTag } from "./generate.endpoints.utils";
7+
import { getNamespaceName } from "./generate.utils";
48

59
export const getAbilityTypeName = (endpoint: Endpoint) =>
610
`Use${capitalize(snakeToCamel(endpoint.operationName))}Ability`;
711

812
export const getAbilityFunctionName = (endpoint: Endpoint) =>
913
`canUse${capitalize(snakeToCamel(endpoint.operationName))}`;
1014

15+
export const getImportedAbilityFunctionName = (endpoint: Endpoint, options: GenerateOptions) => {
16+
const namespacePrefix = options.tsNamespaces
17+
? `${getNamespaceName({ type: GenerateType.Acl, tag: getEndpointTag(endpoint, options), options })}.`
18+
: "";
19+
return `${namespacePrefix}${getAbilityFunctionName(endpoint)}`;
20+
};
21+
1122
export const getAbilityAction = (endpoint: Endpoint) => endpoint.acl?.[0].action;
1223

1324
export const getAbilitySubject = (endpoint: Endpoint) => endpoint.acl?.[0].subject;

src/generators/utils/generate/generate.imports.utils.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Endpoint } from "../../types/endpoint";
44
import { GenerateType, Import } from "../../types/generate";
55
import { GenerateOptions } from "../../types/options";
66
import { getUniqueArray } from "../array.utils";
7+
import { getAbilityFunctionName } from "./generate.acl.utils";
78
import { getEndpointName, getEndpointTag } from "./generate.endpoints.utils";
89
import { getNamespaceName, getTagImportPath } from "./generate.utils";
910
import { getZodSchemaInferedTypeName } from "./generate.zod.utils";
@@ -62,6 +63,25 @@ export function getEndpointsImports({
6263
});
6364
}
6465

66+
export function getAclImports({
67+
tag,
68+
endpoints,
69+
options,
70+
}: {
71+
tag: string;
72+
endpoints: Endpoint[];
73+
options: GenerateOptions;
74+
}) {
75+
return getImports({
76+
type: GenerateType.Acl,
77+
tag,
78+
entities: endpoints,
79+
getTag: (endpoint) => getEndpointTag(endpoint, options),
80+
getEntityName: getAbilityFunctionName,
81+
options,
82+
});
83+
}
84+
6585
export function getEntityImports({
6686
tags,
6787
entityName,

0 commit comments

Comments
 (0)