@povio/openapi-codegen-cli 0.10.8 → 0.11.0

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.10.8",
3
+ "version": "0.11.0",
4
4
  "main": "./dist/index.js",
5
5
  "bin": {
6
6
  "openapi-codegen": "./dist/sh.js"
@@ -18,7 +18,8 @@
18
18
  "lint": "eslint --fix",
19
19
  "format:check": "prettier --check .",
20
20
  "format:fix": "prettier --write .",
21
- "push": "yarn exec ./scripts/publish.sh"
21
+ "push": "yarn exec ./scripts/publish.sh",
22
+ "dev:generate": "rm -rf ./output && yarn start generate --input http://localhost:4000/docs-json"
22
23
  },
23
24
  "files": [
24
25
  "dist/*",
@@ -0,0 +1,22 @@
1
+ import { createContext, use, useMemo } from "react";
2
+
3
+ export namespace QueryConfig {
4
+ interface Type {
5
+ preferUpdate?: boolean;
6
+ }
7
+
8
+ const Context = createContext<Type>({});
9
+
10
+ type ProviderProps = Type;
11
+
12
+ export const Provider = ({ preferUpdate, children }: React.PropsWithChildren<ProviderProps>) => {
13
+ const value = useMemo(() => ({ preferUpdate }), [preferUpdate]);
14
+
15
+ return <Context.Provider value={value}>{children}</Context.Provider>;
16
+ };
17
+
18
+ export const useConfig = () => {
19
+ const context = use(Context);
20
+ return context ?? {};
21
+ };
22
+ }
@@ -0,0 +1,52 @@
1
+ import { QueryKey, useQueryClient } from "@tanstack/react-query";
2
+ import { useCallback } from "react";
3
+
4
+ import { QueryConfig } from "./queryConfig.context";
5
+ import { QueryModule } from "./queryModules";
6
+
7
+ export interface MutationEffectsOptions {
8
+ invalidateCurrentModule?: boolean;
9
+ invalidateModules?: QueryModule[];
10
+ invalidateKeys?: QueryKey[];
11
+ preferUpdate?: boolean;
12
+ }
13
+
14
+ export interface UseMutationEffectsOptions {
15
+ currentModule: QueryModule;
16
+ }
17
+
18
+ export function useMutationEffects({ currentModule }: UseMutationEffectsOptions) {
19
+ const queryClient = useQueryClient();
20
+ const config = QueryConfig.useConfig();
21
+
22
+ const runMutationEffects = useCallback(
23
+ async <TData>(data: TData, options: MutationEffectsOptions = {}, updateKeys?: QueryKey[]) => {
24
+ const { invalidateCurrentModule, invalidateModules, invalidateKeys, preferUpdate } = options;
25
+ const shouldUpdate = preferUpdate || (preferUpdate === undefined && config.preferUpdate);
26
+
27
+ const isQueryKeyEqual = (keyA: QueryKey, keyB: QueryKey) =>
28
+ keyA.length === keyB.length && keyA.every((item, index) => item === keyB[index]);
29
+
30
+ queryClient.invalidateQueries({
31
+ predicate: ({ queryKey }) => {
32
+ const isUpdateKey = updateKeys?.some((key) => isQueryKeyEqual(queryKey, key));
33
+ if (shouldUpdate && isUpdateKey) {
34
+ return false;
35
+ }
36
+
37
+ const isCurrentModule = invalidateCurrentModule && queryKey[0] === currentModule;
38
+ const isInvalidateModule = !!invalidateModules && invalidateModules.some((module) => queryKey[0] === module);
39
+ const isInvalidateKey = !!invalidateKeys && invalidateKeys.some((key) => isQueryKeyEqual(queryKey, key));
40
+ return isCurrentModule || isInvalidateModule || isInvalidateKey;
41
+ },
42
+ });
43
+
44
+ if (shouldUpdate && updateKeys) {
45
+ updateKeys.map((queryKey) => queryClient.setQueryData(queryKey, data));
46
+ }
47
+ },
48
+ [queryClient, currentModule, config.preferUpdate],
49
+ );
50
+
51
+ return { runMutationEffects };
52
+ }
@@ -4,7 +4,7 @@
4
4
  * @summary {{addAsteriskAfterNewLine endpoint.summary}}{{/if}}{{#if endpoint.description}}
5
5
  * @description {{addAsteriskAfterNewLine endpoint.description}}{{/if}}{{#if endpoint.acl}}
6
6
  * @permission Requires `{{abilityFunctionName endpoint}}` ability {{/if}}
7
- {{#if (endpointParams endpoint)}}{{#each (endpointParams endpoint infiniteQuery removePageParam=true) as | endpointParam |}} * @param { {{endpointParam.type}} } object.{{endpointParam.name}} {{{endpointParamDescription endpointParam}}}
7
+ {{#if (endpointParams endpoint)}}{{#each (endpointParams endpoint infiniteQuery excludePageParam=true) as | endpointParam |}} * @param { {{endpointParam.type}} } object.{{endpointParam.name}} {{{endpointParamDescription endpointParam}}}
8
8
  {{/each}}{{/if}} * @param { AppInfiniteQueryOptions } options Infinite query options
9
9
  * @returns { UseInfiniteQueryResult<{{{importedZodSchemaInferedType endpoint.response}}}> } {{endpoint.responseDescription}}
10
10
  * @statusCodes [{{commaSeparated endpoint.responseStatusCodes}}]
@@ -25,7 +25,7 @@
25
25
  * @description {{addAsteriskAfterNewLine endpoint.description}}{{/if}}{{#if endpoint.acl}}
26
26
  * @permission Requires `{{abilityFunctionName endpoint}}` ability {{/if}}
27
27
  {{#if (endpointParams endpoint includeFileParam=true)}}{{#each (endpointParams endpoint includeFileParam=true) as | endpointParam |}} * @param { {{endpointParam.type}} } mutation.{{endpointParam.name}} {{{endpointParamDescription endpointParam}}}
28
- {{/each}}{{/if}} * @param { AppMutationOptions{{#if hasInvalidateQueryOptions}} & {{invalidateQueryOptionsType}}{{/if}} } options Mutation options
28
+ {{/each}}{{/if}} * @param { AppMutationOptions{{#if hasMutationEffects}} & {{mutationEffectsType}}{{/if}} } options Mutation options
29
29
  * @returns { UseMutationResult<{{#if endpoint.mediaDownload}}AxiosResponse<{{/if}}{{{importedZodSchemaInferedType endpoint.response}}}{{#if endpoint.mediaDownload}}>{{/if}}> } {{endpoint.responseDescription}}
30
30
  * @statusCodes [{{commaSeparated endpoint.responseStatusCodes}}]
31
31
  */{{/if}}
@@ -4,7 +4,7 @@ export const keys = {
4
4
  {{endpointName endpoint}}: ({{{genEndpointParams endpoint}}}) => [...keys.all, "{{endpoint.path}}", {{{endpointArgs endpoint}}}] as const,
5
5
  {{#if ../generateInfiniteQueries}}
6
6
  {{#if (isInfiniteQuery endpoint)}}
7
- {{endpointName endpoint}}Infinite: ({{{genEndpointParams endpoint removePageParam=true}}}) => [...keys.all, "{{endpoint.path}}", "infinite", {{{endpointArgs endpoint removePageParam=true}}}] as const,
7
+ {{endpointName endpoint}}Infinite: ({{{genEndpointParams endpoint excludePageParam=true}}}) => [...keys.all, "{{endpoint.path}}", "infinite", {{{endpointArgs endpoint excludePageParam=true}}}] as const,
8
8
  {{/if}}
9
9
  {{/if}}
10
10
  {{/each}}
@@ -1,9 +1,9 @@
1
1
  {{! Js docs }}
2
2
  {{{genQueryJsDocs endpoint infiniteQuery=true}}}
3
3
  {{! Infinite query definition}}
4
- export const {{infiniteQueryName endpoint}} = <TData>({{#if (endpointParams endpoint)}}{ {{{endpointArgs endpoint removePageParam=true}}} }: { {{{genEndpointParams endpoint removePageParam=true}}} }, {{/if}}options?: AppInfiniteQueryOptions<typeof {{importedEndpointName endpoint}}, TData>{{#if hasAxiosRequestConfig}}, {{axiosRequestConfigName}}?: {{axiosRequestConfigType}}{{/if}}) => {
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
5
  return {{infiniteQueryHook}}({
6
- queryKey: keys.{{endpointName endpoint}}Infinite({{#if (endpointParams endpoint)}}{{{endpointArgs endpoint removePageParam=true}}}{{/if}}),
6
+ queryKey: keys.{{endpointName endpoint}}Infinite({{#if (endpointParams endpoint)}}{{{endpointArgs endpoint excludePageParam=true}}}{{/if}}),
7
7
  queryFn: ({ pageParam }) => {{importedEndpointName endpoint}}({{{endpointArgs endpoint replacePageParam=true}}}{{#if hasAxiosRequestConfig}}, {{axiosRequestConfigName}}{{/if}}),
8
8
  initialPageParam: 1,
9
9
  getNextPageParam: ({ {{pageParamName}}, {{totalItemsName}}, {{limitParamName}}: limitParam }) => {
@@ -1,8 +1,9 @@
1
1
  {{! Js docs }}
2
2
  {{{genQueryJsDocs endpoint mutation=true}}}
3
3
  {{! Mutation definition}}
4
- export const {{queryName endpoint mutation=true}} = (options?: AppMutationOptions<typeof {{importedEndpointName endpoint}}, { {{{genEndpointParams endpoint includeFileParam=true}}} }>{{#if hasInvalidateQueryOptions}} & {{invalidateQueryOptionsType}}{{/if}}{{#if hasAxiosRequestConfig}}, {{axiosRequestConfigName}}?: {{axiosRequestConfigType}}{{/if}}) => {
5
- {{#if hasInvalidateQueryOptions}} const queryClient = useQueryClient();{{/if}}
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 mutation effects }}
6
+ {{#if hasMutationEffects}}const { runMutationEffects } = useMutationEffects({ currentModule: {{queriesModuleName}} });{{/if}}
6
7
 
7
8
  return {{queryHook}}({
8
9
  mutationFn: {{#if endpoint.mediaUpload}}async {{/if}}({{#if (endpointParams endpoint includeFileParam=true)}} { {{{endpointArgs endpoint includeFileParam=true}}} } {{/if}}) => {{#if endpoint.mediaUpload}} {
@@ -18,11 +19,15 @@ export const {{queryName endpoint mutation=true}} = (options?: AppMutationOption
18
19
 
19
20
  return uploadInstructions;
20
21
  }{{/if}},
21
- ...options, {{#if hasInvalidateQueryOptions}}
22
- onSuccess: (...args) => {
23
- {{! Invalidation }}
24
- invalidateQueries(queryClient, {{queriesModuleName}}, options);
25
- options?.onSuccess?.(...args);
22
+ ...options, {{#if hasMutationEffects}}
23
+ onSuccess: async (resData, variables, context) => {
24
+ {{! Mutation effects }}
25
+ {{#if updateQueryEndpoints}}
26
+ const { {{endpointArgs endpoint includeOnlyRequiredParams=true excludeBodyParam=true}} } = variables;
27
+ const updateKeys = [{{#each updateQueryEndpoints as | endpoint |}}keys.{{endpointName endpoint}}({{{endpointArgs endpoint includeOnlyRequiredParams=true}}}), {{/each}}];
28
+ {{/if}}
29
+ await runMutationEffects(resData, options{{#if updateQueryEndpoints}}, updateKeys{{/if}});
30
+ options?.onSuccess?.(resData, variables, context);
26
31
  },{{/if}}
27
32
  });
28
33
  };
@@ -4,9 +4,13 @@
4
4
  {{/if}}
5
5
  {{! React query import }}
6
6
  {{{genImport queryImport}}}
7
- {{! Invalidate queries import }}
8
- {{#if hasInvalidateQueryOptions}}
9
- {{{genImport invalidateQueriesImport}}}
7
+ {{! Query modules import }}
8
+ {{#if hasMutationEffects}}
9
+ {{{genImport queryModulesImport}}}
10
+ {{/if}}
11
+ {{! Mutation effects import }}
12
+ {{#if hasMutationEffectsImport}}
13
+ {{{genImport mutationEffectsImport}}}
10
14
  {{/if}}
11
15
  {{! React query types import }}
12
16
  {{{genImport queryTypesImport}}}
@@ -23,7 +27,7 @@
23
27
  export namespace {{namespace}} {
24
28
  {{/if}}
25
29
 
26
- export const {{queriesModuleName}} = {{#if hasInvalidateQueryOptions}}{{queryModuleEnum}}.{{tag}}{{else}}"{{namespace}}"{{/if}};
30
+ export const {{queriesModuleName}} = {{#if hasMutationEffects}}{{queryModuleEnum}}.{{tag}}{{else}}"{{namespace}}"{{/if}};
27
31
 
28
32
  {{! Query keys export}}
29
33
  {{{genQueryKeys queryEndpoints}}}
@@ -32,7 +36,7 @@ export const {{queriesModuleName}} = {{#if hasInvalidateQueryOptions}}{{queryMod
32
36
  {{#each endpoints as | endpoint |}}
33
37
  {{{genQuery endpoint}}}
34
38
 
35
- {{{genMutation endpoint}}}
39
+ {{{genMutation endpoint ../queryEndpoints}}}
36
40
 
37
41
  {{{genInfiniteQuery endpoint}}}
38
42
 
@@ -0,0 +1,5 @@
1
+ export const enum QueryModule {
2
+ {{#each modules as | module |}}
3
+ {{module.tag}} = "{{module.namespace}}",
4
+ {{/each}}
5
+ }
@@ -1,2 +0,0 @@
1
- import { GenerateTypeParams } from "../types/generate";
2
- export declare function generateInvalidateQueries({ resolver, data }: Omit<GenerateTypeParams, "tag">): string;
@@ -1,34 +0,0 @@
1
- import { QueryClient, QueryKey } from "@tanstack/react-query";
2
-
3
- export const enum QueryModule {
4
- {{#each modules as | module |}}
5
- {{module.tag}} = "{{module.namespace}}",
6
- {{/each}}
7
- }
8
-
9
- export interface InvalidateQueryOptions {
10
- invalidateCurrentModule?: boolean;
11
- invalidateModules?: QueryModule[];
12
- invalidateKeys?: QueryKey[];
13
- }
14
-
15
- export async function invalidateQueries(
16
- queryClient: QueryClient,
17
- currentModule: QueryModule,
18
- options: InvalidateQueryOptions = {},
19
- ) {
20
- const { invalidateCurrentModule, invalidateModules, invalidateKeys } = options;
21
-
22
- if (invalidateCurrentModule) {
23
- await queryClient.invalidateQueries({ queryKey: [currentModule] });
24
- }
25
-
26
- if (invalidateModules) {
27
- await Promise.all([...invalidateModules.map((module) => queryClient.invalidateQueries({ queryKey: [module] }))]);
28
- }
29
-
30
- if (invalidateKeys) {
31
- await Promise.all([...invalidateKeys.map((queryKey) => queryClient.invalidateQueries({ queryKey }))]);
32
- }
33
- }
34
-