Skip to content
代码片段 群组 项目
未验证 提交 66ceea4f 编辑于 作者: João Paquim's avatar João Paquim 提交者: GitHub
浏览文件

Use AbortController polyfill even when global fetch is available (#34882)

上级 5a046367
No related branches found
No related tags found
无相关合并请求
...@@ -32,11 +32,17 @@ export class FetchHttpClient extends HttpClient { ...@@ -32,11 +32,17 @@ export class FetchHttpClient extends HttpClient {
// node-fetch doesn't have a nice API for getting and setting cookies // node-fetch doesn't have a nice API for getting and setting cookies
// fetch-cookie will wrap a fetch implementation with a default CookieJar or a provided one // fetch-cookie will wrap a fetch implementation with a default CookieJar or a provided one
this._fetchType = requireFunc("fetch-cookie")(this._fetchType, this._jar); this._fetchType = requireFunc("fetch-cookie")(this._fetchType, this._jar);
} else {
this._fetchType = fetch.bind(globalThis);
}
if (typeof AbortController === "undefined") {
// In order to ignore the dynamic require in webpack builds we need to do this magic
// @ts-ignore: TS doesn't know about these names
const requireFunc = typeof __webpack_require__ === "function" ? __non_webpack_require__ : require;
// Node needs EventListener methods on AbortController which our custom polyfill doesn't provide // Node needs EventListener methods on AbortController which our custom polyfill doesn't provide
this._abortControllerType = requireFunc("abort-controller"); this._abortControllerType = requireFunc("abort-controller");
} else { } else {
this._fetchType = fetch.bind(self);
this._abortControllerType = AbortController; this._abortControllerType = AbortController;
} }
} }
......
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
import { FetchHttpClient } from "../src/FetchHttpClient";
import { NullLogger } from "../src/Loggers";
describe("FetchHttpClient", () => {
it("works if global fetch is available but AbortController is not", async () => {
(global.fetch as any) = () => {
throw new Error("error from test");
};
const httpClient = new FetchHttpClient(NullLogger.instance);
try {
await httpClient.post("/");
} catch (e) {
expect(e).toEqual(new Error("error from test"));
}
});
});
0% 加载中 .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册