whio-api-sdk 1.0.152 → 1.0.154
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/src/sdk/sdk.d.ts +1 -0
- package/dist/src/sdk/sdk.js +19 -3
- package/package.json +1 -1
- package/src/sdk/sdk.ts +20 -3
package/dist/src/sdk/sdk.d.ts
CHANGED
package/dist/src/sdk/sdk.js
CHANGED
|
@@ -23,6 +23,16 @@ export class ApiSDK {
|
|
|
23
23
|
};
|
|
24
24
|
this.initialize();
|
|
25
25
|
}
|
|
26
|
+
fetchConfig(url) {
|
|
27
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
28
|
+
const response = yield fetch(url);
|
|
29
|
+
if (!response.ok) {
|
|
30
|
+
throw new Error(`Failed to fetch config from ${url}`);
|
|
31
|
+
}
|
|
32
|
+
const conf = yield response.json();
|
|
33
|
+
this.baseUrl = conf.baseUrl || this.baseUrl;
|
|
34
|
+
});
|
|
35
|
+
}
|
|
26
36
|
getToken() {
|
|
27
37
|
return __awaiter(this, void 0, void 0, function* () {
|
|
28
38
|
this.accessToken = yield this.storage.getItem('access_token');
|
|
@@ -38,14 +48,20 @@ export class ApiSDK {
|
|
|
38
48
|
this.user = userString ? JSON.parse(userString) : null;
|
|
39
49
|
});
|
|
40
50
|
}
|
|
41
|
-
request(endpoint, method = 'GET', body, headers = {}) {
|
|
51
|
+
request(endpoint, method = 'GET', body, headers = {}, noToken = false) {
|
|
42
52
|
return __awaiter(this, void 0, void 0, function* () {
|
|
43
53
|
const url = `${this.baseUrl}${endpoint}`;
|
|
44
54
|
const defaultHeaders = {
|
|
45
55
|
'Content-Type': 'application/json',
|
|
46
56
|
'ngrok-skip-browser-warning': 'true'
|
|
47
57
|
};
|
|
48
|
-
|
|
58
|
+
if (noToken) {
|
|
59
|
+
defaultHeaders['Authorization'] = '';
|
|
60
|
+
}
|
|
61
|
+
else {
|
|
62
|
+
// If no token is available, try to get it
|
|
63
|
+
yield this.getToken();
|
|
64
|
+
}
|
|
49
65
|
console.log(this.accessToken);
|
|
50
66
|
if (this.accessToken) {
|
|
51
67
|
defaultHeaders['Authorization'] = `Bearer ${this.accessToken}`;
|
|
@@ -87,7 +103,7 @@ export class ApiSDK {
|
|
|
87
103
|
login(credentials) {
|
|
88
104
|
return __awaiter(this, void 0, void 0, function* () {
|
|
89
105
|
try {
|
|
90
|
-
const response = yield this.request('/auth/login', 'POST', credentials);
|
|
106
|
+
const response = yield this.request('/auth/login', 'POST', credentials, {}, true);
|
|
91
107
|
this.accessToken = response.access_token;
|
|
92
108
|
this.refreshToken = response.referesh_token;
|
|
93
109
|
this.user = response.user;
|
package/package.json
CHANGED
package/src/sdk/sdk.ts
CHANGED
|
@@ -46,6 +46,15 @@ export class ApiSDK {
|
|
|
46
46
|
this.initialize();
|
|
47
47
|
}
|
|
48
48
|
|
|
49
|
+
private async fetchConfig(url: string): Promise<void> {
|
|
50
|
+
const response = await fetch(url);
|
|
51
|
+
if (!response.ok) {
|
|
52
|
+
throw new Error(`Failed to fetch config from ${url}`);
|
|
53
|
+
}
|
|
54
|
+
const conf = await response.json();
|
|
55
|
+
this.baseUrl = conf.baseUrl || this.baseUrl;
|
|
56
|
+
}
|
|
57
|
+
|
|
49
58
|
private async getToken() {
|
|
50
59
|
this.accessToken = await this.storage!.getItem('access_token');
|
|
51
60
|
if (!this.accessToken) {
|
|
@@ -63,7 +72,8 @@ export class ApiSDK {
|
|
|
63
72
|
endpoint: string,
|
|
64
73
|
method: string = 'GET',
|
|
65
74
|
body?: any,
|
|
66
|
-
headers: Record<string, string> = {}
|
|
75
|
+
headers: Record<string, string> = {},
|
|
76
|
+
noToken: boolean = false,
|
|
67
77
|
): Promise<T> {
|
|
68
78
|
const url = `${this.baseUrl}${endpoint}`;
|
|
69
79
|
const defaultHeaders: Record<string, string> = {
|
|
@@ -71,7 +81,12 @@ export class ApiSDK {
|
|
|
71
81
|
'ngrok-skip-browser-warning': 'true'
|
|
72
82
|
};
|
|
73
83
|
|
|
74
|
-
|
|
84
|
+
if (noToken) {
|
|
85
|
+
defaultHeaders['Authorization'] = '';
|
|
86
|
+
} else {
|
|
87
|
+
// If no token is available, try to get it
|
|
88
|
+
await this.getToken();
|
|
89
|
+
}
|
|
75
90
|
|
|
76
91
|
console.log(this.accessToken);
|
|
77
92
|
|
|
@@ -131,7 +146,9 @@ export class ApiSDK {
|
|
|
131
146
|
const response = await this.request<LoginResponse>(
|
|
132
147
|
'/auth/login',
|
|
133
148
|
'POST',
|
|
134
|
-
credentials
|
|
149
|
+
credentials,
|
|
150
|
+
{},
|
|
151
|
+
true
|
|
135
152
|
);
|
|
136
153
|
this.accessToken = response.access_token;
|
|
137
154
|
this.refreshToken = response.referesh_token;
|