@toktokhan-dev/cli-plugin-gen-api-react-query 0.0.16 → 0.0.18
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 +3 -3
- package/templates/my/react-query-type.eta +165 -62
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@toktokhan-dev/cli-plugin-gen-api-react-query",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.18",
|
|
4
4
|
"description": "A CLI plugin for generating API hooks with React Query built by TOKTOKHAN.DEV",
|
|
5
5
|
"author": "TOKTOKHAN.DEV <fe-system@toktokhan.dev>",
|
|
6
6
|
"license": "ISC",
|
|
@@ -38,8 +38,8 @@
|
|
|
38
38
|
"lodash": "^4.17.21",
|
|
39
39
|
"prettier-plugin-organize-imports": "^3.2.4",
|
|
40
40
|
"swagger-typescript-api": "^13.0.3",
|
|
41
|
-
"@toktokhan-dev/cli": "0.0.
|
|
42
|
-
"@toktokhan-dev/node": "0.0.
|
|
41
|
+
"@toktokhan-dev/cli": "0.0.11",
|
|
42
|
+
"@toktokhan-dev/node": "0.0.10"
|
|
43
43
|
},
|
|
44
44
|
"devDependencies": {
|
|
45
45
|
"@types/lodash": "^4.17.0",
|
|
@@ -1,70 +1,173 @@
|
|
|
1
1
|
<%
|
|
2
|
-
const { myConfig } = it;
|
|
3
|
-
const
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
2
|
+
const { utils, route, config, myConfig, dataFromApiTemplate } = it;
|
|
3
|
+
const { query } = route.request;
|
|
4
|
+
const { errorType } = route.response;
|
|
5
|
+
const { getConfigByRoute,paginationTargetKeywords } = dataFromApiTemplate;
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
const {
|
|
9
|
+
conditions:{
|
|
10
|
+
hasPagination,
|
|
11
|
+
isQuery,
|
|
12
|
+
isMutation,
|
|
13
|
+
isOptionalVariables,
|
|
14
|
+
},
|
|
15
|
+
data: {
|
|
16
|
+
rawWrapperArgs,
|
|
17
|
+
wrapperArgs,
|
|
18
|
+
queryKeyName,
|
|
19
|
+
functionName,
|
|
20
|
+
apiInstanceName,
|
|
21
|
+
apiClassName,
|
|
22
|
+
hookVariant,
|
|
23
|
+
key,
|
|
24
|
+
methodKey,
|
|
25
|
+
pagination
|
|
26
|
+
},
|
|
27
|
+
utils: {
|
|
28
|
+
upperSnakeCase,
|
|
29
|
+
removeModuleName,
|
|
30
|
+
argToTmpl,
|
|
31
|
+
}
|
|
32
|
+
} = getConfigByRoute(route);
|
|
33
|
+
|
|
34
|
+
const { _, classNameCase } = utils;
|
|
35
|
+
|
|
36
|
+
const conditionalParamsText = isOptionalVariables ? "params?" : "params"
|
|
37
|
+
const conditionCallWithVariabels = isQuery ? `(${conditionalParamsText}.variables)` : "";
|
|
38
|
+
const conditionParamQueryKey = isQuery ? `,${conditionalParamsText}.variables` : "";
|
|
39
|
+
const conditionWrapFunc = isQuery ? "() =>" : "";
|
|
40
|
+
const conditionalQueryKeyParams = isQuery ? `${conditionalParamsText}.variables` : "";
|
|
41
|
+
const conditionalFn = isQuery ? "queryFn" : "mutationFn";
|
|
42
|
+
|
|
43
|
+
const routeDocs = includeFile("@base/route-docs", { config, route, utils });
|
|
44
|
+
const lowerhookVariant = _.lowerCase(hookVariant)
|
|
45
|
+
const hookKeyName = `${lowerhookVariant}Key`;
|
|
46
|
+
|
|
47
|
+
const isAxiosInstance = myConfig.httpClientType === 'axios'
|
|
48
|
+
const fetchErrorType = `{ error: ${errorType} }`
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
const initialPageParam = () => {
|
|
53
|
+
if(typeof pagination.initialPageParam === "string") return pagination.initialPageParam;
|
|
54
|
+
if(typeof pagination.initialPageParam === "function") return pagination.initialPageParam({ apiInstanceName, functionName, pagination})
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const getNextPage = () => {
|
|
58
|
+
if(typeof pagination.getNextPage === "string") return pagination.getNextPage;
|
|
59
|
+
if(typeof pagination.getNextPage === "function") return pagination.getNextPage({ apiInstanceName, functionName, pagination})
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const getNextPageParam = () => {
|
|
63
|
+
if(typeof pagination.getNextPageParam === "string") return pagination.getNextPageParam;
|
|
64
|
+
if(typeof pagination.getNextPageParam === "function") return pagination.getNextPageParam({ apiInstanceName, functionName, pagination})
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
%>
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
<%~ routeDocs.description %>
|
|
72
|
+
|
|
73
|
+
* <% /* Here you can add some other JSDoc tags */ %>
|
|
74
|
+
|
|
75
|
+
<%~ routeDocs.lines %>
|
|
18
76
|
*/
|
|
19
77
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
> = {
|
|
56
|
-
options?: Omit<
|
|
57
|
-
UseMutationOptions<Data, Error, Variables>,
|
|
58
|
-
'mutationFn' | 'mutationKey'
|
|
59
|
-
>;
|
|
78
|
+
<% if (isQuery) { %>
|
|
79
|
+
export const use<%~ classNameCase(functionName) %>Query = <
|
|
80
|
+
TData = RequestFnReturn<typeof <%= apiInstanceName %>.<%~ functionName %>>,
|
|
81
|
+
>(
|
|
82
|
+
<%~ conditionalParamsText %>: QueryHookParams<typeof <%= apiInstanceName %>.<%~ functionName %>,
|
|
83
|
+
<% if (isAxiosInstance) { %>
|
|
84
|
+
AxiosError<<%~ errorType %>>
|
|
85
|
+
<% } else { %>
|
|
86
|
+
<%~ fetchErrorType %>
|
|
87
|
+
<% } %>
|
|
88
|
+
, TData>,
|
|
89
|
+
) => {
|
|
90
|
+
const <%~ hookKeyName %> = <%~ queryKeyName %>.<%~ methodKey %>(<%~ `${conditionalParamsText}.variables` %>);
|
|
91
|
+
return useQuery({
|
|
92
|
+
<%~ hookKeyName %>,
|
|
93
|
+
queryFn: () => <%= apiInstanceName %>.<%~ functionName %>(<%~ `${conditionalParamsText}.variables` %>),
|
|
94
|
+
...params?.options,
|
|
95
|
+
});
|
|
96
|
+
};
|
|
97
|
+
<% } else { %>
|
|
98
|
+
export const use<%~ classNameCase(functionName) %>Mutation = (
|
|
99
|
+
<%~ conditionalParamsText %>: MutationHookParams<typeof <%= apiInstanceName %>.<%~ functionName %>,
|
|
100
|
+
<% if (isAxiosInstance) { %>
|
|
101
|
+
AxiosError<<%~ errorType %>>
|
|
102
|
+
<% } else { %>
|
|
103
|
+
<%~ fetchErrorType %>
|
|
104
|
+
<% } %>
|
|
105
|
+
>,
|
|
106
|
+
) => {
|
|
107
|
+
const <%~ hookKeyName %> = <%~ queryKeyName %>.<%~ methodKey %>();
|
|
108
|
+
return useMutation({
|
|
109
|
+
<%~ hookKeyName %>,
|
|
110
|
+
mutationFn: <%= apiInstanceName %>.<%~ functionName %>,
|
|
111
|
+
...params?.options,
|
|
112
|
+
});
|
|
60
113
|
};
|
|
114
|
+
<% } %>
|
|
115
|
+
|
|
61
116
|
|
|
62
|
-
|
|
63
|
-
? { variables?: T }
|
|
64
|
-
: { variables: T };
|
|
117
|
+
<% if (isQuery && hasPagination) { %>
|
|
65
118
|
|
|
66
|
-
|
|
119
|
+
/**
|
|
120
|
+
<%~ routeDocs.description %>
|
|
67
121
|
|
|
68
|
-
|
|
122
|
+
* <% /* Here you can add some other JSDoc tags */ %>
|
|
123
|
+
|
|
124
|
+
<%~ routeDocs.lines %>
|
|
125
|
+
|
|
126
|
+
*/
|
|
127
|
+
export const use<%~ classNameCase(functionName) %>InfiniteQuery = <
|
|
128
|
+
TData = InfiniteData<
|
|
129
|
+
RequestFnReturn<typeof <%= apiInstanceName %>.<%~ functionName %>>,
|
|
130
|
+
Parameter<typeof <%= apiInstanceName %>.<%~ functionName %>>
|
|
131
|
+
>,
|
|
132
|
+
>(
|
|
133
|
+
<%~ conditionalParamsText %>: InfiniteQueryHookParams<typeof <%= apiInstanceName %>.<%~ functionName %>,
|
|
134
|
+
<% if (isAxiosInstance) { %>
|
|
135
|
+
AxiosError<<%~ errorType %>>
|
|
136
|
+
<% } else { %>
|
|
137
|
+
<%~ errorType %>
|
|
138
|
+
<% } %>
|
|
139
|
+
, TData>,
|
|
140
|
+
) => {
|
|
141
|
+
const <%~ hookKeyName %> = <%~ queryKeyName %>.<%~ methodKey %>_INFINITE(<%~ conditionalQueryKeyParams %>);
|
|
142
|
+
|
|
143
|
+
return useInfiniteQuery({
|
|
144
|
+
<%~ hookKeyName %>,
|
|
145
|
+
initialPageParam: <% if (initialPageParam()) { %>
|
|
146
|
+
<%~ initialPageParam() %>
|
|
147
|
+
<% } else { %>
|
|
148
|
+
null
|
|
149
|
+
<% } %>,
|
|
150
|
+
queryFn: <% if (getNextPage()) { %>
|
|
151
|
+
<%~ getNextPage() %>
|
|
152
|
+
<% } else { %>
|
|
153
|
+
({ pageParam }) => {
|
|
154
|
+
return <%= apiInstanceName %>.<%~ functionName %>({
|
|
155
|
+
...params?.variables,
|
|
156
|
+
query: { ...params?.variables?.query, <%~ pagination.nextKey %>: pageParam, }
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
<% } %>,
|
|
160
|
+
getNextPageParam:
|
|
161
|
+
<% if (getNextPageParam()) { %>
|
|
162
|
+
<%~ getNextPageParam() %>
|
|
163
|
+
<% } else { %>
|
|
164
|
+
(lastPage) => {
|
|
165
|
+
const <%~ pagination.nextKey %> = lastPage.cursor ?? null;
|
|
166
|
+
return <%~ pagination.nextKey %>;
|
|
167
|
+
}
|
|
168
|
+
<% } %>,
|
|
169
|
+
...params?.options,
|
|
170
|
+
});
|
|
171
|
+
};
|
|
69
172
|
|
|
70
|
-
|
|
173
|
+
<% } %>
|