Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cancel request before actual request #1012

Closed
1 task done
Couto opened this issue Jan 4, 2020 · 8 comments
Closed
1 task done

Cancel request before actual request #1012

Couto opened this issue Jan 4, 2020 · 8 comments
Labels
question The issue is a question about Got

Comments

@Couto
Copy link

Couto commented Jan 4, 2020

What would you like to discuss?

I wasn't able to find this on the docs, nor can I find a way using the hooks features. I might be missing something.

I would like to cancel a request before it leaves my machine.

Example (this is not working, it might just better illustrate what I want to do):

import got, {
  BeforeRequestHook,
  AfterResponseHook,
  BeforeErrorHook,
  Response,
} from "got";

export default <R>(response: Response<R>) => {
  const beforeRequest: BeforeRequestHook = () => {
    throw new Error("Canceled");
  };

  const afterRequest: AfterResponseHook = () => {
    return response;
  };

  const beforeError: BeforeErrorHook = _err => {
    return response;
  };

  return got.extend({
    hooks: {
      beforeRequest: [beforeRequest],
      afterResponse: [afterRequest],
      beforeError: [beforeError],
    },
  });
};

My idea is to provide some predefined responses in some specific situations.
Any idea on how to do this?

Thanks

PS: My objective is to mock requests so it would be ideal to return the instance itself instead of using .cancel()

Checklist

  • I have read the documentation.
@szmarczak
Copy link
Collaborator

Example (this is not working, it might just better illustrate what I want to do):

Actually this works. Throwing in a hook cancels the request.

My idea is to provide some predefined responses in some specific situations.

You can use afterResponse hook to modify the response. To provide predefined responses, you may want to set custom request option.

@Couto
Copy link
Author

Couto commented Jan 5, 2020

Actually this works. Throwing in a hook cancels the request.

It does in practice, but it will call the beforeError which only allows me to return an Error. So I have to .catch requests instead of just using a simple .then.

So with the goal of mocking requests, not so much.

I totally missed the request option, yeah that could actually work, will take a look.

Thank you!

@Couto Couto closed this as completed Jan 5, 2020
@szmarczak szmarczak reopened this Jan 6, 2020
@szmarczak
Copy link
Collaborator

@sindresorhus Should it be possible to return a response from a beforeError hook?

@szmarczak szmarczak added the question The issue is a question about Got label Jan 6, 2020
@sindresorhus
Copy link
Owner

@szmarczak Wouldn't it be better to allow returning a new different request from beforeRequest?

@szmarczak
Copy link
Collaborator

szmarczak commented Jan 19, 2020

@sindresorhus You can already modify options in a beforeRequest hook. Alternatively you can pass custom request function.

Or we can allow returning IncomingMessage directly from the request function. That would be very useful e.g. when using cache (the request function would be async so await is needed to catch the ClientRequest/IncomingMessage instance). That way people could do something like this:

const request = (url, options) => new Promise((resolve, reject) => {
    const request = http.request(url, options);
    request.once('error', () => {
        resolve(createIncomingMessage());
    });

    request.once('socket', () => resolve(request));
});

await got(url, {request});

@sindresorhus
Copy link
Owner

You can already modify options in a beforeRequest hook. Alternatively you can pass custom request function.

I'm not sure how modifying options helps with canceling the request? But another use-case for being able to return IncomingMessage in beforeRequest is if you already have it cached yourself, or for prototyping when you have a static response.

Or we can allow returning IncomingMessage directly from the request function.

I wouldn't really find that useful as I don't want to override request and implement all the low-level request details myself.

@szmarczak
Copy link
Collaborator

But another use-case for being able to return IncomingMessage in beforeRequest is if you already have it cached yourself, or for prototyping when you have a static response.

#1051 allows to return an IncomingMessage (ResponseLike) instance using the request option. It's to make the HTTP-based caching mechanism easier to code. (I've already wrapped cacheable-request)

I'll make beforeRequest accept IncomingMessages too.

@sindresorhus
Copy link
Owner

Great 👌

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question The issue is a question about Got
Projects
None yet
Development

No branches or pull requests

3 participants