Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split widgets #43

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 47 additions & 17 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"start": "node ./dist/server.js",
"test": "jest",
"test-cov": "jest --coverage",
"test-file": "jest ./test/utils/parsing-utils.test.ts",
"test-file": "jest ./test/clients/console-client/local.test.ts",
"view-test-cov": "jest --coverage || true && open coverage/lcov-report/index.html"
},
"devDependencies": {
Expand All @@ -37,6 +37,7 @@
"@types/cors": "^2.8.13",
"@types/express": "^4.17.16",
"@types/http-errors": "^2.0.1",
"@types/http-status-codes": "^1.2.0",
"@types/jest": "^29.4.0",
"@types/js-yaml": "^4.0.5",
"@types/lodash.camelcase": "^4.3.7",
Expand Down Expand Up @@ -69,8 +70,8 @@
"@aws-sdk/client-api-gateway": "^3.267.0",
"@aws-sdk/client-s3": "^3.288.0",
"@aws-sdk/credential-providers": "^3.282.0",
"@tinystacks/ops-core": "^0.3.2",
"@tinystacks/ops-model": "^0.4.0",
"@tinystacks/ops-core": "file:../ops-core/tinystacks-ops-core-0.3.2.tgz",
"@tinystacks/ops-model": "file:../ops-model/tinystacks-ops-model-0.4.1.tgz",
"@types/react": "^18.0.28",
"body-parser": "^1.20.1",
"cached": "^6.1.0",
Expand All @@ -80,6 +81,7 @@
"express": "^4.18.2",
"express-openapi": "^12.1.0",
"http-errors": "^2.0.0",
"http-status-codes": "^2.2.0",
"js-yaml": "^4.1.0",
"json-refs": "^3.0.15",
"lodash.camelcase": "^4.3.0",
Expand Down
10 changes: 5 additions & 5 deletions src/clients/console-client/i-console-client.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { ConsoleParser } from '@tinystacks/ops-core';
import { Console } from '@tinystacks/ops-core';

interface IConsoleClient {
getConsoles (): Promise<ConsoleParser[]>;
getConsole (consoleName: string): Promise<ConsoleParser>;
saveConsole (consoleName: string, console: ConsoleParser): Promise<ConsoleParser>;
deleteConsole (consoleName: string): Promise<ConsoleParser>;
getConsoles (): Promise<Console[]>;
getConsole (consoleName: string): Promise<Console>;
saveConsole (consoleName: string, console: Console): Promise<Console>;
deleteConsole (consoleName: string): Promise<Console>;
}

export {
Expand Down
13 changes: 5 additions & 8 deletions src/clients/console-client/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
import { ConsoleParser } from '@tinystacks/ops-core';
import { Console } from '@tinystacks/ops-core';
import IConsoleClient from './i-console-client.js';
import { LocalConsoleClient } from './local.js';
import { S3ConsoleClient } from './s3.js';

/**
* TODO: Eventually this becomes a proxy class which based on the environment returns a specific client i.e. local vs github vs s3 etc.
*/
class ConsoleClient implements IConsoleClient {
client: IConsoleClient;
getConsoles: () => Promise<ConsoleParser[]>;
getConsole: (consoleName: string) => Promise<ConsoleParser>;
saveConsole: (consoleName: string, console: ConsoleParser) => Promise<ConsoleParser>;
deleteConsole: (_consoleName: string) => Promise<ConsoleParser>;
getConsoles: () => Promise<Console[]>;
getConsole: (consoleName: string) => Promise<Console>;
saveConsole: (consoleName: string, console: Console) => Promise<Console>;
deleteConsole: (_consoleName: string) => Promise<Console>;

constructor () {
const configPath = process.env.CONFIG_PATH;
Expand Down
24 changes: 12 additions & 12 deletions src/clients/console-client/local.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import yaml from 'js-yaml';
import isNil from 'lodash.isnil';
import { ConsoleParser } from '@tinystacks/ops-core';
import { Console as ConsoleType, YamlConsole } from '@tinystacks/ops-model';
import { Console } from '@tinystacks/ops-core';
import { Config, Console as ConsoleType, YamlConsole } from '@tinystacks/ops-model';
import HttpError from 'http-errors';
import {
writeFileSync
Expand All @@ -11,37 +10,38 @@ import {
} from 'path';
import FsUtils from '../../utils/fs-utils.js';
import IConsoleClient from './i-console-client.js';
import Yaml from '../../utils/yaml.js';

class LocalConsoleClient implements IConsoleClient {
async getConsole (_consoleName?: string): Promise<ConsoleParser> {
async getConsole (_consoleName?: string): Promise<Console> {
const configPath = process.env.CONFIG_PATH;
if (configPath) {
const configFilePath = resolvePath(configPath);
// console.debug('configFilePath: ', configFilePath);
const configFile = FsUtils.tryToReadFile(configFilePath);
if (!configFile) throw HttpError.NotFound(`Cannot fetch console! Config file ${configPath} not found!`);
const configJson = (yaml.load(configFile.toString()) as any)?.Console as YamlConsole;
const configJson = Yaml.parseAs<Config>(configFile.toString());
// console.debug('configJson: ', JSON.stringify(configJson));
if (!isNil(configJson)) {
const consoleType: ConsoleType = ConsoleParser.parse(configJson);
return ConsoleParser.fromJson(consoleType);
if (!isNil(configJson?.Console)) {
const consoleType: ConsoleType = Console.parse(configJson.Console as YamlConsole);
return Console.fromJson(consoleType);
}
throw HttpError.InternalServerError('Cannot fetch console! The contents of the config file was empty or invalid!');
}
throw HttpError.InternalServerError('Cannot fetch console! No value was found for CONFIG_PATH!');
}
async getConsoles (): Promise<ConsoleParser[]> {
async getConsoles (): Promise<Console[]> {
const consoles = [];
const console = await this.getConsole();
if (console) consoles.push(console);
return consoles;
}
async saveConsole (consoleName: string, console: ConsoleParser): Promise<ConsoleParser> {
async saveConsole (consoleName: string, console: Console): Promise<Console> {
console.name = consoleName;
const previousConsole = await this.getConsole(consoleName);
console.providers = previousConsole.providers;
const yamlConsole = await console.toYaml();
const consoleYml = yaml.dump({ Console: yamlConsole });
const consoleYml = Yaml.stringify({ Console: yamlConsole });
const configPath = process.env.CONFIG_PATH;
if (isNil(configPath)) throw HttpError.InternalServerError(`Cannot save console ${console.name}! No value was found for CONFIG_PATH!`);
try {
Expand All @@ -53,7 +53,7 @@ class LocalConsoleClient implements IConsoleClient {
throw error;
}
}
async deleteConsole (consoleName: string): Promise<ConsoleParser> {
async deleteConsole (consoleName: string): Promise<Console> {
const configPath = process.env.CONFIG_PATH;
if (isNil(configPath)) throw HttpError.InternalServerError(`Cannot delete console ${consoleName}! No value was found for CONFIG_PATH!`);
try {
Expand Down
24 changes: 12 additions & 12 deletions src/clients/console-client/s3.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import yaml from 'js-yaml';
import isNil from 'lodash.isnil';
import { ConsoleParser } from '@tinystacks/ops-core';
import { Console as ConsoleType, YamlConsole } from '@tinystacks/ops-model';
import { Console } from '@tinystacks/ops-core';
import { Config, Console as ConsoleType, YamlConsole } from '@tinystacks/ops-model';
import HttpError from 'http-errors';
import {
mkdirSync,
Expand All @@ -12,6 +11,7 @@ import { S3 } from '@aws-sdk/client-s3';
import FsUtils from '../../utils/fs-utils.js';
import IConsoleClient from './i-console-client.js';
import { TMP_DIR } from '../../constants.js';
import Yaml from '../../utils/yaml.js';

type S3Info = {
bucketName: string;
Expand Down Expand Up @@ -167,7 +167,7 @@ class S3ConsoleClient implements IConsoleClient {
}
}

async getConsole (_consoleName?: string): Promise<ConsoleParser> {
async getConsole (_consoleName?: string): Promise<Console> {
const configPath = process.env.CONFIG_PATH;
if (configPath) {
/**
Expand All @@ -177,30 +177,30 @@ class S3ConsoleClient implements IConsoleClient {
*/
const configFile = await this.getConfig();
if (!configFile) throw HttpError.NotFound(`Cannot fetch console! Config file ${configPath} not found!`);
const configJson = (yaml.load(configFile.toString()) as any)?.Console as YamlConsole;
const configJson = Yaml.parseAs<Config>(configFile.toString());
// console.debug('configJson: ', JSON.stringify(configJson));
if (!isNil(configJson)) {
const consoleType: ConsoleType = ConsoleParser.parse(configJson);
return ConsoleParser.fromJson(consoleType);
if (!isNil(configJson?.Console)) {
const consoleType: ConsoleType = Console.parse(configJson?.Console as YamlConsole);
return Console.fromJson(consoleType);
}
throw HttpError.InternalServerError('Cannot fetch console! The contents of the config file was empty or invalid!');
}
throw HttpError.InternalServerError('Cannot fetch console! No value was found for CONFIG_PATH!');
}

async getConsoles (): Promise<ConsoleParser[]> {
async getConsoles (): Promise<Console[]> {
const consoles = [];
const console = await this.getConsole();
if (console) consoles.push(console);
return consoles;
}

async saveConsole (consoleName: string, console: ConsoleParser): Promise<ConsoleParser> {
async saveConsole (consoleName: string, console: Console): Promise<Console> {
console.name = consoleName;
const previousConsole = await this.getConsole(consoleName);
console.providers = previousConsole.providers;
const yamlConsole = await console.toYaml();
const consoleYml = yaml.dump({ Console: yamlConsole });
const consoleYml = Yaml.stringify({ Console: yamlConsole });
const configPath = process.env.CONFIG_PATH;
if (isNil(configPath)) throw HttpError.InternalServerError(`Cannot save console ${console.name}! No value was found for CONFIG_PATH!`);
try {
Expand All @@ -213,7 +213,7 @@ class S3ConsoleClient implements IConsoleClient {
}
}

async deleteConsole (consoleName: string): Promise<ConsoleParser> {
async deleteConsole (consoleName: string): Promise<Console> {
const configPath = process.env.CONFIG_PATH;
if (isNil(configPath)) throw HttpError.InternalServerError(`Cannot delete console ${consoleName}! No value was found for CONFIG_PATH!`);
try {
Expand Down
Loading