@justins-home/api-services 1.1.0 → 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.0 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
- 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
+ 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,19 @@
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
+
11
+ ## 1.1.1
12
+
13
+ ### Patch Changes
14
+
15
+ - feat(api-client): normalize Laravel API errors for all consumers
16
+
3
17
  ## 1.1.0
4
18
 
5
19
  ### Minor 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,16 +46,53 @@ module.exports = __toCommonJS(index_exports);
45
46
 
46
47
  // src/api-client.ts
47
48
  var import_http_client = require("@justins-home/http-client");
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) {
62
+ const response = error == null ? void 0 : error.response;
63
+ if (!response) {
64
+ throw new ApiError("Network error", void 0, void 0, operation);
65
+ }
66
+ const { status, data } = response;
67
+ let message = "Request failed";
68
+ if (data == null ? void 0 : data.errors) {
69
+ const firstError = Object.values(data.errors)[0];
70
+ if (Array.isArray(firstError)) {
71
+ message = firstError[0];
72
+ }
73
+ } else if (data == null ? void 0 : data.message) {
74
+ message = data.message;
75
+ }
76
+ throw new ApiError(message, status, data, operation);
77
+ }
48
78
  var api = {
49
79
  async post(operation, url, payload) {
50
- const res = await import_http_client.authClient.post(url, payload ?? {});
51
- return res.data;
80
+ try {
81
+ const res = await import_http_client.authClient.post(url, payload ?? {});
82
+ return res.data;
83
+ } catch (error) {
84
+ normalizeError(error, operation);
85
+ }
52
86
  },
53
87
  async get(operation, url, query) {
54
- const res = await import_http_client.authClient.get(url, {
55
- params: query
56
- });
57
- return res.data;
88
+ try {
89
+ const res = await import_http_client.authClient.get(url, {
90
+ params: query
91
+ });
92
+ return res.data;
93
+ } catch (error) {
94
+ normalizeError(error, operation);
95
+ }
58
96
  }
59
97
  };
60
98
 
@@ -595,6 +633,7 @@ var wishlist = {
595
633
  };
596
634
  // Annotate the CommonJS export names for ESM import in node:
597
635
  0 && (module.exports = {
636
+ ApiError,
598
637
  admin,
599
638
  agent,
600
639
  api,
package/dist/index.mjs CHANGED
@@ -1,15 +1,52 @@
1
1
  // src/api-client.ts
2
2
  import { authClient } from "@justins-home/http-client";
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) {
16
+ const response = error == null ? void 0 : error.response;
17
+ if (!response) {
18
+ throw new ApiError("Network error", void 0, void 0, operation);
19
+ }
20
+ const { status, data } = response;
21
+ let message = "Request failed";
22
+ if (data == null ? void 0 : data.errors) {
23
+ const firstError = Object.values(data.errors)[0];
24
+ if (Array.isArray(firstError)) {
25
+ message = firstError[0];
26
+ }
27
+ } else if (data == null ? void 0 : data.message) {
28
+ message = data.message;
29
+ }
30
+ throw new ApiError(message, status, data, operation);
31
+ }
3
32
  var api = {
4
33
  async post(operation, url, payload) {
5
- const res = await authClient.post(url, payload ?? {});
6
- return res.data;
34
+ try {
35
+ const res = await authClient.post(url, payload ?? {});
36
+ return res.data;
37
+ } catch (error) {
38
+ normalizeError(error, operation);
39
+ }
7
40
  },
8
41
  async get(operation, url, query) {
9
- const res = await authClient.get(url, {
10
- params: query
11
- });
12
- return res.data;
42
+ try {
43
+ const res = await authClient.get(url, {
44
+ params: query
45
+ });
46
+ return res.data;
47
+ } catch (error) {
48
+ normalizeError(error, operation);
49
+ }
13
50
  }
14
51
  };
15
52
 
@@ -549,6 +586,7 @@ var wishlist = {
549
586
  }
550
587
  };
551
588
  export {
589
+ ApiError,
552
590
  admin,
553
591
  agent,
554
592
  api,
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@justins-home/api-services",
3
- "version": "1.1.0",
3
+ "version": "1.1.2",
4
4
  "private": false,
5
5
  "main": "src/index.ts",
6
6
  "dependencies": {
7
- "@justins-home/http-client": "1.0.0",
7
+ "@justins-home/http-client": "1.0.1",
8
8
  "@justins-home/types": "1.0.0"
9
9
  },
10
10
  "publishConfig": {
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,14 +33,61 @@ 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
+
50
+ /**
51
+ * Normalizes Laravel API errors
52
+ */
53
+ function normalizeError<T extends OperationId>(
54
+ error: any,
55
+ operation: T,
56
+ ): never {
57
+ const response = error?.response;
58
+
59
+ if (!response) {
60
+ throw new ApiError('Network error', undefined, undefined, operation);
61
+ }
62
+
63
+ const { status, data } = response;
64
+
65
+ let message = 'Request failed';
66
+
67
+ if (data?.errors) {
68
+ const firstError = Object.values(data.errors)[0];
69
+ if (Array.isArray(firstError)) {
70
+ message = firstError[0];
71
+ }
72
+ } else if (data?.message) {
73
+ message = data.message;
74
+ }
75
+
76
+ throw new ApiError<ApiErrorResponse<T>>(message, status, data, operation);
77
+ }
78
+
22
79
  export const api = {
23
80
  async post<T extends OperationId>(
24
81
  operation: T,
25
82
  url: string,
26
83
  payload?: ApiRequest<T>,
27
84
  ): Promise<ApiResponse<T>> {
28
- const res = await authClient.post(url, payload ?? {});
29
- return res.data;
85
+ try {
86
+ const res = await authClient.post(url, payload ?? {});
87
+ return res.data;
88
+ } catch (error) {
89
+ normalizeError(error, operation);
90
+ }
30
91
  },
31
92
 
32
93
  async get<T extends OperationId>(
@@ -34,10 +95,14 @@ export const api = {
34
95
  url: string,
35
96
  query?: ApiRequest<T>,
36
97
  ): Promise<ApiResponse<T>> {
37
- const res = await authClient.get(url, {
38
- params: query,
39
- });
98
+ try {
99
+ const res = await authClient.get(url, {
100
+ params: query,
101
+ });
40
102
 
41
- return res.data;
103
+ return res.data;
104
+ } catch (error) {
105
+ normalizeError(error, operation);
106
+ }
42
107
  },
43
108
  };
package/src/index.ts CHANGED
@@ -1,4 +1,3 @@
1
-
2
1
  /**
3
2
  * Core API utilities
4
3
  */
@@ -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
- };