@justins-home/api-services 1.1.1 → 1.1.2

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,6 +1,6 @@
1
1
   WARN  Issue while reading "/home/runner/work/justins-home-platform-ui/justins-home-platform-ui/.npmrc". Failed to replace env in config: ${NODE_AUTH_TOKEN}
2
2
 
3
- > @justins-home/api-services@1.1.1 build /home/runner/work/justins-home-platform-ui/justins-home-platform-ui/packages/api-services
3
+ > @justins-home/api-services@1.1.2 build /home/runner/work/justins-home-platform-ui/justins-home-platform-ui/packages/api-services
4
4
  > tsup src/index.ts --format esm,cjs
5
5
 
6
6
  CLI Building entry: src/index.ts
@@ -8,7 +8,7 @@
8
8
  CLI Target: node16
9
9
  ESM Build start
10
10
  CJS Build start
11
- ESM dist/index.mjs 24.05 KB
12
- ESM ⚡️ Build success in 70ms
13
- CJS dist/index.js 25.62 KB
14
- CJS ⚡️ Build success in 72ms
11
+ CJS dist/index.js 25.90 KB
12
+ CJS ⚡️ Build success in 45ms
13
+ ESM dist/index.mjs 24.30 KB
14
+ ESM ⚡️ Build success in 45ms
package/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # @justins-home/api-services
2
2
 
3
+ ## 1.1.2
4
+
5
+ ### Patch Changes
6
+
7
+ - added ApiError to normalizeError
8
+ - Updated dependencies
9
+ - @justins-home/http-client@1.0.1
10
+
3
11
  ## 1.1.1
4
12
 
5
13
  ### Patch Changes
package/dist/index.js CHANGED
@@ -19,6 +19,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
19
19
  // src/index.ts
20
20
  var index_exports = {};
21
21
  __export(index_exports, {
22
+ ApiError: () => ApiError,
22
23
  admin: () => admin,
23
24
  agent: () => agent,
24
25
  api: () => api,
@@ -45,23 +46,34 @@ module.exports = __toCommonJS(index_exports);
45
46
 
46
47
  // src/api-client.ts
47
48
  var import_http_client = require("@justins-home/http-client");
48
- function normalizeError(error) {
49
+ var ApiError = class extends Error {
50
+ status;
51
+ data;
52
+ operation;
53
+ constructor(message, status, data, operation) {
54
+ super(message);
55
+ this.name = "ApiError";
56
+ this.status = status;
57
+ this.data = data;
58
+ this.operation = operation;
59
+ }
60
+ };
61
+ function normalizeError(error, operation) {
49
62
  const response = error == null ? void 0 : error.response;
50
63
  if (!response) {
51
- throw new Error((error == null ? void 0 : error.message) || "Network error");
64
+ throw new ApiError("Network error", void 0, void 0, operation);
52
65
  }
53
- const data = response.data;
66
+ const { status, data } = response;
67
+ let message = "Request failed";
54
68
  if (data == null ? void 0 : data.errors) {
55
69
  const firstError = Object.values(data.errors)[0];
56
70
  if (Array.isArray(firstError)) {
57
- throw new Error(firstError[0]);
71
+ message = firstError[0];
58
72
  }
59
- throw new Error("Validation error");
60
- }
61
- if (data == null ? void 0 : data.message) {
62
- throw new Error(data.message);
73
+ } else if (data == null ? void 0 : data.message) {
74
+ message = data.message;
63
75
  }
64
- throw new Error(`Request failed with status ${response.status}`);
76
+ throw new ApiError(message, status, data, operation);
65
77
  }
66
78
  var api = {
67
79
  async post(operation, url, payload) {
@@ -69,7 +81,7 @@ var api = {
69
81
  const res = await import_http_client.authClient.post(url, payload ?? {});
70
82
  return res.data;
71
83
  } catch (error) {
72
- normalizeError(error);
84
+ normalizeError(error, operation);
73
85
  }
74
86
  },
75
87
  async get(operation, url, query) {
@@ -79,7 +91,7 @@ var api = {
79
91
  });
80
92
  return res.data;
81
93
  } catch (error) {
82
- normalizeError(error);
94
+ normalizeError(error, operation);
83
95
  }
84
96
  }
85
97
  };
@@ -621,6 +633,7 @@ var wishlist = {
621
633
  };
622
634
  // Annotate the CommonJS export names for ESM import in node:
623
635
  0 && (module.exports = {
636
+ ApiError,
624
637
  admin,
625
638
  agent,
626
639
  api,
package/dist/index.mjs CHANGED
@@ -1,22 +1,33 @@
1
1
  // src/api-client.ts
2
2
  import { authClient } from "@justins-home/http-client";
3
- function normalizeError(error) {
3
+ var ApiError = class extends Error {
4
+ status;
5
+ data;
6
+ operation;
7
+ constructor(message, status, data, operation) {
8
+ super(message);
9
+ this.name = "ApiError";
10
+ this.status = status;
11
+ this.data = data;
12
+ this.operation = operation;
13
+ }
14
+ };
15
+ function normalizeError(error, operation) {
4
16
  const response = error == null ? void 0 : error.response;
5
17
  if (!response) {
6
- throw new Error((error == null ? void 0 : error.message) || "Network error");
18
+ throw new ApiError("Network error", void 0, void 0, operation);
7
19
  }
8
- const data = response.data;
20
+ const { status, data } = response;
21
+ let message = "Request failed";
9
22
  if (data == null ? void 0 : data.errors) {
10
23
  const firstError = Object.values(data.errors)[0];
11
24
  if (Array.isArray(firstError)) {
12
- throw new Error(firstError[0]);
25
+ message = firstError[0];
13
26
  }
14
- throw new Error("Validation error");
15
- }
16
- if (data == null ? void 0 : data.message) {
17
- throw new Error(data.message);
27
+ } else if (data == null ? void 0 : data.message) {
28
+ message = data.message;
18
29
  }
19
- throw new Error(`Request failed with status ${response.status}`);
30
+ throw new ApiError(message, status, data, operation);
20
31
  }
21
32
  var api = {
22
33
  async post(operation, url, payload) {
@@ -24,7 +35,7 @@ var api = {
24
35
  const res = await authClient.post(url, payload ?? {});
25
36
  return res.data;
26
37
  } catch (error) {
27
- normalizeError(error);
38
+ normalizeError(error, operation);
28
39
  }
29
40
  },
30
41
  async get(operation, url, query) {
@@ -34,7 +45,7 @@ var api = {
34
45
  });
35
46
  return res.data;
36
47
  } catch (error) {
37
- normalizeError(error);
48
+ normalizeError(error, operation);
38
49
  }
39
50
  }
40
51
  };
@@ -575,6 +586,7 @@ var wishlist = {
575
586
  }
576
587
  };
577
588
  export {
589
+ ApiError,
578
590
  admin,
579
591
  agent,
580
592
  api,
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "@justins-home/api-services",
3
- "version": "1.1.1",
3
+ "version": "1.1.2",
4
4
  "private": false,
5
5
  "main": "src/index.ts",
6
6
  "dependencies": {
7
- "@justins-home/types": "1.0.0",
8
- "@justins-home/http-client": "1.0.0"
7
+ "@justins-home/http-client": "1.0.1",
8
+ "@justins-home/types": "1.0.0"
9
9
  },
10
10
  "publishConfig": {
11
11
  "access": "public"
package/src/api-client.ts CHANGED
@@ -1,6 +1,20 @@
1
1
  import { authClient } from '@justins-home/http-client';
2
2
  import type { operations } from '@justins-home/types/generated/api-types';
3
3
 
4
+ export class ApiError<T = unknown> extends Error {
5
+ status?: number;
6
+ data?: T;
7
+ operation?: string;
8
+
9
+ constructor(message: string, status?: number, data?: T, operation?: string) {
10
+ super(message);
11
+ this.name = 'ApiError';
12
+ this.status = status;
13
+ this.data = data;
14
+ this.operation = operation;
15
+ }
16
+ }
17
+
4
18
  type OperationId = keyof operations;
5
19
 
6
20
  type ApiRequest<T extends OperationId> = operations[T] extends {
@@ -19,35 +33,47 @@ type ApiResponse<T extends OperationId> = operations[T] extends {
19
33
  ? R
20
34
  : unknown;
21
35
 
36
+ type ApiErrorResponse<T extends OperationId> = operations[T] extends {
37
+ responses: infer R;
38
+ }
39
+ ? {
40
+ [K in keyof R]: K extends 200
41
+ ? never
42
+ : R[K] extends {
43
+ content: { 'application/json': infer E };
44
+ }
45
+ ? E
46
+ : never;
47
+ }[keyof R]
48
+ : unknown;
49
+
22
50
  /**
23
51
  * Normalizes Laravel API errors
24
52
  */
25
- function normalizeError(error: any): never {
53
+ function normalizeError<T extends OperationId>(
54
+ error: any,
55
+ operation: T,
56
+ ): never {
26
57
  const response = error?.response;
27
58
 
28
59
  if (!response) {
29
- throw new Error(error?.message || 'Network error');
60
+ throw new ApiError('Network error', undefined, undefined, operation);
30
61
  }
31
62
 
32
- const data = response.data;
63
+ const { status, data } = response;
64
+
65
+ let message = 'Request failed';
33
66
 
34
- // Laravel validation errors
35
67
  if (data?.errors) {
36
68
  const firstError = Object.values(data.errors)[0];
37
-
38
69
  if (Array.isArray(firstError)) {
39
- throw new Error(firstError[0]);
70
+ message = firstError[0];
40
71
  }
41
-
42
- throw new Error('Validation error');
43
- }
44
-
45
- // Standard Laravel API error
46
- if (data?.message) {
47
- throw new Error(data.message);
72
+ } else if (data?.message) {
73
+ message = data.message;
48
74
  }
49
75
 
50
- throw new Error(`Request failed with status ${response.status}`);
76
+ throw new ApiError<ApiErrorResponse<T>>(message, status, data, operation);
51
77
  }
52
78
 
53
79
  export const api = {
@@ -60,7 +86,7 @@ export const api = {
60
86
  const res = await authClient.post(url, payload ?? {});
61
87
  return res.data;
62
88
  } catch (error) {
63
- normalizeError(error);
89
+ normalizeError(error, operation);
64
90
  }
65
91
  },
66
92
 
@@ -76,7 +102,7 @@ export const api = {
76
102
 
77
103
  return res.data;
78
104
  } catch (error) {
79
- normalizeError(error);
105
+ normalizeError(error, operation);
80
106
  }
81
107
  },
82
108
  };
package/src/index.ts CHANGED
@@ -1,4 +1,3 @@
1
-
2
1
  /**
3
2
  * Core API utilities
4
3
  */