@povio/openapi-codegen-cli 0.12.1 → 0.13.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@povio/openapi-codegen-cli",
3
- "version": "0.12.1",
3
+ "version": "0.13.2",
4
4
  "main": "./dist/index.js",
5
5
  "bin": {
6
6
  "openapi-codegen": "./dist/sh.js"
@@ -19,7 +19,7 @@
19
19
  "format:check": "prettier --check .",
20
20
  "format:fix": "prettier --write .",
21
21
  "push": "yarn exec ./scripts/publish.sh",
22
- "dev:generate": "rm -rf ./output && yarn start generate --input http://localhost:4000/docs-json"
22
+ "dev:generate": "rm -rf ./output && yarn start generate --input ./test/test.json"
23
23
  },
24
24
  "files": [
25
25
  "dist/*",
@@ -11,11 +11,11 @@ export interface MutationEffectsOptions {
11
11
  preferUpdate?: boolean;
12
12
  }
13
13
 
14
- export interface UseMutationEffectsOptions {
14
+ export interface UseMutationEffectsProps {
15
15
  currentModule: QueryModule;
16
16
  }
17
17
 
18
- export function useMutationEffects({ currentModule }: UseMutationEffectsOptions) {
18
+ export function useMutationEffects({ currentModule }: UseMutationEffectsProps) {
19
19
  const queryClient = useQueryClient();
20
20
  const config = OpenApiQueryConfig.useConfig();
21
21
 
@@ -0,0 +1,23 @@
1
+ import { useCallback } from "react";
2
+ {{! Ability context import }}
3
+ {{{genImport abilityContextImport}}}
4
+ {{! Error handling import }}
5
+ {{{genImport errorHandlingImport}}}
6
+ {{! App abilities import }}
7
+ {{{genImport appAbilitiesImport}}}
8
+
9
+ interface UseAclCheckProps {
10
+ errorHandler?: {{errorHandler}}<never>;
11
+ }
12
+
13
+ export function {{aclCheckHook}}({ errorHandler }: UseAclCheckProps = {}) {
14
+ const ability = {{abilityContext}}.useAbility();
15
+
16
+ const checkAcl = useCallback((appAbility: {{appAbilities}}) => {
17
+ if (!ability.can(...appAbility)) {
18
+ (errorHandler ?? {{sharedErrorHandler}}).rethrowError(new Error("ACL check failed"));
19
+ }
20
+ }, [ability, errorHandler]);
21
+
22
+ return { checkAcl };
23
+ }
@@ -6,8 +6,12 @@
6
6
  {{/each}}
7
7
 
8
8
  {{! AppAbilities union type }}
9
- export type AppAbilities =
9
+ export type {{appAbilities}} =
10
+ {{#if tags}}
10
11
  {{#if includeNamespace}}{{#each namespaces as | namespace |}} | {{namespace}}.{{../allAbilities}}{{/each}}
11
12
  {{else}}{{#each tags as | tag |}} | {{tagAllAbilitiesName tag}}{{/each}}{{/if}}
13
+ {{else}}
14
+ {{abilityTuple}}<string, {{subject}}>;
15
+ {{/if}}
12
16
 
13
- export type AppAbility = PureAbility<AppAbilities>;
17
+ export type AppAbility = PureAbility<{{appAbilities}}>;
@@ -0,0 +1 @@
1
+ {{#if generateAclCheckParams}}{ {{#each (abilityConditionsTypes endpoint) as | propertyType |}}{{propertyType.name}}, {{/each}} } {{/if}}
@@ -1 +1 @@
1
- {{zodExtended}}.{{parse}}({{#if param.parameterSortingEnumSchemaName}}{{zodExtended}}.{{sortingString}}({{importedZodSchemaName param.parameterSortingEnumSchemaName}}){{#if addOptional}}.optional(){{/if}}{{else}}{{importedZodSchemaName param.zodSchema}}{{#if addOptional}}.optional(){{/if}}{{/if}}, {{paramName}})
1
+ {{zodExtended}}.{{parse}}({{#if param.parameterSortingEnumSchemaName}}{{zodExtended}}.{{sortingString}}({{importedZodSchemaName param.parameterSortingEnumSchemaName}}){{#if addOptional}}.optional(){{/if}}{{else}}{{importedZodSchemaName param.zodSchema}}{{#if addOptional}}.optional(){{/if}}{{/if}}, {{paramName}}{{#if isQuery}}, { type: "query", name: "{{paramName}}" }{{/if}})
@@ -2,9 +2,15 @@
2
2
  {{{genQueryJsDocs endpoint infiniteQuery=true}}}
3
3
  {{! Infinite query definition}}
4
4
  export const {{infiniteQueryName endpoint}} = <TData>({{#if (endpointParams endpoint)}}{ {{{endpointArgs endpoint excludePageParam=true}}} }: { {{{genEndpointParams endpoint excludePageParam=true}}} }, {{/if}}options?: AppInfiniteQueryOptions<typeof {{importedEndpointName endpoint}}, TData>{{#if hasAxiosRequestConfig}}, {{axiosRequestConfigName}}?: {{axiosRequestConfigType}}{{/if}}) => {
5
+ {{! Use acl check }}
6
+ {{#if hasAclCheck}}const { checkAcl } = {{aclCheckHook}}();{{/if}}
7
+
5
8
  return {{infiniteQueryHook}}({
6
9
  queryKey: keys.{{endpointName endpoint}}Infinite({{#if (endpointParams endpoint)}}{{{endpointArgs endpoint excludePageParam=true}}}{{/if}}),
7
- queryFn: ({ pageParam }) => {{importedEndpointName endpoint}}({{{endpointArgs endpoint replacePageParam=true}}}{{#if hasAxiosRequestConfig}}, {{axiosRequestConfigName}}{{/if}}),
10
+ queryFn: ({ pageParam }) => {{#if hasQueryFnBody}}{ {{/if}}
11
+ {{#if hasAclCheck}}checkAcl({{genAclCheckParams endpoint}});{{/if}}
12
+ {{#if hasQueryFnBody}}return {{/if}}{{importedEndpointName endpoint}}({{{endpointArgs endpoint replacePageParam=true}}}{{#if hasAxiosRequestConfig}}, {{axiosRequestConfigName}}{{/if}})
13
+ {{#if hasQueryFnBody}} }{{/if}},
8
14
  initialPageParam: 1,
9
15
  getNextPageParam: ({ {{pageParamName}}, {{totalItemsName}}, {{limitParamName}}: limitParam }) => {
10
16
  const pageParam = {{pageParamName}} ?? 1;
@@ -2,12 +2,15 @@
2
2
  {{{genQueryJsDocs endpoint mutation=true}}}
3
3
  {{! Mutation definition}}
4
4
  export const {{queryName endpoint mutation=true}} = (options?: AppMutationOptions<typeof {{importedEndpointName endpoint}}, { {{{genEndpointParams endpoint includeFileParam=true}}} }>{{#if hasMutationEffects}} & {{mutationEffectsType}}{{/if}}{{#if hasAxiosRequestConfig}}, {{axiosRequestConfigName}}?: {{axiosRequestConfigType}}{{/if}}) => {
5
+ {{! Use acl check }}
6
+ {{#if hasAclCheck}}const { checkAcl } = {{aclCheckHook}}();{{/if}}
5
7
  {{! Use mutation effects }}
6
8
  {{#if hasMutationEffects}}const { runMutationEffects } = useMutationEffects({ currentModule: {{queriesModuleName}} });{{/if}}
7
9
 
8
10
  return {{queryHook}}({
9
- mutationFn: {{#if endpoint.mediaUpload}}async {{/if}}({{#if (endpointParams endpoint includeFileParam=true)}} { {{{endpointArgs endpoint includeFileParam=true}}} } {{/if}}) => {{#if endpoint.mediaUpload}} {
10
- const uploadInstructions = await{{/if}} {{importedEndpointName endpoint}}({{{endpointArgs endpoint}}}{{#if hasAxiosRequestConfig}}{{#if (endpointArgs endpoint)}}, {{/if}}{{axiosRequestConfigName}}{{/if}}){{#if endpoint.mediaUpload}};
11
+ 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}}
13
+ {{#if endpoint.mediaUpload}}const uploadInstructions = await {{importedEndpointName endpoint}}({{{endpointArgs endpoint}}}{{#if hasAxiosRequestConfig}}{{#if (endpointArgs endpoint)}}, {{/if}}{{axiosRequestConfigName}}{{/if}});
11
14
 
12
15
  if (file && uploadInstructions.url) {
13
16
  await axios.put(uploadInstructions.url, file, {
@@ -16,14 +19,17 @@ export const {{queryName endpoint mutation=true}} = (options?: AppMutationOption
16
19
  },
17
20
  });
18
21
  }
19
-
22
+
20
23
  return uploadInstructions;
21
- }{{/if}},
24
+ {{else}}
25
+ {{#if hasMutationFnBody}}return {{/if}}{{importedEndpointName endpoint}}({{{endpointArgs endpoint}}}{{#if hasAxiosRequestConfig}}{{#if (endpointArgs endpoint)}}, {{/if}}{{axiosRequestConfigName}}{{/if}})
26
+ {{/if}}
27
+ {{#if hasMutationFnBody}} }{{/if}},
22
28
  ...options, {{#if hasMutationEffects}}
23
29
  onSuccess: async (resData, variables, context) => {
24
30
  {{! Mutation effects }}
25
31
  {{#if updateQueryEndpoints}}
26
- const { {{endpointArgs endpoint includeOnlyRequiredParams=true excludeBodyParam=true}} } = variables;
32
+ {{#if destructuredVariables}}const { {{commaSeparated destructuredVariables }} } = variables;{{/if}}
27
33
  const updateKeys = [{{#each updateQueryEndpoints as | endpoint |}}keys.{{endpointName endpoint}}({{{endpointArgs endpoint includeOnlyRequiredParams=true}}}), {{/each}}];
28
34
  {{/if}}
29
35
  await runMutationEffects(resData, options{{#if updateQueryEndpoints}}, updateKeys{{/if}});
@@ -2,9 +2,15 @@
2
2
  {{{genQueryJsDocs endpoint query=true}}}
3
3
  {{! Query definition}}
4
4
  export const {{queryName endpoint}} = <TData>({{#if (endpointParams endpoint)}}{ {{{endpointArgs endpoint}}} }: { {{{genEndpointParams endpoint}}} }, {{/if}}options?: AppQueryOptions<typeof {{importedEndpointName endpoint}}, TData>{{#if hasAxiosRequestConfig}}, {{axiosRequestConfigName}}?: {{axiosRequestConfigType}}{{/if}}) => {
5
+ {{! Use acl check }}
6
+ {{#if hasAclCheck}}const { checkAcl } = {{aclCheckHook}}();{{/if}}
7
+
5
8
  return {{queryHook}}({
6
9
  queryKey: keys.{{endpointName endpoint}}({{#if (endpointParams endpoint)}}{{{endpointArgs endpoint}}}{{/if}}),
7
- queryFn: {{#if hasQueryFn}}() => {{importedEndpointName endpoint}}({{{endpointArgs endpoint}}}{{#if hasAxiosRequestConfig}}{{#if (endpointArgs endpoint)}}, {{/if}}{{axiosRequestConfigName}}{{/if}}){{else}}{{importedEndpointName endpoint}}{{/if}},
10
+ queryFn: {{#if hasQueryFn}}() => {{#if hasQueryFnBody}}{ {{/if}}
11
+ {{#if hasAclCheck}}checkAcl({{genAclCheckParams endpoint}});{{/if}}
12
+ {{#if hasQueryFnBody}}return {{/if}}{{importedEndpointName endpoint}}({{{endpointArgs endpoint}}}{{#if hasAxiosRequestConfig}}{{#if (endpointArgs endpoint)}}, {{/if}}{{axiosRequestConfigName}}{{/if}}){{else}}{{importedEndpointName endpoint}}{{/if}}
13
+ {{#if hasQueryFnBody}} }{{/if}},
8
14
  ...options,
9
15
  });
10
16
  };
@@ -12,6 +12,10 @@
12
12
  {{#if hasMutationEffectsImport}}
13
13
  {{{genImport mutationEffectsImport}}}
14
14
  {{/if}}
15
+ {{! Acl check import }}
16
+ {{#if hasAclCheck}}
17
+ {{{genImport aclCheckImport}}}
18
+ {{/if}}
15
19
  {{! React query types import }}
16
20
  {{{genImport queryTypesImport}}}
17
21
  {{! Models import }}
@@ -29,10 +29,19 @@ function isSortingStringValid(enumSchema: z.ZodEnum<[string, ...string[]]>, data
29
29
  return new RegExp(`^${commaSeparatedOptions}$`).test(data);
30
30
  }
31
31
 
32
- function {{parse}}(schema: z.ZodSchema, data: unknown, errorHandler?: {{errorHandler}}<never>) {
32
+ interface ParseOptions {
33
+ type: "body" | "query";
34
+ name?: string;
35
+ errorHandler?: {{errorHandler}}<never>;
36
+ }
37
+
38
+ function {{parse}}(schema: z.ZodSchema, data: unknown, { type, name, errorHandler }: ParseOptions = { type: "body" }) {
33
39
  try {
34
40
  return schema.parse(data);
35
- } catch (e: unknown) {
41
+ } catch (e) {
42
+ if (e instanceof z.ZodError) {
43
+ e.name = `FE Request ${type === "body" ? "body" : "query param"}${name ? ` ("${name}")` : ""} schema mismatch - ZodError`;
44
+ }
36
45
  (errorHandler ?? {{sharedErrorHandler}}).rethrowError(e);
37
46
  throw e;
38
47
  }