1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00
lens/src/common/fetch/lens-fetch.injectable.ts
Sebastian Malton afe214b71f Remove last usages of request in our code
Signed-off-by: Sebastian Malton <sebastian@malton.name>
2023-01-11 16:25:08 -05:00

38 lines
1.3 KiB
TypeScript

/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable } from "@ogre-tools/injectable";
import { Agent } from "https";
import type { RequestInit, Response } from "node-fetch";
import lensProxyPortInjectable from "../../main/lens-proxy/lens-proxy-port.injectable";
import lensProxyCertificateInjectable from "../certificate/lens-proxy-certificate.injectable";
import nodeFetchModuleInjectable from "./fetch-module.injectable";
export type LensRequestInit = Omit<RequestInit, "agent">;
export type LensFetch = (pathnameAndQuery: string, init?: LensRequestInit) => Promise<Response>;
const lensFetchInjectable = getInjectable({
id: "lens-fetch",
instantiate: (di): LensFetch => {
const { default: fetch } = di.inject(nodeFetchModuleInjectable);
const lensProxyPort = di.inject(lensProxyPortInjectable);
const lensProxyCertificate = di.inject(lensProxyCertificateInjectable);
return async (pathnameAndQuery, init = {}) => {
const agent = new Agent({
cert: lensProxyCertificate.get().cert,
});
return fetch(`https://127.0.0.1:${lensProxyPort.get()}${pathnameAndQuery}`, {
...init,
agent,
});
};
},
causesSideEffects: true,
});
export default lensFetchInjectable;