@bzbs/react-api-client 0.2.2 → 1.0.0

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/README.md CHANGED
@@ -1,82 +1,82 @@
1
- # Installing
2
-
3
- Using npm :
4
-
5
- ```bash
6
- npm i @bzbs/react-api-client
7
- ```
8
-
9
- Once the package is installed, you can import the library using import or require approach:
10
-
11
- ```js
12
- import { BzbsService } from '@bzbs/react-api-client';
13
- ```
14
-
15
- You must to have AxiosInstance, Base URL and Line URL for library require:
16
-
17
- ```js
18
- const axiosClient = axios.create({});
19
- const baseUrl = 'https://www.xxx-api.com/api/';
20
- const lineUrl = 'https://api.line.me/v2/bot/';
21
- ```
22
-
23
- And defind const and export libraty to using:
24
-
25
- ```js
26
- export const bzbsService = new BzbsService(axiosClient, baseUrl, lineUrl);
27
- ```
28
-
29
- If you have default header you can add to AxiosInstance brfore:
30
-
31
- ```js
32
- const defaultOptions = {
33
- headers: {
34
- 'Content-Type': 'application/json, multipart/form-data',
35
- 'App-Id': 2952697274802274,
36
- 'Ocp-Apim-Subscription-Key': '89c1d9bafb65486aa02606f63cb86b5c',
37
- },
38
- };
39
-
40
- axiosClient.defaults.headers.common = defaultOptions.headers;
41
- ```
42
-
43
- ## Example
44
-
45
- Create service-config.tsx file:
46
-
47
- ```tsx
48
- import { BzbsService } from '@bzbs/react-api-client';
49
- import axios from 'axios';
50
-
51
- const defaultOptions = {
52
- headers: {
53
- 'Content-Type': 'application/json, multipart/form-data',
54
- 'App-Id': 2952697274802274,
55
- 'Ocp-Apim-Subscription-Key': '89c1d9bafb65486aa02606f63cb86b5c',
56
- },
57
- };
58
-
59
- var axiosClient = axios.create({});
60
- axiosClient.defaults.headers.common = defaultOptions.headers;
61
-
62
- const baseUrl = 'https://apigateway.buzzebees-uat.com/api/';
63
- const lineUrl = 'https://api.line.me/v2/bot/';
64
-
65
- export const bzbsService = new BzbsService(axiosClient, baseUrl, lineUrl);
66
- ```
67
-
68
- Using service API in another files:
69
-
70
- ```tsx
71
- import { bzbsService } from './src/service-config';
72
-
73
- const campaigList = async () => {
74
- await bzbsService.campaignApi?.campaign({
75
- config: 'campaign_buzzebeesdemo',
76
- byConfig: true,
77
- skip: 0,
78
- top: 10,
79
- deviceLocale: 1054,
80
- });
81
- };
82
- ```
1
+ # Installing
2
+
3
+ Using npm :
4
+
5
+ ```bash
6
+ npm i @bzbs/react-api-client
7
+ ```
8
+
9
+ Once the package is installed, you can import the library using import or require approach:
10
+
11
+ ```js
12
+ import { BzbsService } from '@bzbs/react-api-client';
13
+ ```
14
+
15
+ You must to have AxiosInstance, Base URL and Line URL for library require:
16
+
17
+ ```js
18
+ const axiosClient = axios.create({});
19
+ const baseUrl = 'https://www.xxx-api.com/api/';
20
+ const lineUrl = 'https://api.line.me/v2/bot/';
21
+ ```
22
+
23
+ And defind const and export libraty to using:
24
+
25
+ ```js
26
+ export const bzbsService = new BzbsService(axiosClient, baseUrl, lineUrl);
27
+ ```
28
+
29
+ If you have default header you can add to AxiosInstance brfore:
30
+
31
+ ```js
32
+ const defaultOptions = {
33
+ headers: {
34
+ 'Content-Type': 'application/json, multipart/form-data',
35
+ 'App-Id': 2952697274802274,
36
+ 'Ocp-Apim-Subscription-Key': '89c1d9bafb65486aa02606f63cb86b5c',
37
+ },
38
+ };
39
+
40
+ axiosClient.defaults.headers.common = defaultOptions.headers;
41
+ ```
42
+
43
+ ## Example
44
+
45
+ Create service-config.tsx file:
46
+
47
+ ```tsx
48
+ import { BzbsService } from '@bzbs/react-api-client';
49
+ import axios from 'axios';
50
+
51
+ const defaultOptions = {
52
+ headers: {
53
+ 'Content-Type': 'application/json, multipart/form-data',
54
+ 'App-Id': 2952697274802274,
55
+ 'Ocp-Apim-Subscription-Key': '89c1d9bafb65486aa02606f63cb86b5c',
56
+ },
57
+ };
58
+
59
+ var axiosClient = axios.create({});
60
+ axiosClient.defaults.headers.common = defaultOptions.headers;
61
+
62
+ const baseUrl = 'https://apigateway.buzzebees-uat.com/api/';
63
+ const lineUrl = 'https://api.line.me/v2/bot/';
64
+
65
+ export const bzbsService = new BzbsService(axiosClient, baseUrl, lineUrl);
66
+ ```
67
+
68
+ Using service API in another files:
69
+
70
+ ```tsx
71
+ import { bzbsService } from './src/service-config';
72
+
73
+ const campaigList = async () => {
74
+ await bzbsService.campaignApi?.campaign({
75
+ config: 'campaign_buzzebeesdemo',
76
+ byConfig: true,
77
+ skip: 0,
78
+ top: 10,
79
+ deviceLocale: 1054,
80
+ });
81
+ };
82
+ ```
package/dist/index.d.mts CHANGED
@@ -14,6 +14,8 @@ type ServiceResponse<T> = SuccessResponse<T> | ErrorResponse;
14
14
  interface ApiResponse<T> {
15
15
  Success: boolean;
16
16
  Data: T;
17
+ success: boolean;
18
+ data: T;
17
19
  }
18
20
  type SuccessResponse<T> = {
19
21
  type: 'success';
@@ -47,14 +49,19 @@ declare class BaseService {
47
49
  private baseUrl;
48
50
  constructor(client: AxiosInstance, baseUrl: string);
49
51
  private normalizeError;
52
+ private normalizeResponse;
50
53
  private request;
51
54
  get<T>(path: string, params?: any, requestOptions?: RequestOptions): Promise<ServiceResponse<T>>;
52
55
  post<T>(path: string, data: any, requestOptions?: RequestOptions): Promise<ServiceResponse<T>>;
53
56
  put<T>(path: string, data: any, requestOptions?: RequestOptions): Promise<ServiceResponse<T>>;
54
57
  delete<T>(path: string, requestOptions?: RequestOptions): Promise<ServiceResponse<T>>;
55
58
  patch<T>(path: string, data: any, requestOptions?: RequestOptions): Promise<ServiceResponse<T>>;
56
- private joinUrl;
57
- private getHeaders;
59
+ joinUrl(baseUrl: string, path: string): string;
60
+ getHeaders(customHeaders?: {
61
+ [key: string]: string;
62
+ }): {
63
+ [key: string]: string;
64
+ };
58
65
  }
59
66
 
60
67
  interface LoginResponse {
@@ -2669,7 +2676,7 @@ declare class BzbsService {
2669
2676
  couponApi: CouponApi;
2670
2677
  dashboardApi: DashboardApi;
2671
2678
  historyApi: HistoryApi;
2672
- lineApi: LineApi;
2679
+ lineApi: LineApi | undefined;
2673
2680
  notificationApi: NotificationApi;
2674
2681
  placeApi: PlaceApi;
2675
2682
  pointLogApi: PointLogApi;
@@ -2679,7 +2686,7 @@ declare class BzbsService {
2679
2686
  stampApi: StampApi;
2680
2687
  forumApi: RequestHelpApi;
2681
2688
  blob: Blob;
2682
- constructor(client: AxiosInstance, baseUrl: string, baseLineUrl: string, baseBlobUrl: string);
2689
+ constructor(client: AxiosInstance, baseUrl: string, baseLineUrl: string | "", baseBlobUrl: string);
2683
2690
  }
2684
2691
 
2685
2692
  export { type Address, AddressApi, type ApiResponse, AuthenticateApi, type Badge, BadgeApi, BaseService, Blob, type Buzzebees, type BzbsErrorResponse, BzbsService, type Campaign, CampaignApi, type CampaignDetail, type CartAccessResponse, CartApi, type CartCountResponse, type Category, CategoryApi, type ChatMessage, type ClientError, type ConfirmOtpResponse, type Consent, ConsentApi, type ConsentVersion, type ConsentVersionValue, CouponApi, type CouponResponse, type CouponResponseData, type CreateStampResponse, type Dashboard, DashboardApi, type Detail, type District, type ErrorResponse, type ExpiringPoints, type FavoriteResponse, type ForgetPasswordResponse, HistoryApi, type LikeForumResponse, LineApi, type LineAuthResponse, type LoginResponse, type Maintenance, type Mission, type Notification, NotificationApi, type OtpResponse, type Picture, type Place, PlaceApi, type PlaceService, type PointLog, PointLogApi, ProfileApi, type ProfileResponse, type Province, type Purchase, type RedeemResponse, RegistrationApi, type RegistrationResponse, RequestHelpApi, type RequestHelpCode, type RequestOptions, type ResumeResponse, type ServerError, type ServiceResponse, type Stamp, StampApi, type StampCampaign, type StampHistory, type StampProfileResponse, type StatusResponse, type Style, type SubCampaign, type SubCampaignStyle, type SubDistrict, type SuccessResponse, type Trace, type UpdatedPoints, type UseCampaignResponse, type ValidateOtpResponse, type Version, type ZipCode };
package/dist/index.d.ts CHANGED
@@ -14,6 +14,8 @@ type ServiceResponse<T> = SuccessResponse<T> | ErrorResponse;
14
14
  interface ApiResponse<T> {
15
15
  Success: boolean;
16
16
  Data: T;
17
+ success: boolean;
18
+ data: T;
17
19
  }
18
20
  type SuccessResponse<T> = {
19
21
  type: 'success';
@@ -47,14 +49,19 @@ declare class BaseService {
47
49
  private baseUrl;
48
50
  constructor(client: AxiosInstance, baseUrl: string);
49
51
  private normalizeError;
52
+ private normalizeResponse;
50
53
  private request;
51
54
  get<T>(path: string, params?: any, requestOptions?: RequestOptions): Promise<ServiceResponse<T>>;
52
55
  post<T>(path: string, data: any, requestOptions?: RequestOptions): Promise<ServiceResponse<T>>;
53
56
  put<T>(path: string, data: any, requestOptions?: RequestOptions): Promise<ServiceResponse<T>>;
54
57
  delete<T>(path: string, requestOptions?: RequestOptions): Promise<ServiceResponse<T>>;
55
58
  patch<T>(path: string, data: any, requestOptions?: RequestOptions): Promise<ServiceResponse<T>>;
56
- private joinUrl;
57
- private getHeaders;
59
+ joinUrl(baseUrl: string, path: string): string;
60
+ getHeaders(customHeaders?: {
61
+ [key: string]: string;
62
+ }): {
63
+ [key: string]: string;
64
+ };
58
65
  }
59
66
 
60
67
  interface LoginResponse {
@@ -2669,7 +2676,7 @@ declare class BzbsService {
2669
2676
  couponApi: CouponApi;
2670
2677
  dashboardApi: DashboardApi;
2671
2678
  historyApi: HistoryApi;
2672
- lineApi: LineApi;
2679
+ lineApi: LineApi | undefined;
2673
2680
  notificationApi: NotificationApi;
2674
2681
  placeApi: PlaceApi;
2675
2682
  pointLogApi: PointLogApi;
@@ -2679,7 +2686,7 @@ declare class BzbsService {
2679
2686
  stampApi: StampApi;
2680
2687
  forumApi: RequestHelpApi;
2681
2688
  blob: Blob;
2682
- constructor(client: AxiosInstance, baseUrl: string, baseLineUrl: string, baseBlobUrl: string);
2689
+ constructor(client: AxiosInstance, baseUrl: string, baseLineUrl: string | "", baseBlobUrl: string);
2683
2690
  }
2684
2691
 
2685
2692
  export { type Address, AddressApi, type ApiResponse, AuthenticateApi, type Badge, BadgeApi, BaseService, Blob, type Buzzebees, type BzbsErrorResponse, BzbsService, type Campaign, CampaignApi, type CampaignDetail, type CartAccessResponse, CartApi, type CartCountResponse, type Category, CategoryApi, type ChatMessage, type ClientError, type ConfirmOtpResponse, type Consent, ConsentApi, type ConsentVersion, type ConsentVersionValue, CouponApi, type CouponResponse, type CouponResponseData, type CreateStampResponse, type Dashboard, DashboardApi, type Detail, type District, type ErrorResponse, type ExpiringPoints, type FavoriteResponse, type ForgetPasswordResponse, HistoryApi, type LikeForumResponse, LineApi, type LineAuthResponse, type LoginResponse, type Maintenance, type Mission, type Notification, NotificationApi, type OtpResponse, type Picture, type Place, PlaceApi, type PlaceService, type PointLog, PointLogApi, ProfileApi, type ProfileResponse, type Province, type Purchase, type RedeemResponse, RegistrationApi, type RegistrationResponse, RequestHelpApi, type RequestHelpCode, type RequestOptions, type ResumeResponse, type ServerError, type ServiceResponse, type Stamp, StampApi, type StampCampaign, type StampHistory, type StampProfileResponse, type StatusResponse, type Style, type SubCampaign, type SubCampaignStyle, type SubDistrict, type SuccessResponse, type Trace, type UpdatedPoints, type UseCampaignResponse, type ValidateOtpResponse, type Version, type ZipCode };
package/dist/index.js CHANGED
@@ -55,8 +55,8 @@ var __async = (__this, __arguments, generator) => {
55
55
  };
56
56
 
57
57
  // src/index.ts
58
- var src_exports = {};
59
- __export(src_exports, {
58
+ var index_exports = {};
59
+ __export(index_exports, {
60
60
  AddressApi: () => AddressApi,
61
61
  AuthenticateApi: () => AuthenticateApi,
62
62
  BadgeApi: () => BadgeApi,
@@ -79,15 +79,13 @@ __export(src_exports, {
79
79
  RequestHelpApi: () => RequestHelpApi,
80
80
  StampApi: () => StampApi
81
81
  });
82
- module.exports = __toCommonJS(src_exports);
82
+ module.exports = __toCommonJS(index_exports);
83
83
 
84
84
  // src/api/base-service.ts
85
85
  var BaseService = class {
86
86
  constructor(client, baseUrl) {
87
- if (!client)
88
- throw new Error("Axios client is required.");
89
- if (!baseUrl)
90
- throw new Error("Base URL is required.");
87
+ if (!client) throw new Error("Axios client is required.");
88
+ if (!baseUrl) throw new Error("Base URL is required.");
91
89
  this.client = client;
92
90
  this.baseUrl = baseUrl;
93
91
  }
@@ -114,6 +112,35 @@ var BaseService = class {
114
112
  details: error
115
113
  };
116
114
  }
115
+ normalizeResponse(response) {
116
+ if (response.status === 204) {
117
+ return { model: {}, response, type: "success" };
118
+ }
119
+ const data = response.data;
120
+ const isSuccess = (data == null ? void 0 : data.Success) === true || (data == null ? void 0 : data.success) === true;
121
+ if (isSuccess) {
122
+ const model = "Data" in data ? data.Data : "data" in data ? data.data : {};
123
+ return { model, response, type: "success" };
124
+ }
125
+ if ("Success" in data || "success" in data) {
126
+ const error = {
127
+ type: "server-error",
128
+ error: {
129
+ requestId: data.RequestId || "",
130
+ error: {
131
+ id: data.Code || data.code || response.status,
132
+ message: data.Message || data.message || response.statusText,
133
+ code: data.Code || data.code || response.status,
134
+ type: "buzzebees"
135
+ }
136
+ },
137
+ statusCode: data.Code || response.status,
138
+ response
139
+ };
140
+ return this.normalizeError(error);
141
+ }
142
+ return { model: data, response, type: "success" };
143
+ }
117
144
  request(method, path, data, requestOptions) {
118
145
  return __async(this, null, function* () {
119
146
  var _a;
@@ -125,41 +152,13 @@ var BaseService = class {
125
152
  data
126
153
  };
127
154
  const response = yield this.client.request(__spreadValues({ method, url }, config));
128
- if (response.status === 204) {
129
- return { model: "", response, type: "success" };
130
- }
131
- if ("Success" in response.data) {
132
- const data2 = response.data;
133
- if (data2.Success) {
134
- if ("Data" in response.data) {
135
- return { model: data2.Data, response, type: "success" };
136
- } else {
137
- return { model: "", response, type: "success" };
138
- }
139
- } else {
140
- const error = {
141
- type: "server-error",
142
- error: {
143
- requestId: response.data.RequestId || "",
144
- error: {
145
- id: response.data.Code || 500,
146
- message: response.data.Message || "An unexpected error occurred.",
147
- code: response.data.Code || 500,
148
- type: "buzzebees"
149
- }
150
- },
151
- statusCode: response.data.Code || 500,
152
- response
153
- };
154
- return this.normalizeError(error);
155
- }
156
- }
157
- return { model: response.data, response, type: "success" };
155
+ return this.normalizeResponse(response);
158
156
  } catch (error) {
159
157
  return this.normalizeError(error);
160
158
  }
161
159
  });
162
160
  }
161
+ /* if blob type we not include header in requestOptions */
163
162
  get(path, params, requestOptions) {
164
163
  return __async(this, null, function* () {
165
164
  return this.request("get", path, void 0, __spreadProps(__spreadValues({}, requestOptions), { params }));
@@ -186,7 +185,9 @@ var BaseService = class {
186
185
  });
187
186
  }
188
187
  joinUrl(baseUrl, path) {
189
- return new URL(path, baseUrl).toString();
188
+ const sanitizedBase = baseUrl.replace(/\/+$/, "");
189
+ const sanitizedPath = path.replace(/^\/+/, "");
190
+ return `${sanitizedBase}/${sanitizedPath}`;
190
191
  }
191
192
  getHeaders(customHeaders) {
192
193
  return __spreadValues({
@@ -2307,7 +2308,9 @@ var BzbsService = class {
2307
2308
  this.couponApi = new CouponApi(this.client, this.baseUrl);
2308
2309
  this.dashboardApi = new DashboardApi(this.client, this.baseUrl);
2309
2310
  this.historyApi = new HistoryApi(this.client, this.baseUrl);
2310
- this.lineApi = new LineApi(this.client, this.baseLineUrl);
2311
+ if (this.baseLineUrl) {
2312
+ this.lineApi = new LineApi(this.client, this.baseLineUrl);
2313
+ }
2311
2314
  this.notificationApi = new NotificationApi(this.client, this.baseUrl);
2312
2315
  this.placeApi = new PlaceApi(this.client, this.baseUrl);
2313
2316
  this.pointLogApi = new PointLogApi(this.client, this.baseUrl);