@justins-home/api-services 1.1.0 → 1.1.1

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.0 build /home/runner/work/justins-home-platform-ui/justins-home-platform-ui/packages/api-services
3
+ > @justins-home/api-services@1.1.1 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
- CJS dist/index.js 24.88 KB
12
- CJS ⚡️ Build success in 36ms
13
- ESM dist/index.mjs 23.31 KB
14
- ESM ⚡️ Build success in 36ms
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
package/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # @justins-home/api-services
2
2
 
3
+ ## 1.1.1
4
+
5
+ ### Patch Changes
6
+
7
+ - feat(api-client): normalize Laravel API errors for all consumers
8
+
3
9
  ## 1.1.0
4
10
 
5
11
  ### Minor Changes
package/dist/index.js CHANGED
@@ -45,16 +45,42 @@ module.exports = __toCommonJS(index_exports);
45
45
 
46
46
  // src/api-client.ts
47
47
  var import_http_client = require("@justins-home/http-client");
48
+ function normalizeError(error) {
49
+ const response = error == null ? void 0 : error.response;
50
+ if (!response) {
51
+ throw new Error((error == null ? void 0 : error.message) || "Network error");
52
+ }
53
+ const data = response.data;
54
+ if (data == null ? void 0 : data.errors) {
55
+ const firstError = Object.values(data.errors)[0];
56
+ if (Array.isArray(firstError)) {
57
+ throw new Error(firstError[0]);
58
+ }
59
+ throw new Error("Validation error");
60
+ }
61
+ if (data == null ? void 0 : data.message) {
62
+ throw new Error(data.message);
63
+ }
64
+ throw new Error(`Request failed with status ${response.status}`);
65
+ }
48
66
  var api = {
49
67
  async post(operation, url, payload) {
50
- const res = await import_http_client.authClient.post(url, payload ?? {});
51
- return res.data;
68
+ try {
69
+ const res = await import_http_client.authClient.post(url, payload ?? {});
70
+ return res.data;
71
+ } catch (error) {
72
+ normalizeError(error);
73
+ }
52
74
  },
53
75
  async get(operation, url, query) {
54
- const res = await import_http_client.authClient.get(url, {
55
- params: query
56
- });
57
- return res.data;
76
+ try {
77
+ const res = await import_http_client.authClient.get(url, {
78
+ params: query
79
+ });
80
+ return res.data;
81
+ } catch (error) {
82
+ normalizeError(error);
83
+ }
58
84
  }
59
85
  };
60
86
 
package/dist/index.mjs CHANGED
@@ -1,15 +1,41 @@
1
1
  // src/api-client.ts
2
2
  import { authClient } from "@justins-home/http-client";
3
+ function normalizeError(error) {
4
+ const response = error == null ? void 0 : error.response;
5
+ if (!response) {
6
+ throw new Error((error == null ? void 0 : error.message) || "Network error");
7
+ }
8
+ const data = response.data;
9
+ if (data == null ? void 0 : data.errors) {
10
+ const firstError = Object.values(data.errors)[0];
11
+ if (Array.isArray(firstError)) {
12
+ throw new Error(firstError[0]);
13
+ }
14
+ throw new Error("Validation error");
15
+ }
16
+ if (data == null ? void 0 : data.message) {
17
+ throw new Error(data.message);
18
+ }
19
+ throw new Error(`Request failed with status ${response.status}`);
20
+ }
3
21
  var api = {
4
22
  async post(operation, url, payload) {
5
- const res = await authClient.post(url, payload ?? {});
6
- return res.data;
23
+ try {
24
+ const res = await authClient.post(url, payload ?? {});
25
+ return res.data;
26
+ } catch (error) {
27
+ normalizeError(error);
28
+ }
7
29
  },
8
30
  async get(operation, url, query) {
9
- const res = await authClient.get(url, {
10
- params: query
11
- });
12
- return res.data;
31
+ try {
32
+ const res = await authClient.get(url, {
33
+ params: query
34
+ });
35
+ return res.data;
36
+ } catch (error) {
37
+ normalizeError(error);
38
+ }
13
39
  }
14
40
  };
15
41
 
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "@justins-home/api-services",
3
- "version": "1.1.0",
3
+ "version": "1.1.1",
4
4
  "private": false,
5
5
  "main": "src/index.ts",
6
6
  "dependencies": {
7
- "@justins-home/http-client": "1.0.0",
8
- "@justins-home/types": "1.0.0"
7
+ "@justins-home/types": "1.0.0",
8
+ "@justins-home/http-client": "1.0.0"
9
9
  },
10
10
  "publishConfig": {
11
11
  "access": "public"
package/src/api-client.ts CHANGED
@@ -19,14 +19,49 @@ type ApiResponse<T extends OperationId> = operations[T] extends {
19
19
  ? R
20
20
  : unknown;
21
21
 
22
+ /**
23
+ * Normalizes Laravel API errors
24
+ */
25
+ function normalizeError(error: any): never {
26
+ const response = error?.response;
27
+
28
+ if (!response) {
29
+ throw new Error(error?.message || 'Network error');
30
+ }
31
+
32
+ const data = response.data;
33
+
34
+ // Laravel validation errors
35
+ if (data?.errors) {
36
+ const firstError = Object.values(data.errors)[0];
37
+
38
+ if (Array.isArray(firstError)) {
39
+ throw new Error(firstError[0]);
40
+ }
41
+
42
+ throw new Error('Validation error');
43
+ }
44
+
45
+ // Standard Laravel API error
46
+ if (data?.message) {
47
+ throw new Error(data.message);
48
+ }
49
+
50
+ throw new Error(`Request failed with status ${response.status}`);
51
+ }
52
+
22
53
  export const api = {
23
54
  async post<T extends OperationId>(
24
55
  operation: T,
25
56
  url: string,
26
57
  payload?: ApiRequest<T>,
27
58
  ): Promise<ApiResponse<T>> {
28
- const res = await authClient.post(url, payload ?? {});
29
- return res.data;
59
+ try {
60
+ const res = await authClient.post(url, payload ?? {});
61
+ return res.data;
62
+ } catch (error) {
63
+ normalizeError(error);
64
+ }
30
65
  },
31
66
 
32
67
  async get<T extends OperationId>(
@@ -34,10 +69,14 @@ export const api = {
34
69
  url: string,
35
70
  query?: ApiRequest<T>,
36
71
  ): Promise<ApiResponse<T>> {
37
- const res = await authClient.get(url, {
38
- params: query,
39
- });
72
+ try {
73
+ const res = await authClient.get(url, {
74
+ params: query,
75
+ });
40
76
 
41
- return res.data;
77
+ return res.data;
78
+ } catch (error) {
79
+ normalizeError(error);
80
+ }
42
81
  },
43
82
  };
@@ -1,11 +0,0 @@
1
- import { publicClient, request } from '@justins-home/http-client';
2
-
3
- export const authService = {
4
- async login(data: { email: string; password: string }) {
5
- return request(publicClient, publicClient.post('/auth/login', data));
6
- },
7
-
8
- async logout() {
9
- return request(publicClient, publicClient.post('/auth/logout'));
10
- },
11
- };
@@ -1,8 +0,0 @@
1
- import { authClient, request } from '@justins-home/http-client';
2
- import { InspectionResource } from '@justins-home/types';
3
-
4
- export const inspectionService = {
5
- async getInspections(): Promise<InspectionResource[]> {
6
- return request(authClient, authClient.get('/inspections'));
7
- },
8
- };
@@ -1,12 +0,0 @@
1
- import { authClient, request } from '@justins-home/http-client';
2
- import { ListingResource } from '@justins-home/types';
3
-
4
- export const listingService = {
5
- async getListings(): Promise<ListingResource[]> {
6
- return request(authClient, authClient.get('/listings'));
7
- },
8
-
9
- async getListing(uid: string): Promise<ListingResource> {
10
- return request(authClient, authClient.get(`/listings/${uid}`));
11
- },
12
- };
@@ -1,8 +0,0 @@
1
- import { authClient, request } from '@justins-home/http-client';
2
- import { MaintenanceRequestResource } from '@justins-home/types';
3
-
4
- export const maintenanceService = {
5
- async getRequests(): Promise<MaintenanceRequestResource[]> {
6
- return request(authClient, authClient.get('/maintenance'));
7
- },
8
- };
@@ -1,8 +0,0 @@
1
- import { authClient, request } from '@justins-home/http-client';
2
- import { NotificationResource } from '@justins-home/types';
3
-
4
- export const notificationService = {
5
- async getNotifications(): Promise<NotificationResource[]> {
6
- return request(authClient, authClient.get('/notifications'));
7
- },
8
- };
@@ -1,8 +0,0 @@
1
- import { authClient, request } from '@justins-home/http-client';
2
- import { TenancyResource } from '@justins-home/types';
3
-
4
- export const tenancyService = {
5
- async getTenancies(): Promise<TenancyResource[]> {
6
- return request(authClient, authClient.get('/tenancies'));
7
- },
8
- };