import type { AxiosRequestConfig } from 'axios'; import type { Stream } from 'stream'; import type { ThreekitAxiosContext } from '../operators/HttpContext.js'; import { stringifyValues } from './stringyValues.js'; export const request = async ( context: ThreekitAxiosContext, requestProps: AxiosRequestConfig ) => { const url = [context.urlPrefix, requestProps.url] .filter((value) => value && value.length > 0) .join('/'); const stringifyProps = stringifyValues(requestProps.params); const fullProps = { ...requestProps, params: stringifyProps, url }; // do this before the promise wait below, so we can get the stack trace const errorForStack = new Error(); // if this is on node, we need to wait for the next tick // to ensure the context is available // details: https://stackoverflow.com/questions/69169492/async-external-function-leaves-open-handles-jest-supertest-express if (typeof window === 'undefined') { await new Promise((resolve) => process.nextTick(resolve)); } // even though it const result = context.axios.request(fullProps); result.then((response) => { if (response.status === 200) { if (requestProps.responseType === 'stream') { const stream = response.data as Stream; // start timer and if the stream hasn't completed in 60 seconds, warn the user const timeoutSeconds = 60; const timer = setTimeout(() => { console.warn( `WARNING: Long request in progress, stream has not completed in ${timeoutSeconds} seconds from start of request.`, fullProps, errorForStack.stack ); }, timeoutSeconds * 1000); stream.on('end', () => { clearTimeout(timer); }); } } return response; }); return result; };