0

I'm using hey-api's Fetch API client and have been trying to set a timeout time on requests, but I'm running into issues and can't seem to figure it out. Has anyone managed to get this working, or could you point me in the right direction? I've also posted this question in the project's GitHub discussions.

I'm following hey-api's documentation on interceptors: https://heyapi.dev/openapi-ts/clients/fetch#interceptors

const abortControllers = new Map<string, AbortController>();
const createAbortKey = (request: { method: string; url: string }) => {
  return `${request.method}:${request.url}`;
};

client.interceptors.request.use((request) => {
  const controller = new AbortController();
  const signal = controller.signal;
  abortControllers.set(createAbortKey(request), controller);


  setTimeout(() => {
    controller.abort();
    console.error("Request timed out");
  }, 15000);

  request.signal = signal; // gives error "Cannot set property signal of #<Request> which has only a getter"
  return request;
  // return { ...request, signal }; // also doesn't work, this causes the requests to look like http://localhost:3000/[object%20Object]
});

client.interceptors.response.use((response, request) => {
  // clean up when we get a response, could probably improved to also remove the setTimeout
  abortControllers.delete(createAbortKey(request));
  return response;
});

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.