itube-specs 0.0.288 → 0.0.289
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.
|
@@ -1,12 +1,15 @@
|
|
|
1
|
-
import { EAsyncData } from '../runtime';
|
|
2
|
-
|
|
3
1
|
export function useApiFetcher<T>(
|
|
4
|
-
key:
|
|
2
|
+
key: string,
|
|
5
3
|
apiMethod: (...args: any[]) => Promise<T>,
|
|
6
4
|
...apiArgs: any[]
|
|
7
5
|
) {
|
|
8
6
|
return async function fetchData() {
|
|
9
|
-
const state = useState<T | null>(
|
|
7
|
+
const state = useState<T | null>(key, () => null);
|
|
8
|
+
|
|
9
|
+
if (state.value !== null) {
|
|
10
|
+
return state.value;
|
|
11
|
+
}
|
|
12
|
+
|
|
10
13
|
try {
|
|
11
14
|
const result = await apiMethod(...apiArgs);
|
|
12
15
|
state.value = result;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { selectDurationItems, sortItemsDefault, selectAddedItems } from '../lib';
|
|
2
|
-
import { EAsyncData, getSelectedQuery } from '../runtime';
|
|
2
|
+
import { EAsyncData, getSelectedQuery, ELanguage } from '../runtime';
|
|
3
3
|
import { useRoute } from 'vue-router';
|
|
4
4
|
import type { IVideoCard, PaginatedResponse } from '../types';
|
|
5
5
|
|
|
@@ -7,49 +7,35 @@ export const useFetchVideos = async (
|
|
|
7
7
|
apiMethod: (...args: any[]) => Promise<PaginatedResponse<IVideoCard>>
|
|
8
8
|
) => {
|
|
9
9
|
const route = useRoute();
|
|
10
|
-
const
|
|
11
|
-
const
|
|
10
|
+
const perPage = 48;
|
|
11
|
+
const lang = useI18n().locale.value as ELanguage;
|
|
12
|
+
const filters = computed(() => useGetVideosFilterRequest(route, selectDurationItems, selectAddedItems));
|
|
13
|
+
const stateKey = computed(() => `data-${EAsyncData.Videos}-${JSON.stringify(route.query)}`);
|
|
12
14
|
|
|
13
|
-
const
|
|
14
|
-
useGetVideosFilterRequest(route, selectDurationItems, selectAddedItems)
|
|
15
|
-
);
|
|
16
|
-
|
|
17
|
-
console.log('useFetchVideos called', route.query);
|
|
18
|
-
const stateKey = `data-${EAsyncData.Videos}-${JSON.stringify(route.query)}`;
|
|
19
|
-
|
|
20
|
-
// глобальный кеш внутри Nuxt SSR/state
|
|
21
|
-
const cached = useState<PaginatedResponse<IVideoCard> | null>(stateKey, () => null);
|
|
22
|
-
|
|
23
|
-
// если значение уже закешировано → не вызываем API
|
|
24
|
-
if (cached.value) {
|
|
25
|
-
return {
|
|
26
|
-
data: cached,
|
|
27
|
-
status: ref('success'),
|
|
28
|
-
error: ref(null)
|
|
29
|
-
};
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
const { data, status, error } = await useAsyncData(
|
|
15
|
+
const { data, status, error } = await useAsyncData<PaginatedResponse<IVideoCard>>(
|
|
33
16
|
EAsyncData.Videos,
|
|
34
|
-
() =>
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
17
|
+
() => useApiFetcher<PaginatedResponse<IVideoCard>>(
|
|
18
|
+
stateKey.value,
|
|
19
|
+
apiMethod,
|
|
20
|
+
lang,
|
|
21
|
+
{
|
|
22
|
+
page: Number(route.query[ 'page' ]) || 1,
|
|
23
|
+
[ 'per-page' ]: perPage,
|
|
24
|
+
},
|
|
25
|
+
filters.value,
|
|
26
|
+
getSelectedQuery(route, sortItemsDefault) || '-popularity,-primary_thumb_rank'
|
|
27
|
+
)(),
|
|
28
|
+
{
|
|
29
|
+
watch: [
|
|
30
|
+
() => {
|
|
31
|
+
if (route.query) {
|
|
32
|
+
const currentQuery = route.query;
|
|
33
|
+
return JSON.stringify(currentQuery);
|
|
34
|
+
}
|
|
42
35
|
},
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
'-popularity,-primary_thumb_rank'
|
|
46
|
-
)()
|
|
36
|
+
],
|
|
37
|
+
}
|
|
47
38
|
);
|
|
48
39
|
|
|
49
|
-
|
|
50
|
-
if (data.value) {
|
|
51
|
-
cached.value = data.value;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
return { data: cached, status, error };
|
|
40
|
+
return { data, status, error };
|
|
55
41
|
};
|