@toktokhan-dev/cli-plugin-gen-api-react-query 0.0.1

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.
Files changed (38) hide show
  1. package/dist/index.d.ts +62 -0
  2. package/dist/index.js +196 -0
  3. package/package.json +40 -0
  4. package/templates/custom-axios/api.eta +176 -0
  5. package/templates/custom-axios/data-contracts.eta +76 -0
  6. package/templates/custom-axios/http-client.eta +144 -0
  7. package/templates/custom-axios/procedure-call.eta +133 -0
  8. package/templates/custom-fetch/api.eta +164 -0
  9. package/templates/custom-fetch/data-contracts.eta +76 -0
  10. package/templates/custom-fetch/http-client.eta +224 -0
  11. package/templates/custom-fetch/procedure-call.eta +151 -0
  12. package/templates/default/README.md +17 -0
  13. package/templates/default/base/README.md +8 -0
  14. package/templates/default/base/data-contract-jsdoc.ejs +37 -0
  15. package/templates/default/base/data-contracts.ejs +28 -0
  16. package/templates/default/base/enum-data-contract.ejs +12 -0
  17. package/templates/default/base/http-client.ejs +3 -0
  18. package/templates/default/base/http-clients/axios-http-client.ejs +138 -0
  19. package/templates/default/base/http-clients/fetch-http-client.ejs +224 -0
  20. package/templates/default/base/interface-data-contract.ejs +10 -0
  21. package/templates/default/base/object-field-jsdoc.ejs +28 -0
  22. package/templates/default/base/route-docs.ejs +30 -0
  23. package/templates/default/base/route-name.ejs +43 -0
  24. package/templates/default/base/route-type.ejs +22 -0
  25. package/templates/default/base/type-data-contract.ejs +15 -0
  26. package/templates/default/default/README.md +7 -0
  27. package/templates/default/default/api.ejs +65 -0
  28. package/templates/default/default/procedure-call.ejs +100 -0
  29. package/templates/default/default/route-types.ejs +28 -0
  30. package/templates/default/modular/README.md +7 -0
  31. package/templates/default/modular/api.ejs +28 -0
  32. package/templates/default/modular/procedure-call.ejs +100 -0
  33. package/templates/default/modular/route-types.ejs +18 -0
  34. package/templates/my/param-serializer-by.eta +41 -0
  35. package/templates/my/react-query-hook.eta +171 -0
  36. package/templates/my/react-query-key.eta +46 -0
  37. package/templates/my/react-query-type.eta +61 -0
  38. package/templates/my/util-types.eta +32 -0
@@ -0,0 +1,171 @@
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
+ isOptialnalVariabels,
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 = isOptialnalVariabels ? "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
+
49
+
50
+ const initialPageParam = () => {
51
+ if(typeof pagination.initialPageParam === "string") return pagination.initialPageParam;
52
+ if(typeof pagination.initialPageParam === "function") return pagination.initialPageParam({ apiInstanceName, functionName, pagination})
53
+ }
54
+
55
+ const getNextPage = () => {
56
+ if(typeof pagination.getNextPage === "string") return pagination.getNextPage;
57
+ if(typeof pagination.getNextPage === "function") return pagination.getNextPage({ apiInstanceName, functionName, pagination})
58
+ }
59
+
60
+ const getNextPageParam = () => {
61
+ if(typeof pagination.getNextPageParam === "string") return pagination.getNextPageParam;
62
+ if(typeof pagination.getNextPageParam === "function") return pagination.getNextPageParam({ apiInstanceName, functionName, pagination})
63
+ }
64
+
65
+
66
+ %>
67
+
68
+ /**
69
+ <%~ routeDocs.description %>
70
+
71
+ * <% /* Here you can add some other JSDoc tags */ %>
72
+
73
+ <%~ routeDocs.lines %>
74
+ */
75
+
76
+ <% if (isQuery) { %>
77
+ export const use<%~ classNameCase(functionName) %>Query = <
78
+ TData = RequestFnReturn<typeof <%= apiInstanceName %>.<%~ functionName %>>,
79
+ >(
80
+ <%~ conditionalParamsText %>: QueryHookParams<typeof <%= apiInstanceName %>.<%~ functionName %>,
81
+ <% if (isAxiosInstance) { %>
82
+ AxiosError<<%~ errorType %>>
83
+ <% } else { %>
84
+ <%~ errorType %>
85
+ <% } %>
86
+ , TData>,
87
+ ) => {
88
+ const <%~ hookKeyName %> = <%~ queryKeyName %>.<%~ methodKey %>(<%~ `${conditionalParamsText}.variables` %>);
89
+ return useQuery({
90
+ <%~ hookKeyName %>,
91
+ queryFn: () => <%= apiInstanceName %>.<%~ functionName %>(<%~ `${conditionalParamsText}.variables` %>),
92
+ ...params?.options,
93
+ });
94
+ };
95
+ <% } else { %>
96
+ export const use<%~ classNameCase(functionName) %>Mutation = (
97
+ <%~ conditionalParamsText %>: MutationHookParams<typeof <%= apiInstanceName %>.<%~ functionName %>,
98
+ <% if (isAxiosInstance) { %>
99
+ AxiosError<<%~ errorType %>>
100
+ <% } else { %>
101
+ <%~ errorType %>
102
+ <% } %>
103
+ >,
104
+ ) => {
105
+ const <%~ hookKeyName %> = <%~ queryKeyName %>.<%~ methodKey %>();
106
+ return useMutation({
107
+ <%~ hookKeyName %>,
108
+ mutationFn: <%= apiInstanceName %>.<%~ functionName %>,
109
+ ...params?.options,
110
+ });
111
+ };
112
+ <% } %>
113
+
114
+
115
+ <% if (isQuery && hasPagination) { %>
116
+
117
+ /**
118
+ <%~ routeDocs.description %>
119
+
120
+ * <% /* Here you can add some other JSDoc tags */ %>
121
+
122
+ <%~ routeDocs.lines %>
123
+
124
+ */
125
+ export const use<%~ classNameCase(functionName) %>InfiniteQuery = <
126
+ TData = RequestFnReturn<typeof <%= apiInstanceName %>.<%~ functionName %>>,
127
+ >(
128
+ <%~ conditionalParamsText %>: InfiniteQueryHookParams<typeof <%= apiInstanceName %>.<%~ functionName %>,
129
+ <% if (isAxiosInstance) { %>
130
+ AxiosError<<%~ errorType %>>
131
+ <% } else { %>
132
+ <%~ errorType %>
133
+ <% } %>
134
+ , TData>,
135
+ ) => {
136
+ const <%~ hookKeyName %> = <%~ queryKeyName %>.<%~ methodKey %>_INFINITE(<%~ conditionalQueryKeyParams %>);
137
+
138
+ return useInfiniteQuery({
139
+ <%~ hookKeyName %>,
140
+ initialPageParam: <% if (initialPageParam()) { %>
141
+ <%~ initialPageParam() %>
142
+ <% } else { %>
143
+ null
144
+ <% } %>,
145
+ queryFn: <% if (getNextPage()) { %>
146
+ <%~ getNextPage() %>
147
+ <% } else { %>
148
+ ({ pageParam }) => {
149
+ return <%= apiInstanceName %>.<%~ functionName %>({
150
+ ...params?.variables,
151
+ query: { ...params?.variables?.query, <%~ pagination.nextKey %>: pageParam, }
152
+ });
153
+ }
154
+ <% } %>,
155
+ getNextPageParam:
156
+ <% if (getNextPageParam()) { %>
157
+ <%~ getNextPageParam() %>
158
+ <% } else { %>
159
+ (lastPage) => {
160
+ const params = lastPage.next
161
+ ? new URL(lastPage.next).searchParams
162
+ : null;
163
+ const <%~ pagination.nextKey %> = params ? params.get('<%~ pagination.nextKey %>') : null;
164
+ return <%~ pagination.nextKey %>;
165
+ }
166
+ <% } %>,
167
+ ...params?.options,
168
+ });
169
+ };
170
+
171
+ <% } %>
@@ -0,0 +1,46 @@
1
+ <%
2
+ const { utils, route, config, modelTypes,dataFromApiTemplate } = it;
3
+ const routes = route.routes;
4
+ const { getConfigByRoute ,queryKeyName } = dataFromApiTemplate;
5
+ %>
6
+ export const <%= queryKeyName %> = {
7
+ <% routes.forEach((route) => { %>
8
+ <%
9
+ const {
10
+ conditions:{
11
+ hasPagination,
12
+ isQuery,
13
+ isMutation,
14
+ isOptialnalVariabels,
15
+ },
16
+ data: {
17
+ rawWrapperArgs,
18
+ wrapperArgs,
19
+ functionName,
20
+ apiInstanceName,
21
+ apiClassName,
22
+ hookVariant,
23
+ key,
24
+ methodKey,
25
+ },
26
+ utils: {
27
+ upperSnakeCase,
28
+ removeModuleName,
29
+ argToTmpl,
30
+ }
31
+ } = getConfigByRoute(route);
32
+ const conditionalParam = isQuery ? `(variables? : Parameter<typeof ${apiInstanceName}.${functionName}>)`: "()"
33
+ const conditionalHookKeyVariant = isQuery ? "variables" : ""
34
+ %>
35
+ <%~ methodKey %>: <%~ conditionalParam %> => ["<%~ key %>", <%~ conditionalHookKeyVariant %>].filter(key => typeof key !== 'undefined'),
36
+ <% if(hasPagination) { %>
37
+ <%~ methodKey %>_INFINITE : <%~ conditionalParam %> => ["<%~ key %>_INFINITE", <%~ conditionalHookKeyVariant %>].filter(key => typeof key !== 'undefined'),
38
+ <% }%>
39
+ <% }) %>
40
+ };
41
+
42
+
43
+
44
+
45
+
46
+
@@ -0,0 +1,61 @@
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
+ export type QueryHookParams<
14
+ T extends CustomRequestFn,
15
+ Error = <%= errorType %>,
16
+ TData = RequestFnReturn<T>,
17
+ OriginData = RequestFnReturn<T>,
18
+ Variables = Parameter<T>,
19
+ > = {
20
+ options?: Omit<
21
+ UseQueryOptions<OriginData, Error, TData>,
22
+ 'queryKey' | 'queryFn'
23
+ >;
24
+ } & OptionalVariables<Variables>;
25
+
26
+ export type InfiniteQueryHookParams<
27
+ T extends CustomRequestFn,
28
+ Error = <%= errorType %>,
29
+ TData = RequestFnReturn<T>,
30
+ OriginData = RequestFnReturn<T>,
31
+ Variables = Parameter<T>,
32
+ > = {
33
+ options?: Partial<
34
+ Omit<
35
+ UseInfiniteQueryOptions<OriginData, Error, TData, OriginData, any, any>,
36
+ 'queryKey' | 'queryFn'
37
+ >
38
+ >;
39
+ } & OptionalVariables<Variables>;
40
+
41
+ export type MutationHookParams<
42
+ T extends CustomRequestFn,
43
+ Error = <%= errorType %>,
44
+ Data = RequestFnReturn<T>,
45
+ Variables = Parameter<T>,
46
+ > = {
47
+ options?: Omit<
48
+ UseMutationOptions<Data, Error, Variables>,
49
+ 'mutationFn' | 'mutationKey'
50
+ >;
51
+ };
52
+
53
+ export type OptionalVariables<T> = undefined extends T
54
+ ? { variables?: T }
55
+ : { variables: T };
56
+
57
+ export type Parameter<T> = T extends (param: infer U) => any ? U : never;
58
+
59
+ export type CustomRequestFn = (variables?: any) => Promise<any>;
60
+
61
+ export type RequestFnReturn<T extends CustomRequestFn> = Awaited<ReturnType<T>>;
@@ -0,0 +1,32 @@
1
+ export type DeepOmitReadOnly<T extends object | undefined> = Omit<
2
+ {
3
+ [P in keyof T]: NonNullable<T[P]> extends Array<infer I>
4
+ ? I extends object
5
+ ? null extends T[P]
6
+ ? DeepOmitReadOnly<NonNullable<I>>[] | null
7
+ : DeepOmitReadOnly<NonNullable<I>>[]
8
+ : I[]
9
+ : NonNullable<T[P]> extends object | undefined
10
+ ? null extends T[P]
11
+ ? DeepOmitReadOnly<NonNullable<T[P]>> | null
12
+ : DeepOmitReadOnly<NonNullable<T[P]>>
13
+ : T[P];
14
+ },
15
+ ReadonlyKeys<NonNullable<T>>
16
+ >;
17
+ export type OmitReadOnly<T extends object> = Omit<T, ReadonlyKeys<T>>;
18
+
19
+ export type ReadonlyKeys<T extends object> = {
20
+ [P in keyof T]-?: IfEquals<
21
+ { [Q in P]: T[P] },
22
+ { -readonly [Q in P]: T[P] },
23
+ never,
24
+ P
25
+ >;
26
+ }[keyof T];
27
+
28
+ export type IfEquals<X, Y, A = X, B = never> = (<T>() => T extends X
29
+ ? 1
30
+ : 2) extends <T>() => T extends Y ? 1 : 2
31
+ ? A
32
+ : B;