@vitalfit/sdk 0.2.7 → 0.2.8
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.cjs +99 -32
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +17 -8
- package/dist/index.d.ts +17 -8
- package/dist/index.js +99 -32
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -62,7 +62,15 @@ var isAPIError = (error) => {
|
|
|
62
62
|
// src/client.ts
|
|
63
63
|
var Client = class {
|
|
64
64
|
client;
|
|
65
|
-
|
|
65
|
+
isRefreshing = false;
|
|
66
|
+
failedQueue = [];
|
|
67
|
+
// Guardamos los tokens internamente en el cliente
|
|
68
|
+
accessToken;
|
|
69
|
+
refreshToken;
|
|
70
|
+
// Callback para notificar al frontend (AuthProvider) que los tokens cambiaron
|
|
71
|
+
onTokenUpdate;
|
|
72
|
+
// Callback para forzar logout si el refresh falla
|
|
73
|
+
onLogout;
|
|
66
74
|
constructor(isDevMode, origin) {
|
|
67
75
|
let headers = {
|
|
68
76
|
"Content-Type": "application/json"
|
|
@@ -74,24 +82,89 @@ var Client = class {
|
|
|
74
82
|
baseURL: isDevMode ? DEV_URL : BASE_URL,
|
|
75
83
|
headers
|
|
76
84
|
});
|
|
85
|
+
this.setupInterceptors();
|
|
77
86
|
}
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
87
|
+
// Configurar callbacks desde el AuthProvider
|
|
88
|
+
setCallbacks(onTokenUpdate, onLogout) {
|
|
89
|
+
this.onTokenUpdate = onTokenUpdate;
|
|
90
|
+
this.onLogout = onLogout;
|
|
91
|
+
}
|
|
92
|
+
setTokens(access, refresh) {
|
|
93
|
+
this.accessToken = access;
|
|
94
|
+
this.refreshToken = refresh;
|
|
95
|
+
}
|
|
96
|
+
removeTokens() {
|
|
97
|
+
this.accessToken = void 0;
|
|
98
|
+
this.refreshToken = void 0;
|
|
99
|
+
}
|
|
100
|
+
processQueue(error, token = null) {
|
|
101
|
+
this.failedQueue.forEach((prom) => {
|
|
102
|
+
if (error) {
|
|
103
|
+
prom.reject(error);
|
|
104
|
+
} else {
|
|
105
|
+
prom.resolve(token);
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
this.failedQueue = [];
|
|
109
|
+
}
|
|
110
|
+
setupInterceptors() {
|
|
111
|
+
this.client.interceptors.response.use(
|
|
112
|
+
(response) => response,
|
|
113
|
+
async (error) => {
|
|
114
|
+
const originalRequest = error.config;
|
|
115
|
+
if (error.response?.status === 401 && !originalRequest._retry) {
|
|
116
|
+
if (!this.refreshToken) {
|
|
117
|
+
return Promise.reject(error);
|
|
118
|
+
}
|
|
119
|
+
if (this.isRefreshing) {
|
|
120
|
+
return new Promise((resolve, reject) => {
|
|
121
|
+
this.failedQueue.push({ resolve, reject });
|
|
122
|
+
}).then((token) => {
|
|
123
|
+
originalRequest.headers["Authorization"] = `Bearer ${token}`;
|
|
124
|
+
return this.client(originalRequest);
|
|
125
|
+
}).catch((err) => Promise.reject(err));
|
|
126
|
+
}
|
|
127
|
+
originalRequest._retry = true;
|
|
128
|
+
this.isRefreshing = true;
|
|
129
|
+
try {
|
|
130
|
+
const response = await import_axios.default.post(`${this.client.defaults.baseURL}/auth/refresh`, {
|
|
131
|
+
refresh_token: this.refreshToken
|
|
132
|
+
});
|
|
133
|
+
const { access_token, refresh_token } = response.data;
|
|
134
|
+
this.setTokens(access_token, refresh_token);
|
|
135
|
+
if (this.onTokenUpdate) {
|
|
136
|
+
this.onTokenUpdate(access_token, refresh_token);
|
|
137
|
+
}
|
|
138
|
+
this.processQueue(null, access_token);
|
|
139
|
+
originalRequest.headers["Authorization"] = `Bearer ${access_token}`;
|
|
140
|
+
return this.client(originalRequest);
|
|
141
|
+
} catch (refreshError) {
|
|
142
|
+
this.processQueue(refreshError, null);
|
|
143
|
+
this.removeTokens();
|
|
144
|
+
if (this.onLogout) this.onLogout();
|
|
145
|
+
return Promise.reject(refreshError);
|
|
146
|
+
} finally {
|
|
147
|
+
this.isRefreshing = false;
|
|
148
|
+
}
|
|
83
149
|
}
|
|
84
|
-
return
|
|
85
|
-
}
|
|
150
|
+
return Promise.reject(error);
|
|
151
|
+
}
|
|
152
|
+
);
|
|
153
|
+
}
|
|
154
|
+
async call(method, config) {
|
|
155
|
+
const tokenToUse = this.accessToken || config.jwt;
|
|
156
|
+
const axiosConfig = {
|
|
157
|
+
method,
|
|
158
|
+
url: config.url,
|
|
159
|
+
data: config.data,
|
|
160
|
+
params: config.params,
|
|
161
|
+
headers: {}
|
|
162
|
+
};
|
|
163
|
+
if (tokenToUse && axiosConfig.headers) {
|
|
164
|
+
axiosConfig.headers["Authorization"] = `Bearer ${tokenToUse}`;
|
|
86
165
|
}
|
|
87
166
|
try {
|
|
88
|
-
const response = await this.client.request(
|
|
89
|
-
method,
|
|
90
|
-
url: config.url,
|
|
91
|
-
data: config.data,
|
|
92
|
-
params: config.params
|
|
93
|
-
});
|
|
94
|
-
this.client.interceptors.request.clear();
|
|
167
|
+
const response = await this.client.request(axiosConfig);
|
|
95
168
|
return response.data;
|
|
96
169
|
} catch (error) {
|
|
97
170
|
if (import_axios.default.isAxiosError(error)) {
|
|
@@ -128,26 +201,16 @@ var Client = class {
|
|
|
128
201
|
}
|
|
129
202
|
}
|
|
130
203
|
async post(config) {
|
|
131
|
-
|
|
132
|
-
return data;
|
|
204
|
+
return this.call("post", config);
|
|
133
205
|
}
|
|
134
206
|
async put(config) {
|
|
135
|
-
|
|
136
|
-
return data;
|
|
207
|
+
return this.call("put", config);
|
|
137
208
|
}
|
|
138
209
|
async patch(config) {
|
|
139
|
-
|
|
140
|
-
return data;
|
|
210
|
+
return this.call("patch", config);
|
|
141
211
|
}
|
|
142
212
|
async delete(config) {
|
|
143
|
-
|
|
144
|
-
return data;
|
|
145
|
-
}
|
|
146
|
-
setJWT(jwt) {
|
|
147
|
-
this.jwt = jwt;
|
|
148
|
-
}
|
|
149
|
-
removeJWT() {
|
|
150
|
-
this.jwt = void 0;
|
|
213
|
+
return this.call("delete", config);
|
|
151
214
|
}
|
|
152
215
|
};
|
|
153
216
|
|
|
@@ -167,6 +230,7 @@ var AuthService = class {
|
|
|
167
230
|
this.oAuthLogin = this.oAuthLogin.bind(this);
|
|
168
231
|
this.logout = this.logout.bind(this);
|
|
169
232
|
this.saveJWT = this.saveJWT.bind(this);
|
|
233
|
+
this.saveTokens = this.saveTokens.bind(this);
|
|
170
234
|
this.renewToken = this.renewToken.bind(this);
|
|
171
235
|
this.revokeAllSessions = this.revokeAllSessions.bind(this);
|
|
172
236
|
this.revokeAllSessionsByUserID = this.revokeAllSessionsByUserID.bind(this);
|
|
@@ -238,10 +302,13 @@ var AuthService = class {
|
|
|
238
302
|
return response;
|
|
239
303
|
}
|
|
240
304
|
logout() {
|
|
241
|
-
this.client.
|
|
305
|
+
this.client.removeTokens();
|
|
242
306
|
}
|
|
243
307
|
saveJWT(jwt) {
|
|
244
|
-
this.client.
|
|
308
|
+
this.client.setTokens(jwt, "");
|
|
309
|
+
}
|
|
310
|
+
saveTokens(access, refresh) {
|
|
311
|
+
this.client.setTokens(access, refresh);
|
|
245
312
|
}
|
|
246
313
|
async signUp(signUpData) {
|
|
247
314
|
const birthDateRegex = /^\d{4}-\d{2}-\d{2}$/;
|
|
@@ -2302,7 +2369,7 @@ var VitalFit = class _VitalFit {
|
|
|
2302
2369
|
return _VitalFit.instance;
|
|
2303
2370
|
}
|
|
2304
2371
|
version() {
|
|
2305
|
-
return "0.2.
|
|
2372
|
+
return "0.2.8";
|
|
2306
2373
|
}
|
|
2307
2374
|
};
|
|
2308
2375
|
// Annotate the CommonJS export names for ESM import in node:
|