Remove usage of legacy global .getInstance

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton committed 2022-12-06 09:18:59 -05:00
1 parent 303a397209
commit bd47377dd1
13 files changed
+79 -85

No files matched your search

+16 -1
View File
@@ -3,7 +3,9 @@
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable } from "@ogre-tools/injectable";
import { HttpsProxyAgent } from "hpagent";
import type * as FetchModule from "node-fetch";
import userStoreInjectable from "../user-store/user-store.injectable";
const { NodeFetch: { default: fetch }} = require("../../../build/webpack/node-fetch.bundle") as { NodeFetch: typeof FetchModule };
@@ -14,7 +16,20 @@ export type Fetch = (url: string, init?: RequestInit) => Promise<Response>;
const fetchInjectable = getInjectable({
id: "fetch",
instantiate: (): Fetch => fetch,
instantiate: (di): Fetch => {
const { httpsProxy, allowUntrustedCAs } = di.inject(userStoreInjectable);
const agent = httpsProxy
? new HttpsProxyAgent({
proxy: httpsProxy,
rejectUnauthorized: !allowUntrustedCAs,
})
: undefined;
return (url, init = {}) => fetch(url, {
agent,
...init,
});
},
causesSideEffects: true,
});
-34
View File
@@ -1,34 +0,0 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import request from "request";
import requestPromise from "request-promise-native";
import { UserStore } from "./user-store";
// todo: get rid of "request" (deprecated)
// https://github.com/lensapp/lens/issues/459
function getDefaultRequestOpts(): Partial<request.Options> {
const { httpsProxy, allowUntrustedCAs } = UserStore.getInstance();
return {
proxy: httpsProxy || undefined,
rejectUnauthorized: !allowUntrustedCAs,
};
}
/**
* @deprecated
*/
export function customRequest(opts: request.Options) {
return request.defaults(getDefaultRequestOpts())(opts);
}
/**
* @deprecated
*/
export function customRequestPromise(opts: requestPromise.Options) {
return requestPromise.defaults(getDefaultRequestOpts())(opts);
}