@vitalfit/sdk 0.2.6 → 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 CHANGED
@@ -62,7 +62,15 @@ var isAPIError = (error) => {
62
62
  // src/client.ts
63
63
  var Client = class {
64
64
  client;
65
- jwt;
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
- async call(method, config) {
79
- if (config.jwt) {
80
- this.client.interceptors.request.use((axiosConfig) => {
81
- if (axiosConfig.headers) {
82
- axiosConfig.headers["Authorization"] = `Bearer ${config.jwt}`;
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 axiosConfig;
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
- const data = await this.call("post", config);
132
- return data;
204
+ return this.call("post", config);
133
205
  }
134
206
  async put(config) {
135
- const data = await this.call("put", config);
136
- return data;
207
+ return this.call("put", config);
137
208
  }
138
209
  async patch(config) {
139
- const data = await this.call("patch", config);
140
- return data;
210
+ return this.call("patch", config);
141
211
  }
142
212
  async delete(config) {
143
- const data = await this.call("delete", config);
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,61 @@ 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);
234
+ this.renewToken = this.renewToken.bind(this);
235
+ this.revokeAllSessions = this.revokeAllSessions.bind(this);
236
+ this.revokeAllSessionsByUserID = this.revokeAllSessionsByUserID.bind(this);
237
+ this.revokeSession = this.revokeSession.bind(this);
238
+ this.revokeSessionByID = this.revokeSessionByID.bind(this);
239
+ this.getUserSessions = this.getUserSessions.bind(this);
240
+ this.getUserSessionByID = this.getUserSessionByID.bind(this);
241
+ }
242
+ async renewToken(refresh_token) {
243
+ const response = await this.client.post({
244
+ url: "/auth/refresh",
245
+ data: {
246
+ refresh_token
247
+ }
248
+ });
249
+ return response;
250
+ }
251
+ async revokeAllSessions(jwt) {
252
+ await this.client.delete({
253
+ url: "/user/sessions",
254
+ jwt
255
+ });
256
+ }
257
+ async revokeAllSessionsByUserID(userId, jwt) {
258
+ await this.client.delete({
259
+ url: `/user/${userId}/sessions`,
260
+ jwt
261
+ });
262
+ }
263
+ async revokeSession(sessionId, jwt) {
264
+ await this.client.delete({
265
+ url: `/user/sessions/${sessionId}`,
266
+ jwt
267
+ });
268
+ }
269
+ async revokeSessionByID(sessionId, jwt) {
270
+ await this.client.delete({
271
+ url: `/user/sessions/${sessionId}`,
272
+ jwt
273
+ });
274
+ }
275
+ async getUserSessions(jwt) {
276
+ const response = await this.client.get({
277
+ url: "/user/sessions",
278
+ jwt
279
+ });
280
+ return response;
281
+ }
282
+ async getUserSessionByID(userId, jwt) {
283
+ const response = await this.client.get({
284
+ url: `/user/${userId}/sessions`,
285
+ jwt
286
+ });
287
+ return response;
170
288
  }
171
289
  async login({
172
290
  email,
@@ -184,10 +302,13 @@ var AuthService = class {
184
302
  return response;
185
303
  }
186
304
  logout() {
187
- this.client.removeJWT();
305
+ this.client.removeTokens();
188
306
  }
189
307
  saveJWT(jwt) {
190
- this.client.setJWT(jwt);
308
+ this.client.setTokens(jwt, "");
309
+ }
310
+ saveTokens(access, refresh) {
311
+ this.client.setTokens(access, refresh);
191
312
  }
192
313
  async signUp(signUpData) {
193
314
  const birthDateRegex = /^\d{4}-\d{2}-\d{2}$/;
@@ -2248,7 +2369,7 @@ var VitalFit = class _VitalFit {
2248
2369
  return _VitalFit.instance;
2249
2370
  }
2250
2371
  version() {
2251
- return "0.2.6";
2372
+ return "0.2.8";
2252
2373
  }
2253
2374
  };
2254
2375
  // Annotate the CommonJS export names for ESM import in node: