@akinon/next 1.21.0-rc.7 → 1.21.0-rc.9

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/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # @akinon/next
2
2
 
3
+ ## 1.21.0-rc.9
4
+
5
+ ### Minor Changes
6
+
7
+ - 8d6caba: ZERO-2434: enhance error handling and logging in appFetch function
8
+
9
+ ## 1.21.0-rc.8
10
+
11
+ ### Minor Changes
12
+
13
+ - 07cc81a: Add infinite and more types to pagination
14
+
3
15
  ## 1.21.0-rc.7
4
16
 
5
17
  ### Minor Changes
File without changes
File without changes
File without changes
package/bin/pz-postdev.js CHANGED
File without changes
File without changes
File without changes
File without changes
package/bin/pz-predev.js CHANGED
File without changes
File without changes
@@ -20,6 +20,8 @@ interface GetStockParams {
20
20
  }
21
21
 
22
22
  interface GetFavoritesResponse {
23
+ next?: string | null;
24
+ previous?: string | null;
23
25
  count: number;
24
26
  results: FavoriteItem[];
25
27
  }
@@ -15,7 +15,7 @@ function getCategoryDataHandler(
15
15
  const params = generateCommerceSearchParams(searchParams);
16
16
 
17
17
  const rawData = await appFetch<string>(
18
- `${category.getCategoryByPk(pk)}${params ? params : ''}`,
18
+ `${category.getCategoryByPk(pk)}s${params ? params : ''}`,
19
19
  {
20
20
  headers: {
21
21
  Accept: 'application/json',
@@ -39,7 +39,7 @@ function getCategoryDataHandler(
39
39
  numberValueParser
40
40
  ) as GetCategoryResponse;
41
41
  } catch (error) {
42
- logger.error('Error while parsing category data', {
42
+ logger.fatal('Error while parsing category data', {
43
43
  handler: 'getCategoryDataHandler',
44
44
  error,
45
45
  rawData: rawData.startsWith('<!DOCTYPE html>')
@@ -108,7 +108,7 @@ function getCategoryBySlugDataHandler(slug: string) {
108
108
  numberValueParser
109
109
  ) as GetCategoryResponse;
110
110
  } catch (error) {
111
- logger.error('Error while parsing category data', {
111
+ logger.fatal('Error while parsing category data', {
112
112
  handler: 'getCategoryBySlugDataHandler',
113
113
  error,
114
114
  rawData: rawData.startsWith('<!DOCTYPE html>')
@@ -38,7 +38,7 @@ const getListDataHandler = (
38
38
  numberValueParser
39
39
  ) as GetCategoryResponse;
40
40
  } catch (error) {
41
- logger.error('Error while parsing list data', {
41
+ logger.fatal('Error while parsing list data', {
42
42
  error,
43
43
  rawData: rawData.startsWith('<!DOCTYPE html>')
44
44
  ? `${rawData.substring(0, 50)}...`
@@ -70,10 +70,6 @@ export default function usePagination(
70
70
  dispatch({ type: 'setLimit', payload: limit });
71
71
  }, [limit]);
72
72
 
73
- useEffect(() => {
74
- window.scrollTo(0, 0);
75
- }, [state.page, state.limit]);
76
-
77
73
  const setTotal = useCallback(
78
74
  (total: number) => {
79
75
  dispatch({ type: 'setTotal', payload: total });
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@akinon/next",
3
3
  "description": "Core package for Project Zero Next",
4
- "version": "1.21.0-rc.7",
4
+ "version": "1.21.0-rc.9",
5
5
  "private": false,
6
6
  "license": "MIT",
7
7
  "bin": {
@@ -32,7 +32,7 @@
32
32
  "@typescript-eslint/eslint-plugin": "6.7.4",
33
33
  "@typescript-eslint/parser": "6.7.4",
34
34
  "eslint": "^8.14.0",
35
- "@akinon/eslint-plugin-projectzero": "1.21.0-rc.7",
35
+ "@akinon/eslint-plugin-projectzero": "1.21.0-rc.9",
36
36
  "eslint-config-prettier": "8.5.0"
37
37
  }
38
38
  }
@@ -12,7 +12,7 @@ const appFetch = async <T>(
12
12
  url: RequestInfo,
13
13
  init: RequestInit = {},
14
14
  responseType = FetchResponseType.JSON
15
- ) => {
15
+ ): Promise<T> => {
16
16
  let response: T;
17
17
  let status: number;
18
18
  let ip = '';
@@ -28,7 +28,7 @@ const appFetch = async <T>(
28
28
 
29
29
  if (commerceUrl === 'default') {
30
30
  logger.error('Commerce URL is not set. Current value is "default"');
31
- return undefined;
31
+ throw new Error('Commerce URL is not set');
32
32
  }
33
33
 
34
34
  const requestURL = `${decodeURIComponent(commerceUrl)}${url}`;
@@ -48,19 +48,30 @@ const appFetch = async <T>(
48
48
  status = req.status;
49
49
  logger.debug(`FETCH END ${url}`, { status: req.status, ip });
50
50
 
51
- const rawData = await req.text();
51
+ if (!req.ok) {
52
+ throw new Error(`Request failed with status ${status}`);
53
+ }
52
54
 
53
55
  if (responseType === FetchResponseType.JSON) {
54
- response = JSON.parse(rawData);
56
+ response = (await req.json()) as T;
55
57
  } else {
56
- response = rawData as unknown as T;
58
+ response = (await req.text()) as unknown as T;
57
59
  }
58
60
 
59
61
  logger.trace(`FETCH RESPONSE`, { url, response, ip });
60
62
  } catch (error) {
63
+ const logType = status === 500 ? 'fatal' : 'error';
64
+
61
65
  if (!url.toString().includes('/cms/seo/')) {
62
- logger.error(`FETCH FAILED`, { url, status, error, ip });
66
+ logger[logType](`FETCH FAILED`, { url, status, error, ip });
63
67
  }
68
+
69
+ // throw the error if it's fatal, so it can be caught and handled at higher levels
70
+ if (logType === 'fatal') {
71
+ throw error;
72
+ }
73
+
74
+ return Promise.reject(error);
64
75
  }
65
76
 
66
77
  return response;