2026-02-05 17:46:29 +01:00
|
|
|
import axios, { AxiosInstance } from "axios"
|
2026-02-10 13:20:39 +01:00
|
|
|
import { IJWTService, JWTToken } from "../domain/JWT.js"
|
|
|
|
|
|
|
|
|
|
// Cambiar por IJWRGeneralService
|
2026-01-28 13:42:27 +01:00
|
|
|
|
|
|
|
|
export type JWTProvider<T> = {
|
|
|
|
|
/** El servidor está solicitando un token nuevo o refrescando el actual*/
|
|
|
|
|
isRefreshing: boolean
|
|
|
|
|
authToken: JWTToken<T> | undefined
|
|
|
|
|
tryRefreshToken: () => Promise<JWTToken<T>>
|
|
|
|
|
getAccessToken: () => Promise<JWTToken<T>>
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export class HttpClient {
|
|
|
|
|
|
|
|
|
|
public client: AxiosInstance
|
|
|
|
|
private jwtManager: JWTProvider<{}>
|
2026-02-10 13:20:39 +01:00
|
|
|
private jwtService: IJWTService<any> | undefined;
|
2026-01-28 13:42:27 +01:00
|
|
|
|
|
|
|
|
constructor(args: {
|
|
|
|
|
baseURL: string,
|
2026-04-30 15:49:59 +02:00
|
|
|
headers: Record<string, string>,
|
2026-02-10 13:20:39 +01:00
|
|
|
jwtManager: JWTProvider<{}> // todo: asociar el tipo de token,
|
|
|
|
|
jwtService?: IJWTService<any>
|
2026-01-28 13:42:27 +01:00
|
|
|
}) {
|
2026-02-05 17:46:29 +01:00
|
|
|
this.client = axios.create({
|
2026-01-28 13:42:27 +01:00
|
|
|
...args
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
this.jwtManager = args.jwtManager
|
|
|
|
|
|
2026-02-10 13:20:39 +01:00
|
|
|
if (args.jwtService != undefined) this.jwtService = args.jwtService
|
2026-01-28 13:42:27 +01:00
|
|
|
|
|
|
|
|
this.client.interceptors.request.use(
|
|
|
|
|
async (config) => {
|
|
|
|
|
// Idealmente estas condiciones no deberian de darse si mantenemos el
|
|
|
|
|
// token valido de forma preventiva
|
2026-01-29 12:57:51 +01:00
|
|
|
const token = await this.jwtManager.getAccessToken()
|
2026-01-28 13:42:27 +01:00
|
|
|
|
2026-04-30 15:49:59 +02:00
|
|
|
if (token == undefined) throw new Error("No se ha obtenido el token para la petición")
|
2026-01-28 13:42:27 +01:00
|
|
|
|
|
|
|
|
config.headers.Authorization = `Bearer ${this.jwtManager.authToken!.rawToken}`
|
2026-01-29 12:57:51 +01:00
|
|
|
console.log("request completa", config.data)
|
2026-01-28 13:42:27 +01:00
|
|
|
return config;
|
|
|
|
|
},
|
|
|
|
|
(error) => Promise.reject(error)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// No deberia usarlos de momento
|
|
|
|
|
this.client.interceptors.response.use(
|
|
|
|
|
(response) => {
|
|
|
|
|
return response;
|
2026-04-30 15:49:59 +02:00
|
|
|
}, async (error) => {
|
2026-01-28 13:42:27 +01:00
|
|
|
// TODO: Esta parte no tiene tipos, hay que asegurar el error
|
|
|
|
|
const req = error.config
|
2026-02-06 12:14:46 +01:00
|
|
|
console.error("[http] Error en la respuesta ", error, error.response)
|
2026-01-28 13:42:27 +01:00
|
|
|
if (error.response?.status == 401) {
|
|
|
|
|
this.jwtManager.getAccessToken()
|
|
|
|
|
}
|
2026-01-30 10:42:48 +01:00
|
|
|
return Promise.reject(error)
|
2026-01-28 13:42:27 +01:00
|
|
|
}
|
|
|
|
|
)
|
2026-04-30 15:49:59 +02:00
|
|
|
}
|
2026-01-26 15:04:17 +01:00
|
|
|
|
2026-04-30 15:49:59 +02:00
|
|
|
get get() {
|
|
|
|
|
return this.client.get
|
2026-01-26 15:04:17 +01:00
|
|
|
}
|
|
|
|
|
|
2026-04-30 15:49:59 +02:00
|
|
|
get post() {
|
|
|
|
|
return this.client.post
|
|
|
|
|
}
|
2026-01-28 13:42:27 +01:00
|
|
|
|
2026-04-30 15:49:59 +02:00
|
|
|
get patch() {
|
|
|
|
|
return this.client.patch
|
|
|
|
|
}
|
2026-01-26 15:04:17 +01:00
|
|
|
}
|