Add types in local-cache.ts
This commit is contained in:
1 parent
37d8222479
commit
dd907a67b0
3 files changed
+79
-31
No files matched your search
+32
-29
@@ -27,25 +27,34 @@ import * as logger from "./logger";
|
||||
import * as scheduling from "./scheduling";
|
||||
import * as expression from "./common/expression";
|
||||
import { parse } from "./common/expression-parser";
|
||||
import { Preset, Expression } from "./types";
|
||||
import {
|
||||
Preset,
|
||||
Expression,
|
||||
Provisions,
|
||||
VirtualParameters,
|
||||
Files,
|
||||
Users,
|
||||
Permissions,
|
||||
Config,
|
||||
UiConfig
|
||||
} from "./types";
|
||||
|
||||
interface Permissions {
|
||||
[role: string]: {
|
||||
[access: number]: {
|
||||
[resource: string]: {
|
||||
access: number;
|
||||
filter: Expression;
|
||||
validate?: Expression;
|
||||
};
|
||||
};
|
||||
};
|
||||
interface Snapshot {
|
||||
presets: Preset[];
|
||||
provisions: Provisions;
|
||||
virtualParameters: VirtualParameters;
|
||||
files: Files;
|
||||
permissions: Permissions;
|
||||
users: Users;
|
||||
config: Config;
|
||||
ui: UiConfig;
|
||||
}
|
||||
|
||||
const REFRESH = 3000;
|
||||
const EVICT_TIMEOUT = 60000;
|
||||
|
||||
const snapshots = new Map();
|
||||
let currentSnapshot = null;
|
||||
const snapshots = new Map<string, Snapshot>();
|
||||
let currentSnapshot: string = null;
|
||||
let nextRefresh = 1;
|
||||
|
||||
function computeHash(snapshot): string {
|
||||
@@ -244,7 +253,7 @@ async function fetchPresets(): Promise<Preset[]> {
|
||||
return presets;
|
||||
}
|
||||
|
||||
async function fetchProvisions(): Promise<{}> {
|
||||
async function fetchProvisions(): Promise<Provisions> {
|
||||
const res = await db.getProvisions();
|
||||
|
||||
const provisions = {};
|
||||
@@ -263,7 +272,7 @@ async function fetchProvisions(): Promise<{}> {
|
||||
return provisions;
|
||||
}
|
||||
|
||||
async function fetchVirtualParameters(): Promise<{}> {
|
||||
async function fetchVirtualParameters(): Promise<VirtualParameters> {
|
||||
const res = await db.getVirtualParameters();
|
||||
|
||||
const virtualParameters = {};
|
||||
@@ -301,7 +310,7 @@ async function fetchPermissions(): Promise<Permissions> {
|
||||
return permissions;
|
||||
}
|
||||
|
||||
async function fetchFiles(): Promise<{}> {
|
||||
async function fetchFiles(): Promise<Files> {
|
||||
const res = await db.getFiles();
|
||||
const files = {};
|
||||
|
||||
@@ -316,7 +325,7 @@ async function fetchFiles(): Promise<{}> {
|
||||
return files;
|
||||
}
|
||||
|
||||
async function fetchUsers(): Promise<{}> {
|
||||
async function fetchUsers(): Promise<Users> {
|
||||
const _users = await db.getUsers();
|
||||
const users = {};
|
||||
|
||||
@@ -331,7 +340,7 @@ async function fetchUsers(): Promise<{}> {
|
||||
return users;
|
||||
}
|
||||
|
||||
async function fetchConfig(): Promise<[{}, {}]> {
|
||||
async function fetchConfig(): Promise<[Config, UiConfig]> {
|
||||
const conf = await db.getConfig();
|
||||
|
||||
conf.sort((a, b) => (a.id > b.id ? 1 : a.id < b.id ? -1 : 0));
|
||||
@@ -436,25 +445,19 @@ export function getPresets(snapshotKey): Preset[] {
|
||||
return snapshot.presets;
|
||||
}
|
||||
|
||||
export function getProvisions(
|
||||
snapshotKey
|
||||
): { [name: string]: { md5: string; script: vm.Script } } {
|
||||
export function getProvisions(snapshotKey): Provisions {
|
||||
const snapshot = snapshots.get(snapshotKey);
|
||||
if (!snapshot) throw new Error("Cache snapshot does not exist");
|
||||
return snapshot.provisions;
|
||||
}
|
||||
|
||||
export function getVirtualParameters(
|
||||
snapshotKey
|
||||
): { [name: string]: { md5: string; script: vm.Script } } {
|
||||
export function getVirtualParameters(snapshotKey): VirtualParameters {
|
||||
const snapshot = snapshots.get(snapshotKey);
|
||||
if (!snapshot) throw new Error("Cache snapshot does not exist");
|
||||
return snapshot.virtualParameters;
|
||||
}
|
||||
|
||||
export function getFiles(
|
||||
snapshotKey
|
||||
): { [name: string]: { length: number; md5: string; contentType: string } } {
|
||||
export function getFiles(snapshotKey): Files {
|
||||
const snapshot = snapshots.get(snapshotKey);
|
||||
if (!snapshot) throw new Error("Cache snapshot does not exist");
|
||||
return snapshot.files;
|
||||
@@ -520,14 +523,14 @@ export function getUsers(snapshotKey): {} {
|
||||
return snapshot.users;
|
||||
}
|
||||
|
||||
export function getPermissions(snapshotKey): {} {
|
||||
export function getPermissions(snapshotKey): Permissions {
|
||||
const snapshot = snapshots.get(snapshotKey);
|
||||
if (!snapshot) throw new Error("Cache snapshot does not exist");
|
||||
|
||||
return snapshot.permissions;
|
||||
}
|
||||
|
||||
export function getUiConfig(snapshotKey): {} {
|
||||
export function getUiConfig(snapshotKey): UiConfig {
|
||||
const snapshot = snapshots.get(snapshotKey);
|
||||
if (!snapshot) throw new Error("Cache snapshot does not exist");
|
||||
|
||||
|
||||
@@ -22,6 +22,7 @@ import PathSet from "./common/path-set";
|
||||
import VersionedMap from "./versioned-map";
|
||||
import InstanceSet from "./instance-set";
|
||||
import { IncomingMessage, ServerResponse } from "http";
|
||||
import { Script } from "vm";
|
||||
|
||||
export type Expression = string | number | boolean | null | any[];
|
||||
|
||||
@@ -324,6 +325,34 @@ export interface Preset {
|
||||
provisions: string[][];
|
||||
}
|
||||
|
||||
export interface Provisions {
|
||||
[name: string]: { md5: string; script: Script };
|
||||
}
|
||||
|
||||
export interface VirtualParameters {
|
||||
[name: string]: { md5: string; script: Script };
|
||||
}
|
||||
|
||||
export interface Files {
|
||||
[name: string]: { length: number; md5: string; contentType: string };
|
||||
}
|
||||
|
||||
export interface Users {
|
||||
[name: string]: { password: string; salt: string; roles: string[] };
|
||||
}
|
||||
|
||||
export interface Permissions {
|
||||
[role: string]: {
|
||||
[access: number]: {
|
||||
[resource: string]: {
|
||||
access: number;
|
||||
filter: Expression;
|
||||
validate?: Expression;
|
||||
};
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
export type PermissionSet = {
|
||||
[resource: string]: {
|
||||
access: number;
|
||||
@@ -332,6 +361,21 @@ export type PermissionSet = {
|
||||
};
|
||||
}[];
|
||||
|
||||
export interface Config {
|
||||
[name: string]: Expression;
|
||||
}
|
||||
|
||||
export interface UiConfig {
|
||||
filters: {};
|
||||
device: {};
|
||||
index: {};
|
||||
overview: {
|
||||
charts?: {};
|
||||
groups?: {};
|
||||
};
|
||||
pageSize?: Expression;
|
||||
}
|
||||
|
||||
export interface SoapMessage {
|
||||
id: string;
|
||||
cwmpVersion: string;
|
||||
|
||||
@@ -37,13 +37,14 @@ import * as usersPage from "./users-page";
|
||||
import Authorizer from "../lib//common/authorizer";
|
||||
import * as notifications from "./notifications";
|
||||
import { contextifyComponent } from "./components";
|
||||
import { PermissionSet, UiConfig } from "../lib/types";
|
||||
|
||||
declare global {
|
||||
interface Window {
|
||||
authorizer: Authorizer;
|
||||
permissionSets: any;
|
||||
permissionSets: PermissionSet[];
|
||||
username: string;
|
||||
clientConfig: any;
|
||||
clientConfig: { ui: UiConfig };
|
||||
configSnapshot: string;
|
||||
genieacsVersion: string;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user