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?
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