@bzbs/react-api-client 0.0.20 → 0.0.21

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/dist/index.d.mts CHANGED
@@ -10,10 +10,23 @@ interface BzbsErrorResponse {
10
10
  };
11
11
  }
12
12
 
13
- type ServiceResponse<T> = {
14
- model: T | undefined;
15
- response: AxiosResponse | undefined;
16
- error: any | undefined | BzbsErrorResponse;
13
+ type ServiceResponse<T> = SuccessResponse<T> | ErrorResponse;
14
+ type SuccessResponse<T> = {
15
+ type: 'success';
16
+ model: T;
17
+ response: AxiosResponse;
18
+ };
19
+ type ErrorResponse = ClientError | ServerError;
20
+ type ClientError = {
21
+ type: 'client-error';
22
+ message: string;
23
+ details?: any;
24
+ };
25
+ type ServerError = {
26
+ type: 'server-error';
27
+ error: BzbsErrorResponse;
28
+ statusCode: number;
29
+ response: AxiosResponse;
17
30
  };
18
31
  type RequestOptions = {
19
32
  headers?: {
@@ -22,21 +35,22 @@ type RequestOptions = {
22
35
  params?: {
23
36
  [key: string]: string;
24
37
  };
25
- data?: {
26
- [key: string]: string;
27
- } | any;
38
+ data?: any;
28
39
  baseUrl?: string;
29
40
  };
30
41
  declare class BaseService {
31
- client: AxiosInstance;
32
- baseUrl: string;
42
+ private client;
43
+ private baseUrl;
33
44
  constructor(client: AxiosInstance, baseUrl: string);
45
+ private normalizeError;
46
+ private request;
34
47
  get<T>(path: string, params?: any, requestOptions?: RequestOptions): Promise<ServiceResponse<T>>;
35
48
  post<T>(path: string, data: any, requestOptions?: RequestOptions): Promise<ServiceResponse<T>>;
36
49
  put<T>(path: string, data: any, requestOptions?: RequestOptions): Promise<ServiceResponse<T>>;
37
50
  delete<T>(path: string, requestOptions?: RequestOptions): Promise<ServiceResponse<T>>;
38
51
  patch<T>(path: string, data: any, requestOptions?: RequestOptions): Promise<ServiceResponse<T>>;
39
- joinUrl(baseUrl: string, path: string): string;
52
+ private joinUrl;
53
+ private getHeaders;
40
54
  }
41
55
 
42
56
  interface LoginResponse {
package/dist/index.d.ts CHANGED
@@ -10,10 +10,23 @@ interface BzbsErrorResponse {
10
10
  };
11
11
  }
12
12
 
13
- type ServiceResponse<T> = {
14
- model: T | undefined;
15
- response: AxiosResponse | undefined;
16
- error: any | undefined | BzbsErrorResponse;
13
+ type ServiceResponse<T> = SuccessResponse<T> | ErrorResponse;
14
+ type SuccessResponse<T> = {
15
+ type: 'success';
16
+ model: T;
17
+ response: AxiosResponse;
18
+ };
19
+ type ErrorResponse = ClientError | ServerError;
20
+ type ClientError = {
21
+ type: 'client-error';
22
+ message: string;
23
+ details?: any;
24
+ };
25
+ type ServerError = {
26
+ type: 'server-error';
27
+ error: BzbsErrorResponse;
28
+ statusCode: number;
29
+ response: AxiosResponse;
17
30
  };
18
31
  type RequestOptions = {
19
32
  headers?: {
@@ -22,21 +35,22 @@ type RequestOptions = {
22
35
  params?: {
23
36
  [key: string]: string;
24
37
  };
25
- data?: {
26
- [key: string]: string;
27
- } | any;
38
+ data?: any;
28
39
  baseUrl?: string;
29
40
  };
30
41
  declare class BaseService {
31
- client: AxiosInstance;
32
- baseUrl: string;
42
+ private client;
43
+ private baseUrl;
33
44
  constructor(client: AxiosInstance, baseUrl: string);
45
+ private normalizeError;
46
+ private request;
34
47
  get<T>(path: string, params?: any, requestOptions?: RequestOptions): Promise<ServiceResponse<T>>;
35
48
  post<T>(path: string, data: any, requestOptions?: RequestOptions): Promise<ServiceResponse<T>>;
36
49
  put<T>(path: string, data: any, requestOptions?: RequestOptions): Promise<ServiceResponse<T>>;
37
50
  delete<T>(path: string, requestOptions?: RequestOptions): Promise<ServiceResponse<T>>;
38
51
  patch<T>(path: string, data: any, requestOptions?: RequestOptions): Promise<ServiceResponse<T>>;
39
- joinUrl(baseUrl: string, path: string): string;
52
+ private joinUrl;
53
+ private getHeaders;
40
54
  }
41
55
 
42
56
  interface LoginResponse {
package/dist/index.js CHANGED
@@ -1,6 +1,8 @@
1
1
  "use strict";
2
2
  var __defProp = Object.defineProperty;
3
+ var __defProps = Object.defineProperties;
3
4
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
4
6
  var __getOwnPropNames = Object.getOwnPropertyNames;
5
7
  var __getOwnPropSymbols = Object.getOwnPropertySymbols;
6
8
  var __hasOwnProp = Object.prototype.hasOwnProperty;
@@ -17,6 +19,7 @@ var __spreadValues = (a, b) => {
17
19
  }
18
20
  return a;
19
21
  };
22
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
20
23
  var __export = (target, all) => {
21
24
  for (var name in all)
22
25
  __defProp(target, name, { get: all[name], enumerable: true });
@@ -62,133 +65,77 @@ module.exports = __toCommonJS(src_exports);
62
65
  // src/api/base-service.ts
63
66
  var BaseService = class {
64
67
  constructor(client, baseUrl) {
68
+ if (!client)
69
+ throw new Error("Axios client is required.");
70
+ if (!baseUrl)
71
+ throw new Error("Base URL is required.");
65
72
  this.client = client;
66
73
  this.baseUrl = baseUrl;
67
- if (!this.baseUrl) {
68
- throw "Please check base_URL";
69
- }
70
- if (!this.client) {
71
- throw "Please check client";
74
+ }
75
+ normalizeError(error) {
76
+ if (error.response && error.response.data) {
77
+ return {
78
+ type: "server-error",
79
+ error: error.response.data,
80
+ statusCode: error.response.status,
81
+ response: error.response
82
+ };
72
83
  }
84
+ return {
85
+ type: "client-error",
86
+ message: error.message || "An unexpected error occurred.",
87
+ details: error
88
+ };
73
89
  }
74
- get(path, params, requestOptions) {
90
+ request(method, path, data, requestOptions) {
75
91
  return __async(this, null, function* () {
76
- var _a, _b;
92
+ var _a;
77
93
  try {
78
- const response = yield this.client.get(this.joinUrl((_a = requestOptions == null ? void 0 : requestOptions.baseUrl) != null ? _a : this.baseUrl, path), {
79
- params: __spreadValues(__spreadValues({}, params ? params : {}), (requestOptions == null ? void 0 : requestOptions.params) ? requestOptions.params : {}),
80
- headers: requestOptions == null ? void 0 : requestOptions.headers,
81
- data: requestOptions == null ? void 0 : requestOptions.data
82
- });
83
- return {
84
- model: response == null ? void 0 : response.data,
85
- response,
86
- error: void 0
94
+ const url = this.joinUrl((_a = requestOptions == null ? void 0 : requestOptions.baseUrl) != null ? _a : this.baseUrl, path);
95
+ const config = {
96
+ params: requestOptions == null ? void 0 : requestOptions.params,
97
+ headers: this.getHeaders(requestOptions == null ? void 0 : requestOptions.headers),
98
+ data
87
99
  };
100
+ const response = yield this.client.request(__spreadValues({ method, url }, config));
101
+ return { model: response.data, response, type: "success" };
88
102
  } catch (error) {
89
- return {
90
- model: void 0,
91
- response: void 0,
92
- error: error.response ? error.response.data : (_b = error.response) != null ? _b : error
93
- };
103
+ return this.normalizeError(error);
94
104
  }
95
105
  });
96
106
  }
107
+ get(path, params, requestOptions) {
108
+ return __async(this, null, function* () {
109
+ return this.request("get", path, void 0, __spreadProps(__spreadValues({}, requestOptions), { params }));
110
+ });
111
+ }
97
112
  post(path, data, requestOptions) {
98
113
  return __async(this, null, function* () {
99
- var _a, _b;
100
- try {
101
- const response = yield this.client.post(
102
- this.joinUrl((_a = requestOptions == null ? void 0 : requestOptions.baseUrl) != null ? _a : this.baseUrl, path),
103
- data,
104
- requestOptions
105
- );
106
- return {
107
- model: response == null ? void 0 : response.data,
108
- response,
109
- error: void 0
110
- };
111
- } catch (error) {
112
- return {
113
- model: void 0,
114
- response: void 0,
115
- error: error.response ? error.response.data : (_b = error.response) != null ? _b : error
116
- };
117
- }
114
+ return this.request("post", path, data, requestOptions);
118
115
  });
119
116
  }
120
117
  put(path, data, requestOptions) {
121
118
  return __async(this, null, function* () {
122
- var _a, _b;
123
- try {
124
- const response = yield this.client.put(
125
- this.joinUrl((_a = requestOptions == null ? void 0 : requestOptions.baseUrl) != null ? _a : this.baseUrl, path),
126
- data,
127
- requestOptions
128
- );
129
- return {
130
- model: response == null ? void 0 : response.data,
131
- response,
132
- error: void 0
133
- };
134
- } catch (error) {
135
- return {
136
- model: void 0,
137
- response: void 0,
138
- error: error.response ? error.response.data : (_b = error.response) != null ? _b : error
139
- };
140
- }
119
+ return this.request("put", path, data, requestOptions);
141
120
  });
142
121
  }
143
122
  delete(path, requestOptions) {
144
123
  return __async(this, null, function* () {
145
- var _a, _b;
146
- try {
147
- const response = yield this.client.delete(
148
- this.joinUrl((_a = requestOptions == null ? void 0 : requestOptions.baseUrl) != null ? _a : this.baseUrl, path),
149
- requestOptions
150
- );
151
- return {
152
- model: response == null ? void 0 : response.data,
153
- response,
154
- error: void 0
155
- };
156
- } catch (error) {
157
- return {
158
- model: void 0,
159
- response: void 0,
160
- error: error.response ? error.response.data : (_b = error.response) != null ? _b : error
161
- };
162
- }
124
+ return this.request("delete", path, void 0, requestOptions);
163
125
  });
164
126
  }
165
127
  patch(path, data, requestOptions) {
166
128
  return __async(this, null, function* () {
167
- var _a, _b;
168
- try {
169
- const response = yield this.client.patch(
170
- this.joinUrl((_a = requestOptions == null ? void 0 : requestOptions.baseUrl) != null ? _a : this.baseUrl, path),
171
- data,
172
- requestOptions
173
- );
174
- return {
175
- model: response == null ? void 0 : response.data,
176
- response,
177
- error: void 0
178
- };
179
- } catch (error) {
180
- return {
181
- model: void 0,
182
- response: void 0,
183
- error: error.response ? error.response.data : (_b = error.response) != null ? _b : error
184
- };
185
- }
129
+ return this.request("patch", path, data, requestOptions);
186
130
  });
187
131
  }
188
132
  joinUrl(baseUrl, path) {
189
- baseUrl = baseUrl.endsWith("/") ? baseUrl : baseUrl + "/";
190
- const url = new URL(path, baseUrl);
191
- return url.toString();
133
+ return new URL(path, baseUrl).toString();
134
+ }
135
+ getHeaders(customHeaders) {
136
+ return __spreadValues({
137
+ "Content-Type": "application/json"
138
+ }, customHeaders);
192
139
  }
193
140
  };
194
141
 
@@ -888,7 +835,7 @@ var CartApi = class extends BaseService {
888
835
  */
889
836
  cartCount(requestOptions) {
890
837
  return __async(this, null, function* () {
891
- return yield this.get(this.baseUrl + "cart/count", {}, requestOptions);
838
+ return yield this.get("cart/count", {}, requestOptions);
892
839
  });
893
840
  }
894
841
  /**