itube-specs 0.0.478 → 0.0.479

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/package.json CHANGED
@@ -1,12 +1,13 @@
1
1
  {
2
2
  "name": "itube-specs",
3
3
  "type": "module",
4
- "version": "0.0.478",
4
+ "version": "0.0.479",
5
5
  "main": "./nuxt.config.ts",
6
6
  "types": "./types/index.d.ts",
7
7
  "scripts": {
8
8
  "prepublish": "npm install && npx nuxi prepare",
9
- "patch": "npm version patch"
9
+ "patch": "npm version patch",
10
+ "eslint fix": "npx eslint . --ext .ts,.vue,.js --fix"
10
11
  },
11
12
  "exports": {
12
13
  ".": {
@@ -1,41 +1,31 @@
1
- import type { NitroFetchOptions } from "nitropack/types";
2
- import { AbortControllerUtil } from '../server/abort-controller';
1
+ import type { NitroFetchOptions } from 'nitropack/types'
3
2
 
4
- /** Хелпер для работы с API */
5
3
  export class ApiHelper {
6
- /** Метод для получения данных */
7
- static async fetch<T>(
8
- path: string,
9
- fetchOptions: NitroFetchOptions<string>,
10
- runtimeConfig?: any,
11
- ): Promise<T> {
12
- // TODO поменяется апи ендпойнт, после переделать
13
- // const apiDomain = useRuntimeConfig()?.public?.apiDomain;
14
- const signal = AbortControllerUtil.getSignalAbortController();
4
+ static async fetch<T>(
5
+ path: string,
6
+ options: NitroFetchOptions<string> = {}
7
+ ): Promise<T> {
8
+ const config = useRuntimeConfig()
15
9
 
16
- const _fetchOptions = {
17
- ...fetchOptions,
18
- headers: {
19
- ...(fetchOptions.headers || {}),
20
- 'x-domain': runtimeConfig.public.xDomain,
21
- },
22
- body: fetchOptions.body !== undefined
23
- ? fetchOptions.body : undefined,
24
- };
10
+ try {
11
+ const response = await $fetch<{ data: T }>(`/bff${path}`, {
12
+ retry: 1,
13
+ headers: {
14
+ ...(options.headers || {}),
15
+ 'x-domain': config.public.xDomain,
16
+ },
17
+ ...options,
18
+ })
25
19
 
26
- try {
27
- const res = await $fetch(`/bff${path}`, { retry: 0, ..._fetchOptions, signal, });
28
-
29
- if (typeof res === 'string') {
30
- return JSON.parse(res).data;
31
- } else {
32
- return res as T;
33
- }
34
- } catch (error) {
35
- if (signal?.aborted) {
36
- throw AbortControllerUtil.createAbortController();
37
- }
38
- throw error;
39
- }
20
+ return response?.data as T
21
+ } catch (error: any) {
22
+ throw createError({
23
+ statusCode: error?.status || 500,
24
+ statusMessage:
25
+ error?.data?.message ||
26
+ error?.message ||
27
+ 'API request failed',
28
+ })
40
29
  }
30
+ }
41
31
  }
@@ -2,27 +2,23 @@ import type { H3Event } from "h3";
2
2
 
3
3
  /** Класс для работы с серверным API (бекенд) */
4
4
  export class ServerApiHelper {
5
- /* Класс для запроса данных на бекенде */
6
- public static async fetch<T>(url: string, event: H3Event, requestOptions: RequestInit): Promise<T> {
5
+ public static async fetch<T>(
6
+ url: string,
7
+ event: H3Event,
8
+ requestOptions: RequestInit = {}
9
+ ): Promise<T> {
7
10
  try {
8
- const response = await fetch(url, {
11
+ const response = await $fetch<{ data: T }>(url, {
9
12
  headers: getHeaders(event),
13
+ retry: 1,
10
14
  ...requestOptions,
11
15
  });
12
- return (await response.json())?.data as T;
13
- } catch (error) {
14
- throw new Error(`Cannot fetch: ${error.data}`);
16
+
17
+ return response?.data as T;
18
+ } catch (error: any) {
19
+ throw new Error(
20
+ `Cannot fetch: ${error?.data?.message || error?.message || 'Unknown error'}`
21
+ );
15
22
  }
16
23
  }
17
- // TODO проверить а надо ли, если ничего не сломается оставить только один метод
18
- public static async fetchNitro<T>(url: string, event: H3Event, requestOptions: RequestInit): Promise<T> {
19
- try {
20
- return JSON.parse(String(await $fetch(url, {
21
- headers: getHeaders(event),
22
- ...requestOptions,
23
- })));
24
- } catch (error) {
25
- throw new Error(`Cannot fetch: ${error.data}`);
26
- }
27
- };
28
24
  }