@toktokhan-dev/cli-plugin-gen-api-react-query 0.0.17 → 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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@toktokhan-dev/cli-plugin-gen-api-react-query",
3
- "version": "0.0.17",
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",
@@ -1,70 +1,173 @@
1
1
  <%
2
- const { myConfig } = it;
3
- const errorType = myConfig.httpClientType === 'axios' ? 'AxiosError<any>' : 'any';
4
- %>
5
- import { AxiosError } from 'axios';
6
-
7
- import {
8
- UseInfiniteQueryOptions,
9
- UseMutationOptions,
10
- UseQueryOptions,
11
- } from '@tanstack/react-query';
12
-
13
-
14
- /**
15
- * !DO NOT EDIT THIS FILE
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
- export type QueryHookParams<
23
- T extends CustomRequestFn,
24
- Error = <%~ errorType %>,
25
- TData = RequestFnReturn<T>,
26
- OriginData = RequestFnReturn<T>,
27
- Variables = Parameter<T>,
28
- > = {
29
- options?: Omit<
30
- UseQueryOptions<OriginData, Error, TData>,
31
- 'queryKey' | 'queryFn'
32
- >;
33
- } & OptionalVariables<Variables>;
34
-
35
- export type InfiniteQueryHookParams<
36
- T extends CustomRequestFn,
37
- Error = <%~ errorType %>,
38
- TData = RequestFnReturn<T>,
39
- OriginData = RequestFnReturn<T>,
40
- Variables = Parameter<T>,
41
- > = {
42
- options?: Partial<
43
- Omit<
44
- UseInfiniteQueryOptions<OriginData, Error, TData, OriginData, any, any>,
45
- 'queryKey' | 'queryFn'
46
- >
47
- >;
48
- } & OptionalVariables<Variables>;
49
-
50
- export type MutationHookParams<
51
- T extends CustomRequestFn,
52
- Error = <%~ errorType %>,
53
- Data = RequestFnReturn<T>,
54
- Variables = Parameter<T>,
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
- export type OptionalVariables<T> = undefined extends T
63
- ? { variables?: T }
64
- : { variables: T };
117
+ <% if (isQuery && hasPagination) { %>
65
118
 
66
- export type Parameter<T> = T extends (param: infer U) => any ? U : never;
119
+ /**
120
+ <%~ routeDocs.description %>
67
121
 
68
- export type CustomRequestFn = (variables?: any) => Promise<any>;
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
- export type RequestFnReturn<T extends CustomRequestFn> = Awaited<ReturnType<T>>;
173
+ <% } %>