@toktokhan-dev/cli-plugin-gen-api-react-query 0.0.7 → 0.0.9

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/dist/index.js CHANGED
@@ -1,9 +1,10 @@
1
1
  import { defineCommand } from '@toktokhan-dev/cli';
2
- import { createPackageRoot, prettierString, prettierFile, withLoading, cwd } from '@toktokhan-dev/node';
2
+ import { createPackageRoot, prettierString, withLoading, cwd } from '@toktokhan-dev/node';
3
3
  import path from 'path';
4
4
  import { generateApi } from 'swagger-typescript-api';
5
5
  import fs from 'fs';
6
6
  import camelCase from 'lodash/camelCase.js';
7
+ import upperFirst from 'lodash/upperFirst.js';
7
8
 
8
9
 
9
10
  // -- Shims --
@@ -22,7 +23,7 @@ const GENERATE_SWAGGER_DATA = {
22
23
  TYPE_FILE: ['react-query-type.ts', 'data-contracts.ts', 'util-types.ts'],
23
24
  UTIL_FILE: ['param-serializer-by.ts'],
24
25
  QUERY_HOOK_INDICATOR: '@indicator-for-query-hook',
25
- AXIOS_DEFAULT_INSTANCE_PATH: '@apis/_axios/instance',
26
+ AXIOS_DEFAULT_INSTANCE_PATH: '@/configs/axios/instance',
26
27
  FETCH_DEFAULT_INSTANCE_PATH: '@/configs/fetch/fetch-extend',
27
28
  };
28
29
 
@@ -109,11 +110,15 @@ const writeSwaggerApiFile = (params) => {
109
110
  });
110
111
  };
111
112
  async function genreatePretty(path, contents) {
112
- generate(path, await prettierString(contents, {
113
+ const organized = await prettierString(contents, {
113
114
  parser: 'babel-ts',
114
115
  plugins: ['prettier-plugin-organize-imports'],
115
- }));
116
- await prettierFile(path, { parser: 'typescript' });
116
+ });
117
+ const formatted = await prettierString(organized, {
118
+ parser: 'typescript',
119
+ configPath: 'auto',
120
+ });
121
+ generate(path, formatted);
117
122
  }
118
123
  function generate(path, contents) {
119
124
  fs.writeFileSync(path, contents);
@@ -123,7 +128,7 @@ function spilitHookContents(filename, content) {
123
128
  const lastImport = getLastImportLine(content);
124
129
  const lines = content.split('\n');
125
130
  const importArea = [
126
- `import { ${camelCase(filename)}Api } from './${filename}.api';`,
131
+ `import { ${upperFirst(camelCase(filename))}Api } from './${filename}.api';`,
127
132
  ...lines.slice(0, lastImport),
128
133
  ].join('\n');
129
134
  return {
@@ -152,7 +157,7 @@ const genApi = defineCommand({
152
157
  includeReactQuery: true,
153
158
  includeReactInfiniteQuery: true,
154
159
  httpClientType: 'axios',
155
- instancePath: '@apis/_axios/instance',
160
+ instancePath: GENERATE_SWAGGER_DATA.AXIOS_DEFAULT_INSTANCE_PATH,
156
161
  paginations: [
157
162
  {
158
163
  keywords: ['cursor'],
@@ -161,7 +166,6 @@ const genApi = defineCommand({
161
166
  ],
162
167
  },
163
168
  run: async (config) => {
164
- console.log(config);
165
169
  const isWebUrl = (string) => string.startsWith('http://') || string.startsWith('https://');
166
170
  const coverPath = (config) => {
167
171
  const { httpClientType, swaggerSchemaUrl, output } = config;
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.7",
3
+ "version": "0.0.9",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -27,8 +27,8 @@
27
27
  "lodash": "^4.17.21",
28
28
  "prettier-plugin-organize-imports": "^3.2.4",
29
29
  "swagger-typescript-api": "^13.0.3",
30
- "@toktokhan-dev/cli": "0.0.5",
31
- "@toktokhan-dev/node": "0.0.4"
30
+ "@toktokhan-dev/cli": "0.0.6",
31
+ "@toktokhan-dev/node": "0.0.5"
32
32
  },
33
33
  "scripts": {
34
34
  "build": "rollup -c",
@@ -14,7 +14,7 @@ const reactQuerKeyTemplatePath = `${myTemeplatePath}/react-query-key.eta`
14
14
 
15
15
  const apiClassName = classNameCase(route.moduleName) + 'Api';
16
16
  const paginations = myConfig?.paginations
17
- const instancePath = myConfig?.instancePath || "@apis/_axios/instance.ts"
17
+ const instancePath = myConfig?.instancePath
18
18
 
19
19
  const apiInstanceName =route.moduleName + "Api";
20
20
  const queryKeyName = "QUERY_KEY_" + _.upperCase(apiClassName).replace(/ /g, '_');
@@ -143,6 +143,14 @@ import { DeepOmitReadOnly } from '../@types/util-types';
143
143
  import { <%~ dataContracts.join(", ") %> } from "../@types/<%~ config.fileNames.dataContracts %>"
144
144
  <% } %>
145
145
 
146
+ /**
147
+ * !DO NOT EDIT THIS FILE
148
+ *
149
+ * 스크립트가 실행될때, 파일을 항상 새로 쓰기 때문에 파일 수정시 작성내용이 제거 될 수 있습니다.
150
+ */
151
+
152
+
153
+
146
154
  export class <%= apiClassName %><SecurityDataType = unknown><% if (!config.singleHttpClient) { %> extends HttpClient<SecurityDataType> <% } %> {
147
155
  <% if(config.singleHttpClient) { %>
148
156
  http: HttpClient<SecurityDataType>;
@@ -158,14 +166,22 @@ export class <%= apiClassName %><SecurityDataType = unknown><% if (!config.singl
158
166
  }
159
167
 
160
168
 
161
- export const <%= apiInstanceName %> = new <%= apiClassName %>({ instance })
162
169
 
163
170
  <% if(myConfig.includeReactQuery) { %>
164
171
  //<%~ myConfig.QUERY_HOOK_INDICATOR %>
165
172
 
166
- /**
167
- * QUERY_KEYS
168
- */
173
+ /**
174
+ * !DO NOT EDIT THIS FILE
175
+ *
176
+ * 스크립트가 실행될때, 파일을 항상 새로 쓰기 때문에 파일 수정시 작성내용이 제거 될 수 있습니다.
177
+ */
178
+
179
+
180
+ /**
181
+ * tok-cli.config.ts 에서 설정된 instance 경로의 axios instace 가 적용된, api 의 instance 입니다.
182
+ */
183
+ export const <%= apiInstanceName %> = new <%= apiClassName %>({ instance:instance })
184
+
169
185
  <%~ includeFile(reactQuerKeyTemplatePath, { ...it, route , dataFromApiTemplate:dataForReactHookTemplate}) %>
170
186
 
171
187
  <% routes.forEach((route) => { %>
@@ -62,6 +62,13 @@
62
62
  ]);
63
63
  }
64
64
  %>
65
+
66
+ /**
67
+ * !DO NOT EDIT THIS FILE
68
+ *
69
+ * 스크립트가 실행될때, 파일을 항상 새로 쓰기 때문에 파일 수정시 작성내용이 제거 될 수 있습니다.
70
+ */
71
+
65
72
  <% modelTypes.forEach((contract) => { %>
66
73
  <% const description = createDescription(contract); %>
67
74
  <% if (description.length) { %>
@@ -4,6 +4,14 @@ const { apiConfig, generateResponses, config } = it;
4
4
 
5
5
  import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse, ResponseType, HeadersDefaults } from "axios";
6
6
 
7
+ /**
8
+ * !DO NOT EDIT THIS FILE
9
+ *
10
+ * 스크립트가 실행될때, 파일을 항상 새로 쓰기 때문에 파일 수정시 작성내용이 제거 될 수 있습니다.
11
+ */
12
+
13
+
14
+
7
15
  export type QueryParamsType = Record<string | number, any>;
8
16
 
9
17
  export interface FullRequestParams extends Omit<AxiosRequestConfig, "data" | "params" | "url" | "responseType"> {
@@ -134,6 +134,15 @@ import { DeepOmitReadOnly } from '../@types/util-types';
134
134
  import { <%~ dataContracts.join(", ") %> } from "../@types/<%~ config.fileNames.dataContracts %>";
135
135
  <% } %>
136
136
 
137
+
138
+ /**
139
+ * !DO NOT EDIT THIS FILE
140
+ *
141
+ * 스크립트가 실행될때, 파일을 항상 새로 쓰기 때문에 파일 수정시 작성내용이 제거 될 수 있습니다.
142
+ */
143
+
144
+
145
+
137
146
  export class <%= apiClassName %><SecurityDataType = unknown><% if (!config.singleHttpClient) { %> extends HttpClient<SecurityDataType> <% } %> {
138
147
  <% if (config.singleHttpClient) { %>
139
148
  http: HttpClient<SecurityDataType>;
@@ -148,14 +157,22 @@ export class <%= apiClassName %><SecurityDataType = unknown><% if (!config.singl
148
157
  <% }) %>
149
158
  }
150
159
 
151
- export const <%= apiInstanceName %> = new <%= apiClassName %>({ customFetch: fetchExtended });
152
160
 
153
161
  <% if (myConfig.includeReactQuery) { %>
154
162
  //<%~ myConfig.QUERY_HOOK_INDICATOR %>
155
163
 
156
- /**
157
- * QUERY_KEYS
158
- */
164
+
165
+ /**
166
+ * !DO NOT EDIT THIS FILE
167
+ *
168
+ * 스크립트가 실행될때, 파일을 항상 새로 쓰기 때문에 파일 수정시 작성내용이 제거 될 수 있습니다.
169
+ */
170
+
171
+
172
+ /**
173
+ * tok-cli.config.ts 에서 설정된 instance 경로의 fetch instance 가 적용된, api 의 instance 입니다.
174
+ */
175
+ export const <%= apiInstanceName %> = new <%= apiClassName %>({ customFetch: fetchExtended });
159
176
  <%~ includeFile(reactQuerKeyTemplatePath, { ...it, route, dataFromApiTemplate: dataForReactHookTemplate }) %>
160
177
 
161
178
  <% routes.forEach((route) => { %>
@@ -62,6 +62,15 @@
62
62
  ]);
63
63
  }
64
64
  %>
65
+
66
+ /**
67
+ * !DO NOT EDIT THIS FILE
68
+ *
69
+ * 스크립트가 실행될때, 파일을 항상 새로 쓰기 때문에 파일 수정시 작성내용이 제거 될 수 있습니다.
70
+ */
71
+
72
+
73
+
65
74
  <% modelTypes.forEach((contract) => { %>
66
75
  <% const description = createDescription(contract); %>
67
76
  <% if (description.length) { %>
@@ -2,6 +2,14 @@
2
2
  const { apiConfig, generateResponses, config } = it;
3
3
  %>
4
4
 
5
+ /**
6
+ * !DO NOT EDIT THIS FILE
7
+ *
8
+ * 스크립트가 실행될때, 파일을 항상 새로 쓰기 때문에 파일 수정시 작성내용이 제거 될 수 있습니다.
9
+ */
10
+
11
+
12
+
5
13
  export type QueryParamsType = Record<string | number, any>;
6
14
  export type ResponseFormat = keyof Omit<Body, "body" | "bodyUsed">;
7
15
 
@@ -1,3 +1,10 @@
1
+ /**
2
+ * !DO NOT EDIT THIS FILE
3
+ *
4
+ * 스크립트가 실행될때, 파일을 항상 새로 쓰기 때문에 파일 수정시 작성내용이 제거 될 수 있습니다.
5
+ */
6
+
7
+
1
8
  export const paramsSerializerBy =
2
9
  (
3
10
  format?: Partial<
@@ -75,10 +75,7 @@
75
75
 
76
76
  <% if (isQuery) { %>
77
77
  export const use<%~ classNameCase(functionName) %>Query = <
78
- TData = InfiniteData<
79
- RequestFnReturn<typeof <%= apiInstanceName %>.<%~ functionName %>>,
80
- Parameter<typeof <%= apiInstanceName %>.<%~ functionName %>>
81
- >,
78
+ TData = RequestFnReturn<typeof <%= apiInstanceName %>.<%~ functionName %>>,
82
79
  >(
83
80
  <%~ conditionalParamsText %>: QueryHookParams<typeof <%= apiInstanceName %>.<%~ functionName %>,
84
81
  <% if (isAxiosInstance) { %>
@@ -126,7 +123,10 @@
126
123
 
127
124
  */
128
125
  export const use<%~ classNameCase(functionName) %>InfiniteQuery = <
129
- TData = RequestFnReturn<typeof <%= apiInstanceName %>.<%~ functionName %>>,
126
+ TData = InfiniteData<
127
+ RequestFnReturn<typeof <%= apiInstanceName %>.<%~ functionName %>>,
128
+ Parameter<typeof <%= apiInstanceName %>.<%~ functionName %>>
129
+ >,
130
130
  >(
131
131
  <%~ conditionalParamsText %>: InfiniteQueryHookParams<typeof <%= apiInstanceName %>.<%~ functionName %>,
132
132
  <% if (isAxiosInstance) { %>
@@ -160,10 +160,7 @@ export const use<%~ classNameCase(functionName) %>InfiniteQuery = <
160
160
  <%~ getNextPageParam() %>
161
161
  <% } else { %>
162
162
  (lastPage) => {
163
- const params = lastPage.next
164
- ? new URL(lastPage.next).searchParams
165
- : null;
166
- const <%~ pagination.nextKey %> = params ? params.get('<%~ pagination.nextKey %>') : null;
163
+ const <%~ pagination.nextKey %> = lastPage.cursor ?? null;
167
164
  return <%~ pagination.nextKey %>;
168
165
  }
169
166
  <% } %>,
@@ -1,8 +1,17 @@
1
1
  <%
2
2
  const { utils, route, config, modelTypes,dataFromApiTemplate } = it;
3
3
  const routes = route.routes;
4
- const { getConfigByRoute ,queryKeyName } = dataFromApiTemplate;
4
+ const { getConfigByRoute, queryKeyName } = dataFromApiTemplate;
5
5
  %>
6
+
7
+ /**
8
+ * query key 에 undfined 를 포함시키지 않기 위한 함수입니다.
9
+ */
10
+ const isDefined = (v: unknown) => typeof v !== "undefined"
11
+
12
+ /**
13
+ * query-keys
14
+ */
6
15
  export const <%= queryKeyName %> = {
7
16
  <% routes.forEach((route) => { %>
8
17
  <%
@@ -29,12 +38,13 @@ export const <%= queryKeyName %> = {
29
38
  argToTmpl,
30
39
  }
31
40
  } = getConfigByRoute(route);
32
- const conditionalParam = isQuery ? `(variables? : Parameter<typeof ${apiInstanceName}.${functionName}>)`: "()"
33
- const conditionalHookKeyVariant = isQuery ? "variables" : ""
41
+ const queryKey = `(variables? : Parameter<typeof ${apiInstanceName}.${functionName}>) => ["${key}", variables].filter(isDefined)`
42
+ const mutationKey = `() => ["${key}"]`
43
+ const infiniteQueryKey = `(variables? : Parameter<typeof ${apiInstanceName}.${functionName}>) => ["${key}_INFINITE", variables].filter(isDefined)`
34
44
  %>
35
- <%~ methodKey %>: <%~ conditionalParam %> => ["<%~ key %>", <%~ conditionalHookKeyVariant %>].filter(key => typeof key !== 'undefined'),
45
+ <%~ methodKey %>: <%~ isQuery ? queryKey : mutationKey %>,
36
46
  <% if(hasPagination) { %>
37
- <%~ methodKey %>_INFINITE : <%~ conditionalParam %> => ["<%~ key %>_INFINITE", <%~ conditionalHookKeyVariant %>].filter(key => typeof key !== 'undefined'),
47
+ <%~ methodKey %>_INFINITE : <%~ infiniteQueryKey %>,
38
48
  <% }%>
39
49
  <% }) %>
40
50
  };
@@ -10,6 +10,15 @@ import {
10
10
  UseQueryOptions,
11
11
  } from '@tanstack/react-query';
12
12
 
13
+
14
+ /**
15
+ * !DO NOT EDIT THIS FILE
16
+ *
17
+ * 스크립트가 실행될때, 파일을 항상 새로 쓰기 때문에 파일 수정시 작성내용이 제거 될 수 있습니다.
18
+ */
19
+
20
+
21
+
13
22
  export type QueryHookParams<
14
23
  T extends CustomRequestFn,
15
24
  Error = <%~ errorType %>,
@@ -1,3 +1,12 @@
1
+ /**
2
+ * !DO NOT EDIT THIS FILE
3
+ *
4
+ * 스크립트가 실행될때, 파일을 항상 새로 쓰기 때문에 파일 수정시 작성내용이 제거 될 수 있습니다.
5
+ */
6
+
7
+
8
+
9
+
1
10
  export type DeepOmitReadOnly<T extends object | undefined> = Omit<
2
11
  {
3
12
  [P in keyof T]: NonNullable<T[P]> extends Array<infer I>