@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.
- package/.turbo/turbo-build.log +5 -5
- package/CHANGELOG.md +8 -0
- package/dist/index.js +24 -11
- package/dist/index.mjs +23 -11
- package/package.json +3 -3
- package/src/api-client.ts +42 -16
- package/src/index.ts +0 -1
package/.turbo/turbo-build.log
CHANGED
|
@@ -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.
|
|
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
|
[34mCLI[39m Building entry: src/index.ts
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
[34mCLI[39m Target: node16
|
|
9
9
|
[34mESM[39m Build start
|
|
10
10
|
[34mCJS[39m Build start
|
|
11
|
-
[
|
|
12
|
-
[
|
|
13
|
-
[
|
|
14
|
-
[
|
|
11
|
+
[32mCJS[39m [1mdist/index.js [22m[32m25.90 KB[39m
|
|
12
|
+
[32mCJS[39m ⚡️ Build success in 45ms
|
|
13
|
+
[32mESM[39m [1mdist/index.mjs [22m[32m24.30 KB[39m
|
|
14
|
+
[32mESM[39m ⚡️ Build success in 45ms
|
package/CHANGELOG.md
CHANGED
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
|
-
|
|
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
|
|
64
|
+
throw new ApiError("Network error", void 0, void 0, operation);
|
|
52
65
|
}
|
|
53
|
-
const data = response
|
|
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
|
-
|
|
71
|
+
message = firstError[0];
|
|
58
72
|
}
|
|
59
|
-
|
|
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
|
|
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
|
-
|
|
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
|
|
18
|
+
throw new ApiError("Network error", void 0, void 0, operation);
|
|
7
19
|
}
|
|
8
|
-
const data = response
|
|
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
|
-
|
|
25
|
+
message = firstError[0];
|
|
13
26
|
}
|
|
14
|
-
|
|
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
|
|
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.
|
|
3
|
+
"version": "1.1.2",
|
|
4
4
|
"private": false,
|
|
5
5
|
"main": "src/index.ts",
|
|
6
6
|
"dependencies": {
|
|
7
|
-
"@justins-home/
|
|
8
|
-
"@justins-home/
|
|
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
|
|
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
|
|
60
|
+
throw new ApiError('Network error', undefined, undefined, operation);
|
|
30
61
|
}
|
|
31
62
|
|
|
32
|
-
const data = response
|
|
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
|
-
|
|
70
|
+
message = firstError[0];
|
|
40
71
|
}
|
|
41
|
-
|
|
42
|
-
|
|
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
|
|
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