@wisemen/vue-core-api-utils 0.0.1-beta.3 → 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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Wisemen
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -1,6 +1,6 @@
1
- import { Result } from "neverthrow";
2
1
  import * as vue0 from "vue";
3
2
  import { ComputedRef, MaybeRef, Ref, UnwrapRef } from "vue";
3
+ import { Result } from "neverthrow";
4
4
 
5
5
  //#region src/types/queryKeys.type.d.ts
6
6
  interface QueryKeys {}
package/dist/index.mjs ADDED
@@ -0,0 +1,233 @@
1
+ import { useInfiniteQuery, useMutation as useMutation$1, useQuery as useQuery$1, useQueryClient } from "@tanstack/vue-query";
2
+ import { computed } from "vue";
3
+ import { err, ok } from "neverthrow";
4
+
5
+ //#region src/composables/mutation/mutation.composable.ts
6
+ function useMutation(options) {
7
+ const isDebug = options.isDebug ?? false;
8
+ const queryClient = useQueryClient();
9
+ async function onSuccess(responseData, params) {
10
+ await Promise.all(Object.entries(options.queryKeysToInvalidate).map(async ([queryKey, queryKeyParams]) => {
11
+ const qkp = queryKeyParams;
12
+ const paramsWithValues = Object.entries(qkp).reduce((acc, [key, value]) => {
13
+ acc[key] = value(params, responseData);
14
+ return acc;
15
+ }, {});
16
+ if (isDebug) console.log(`[MUTATION] Invalidating ${queryKey}`, paramsWithValues);
17
+ await queryClient.invalidateQueries({
18
+ exact: false,
19
+ queryKey: [queryKey, paramsWithValues]
20
+ });
21
+ }));
22
+ }
23
+ const mutation = useMutation$1({
24
+ mutationFn: options.queryFn,
25
+ onSuccess: async (data, variables) => {
26
+ if (variables !== void 0 && "params" in variables) {
27
+ await onSuccess(data, variables.params);
28
+ return;
29
+ }
30
+ await onSuccess(data, {});
31
+ }
32
+ });
33
+ async function execute(data) {
34
+ return await mutation.mutateAsync(data);
35
+ }
36
+ return {
37
+ isLoading: computed(() => mutation.isPending.value),
38
+ data: computed(() => mutation.data.value),
39
+ execute
40
+ };
41
+ }
42
+
43
+ //#endregion
44
+ //#region src/config/config.ts
45
+ const DEFAULT_LIMIT$2 = 20;
46
+ const DEFAULT_PREFETCH_STALE_TIME = 60;
47
+ const QUERY_CONFIG = {
48
+ prefetchStaleTime: DEFAULT_PREFETCH_STALE_TIME,
49
+ limit: DEFAULT_LIMIT$2
50
+ };
51
+ function setQueryConfig(config) {
52
+ if (config.limit != null && config.limit > 0) QUERY_CONFIG.limit = config.limit;
53
+ if (config.prefetchStaleTime != null && config.prefetchStaleTime > 0) QUERY_CONFIG.prefetchStaleTime = config.prefetchStaleTime;
54
+ }
55
+
56
+ //#endregion
57
+ //#region src/composables/query/keysetInfiniteQuery.composable.ts
58
+ const DEFAULT_LIMIT$1 = QUERY_CONFIG.limit;
59
+ function useKeysetInfiniteQuery(options) {
60
+ function getQueryKey() {
61
+ const [queryKey, params] = Object.entries(options.queryKey)[0];
62
+ return [queryKey, params];
63
+ }
64
+ const infiniteQuery = useInfiniteQuery({
65
+ staleTime: options.staleTime,
66
+ enabled: options.isEnabled,
67
+ getNextPageParam: (lastPage) => {
68
+ if (lastPage.isErr()) return null;
69
+ return lastPage.value.meta.next ?? null;
70
+ },
71
+ initialPageParam: void 0,
72
+ placeholderData: (data) => data,
73
+ queryFn: ({ pageParam }) => options.queryFn({
74
+ key: pageParam,
75
+ limit: options.limit ?? DEFAULT_LIMIT$1
76
+ }),
77
+ queryKey: getQueryKey()
78
+ });
79
+ const hasError = computed(() => {
80
+ return Boolean(infiniteQuery.data.value?.pages.find((page) => page.isErr()));
81
+ });
82
+ const result = computed(() => {
83
+ const firstError = infiniteQuery.data.value?.pages.find((page) => page.isErr());
84
+ if (firstError) return err(firstError.error);
85
+ const data = infiniteQuery.data.value?.pages.filter((page) => page.isOk()).flatMap((page) => page.value.data) ?? [];
86
+ const firstPage = infiniteQuery.data.value?.pages[0];
87
+ const meta = firstPage?.isOk() ? firstPage.value.meta : {
88
+ next: null,
89
+ total: data.length
90
+ };
91
+ return ok({
92
+ data,
93
+ meta: { next: infiniteQuery.hasNextPage.value ? meta.next : null }
94
+ });
95
+ });
96
+ function fetchNextPage() {
97
+ if (!infiniteQuery.hasNextPage.value || infiniteQuery.isFetchingNextPage.value) return;
98
+ return infiniteQuery.fetchNextPage();
99
+ }
100
+ return {
101
+ hasNextPage: computed(() => infiniteQuery.hasNextPage.value),
102
+ isError: computed(() => hasError.value),
103
+ isFetching: computed(() => infiniteQuery.isFetching.value),
104
+ isFetchingNextPage: computed(() => infiniteQuery.isFetchingNextPage.value),
105
+ isLoading: computed(() => infiniteQuery.isLoading.value),
106
+ isSuccess: computed(() => !hasError.value),
107
+ fetchNextPage: async () => {
108
+ await fetchNextPage();
109
+ },
110
+ refetch: async () => {
111
+ await infiniteQuery.refetch();
112
+ },
113
+ result
114
+ };
115
+ }
116
+
117
+ //#endregion
118
+ //#region src/composables/query/offsetInfiniteQuery.composable.ts
119
+ const DEFAULT_LIMIT = QUERY_CONFIG.limit;
120
+ function useOffsetInfiniteQuery(options) {
121
+ function getQueryKey() {
122
+ const [first] = Object.entries(options.queryKey);
123
+ if (!first) return [];
124
+ const [queryKey, params] = first;
125
+ return [queryKey, params];
126
+ }
127
+ const infiniteQuery = useInfiniteQuery({
128
+ staleTime: options.staleTime,
129
+ enabled: options.isEnabled,
130
+ getNextPageParam: (lastPage) => {
131
+ if (lastPage.isErr()) return null;
132
+ const total = lastPage.value.meta.offset + lastPage.value.meta.limit;
133
+ if (total >= lastPage.value.meta.total) return null;
134
+ return total;
135
+ },
136
+ initialPageParam: 0,
137
+ placeholderData: (data) => data,
138
+ queryFn: ({ pageParam }) => options.queryFn({
139
+ limit: options.limit ?? DEFAULT_LIMIT,
140
+ offset: pageParam ?? 0
141
+ }),
142
+ queryKey: getQueryKey()
143
+ });
144
+ const hasError = computed(() => {
145
+ return Boolean(infiniteQuery.data.value?.pages.find((page) => page.isErr()));
146
+ });
147
+ const result = computed(() => {
148
+ const firstError = infiniteQuery.data.value?.pages.find((page) => page.isErr());
149
+ if (firstError) return err(firstError.error);
150
+ const data = infiniteQuery.data.value?.pages.filter((page) => page.isOk()).flatMap((page) => page.value.data) ?? [];
151
+ const firstPage = infiniteQuery.data.value?.pages[0];
152
+ const meta = firstPage?.isOk() ? firstPage.value.meta : null;
153
+ return ok({
154
+ data,
155
+ meta: {
156
+ limit: meta?.limit ?? 0,
157
+ offset: meta?.offset ?? 0,
158
+ total: meta?.total ?? data.length
159
+ }
160
+ });
161
+ });
162
+ function fetchNextPage() {
163
+ if (!infiniteQuery.hasNextPage.value || infiniteQuery.isFetchingNextPage.value) return;
164
+ return infiniteQuery.fetchNextPage();
165
+ }
166
+ return {
167
+ hasNextPage: computed(() => infiniteQuery.hasNextPage.value),
168
+ isError: computed(() => hasError.value),
169
+ isFetching: computed(() => infiniteQuery.isFetching.value),
170
+ isFetchingNextPage: computed(() => infiniteQuery.isFetchingNextPage.value),
171
+ isLoading: computed(() => infiniteQuery.isLoading.value),
172
+ isSuccess: computed(() => !hasError.value),
173
+ fetchNextPage: async () => {
174
+ await fetchNextPage();
175
+ },
176
+ refetch: async () => {
177
+ await infiniteQuery.refetch();
178
+ },
179
+ result
180
+ };
181
+ }
182
+
183
+ //#endregion
184
+ //#region src/composables/query/prefetchQuery.composable.ts
185
+ function usePrefetchQuery(query) {
186
+ const queryClient = useQueryClient();
187
+ function getQueryKey() {
188
+ const [first] = Object.entries(query.queryKey);
189
+ if (!first) return [];
190
+ const [queryKey, params] = first;
191
+ return [queryKey, params];
192
+ }
193
+ async function execute() {
194
+ await queryClient.prefetchQuery({
195
+ staleTime: query.staleTime ?? QUERY_CONFIG.prefetchStaleTime,
196
+ queryFn: query.queryFn,
197
+ queryKey: getQueryKey()
198
+ });
199
+ }
200
+ return { execute };
201
+ }
202
+
203
+ //#endregion
204
+ //#region src/composables/query/query.composable.ts
205
+ function useQuery(options) {
206
+ const isDebug = options.isDebug ?? false;
207
+ const query = useQuery$1({
208
+ staleTime: options.staleTime,
209
+ enabled: options.isEnabled,
210
+ placeholderData: (data) => data,
211
+ queryFn: options.queryFn,
212
+ queryKey: getQueryKey()
213
+ });
214
+ function getQueryKey() {
215
+ const [queryKey, params] = Object.entries(options.queryKey)[0];
216
+ if (isDebug) console.debug(`Create query with key ${queryKey}`, params);
217
+ return [queryKey, params];
218
+ }
219
+ async function refetch() {
220
+ await query.refetch();
221
+ }
222
+ return {
223
+ isError: computed(() => query.data.value?.isErr() ?? false),
224
+ isFetching: computed(() => query.isFetching.value),
225
+ isLoading: computed(() => query.isLoading.value),
226
+ isSuccess: computed(() => query.data.value?.isOk() ?? false),
227
+ refetch,
228
+ result: computed(() => query.data.value ?? null)
229
+ };
230
+ }
231
+
232
+ //#endregion
233
+ export { setQueryConfig, useKeysetInfiniteQuery, useMutation, useOffsetInfiniteQuery, usePrefetchQuery, useQuery };
package/package.json CHANGED
@@ -4,37 +4,38 @@
4
4
  "access": "public"
5
5
  },
6
6
  "type": "module",
7
- "version": "0.0.1-beta.3",
7
+ "version": "0.0.1",
8
8
  "license": "MIT",
9
9
  "sideEffects": false,
10
10
  "exports": {
11
11
  ".": {
12
- "types": "./dist/index.d.ts",
13
- "import": "./dist/index.js"
12
+ "types": "./dist/index.d.mts",
13
+ "import": "./dist/index.mjs"
14
14
  }
15
15
  },
16
- "main": "./dist/index.umd.cjs",
17
- "module": "./dist/index.js",
18
- "types": "./dist/index.d.ts",
19
- "typings": "./dist/index.d.ts",
16
+ "main": "./dist/index.mjs",
17
+ "module": "./dist/index.mjs",
18
+ "types": "./dist/index.d.mts",
19
+ "typings": "./dist/index.d.mts",
20
20
  "files": [
21
21
  "./dist"
22
22
  ],
23
23
  "peerDependencies": {
24
- "@tanstack/vue-query": "5.90.2",
25
- "neverthrow": "^8.2.0"
24
+ "@tanstack/vue-query": ">=5.90.5",
25
+ "neverthrow": ">=8.2.0",
26
+ "vue": ">=3.5.22"
26
27
  },
27
28
  "devDependencies": {
28
- "@types/node": "24.5.2",
29
- "@wisemen/eslint-config-vue": "1.7.3",
30
- "eslint": "9.36.0",
31
- "tsdown": "0.15.4",
32
- "typescript": "5.9.2",
33
- "vitest": "3.2.4",
34
- "vue": "3.5.21"
29
+ "@types/node": "24.8.1",
30
+ "eslint": "9.39.2",
31
+ "tsdown": "0.18.4",
32
+ "typescript": "5.9.3",
33
+ "vitest": "4.0.17",
34
+ "@wisemen/eslint-config-vue": "2.0.0"
35
35
  },
36
36
  "scripts": {
37
37
  "build": "tsdown",
38
+ "dev": "tsdown --watch",
38
39
  "pub:release": "pnpm publish --access public",
39
40
  "pub:beta": "pnpm publish --no-git-checks --access public --tag beta",
40
41
  "pub:next": "pnpm publish --no-git-checks --access public --tag next",