Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

175
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!