Estoy tratando de hacer una función de utilidad para las solicitudes de Axios.
Empecé creando un alias de AxiosConfig en global.d.ts :
import { Method } from 'axios'; export {}; declare global { type AxiosConfig = {url: string, method: Method, data: object}; } Luego, en mi utils.ts creé la siguiente función:
import axios, { AxiosResponse } from 'axios'; import * as https from 'https'; export async function axiosReq(options: AxiosConfig): Promise<AxiosResponse<unknown, any>> { try { const data = await axios({ url: options.url, method: options.method, data: options.data, httpsAgent: new https.Agent({ rejectUnauthorized: false }) }); return data; } catch (error) { console.log(error) throw new Error('Failed to fetch videos...') } }Finalmente, usando la función:
import { axiosReq } from "../utils"; const AxiosConfig = { url: 'https://172.24.14.12/api/baseline', method: 'POST', data: {} }; const res = await axiosReq(AxiosConfig); El nodo falla al principio (incluso antes de usar axiosReq ). Solo cuando comento const res = await axiosReq(AxiosConfig) inicia sin fallar. No recibo errores informativos.
¿Qué estoy haciendo mal?
Está tratando a AxiosConfig como un valor o variable cuando en realidad es solo un tipo. Probar
const postBaseline: AxiosConfig = {...luego reemplace AxiosConfig en la llamada res con postBaseline