@toktokhan-dev/cli-plugin-gen-api-react-query 0.0.18 → 0.0.19

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.18",
3
+ "version": "0.0.19",
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.11",
42
- "@toktokhan-dev/node": "0.0.10"
41
+ "@toktokhan-dev/node": "0.0.10",
42
+ "@toktokhan-dev/cli": "0.0.11"
43
43
  },
44
44
  "devDependencies": {
45
45
  "@types/lodash": "^4.17.0",
@@ -45,6 +45,8 @@
45
45
  const hookKeyName = `${lowerhookVariant}Key`;
46
46
 
47
47
  const isAxiosInstance = myConfig.httpClientType === 'axios'
48
+ const fetchErrorType = `{ error: ${errorType} }`
49
+
48
50
 
49
51
 
50
52
  const initialPageParam = () => {
@@ -81,7 +83,7 @@
81
83
  <% if (isAxiosInstance) { %>
82
84
  AxiosError<<%~ errorType %>>
83
85
  <% } else { %>
84
- <%~ errorType %>
86
+ <%~ fetchErrorType %>
85
87
  <% } %>
86
88
  , TData>,
87
89
  ) => {
@@ -98,7 +100,7 @@
98
100
  <% if (isAxiosInstance) { %>
99
101
  AxiosError<<%~ errorType %>>
100
102
  <% } else { %>
101
- <%~ errorType %>
103
+ <%~ fetchErrorType %>
102
104
  <% } %>
103
105
  >,
104
106
  ) => {
@@ -1,173 +1,70 @@
1
1
  <%
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 %>
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
+ * 스크립트가 실행될때, 파일을 항상 새로 쓰기 때문에 파일 수정시 작성내용이 제거 될 수 있습니다.
76
18
  */
77
19
 
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
- });
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
+ >;
113
60
  };
114
- <% } %>
115
-
116
61
 
117
- <% if (isQuery && hasPagination) { %>
62
+ export type OptionalVariables<T> = undefined extends T
63
+ ? { variables?: T }
64
+ : { variables: T };
118
65
 
119
- /**
120
- <%~ routeDocs.description %>
66
+ export type Parameter<T> = T extends (param: infer U) => any ? U : never;
121
67
 
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
- };
68
+ export type CustomRequestFn = (variables?: any) => Promise<any>;
172
69
 
173
- <% } %>
70
+ export type RequestFnReturn<T extends CustomRequestFn> = Awaited<ReturnType<T>>;