itube-specs 0.0.288 → 0.0.290

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,22 +1,28 @@
1
- import { EAsyncData } from '../runtime';
2
-
3
1
  export function useApiFetcher<T>(
4
- key: EAsyncData,
5
- apiMethod: (...args: any[]) => Promise<T>,
6
- ...apiArgs: any[]
2
+ key: string | null,
3
+ apiMethod: (...args: any[]) => Promise<T>,
4
+ ...apiArgs: any[]
7
5
  ) {
8
- return async function fetchData() {
9
- const state = useState<T | null>(`data-${String(key)}`, () => null);
10
- try {
11
- const result = await apiMethod(...apiArgs);
12
- state.value = result;
13
- return result;
14
- } catch (err) {
15
- throw createError({
16
- statusCode: 500,
17
- statusMessage: `Error fetching ${key}`,
18
- message: err instanceof Error ? err.message : 'An unknown error occurred',
19
- });
20
- }
21
- };
6
+ return async function fetchData() {
7
+ const hasKey = key?.trim();
8
+ const state = hasKey ? useState<T | null>(key!, () => null) : null;
9
+
10
+ if (state && state.value !== null && state.value !== undefined) {
11
+ return state.value;
12
+ }
13
+
14
+ try {
15
+ const result = await apiMethod(...apiArgs);
16
+
17
+ if (state) state.value = result;
18
+
19
+ return result;
20
+ } catch (err) {
21
+ throw createError({
22
+ statusCode: 500,
23
+ statusMessage: `Error fetching ${key}`,
24
+ message: err instanceof Error ? err.message : 'An unknown error occurred',
25
+ });
26
+ }
27
+ };
22
28
  }
@@ -14,7 +14,7 @@ export const useFetchVideosByCategories = async (
14
14
  const { data, status, error } = await useAsyncData<PaginatedResponse<IVideoCard>>(
15
15
  EAsyncData.VideosByCategory,
16
16
  () => useApiFetcher<PaginatedResponse<IVideoCard>>(
17
- EAsyncData.VideosByCategory,
17
+ null,
18
18
  apiMethod,
19
19
  lang,
20
20
  {
@@ -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 lang = useI18n().locale.value;
11
- const PER_PAGE = 48;
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)}-${lang}`);
12
14
 
13
- const filters = computed(() =>
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
- useApiFetcher(
36
- EAsyncData.Videos,
37
- apiMethod,
38
- lang,
39
- {
40
- page: Number(route.query.page) || 1,
41
- 'per-page': PER_PAGE,
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
- filters.value,
44
- getSelectedQuery(route, sortItemsDefault) ||
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
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "itube-specs",
3
3
  "type": "module",
4
- "version": "0.0.288",
4
+ "version": "0.0.290",
5
5
  "main": "./nuxt.config.ts",
6
6
  "types": "./types/index.d.ts",
7
7
  "scripts": {