@anweb/nuxt-ancore 1.6.0 → 1.7.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/dist/module.json
CHANGED
|
@@ -1,8 +1,15 @@
|
|
|
1
|
-
import type { NitroFetchRequest } from 'nitropack';
|
|
1
|
+
import type { NitroFetchOptions, NitroFetchRequest } from 'nitropack';
|
|
2
2
|
import { type Ref, type ComputedRef } from 'vue';
|
|
3
3
|
import { useAsyncData } from '#app';
|
|
4
|
-
|
|
4
|
+
import type { UseEventBusReturn } from '@vueuse/core';
|
|
5
|
+
import type { KeysOf, PickFrom } from '#app/composables/asyncData';
|
|
6
|
+
interface TConfig<TData> {
|
|
5
7
|
request: NitroFetchRequest;
|
|
8
|
+
apiConfig?: NitroFetchOptions<string>;
|
|
9
|
+
events?: {
|
|
10
|
+
bus: UseEventBusReturn<any, any>;
|
|
11
|
+
callback: (info: TData) => PickFrom<TData, KeysOf<TData>> | undefined;
|
|
12
|
+
}[];
|
|
6
13
|
}
|
|
7
14
|
interface TUseAnData<TData, TError> {
|
|
8
15
|
init: () => Promise<void>;
|
|
@@ -12,5 +19,5 @@ interface TUseAnData<TData, TError> {
|
|
|
12
19
|
error: ComputedRef<TError>;
|
|
13
20
|
status: ReturnType<typeof useAsyncData>['status'];
|
|
14
21
|
}
|
|
15
|
-
export declare const useAnData: <TData = unknown, TError = unknown>(config: TConfig) => TUseAnData<TData, TError>;
|
|
22
|
+
export declare const useAnData: <TData = unknown, TError = unknown>(config: TConfig<TData>) => TUseAnData<TData, TError>;
|
|
16
23
|
export {};
|
|
@@ -16,9 +16,16 @@ export const useAnData = (config) => {
|
|
|
16
16
|
status
|
|
17
17
|
} = useAsyncData(
|
|
18
18
|
key,
|
|
19
|
-
() => userApi(request.value, { method: "GET" }),
|
|
19
|
+
() => userApi(request.value, { method: "GET", ...config.apiConfig || {} }),
|
|
20
20
|
{ immediate: false }
|
|
21
21
|
);
|
|
22
|
+
if (config.events) {
|
|
23
|
+
for (const event of config.events) {
|
|
24
|
+
event.bus.on((info) => {
|
|
25
|
+
data.value = event.callback(info);
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
}
|
|
22
29
|
return {
|
|
23
30
|
init: execute,
|
|
24
31
|
loading,
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import type { NitroFetchRequest } from 'nitropack';
|
|
1
|
+
import type { NitroFetchOptions, NitroFetchRequest } from 'nitropack';
|
|
2
2
|
import { type UseEventBusReturn } from '@vueuse/core';
|
|
3
|
-
import type {
|
|
3
|
+
import type { TWS as TWSDefault } from '#ancore/types';
|
|
4
4
|
interface TConfig<TData, TFilter, TWS extends TWSDefault> {
|
|
5
5
|
request: NitroFetchRequest;
|
|
6
6
|
filter?: TFilter;
|
|
@@ -13,7 +13,7 @@ interface TConfig<TData, TFilter, TWS extends TWSDefault> {
|
|
|
13
13
|
type: TWS['type'];
|
|
14
14
|
callback: (list: TData[], count: number, data: any) => number | void;
|
|
15
15
|
}[];
|
|
16
|
-
|
|
16
|
+
apiConfig?: NitroFetchOptions<string>;
|
|
17
17
|
}
|
|
18
18
|
declare const _default: <TData, TFilter = unknown, TWS extends TWSDefault = TWSDefault>(config: TConfig<TData, TFilter, TWS>) => {
|
|
19
19
|
init: (newFilter?: TFilter) => Promise<void>;
|
|
@@ -41,17 +41,16 @@ export default (config) => {
|
|
|
41
41
|
};
|
|
42
42
|
const refresh = () => {
|
|
43
43
|
if (!data.value) return;
|
|
44
|
-
const tempData = config.renderRaw ? config.renderRaw(data.value) : data.value;
|
|
45
44
|
if (!filter.value.skip) {
|
|
46
45
|
count.value = null;
|
|
47
46
|
items.length = 0;
|
|
48
47
|
}
|
|
49
48
|
if (config.reverse) {
|
|
50
|
-
items.unshift(...
|
|
49
|
+
items.unshift(...data.value.items);
|
|
51
50
|
} else {
|
|
52
|
-
items.push(...
|
|
51
|
+
items.push(...data.value.items);
|
|
53
52
|
}
|
|
54
|
-
count.value =
|
|
53
|
+
count.value = data.value.count;
|
|
55
54
|
};
|
|
56
55
|
const key = computed(() => {
|
|
57
56
|
return config.request + "?" + toQuery(filter.value);
|
|
@@ -66,7 +65,10 @@ export default (config) => {
|
|
|
66
65
|
status
|
|
67
66
|
} = useAsyncData(
|
|
68
67
|
key,
|
|
69
|
-
() => userApi(
|
|
68
|
+
() => userApi(
|
|
69
|
+
config.request,
|
|
70
|
+
{ method: "GET", query: filter.value, ...config.apiConfig || {} }
|
|
71
|
+
),
|
|
70
72
|
{ immediate: false }
|
|
71
73
|
);
|
|
72
74
|
watch(data, refresh);
|