Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

176
Visualizações
Typescript utility function to make Axios requests

I'm trying to make a utility function for Axios requests.

I started by making an alias of AxiosConfig at global.d.ts:

import { Method } from 'axios';
export {};

declare global { 
    type AxiosConfig = {url: string, method: Method, data: object};
}

Then, at my utils.ts I created the following function:

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...')
  }
}

Finally, using the function:

import { axiosReq } from "../utils";

const AxiosConfig = { 
    url: 'https://172.24.14.12/api/baseline', 
    method: 'POST',
    data: {}
    };
const res = await axiosReq(AxiosConfig);

The node crashes at the start (even before using axiosReq). Only when I comment out const res = await axiosReq(AxiosConfig) node starts without crashing. I don't receive any informative errors.

What am I doing wrong?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You're treating AxiosConfig as a value or variable when it's really just a type. Try

const postBaseline: AxiosConfig = {...

then replace AxiosConfig in the res call with postBaseline

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda