@bzbs/react-api-client 0.2.3 → 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 +82 -82
- package/dist/index.d.mts +6 -2
- package/dist/index.d.ts +6 -2
- package/dist/index.js +30 -43
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +27 -40
- package/dist/index.mjs.map +1 -1
- package/package.json +63 -63
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
|
@@ -56,8 +56,12 @@ declare class BaseService {
|
|
|
56
56
|
put<T>(path: string, data: any, requestOptions?: RequestOptions): Promise<ServiceResponse<T>>;
|
|
57
57
|
delete<T>(path: string, requestOptions?: RequestOptions): Promise<ServiceResponse<T>>;
|
|
58
58
|
patch<T>(path: string, data: any, requestOptions?: RequestOptions): Promise<ServiceResponse<T>>;
|
|
59
|
-
|
|
60
|
-
|
|
59
|
+
joinUrl(baseUrl: string, path: string): string;
|
|
60
|
+
getHeaders(customHeaders?: {
|
|
61
|
+
[key: string]: string;
|
|
62
|
+
}): {
|
|
63
|
+
[key: string]: string;
|
|
64
|
+
};
|
|
61
65
|
}
|
|
62
66
|
|
|
63
67
|
interface LoginResponse {
|
package/dist/index.d.ts
CHANGED
|
@@ -56,8 +56,12 @@ declare class BaseService {
|
|
|
56
56
|
put<T>(path: string, data: any, requestOptions?: RequestOptions): Promise<ServiceResponse<T>>;
|
|
57
57
|
delete<T>(path: string, requestOptions?: RequestOptions): Promise<ServiceResponse<T>>;
|
|
58
58
|
patch<T>(path: string, data: any, requestOptions?: RequestOptions): Promise<ServiceResponse<T>>;
|
|
59
|
-
|
|
60
|
-
|
|
59
|
+
joinUrl(baseUrl: string, path: string): string;
|
|
60
|
+
getHeaders(customHeaders?: {
|
|
61
|
+
[key: string]: string;
|
|
62
|
+
}): {
|
|
63
|
+
[key: string]: string;
|
|
64
|
+
};
|
|
61
65
|
}
|
|
62
66
|
|
|
63
67
|
interface LoginResponse {
|
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
|
|
59
|
-
__export(
|
|
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(
|
|
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
|
-
|
|
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
|
}
|
|
@@ -118,43 +116,30 @@ var BaseService = class {
|
|
|
118
116
|
if (response.status === 204) {
|
|
119
117
|
return { model: {}, response, type: "success" };
|
|
120
118
|
}
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
if ("Data" in response.data) {
|
|
133
|
-
return { model: data.Data, response, type: "success" };
|
|
134
|
-
} else if ("data" in response.data) {
|
|
135
|
-
return { model: data.data, response, type: "success" };
|
|
136
|
-
} else {
|
|
137
|
-
return { model: {}, response, type: "success" };
|
|
138
|
-
}
|
|
139
|
-
} else {
|
|
140
|
-
const error = {
|
|
141
|
-
type: "server-error",
|
|
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 || "",
|
|
142
130
|
error: {
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
};
|
|
154
|
-
return this.normalizeError(error);
|
|
155
|
-
}
|
|
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);
|
|
156
141
|
}
|
|
157
|
-
return { model:
|
|
142
|
+
return { model: data, response, type: "success" };
|
|
158
143
|
}
|
|
159
144
|
request(method, path, data, requestOptions) {
|
|
160
145
|
return __async(this, null, function* () {
|
|
@@ -200,7 +185,9 @@ var BaseService = class {
|
|
|
200
185
|
});
|
|
201
186
|
}
|
|
202
187
|
joinUrl(baseUrl, path) {
|
|
203
|
-
|
|
188
|
+
const sanitizedBase = baseUrl.replace(/\/+$/, "");
|
|
189
|
+
const sanitizedPath = path.replace(/^\/+/, "");
|
|
190
|
+
return `${sanitizedBase}/${sanitizedPath}`;
|
|
204
191
|
}
|
|
205
192
|
getHeaders(customHeaders) {
|
|
206
193
|
return __spreadValues({
|