1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Introduce BundledExtensionParser

Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com>
This commit is contained in:
Alex Andreev 2022-02-17 12:03:30 +03:00
parent 6d88442311
commit 0ac5571946
2 changed files with 109 additions and 0 deletions

View File

@ -0,0 +1,52 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { BundledExtensionParser } from "../bundled-extension-parser";
import fetchMock from "fetch-mock";
fetchMock.config.overwriteRoutes = true;
describe("BundledExtensionParser", () => {
afterAll(() => {
fetchMock.reset();
});
it("Should return empty arrays if no url passed", async () => {
const lists = await new BundledExtensionParser("5.4.0-latest12345", "").getExtensionLists();
expect(lists).toEqual({
release: [],
available: [],
});
});
it("Should return empty releases array if no release json found", async () => {
fetchMock
.get("http://my-example-url.com/versions.json", [ { "node-menu": "0.0.1" } ] )
.get("http://my-example-url.com/5.4.0-latest12345.json", 408 );
const lists = await new BundledExtensionParser("5.4.0-latest12345", "http://my-example-url.com").getExtensionLists();
expect(lists).toEqual({
release: [],
available: [{ "node-menu": "0.0.1" }],
});
});
it("Should return empty available array if no versions json found", async () => {
fetchMock
.get("http://my-example-url.com/versions.json", 408 )
.get("http://my-example-url.com/5.4.0-latest12345.json", [ { "node-menu": "0.0.1" } ]);
const lists = await new BundledExtensionParser("5.4.0-latest12345", "http://my-example-url.com").getExtensionLists();
expect(lists).toEqual({
release: [{ "node-menu": "0.0.1" }],
available: [],
});
});
});

View File

@ -0,0 +1,57 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import logger from "../logger";
type Extensions = Record<string, string>[];
interface ExtensionList {
release: Extensions,
available: Extensions
}
export class BundledExtensionParser {
constructor(private lensVersion: string, private url: string) {
}
get releaseJsonUrl() {
return `${this.url}/${this.lensVersion}.json`;
}
get availableJsonUrl() {
return `${this.url}/versions.json`;
}
fetchJsonList(path: string): Promise<Extensions> {
return fetch(path, { method: "GET" }).then(response => {
if (response.ok) {
return response.json();
}
return [];
}).catch(error => {
logger.error(`[EXTENSION-PARSER]: Failed to download and parse extension list: ${error}`);
return [];
});
}
public async getExtensionLists(): Promise<ExtensionList> {
if (!this.url) {
return {
release: [],
available: [],
};
}
const release = await this.fetchJsonList(this.releaseJsonUrl);
const available = await this.fetchJsonList(this.availableJsonUrl);
return {
release,
available,
};
}
}