@povio/openapi-codegen-cli 0.6.5 → 0.7.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/README.md +24 -22
- package/dist/commands/generate.d.ts +1 -1
- package/dist/generators/const/deps.const.d.ts +6 -0
- package/dist/generators/const/endpoints.const.d.ts +4 -0
- package/dist/generators/const/{query.const.d.ts → queries.const.d.ts} +1 -0
- package/dist/generators/generate/generateQueryModules.d.ts +2 -0
- package/dist/generators/utils/generate/generate.imports.utils.d.ts +6 -3
- package/dist/generators/utils/generate/generate.query.utils.d.ts +2 -0
- package/dist/generators/utils/generate/generate.utils.d.ts +1 -0
- package/dist/index.js +47 -47
- package/dist/sh.js +66 -66
- package/package.json +1 -1
- package/src/assets/invalidateQueries.ts +31 -0
- package/src/assets/react-query.types.ts +1 -3
- package/src/generators/templates/endpoints.hbs +5 -1
- package/src/generators/templates/partials/endpoint-config.hbs +3 -0
- package/src/generators/templates/partials/query-js-docs.hbs +2 -3
- package/src/generators/templates/partials/query-keys.hbs +1 -1
- package/src/generators/templates/partials/query-use-infinite-query.hbs +2 -2
- package/src/generators/templates/partials/query-use-mutation.hbs +5 -7
- package/src/generators/templates/partials/query-use-query.hbs +2 -2
- package/src/generators/templates/queries.hbs +14 -3
- package/src/generators/templates/query-modules.hbs +14 -0
package/package.json
CHANGED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { QueryClient } from "@tanstack/react-query";
|
|
2
|
+
|
|
3
|
+
import { QueriesModule, QueriesModuleKeysAll } from "./queryModules";
|
|
4
|
+
|
|
5
|
+
export interface InvalidateQueryOptions {
|
|
6
|
+
invalidateCurrentModule?: boolean;
|
|
7
|
+
invalidateModules?: QueriesModule[];
|
|
8
|
+
invalidateKeys?: readonly unknown[][];
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export async function invalidateQueries(
|
|
12
|
+
queryClient: QueryClient,
|
|
13
|
+
currentModule: QueriesModule,
|
|
14
|
+
options: InvalidateQueryOptions = {},
|
|
15
|
+
) {
|
|
16
|
+
const { invalidateCurrentModule, invalidateModules, invalidateKeys } = options;
|
|
17
|
+
|
|
18
|
+
if (invalidateCurrentModule && QueriesModuleKeysAll[currentModule]) {
|
|
19
|
+
await queryClient.invalidateQueries({ queryKey: QueriesModuleKeysAll[currentModule] });
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
if (invalidateModules) {
|
|
23
|
+
await Promise.all([
|
|
24
|
+
...invalidateModules.map((module) => queryClient.invalidateQueries({ queryKey: QueriesModuleKeysAll[module] })),
|
|
25
|
+
]);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
if (invalidateKeys) {
|
|
29
|
+
await Promise.all([...invalidateKeys.map((queryKey) => queryClient.invalidateQueries({ queryKey }))]);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
@@ -20,9 +20,7 @@ export type AppMutationOptions<
|
|
|
20
20
|
TFunction extends Function,
|
|
21
21
|
TVariables = void,
|
|
22
22
|
TData = Awaited<ReturnType<TFunction>>,
|
|
23
|
-
> = Omit<UseMutationOptions<TData, Error, TVariables>, "mutationKey" | "mutationFn"
|
|
24
|
-
enableInvalidateAll?: boolean;
|
|
25
|
-
};
|
|
23
|
+
> = Omit<UseMutationOptions<TData, Error, TVariables>, "mutationKey" | "mutationFn">;
|
|
26
24
|
|
|
27
25
|
export type AppInfiniteQueryOptions<
|
|
28
26
|
TFunction extends Function,
|
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
{{! App rest client import}}
|
|
2
2
|
{{{genImport appRestClientImport}}}
|
|
3
|
+
{{! Axios import }}
|
|
4
|
+
{{#if hasAxiosImport}}
|
|
5
|
+
{{{genImport axiosImport}}}
|
|
6
|
+
{{/if}}
|
|
3
7
|
{{! Zod import }}
|
|
4
8
|
{{#if hasZodImport}}
|
|
5
9
|
{{{genImport zodImport}}}
|
|
@@ -14,7 +18,7 @@ export namespace {{namespace}} {
|
|
|
14
18
|
{{/if}}
|
|
15
19
|
{{! Endpoints export }}
|
|
16
20
|
{{#each endpoints as | endpoint |}}
|
|
17
|
-
export const {{endpointName endpoint}} = ({{{genEndpointParams endpoint}}}) => {
|
|
21
|
+
export const {{endpointName endpoint}} = ({{{genEndpointParams endpoint}}}{{#if ../hasAxiosRequestConfig}}{{../axiosRequestConfigName}}?: {{../axiosRequestConfigType}}{{/if}}) => {
|
|
18
22
|
return {{../restClientName}}.{{endpoint.method}}(
|
|
19
23
|
{{! Response }}
|
|
20
24
|
{ resSchema: {{importedZodSchemaName endpoint.response}} },
|
|
@@ -8,15 +8,14 @@
|
|
|
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}}]
|
|
11
|
-
*/
|
|
12
|
-
{{else}}
|
|
11
|
+
*/{{else}}
|
|
13
12
|
/**
|
|
14
13
|
* {{#if (isQuery endpoint)}}Query{{else}}Mutation{{/if}} `{{queryName endpoint}}`{{#if endpoint.summary}}
|
|
15
14
|
* @summary {{addAsteriskAfterNewLine endpoint.summary}}{{/if}}{{#if endpoint.description}}
|
|
16
15
|
* @description {{addAsteriskAfterNewLine endpoint.description}}{{/if}}{{#if endpoint.acl}}
|
|
17
16
|
* @permission Requires `{{abilityFunctionName endpoint}}` ability {{/if}}
|
|
18
17
|
{{#if (endpointParams endpoint)}}{{#each (endpointParams endpoint) as | endpointParam |}} * @param { {{endpointParam.type}} } {{#if (isQuery ../endpoint)}}object{{else}}mutation{{/if}}.{{endpointParam.name}} {{{endpointParamDescription endpointParam}}}
|
|
19
|
-
{{/each}}{{/if}} * @param {{#if (isQuery endpoint)}}{ AppQueryOptions } options Query options{{else}}{ AppMutationOptions } options Mutation options{{/if}}
|
|
18
|
+
{{/each}}{{/if}} * @param {{#if (isQuery endpoint)}}{ AppQueryOptions } options Query options{{else}}{ AppMutationOptions{{#if hasInvalidateQueryOptions}} & {{invalidateQueryOptionsType}}{{/if}} } options Mutation options{{/if}}
|
|
20
19
|
* @returns { {{#if (isQuery endpoint)}}UseQueryResult{{else}}UseMutationResult{{/if}}<{{{importedZodSchemaInferedType endpoint.response}}}> } {{endpoint.responseDescription}}
|
|
21
20
|
* @statusCodes [{{commaSeparated endpoint.responseStatusCodes}}]
|
|
22
21
|
*/{{/if}}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export const keys = {
|
|
2
|
-
all: [
|
|
2
|
+
all: [{{queriesModuleName}}] as const,
|
|
3
3
|
{{#each queryEndpoints as | endpoint |}}
|
|
4
4
|
{{endpointName endpoint}}: ({{{genEndpointParams endpoint}}}) => [...keys.all, "{{endpoint.path}}", {{{endpointArgs endpoint}}}] as const,
|
|
5
5
|
{{#if ../generateInfiniteQueries}}
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{{! Js docs }}
|
|
2
2
|
{{{genQueryJsDocs endpoint "infiniteQuery" }}}
|
|
3
3
|
{{! Infinite query definition}}
|
|
4
|
-
export const {{infiniteQueryName endpoint}} = <TData>({{#if (endpointParams endpoint)}}{ {{{endpointArgs endpoint "removePageParam"}}} }: { {{{genEndpointParams endpoint "removePageParam"}}} }, {{/if}}options?: AppInfiniteQueryOptions<typeof {{importedEndpointName endpoint}}, TData>) => {
|
|
4
|
+
export const {{infiniteQueryName endpoint}} = <TData>({{#if (endpointParams endpoint)}}{ {{{endpointArgs endpoint "removePageParam"}}} }: { {{{genEndpointParams endpoint "removePageParam"}}} }, {{/if}}options?: AppInfiniteQueryOptions<typeof {{importedEndpointName endpoint}}, TData>{{#if hasAxiosRequestConfig}}, {{axiosRequestConfigName}}?: {{axiosRequestConfigType}}{{/if}}) => {
|
|
5
5
|
return {{infiniteQueryHook}}({
|
|
6
6
|
queryKey: keys.{{endpointName endpoint}}Infinite({{#if (endpointParams endpoint)}}{{{endpointArgs endpoint "removePageParam"}}}{{/if}}),
|
|
7
|
-
queryFn: ({ pageParam }) => {{importedEndpointName endpoint}}({{{endpointArgs endpoint "replacePageParam"}}}),
|
|
7
|
+
queryFn: ({ pageParam }) => {{importedEndpointName endpoint}}({{{endpointArgs endpoint "replacePageParam"}}}{{#if hasAxiosRequestConfig}}, {{axiosRequestConfigName}}{{/if}}),
|
|
8
8
|
initialPageParam: 1,
|
|
9
9
|
getNextPageParam: ({ {{pageParamName}}, {{totalItemsName}}, {{limitParamName}}: limitParam }) => {
|
|
10
10
|
const pageParam = {{pageParamName}} ?? 1;
|
|
@@ -1,17 +1,15 @@
|
|
|
1
1
|
{{! Js docs }}
|
|
2
2
|
{{{genQueryJsDocs endpoint}}}
|
|
3
3
|
{{! Mutation definition}}
|
|
4
|
-
export const {{queryName endpoint}} = (options?: AppMutationOptions<typeof {{importedEndpointName endpoint}}, { {{{genEndpointParams endpoint}}} }>) => {
|
|
5
|
-
{{#if
|
|
4
|
+
export const {{queryName endpoint}} = (options?: AppMutationOptions<typeof {{importedEndpointName endpoint}}, { {{{genEndpointParams endpoint}}} }>{{#if hasInvalidateQueryOptions}} & {{invalidateQueryOptionsType}}{{/if}}{{#if hasAxiosRequestConfig}}, {{axiosRequestConfigName}}?: {{axiosRequestConfigType}}{{/if}}) => {
|
|
5
|
+
{{#if hasInvalidateQueryOptions}} const queryClient = useQueryClient();{{/if}}
|
|
6
6
|
|
|
7
7
|
return {{queryHook}}({
|
|
8
|
-
mutationFn: ({{#if (endpointParams endpoint)}} { {{{endpointArgs endpoint}}} } {{/if}}) => {{importedEndpointName endpoint}}({{{endpointArgs endpoint}}}),
|
|
9
|
-
...options, {{#if
|
|
8
|
+
mutationFn: ({{#if (endpointParams endpoint)}} { {{{endpointArgs endpoint}}} } {{/if}}) => {{importedEndpointName endpoint}}({{{endpointArgs endpoint}}}{{#if hasAxiosRequestConfig}}{{#if (endpointArgs endpoint)}}, {{/if}}{{axiosRequestConfigName}}{{/if}}),
|
|
9
|
+
...options, {{#if hasInvalidateQueryOptions}}
|
|
10
10
|
onSuccess: (...args) => {
|
|
11
11
|
{{! Invalidation }}
|
|
12
|
-
|
|
13
|
-
queryClient.invalidateQueries({ queryKey: keys.all });
|
|
14
|
-
}
|
|
12
|
+
invalidateQueries(queryClient, {{queriesModuleName}}, options);
|
|
15
13
|
options?.onSuccess?.(...args);
|
|
16
14
|
},{{/if}}
|
|
17
15
|
});
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{{! Js docs }}
|
|
2
2
|
{{{genQueryJsDocs endpoint}}}
|
|
3
3
|
{{! Query definition}}
|
|
4
|
-
export const {{queryName endpoint}} = <TData>({{#if (endpointParams endpoint)}}{ {{{endpointArgs endpoint}}} }: { {{{genEndpointParams endpoint}}} }, {{/if}}options?: AppQueryOptions<typeof {{importedEndpointName endpoint}}, TData>) => {
|
|
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
5
|
return {{queryHook}}({
|
|
6
6
|
queryKey: keys.{{endpointName endpoint}}({{#if (endpointParams endpoint)}}{{{endpointArgs endpoint}}}{{/if}}),
|
|
7
|
-
queryFn: {{#if
|
|
7
|
+
queryFn: {{#if hasEndpointArgs}}() => {{importedEndpointName endpoint}}({{{endpointArgs endpoint}}}{{#if hasAxiosRequestConfig}}{{#if (endpointArgs endpoint)}}, {{/if}}{{axiosRequestConfigName}}{{/if}}){{else}}{{importedEndpointName endpoint}}{{/if}},
|
|
8
8
|
...options,
|
|
9
9
|
});
|
|
10
10
|
};
|
|
@@ -1,5 +1,13 @@
|
|
|
1
|
+
{{! Axios import }}
|
|
2
|
+
{{#if hasAxiosImport}}
|
|
3
|
+
{{{genImport axiosImport}}}
|
|
4
|
+
{{/if}}
|
|
1
5
|
{{! React query import }}
|
|
2
6
|
{{{genImport queryImport}}}
|
|
7
|
+
{{! Invalidate queries import }}
|
|
8
|
+
{{#if hasInvalidateQueriesImport}}
|
|
9
|
+
{{{genImport invalidateQueriesImport}}}
|
|
10
|
+
{{/if}}
|
|
3
11
|
{{! React query types import }}
|
|
4
12
|
{{{genImport queryTypesImport}}}
|
|
5
13
|
{{! Models import }}
|
|
@@ -14,16 +22,19 @@
|
|
|
14
22
|
{{#if includeNamespace}}
|
|
15
23
|
export namespace {{namespace}} {
|
|
16
24
|
{{/if}}
|
|
25
|
+
|
|
26
|
+
export const {{queriesModuleName}} = "{{namespace}}";
|
|
27
|
+
|
|
17
28
|
{{! Query keys export}}
|
|
18
|
-
{{{genQueryKeys queryEndpoints
|
|
29
|
+
{{{genQueryKeys queryEndpoints}}}
|
|
19
30
|
|
|
20
31
|
{{! Query export }}
|
|
21
32
|
{{#each endpoints as | endpoint |}}
|
|
22
|
-
{{{genQuery endpoint
|
|
33
|
+
{{{genQuery endpoint}}}
|
|
23
34
|
|
|
24
35
|
{{#if ../generateInfiniteQueries}}
|
|
25
36
|
{{#if (isInfiniteQuery endpoint)}}
|
|
26
|
-
{{{genInfiniteQuery endpoint
|
|
37
|
+
{{{genInfiniteQuery endpoint}}}
|
|
27
38
|
{{/if}}
|
|
28
39
|
{{/if}}
|
|
29
40
|
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
{{! Module name and keys imports }}
|
|
2
|
+
{{#each imports as | import |}}
|
|
3
|
+
{{{genImport import}}}
|
|
4
|
+
{{/each}}
|
|
5
|
+
|
|
6
|
+
{{! Queries module name and keys object }}
|
|
7
|
+
export const QueriesModuleKeysAll = {
|
|
8
|
+
{{#if includeNamespace}}{{#each modules as | module |}} [{{module.namespace}}.{{../queriesModuleName}}]: {{#if module.hasKeys}}{{module.namespace}}.keys.all{{else}}undefined{{/if}}, {{/each}}
|
|
9
|
+
{{else}}{{#each modules as | module |}} [{{tagModuleName module.tag}}]:{{#if module.hasKeys}}{{tagKeys module.tag}}.all{{else}}undefined{{/if}}, {{/each}}{{/if}}
|
|
10
|
+
} as const;
|
|
11
|
+
|
|
12
|
+
export type QueriesModule = keyof typeof QueriesModuleKeysAll;
|
|
13
|
+
|
|
14
|
+
|