Modelo de activacion de sim con token automatico

This commit is contained in:
2026-01-28 13:42:27 +01:00
parent ad217a277b
commit ca75f00e22
10 changed files with 184 additions and 38 deletions

View File

@@ -1,7 +1,62 @@
export class HTTPClient {
import { AxiosInstance, create as axiosCreate } from "axios"
import { JWTToken } from "../domain/JWT"
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<{}>
constructor(args: {
baseURL: string,
headers: Object,
jwtManager: JWTProvider<{}> // todo: asociar el tipo de token
}) {
this.client = axiosCreate({
...args
})
this.jwtManager = args.jwtManager
this.client.interceptors.request.use(
async (config) => {
// Idealmente estas condiciones no deberian de darse si mantenemos el
// token valido de forma preventiva
const token = this.jwtManager.authToken?.rawToken
if (token == undefined) throw new Error("No se ha obtenido el token para la peticion")
config.headers.Authorization = `Bearer ${this.jwtManager.authToken!.rawToken}`
console.log("request completa", config.headers, config.data)
return config;
},
(error) => Promise.reject(error)
)
// No deberia usarlos de momento
this.client.interceptors.response.use(
(response) => {
return response;
},
async (error) => {
// TODO: Esta parte no tiene tipos, hay que asegurar el error
const req = error.config
console.error("[http] Error en la respuesta ", error)
if (error.response?.status == 401) {
this.jwtManager.getAccessToken()
}
}
)
constructor() {
// JWT?
}
}

View File

@@ -13,8 +13,6 @@ export type RMQConnectionParams = {
secure: boolean
}
const RETRY_DELAY = 1000
const PREFETCH_LIMIT = 1
export class RabbitMQEventBus implements EventBus {
private buildStructure?: (chan: Channel) => void
@@ -126,11 +124,20 @@ export class RabbitMQEventBus implements EventBus {
}
protected async createConfirmChannel() {
if (this.connection == undefined) throw new Error("Intentando crear un canal sin una conexion")
const channel = this.connection?.createChannel({
if (this.connection == undefined) throw new Error("[RMQ] Intentando crear un canal sin una conexion")
const channel = this.connection.createChannel({
json: true,
setup: (channel: Channel) => {
if (this.buildStructure != undefined) this.buildStructure(channel)
// Exchanges comunes a todos
channel.assertExchange("sim.exchange", "topic", { durable: true })
channel.assertExchange("sim.dlx", "topic", { durable: true })
// Estructuras propias de cada servicio
if (this.buildStructure != undefined) {
this.buildStructure(channel)
} else {
console.warn("[i] Se ha creado un canal sin garantizar que exista la/s cola/s que se van a usar")
}
},
})
@@ -144,6 +151,5 @@ export class RabbitMQEventBus implements EventBus {
});
return channel;
}
}