-
Notifications
You must be signed in to change notification settings - Fork 565
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
feat: New Interceptor API #3919
Comments
I can see what are you suggesting, but what's the problem they are aiming to solve besides being less noisy by reducing the amount of hooks? I can see some complications here due to the design of the The The biggest reason to use the |
I suggest not completely abandoning the current implementation of interceptors on cookies, since the internal functions of the library can be strictly tied to them, but adding a new type of interceptors that will be much more convenient to use to create interceptors, without creating a bunch of unnecessary code to solve a simple task. For example, I need to embed a cookie Jar in requests without an additional wrapper over the request, on new interceptors it would look something like this: import { CookieJar, GetCookiesOptions, SetCookieOptions } from 'tough-cookie';
const kCookieJar = Symbol('cookie jar');
function createCookieJarInterceptor(
options: { set?: SetCookieOptions; get?: GetCookiesOptions } = {}
): Interceptor {
return next => async request => {
const cookieJar = request.jar as CookieJar;
if (request[kCookieJar] === true || !(cookieJar)) return next(request);
request[kCookieJar] = true;
const requestURL = new URL(request.path, request.origin).toString();
request.headers.set(
'cookie',
cookieJar.getCookieStringSync(requestURL, options.get)
);
const resp = await next(request);
const setCookie = resp.headers.getSetCookie();
if (setCookie.length > 0) {
setCookie.forEach(cookieString =>
cookieJar.setCookieSync(cookieString, requestURL, options.set)
);
}
return resp;
};
} And on the version with interceptors on hooks, I would have to create handlers into which I would need to throw data and so on, and the code on hooks for interceptors turns out to be very complex, most likely even worse for performance than just transferring data along the chain between layers. For a new type of interceptors, I also suggest immediately normalizing requests and responses, namely request headers, which can be like an object/array/generator in Headers. Checking and making all the data look right at each step of the interceptor/core undici is not what we need, we would like all the data to be normalized and reduced to the promised type when creating a query |
We could also do asynchronous hooks like it's done in got. |
Yeah, I said earlier; see your point of how cluttered can it be when aiming to have a small interceptor.
Nevertheless, I've some concerns about the proposal:
|
The current dispatcher API has been designed from the ground up to be performant and minimize overhead, it's a low level API and it makes sense to put performance as its focus. I'm not really convinced we need a high level interceptor API to wrap things in this way. |
How about making a more convenient implementation of the interceptors?
In almost 99% of cases, we need the request/response object at once, and not in separate hooks.
I don't think we'll get a performance drawdown when adding such an api.
The text was updated successfully, but these errors were encountered: