mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
* Change notification when extension is not found - Removes legacy downloadFile and downloadJson in favour of using `node-fetch` - A notification will be displayed if the URL provided results in a status of 404 Signed-off-by: Sebastian Malton <sebastian@malton.name> * Resolve PR comments Signed-off-by: Sebastian Malton <sebastian@malton.name> Signed-off-by: Sebastian Malton <sebastian@malton.name>
57 lines
1.5 KiB
TypeScript
57 lines
1.5 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 type { RequestInit, Response } from "node-fetch";
|
|
import type { AsyncResult } from "../utils/async-result";
|
|
import fetchInjectable from "./fetch.injectable";
|
|
|
|
export interface DownloadBinaryOptions {
|
|
signal?: AbortSignal | null | undefined;
|
|
}
|
|
|
|
export type DownloadBinary = (url: string, opts?: DownloadBinaryOptions) => Promise<AsyncResult<Buffer, string>>;
|
|
|
|
const downloadBinaryInjectable = getInjectable({
|
|
id: "download-binary",
|
|
instantiate: (di): DownloadBinary => {
|
|
const fetch = di.inject(fetchInjectable);
|
|
|
|
return async (url, opts) => {
|
|
let result: Response;
|
|
|
|
try {
|
|
// TODO: upgrade node-fetch once we switch to ESM
|
|
result = await fetch(url, opts as RequestInit);
|
|
} catch (error) {
|
|
return {
|
|
callWasSuccessful: false,
|
|
error: String(error),
|
|
};
|
|
}
|
|
|
|
if (result.status < 200 || 300 <= result.status) {
|
|
return {
|
|
callWasSuccessful: false,
|
|
error: result.statusText,
|
|
};
|
|
}
|
|
|
|
try {
|
|
return {
|
|
callWasSuccessful: true,
|
|
response: await result.buffer(),
|
|
};
|
|
} catch (error) {
|
|
return {
|
|
callWasSuccessful: false,
|
|
error: String(error),
|
|
};
|
|
}
|
|
};
|
|
},
|
|
});
|
|
|
|
export default downloadBinaryInjectable;
|