From dd6315caf961e23e1283a029d90e6da2365665b9 Mon Sep 17 00:00:00 2001 From: Tsu-ba-me Date: Tue, 28 Feb 2023 16:40:49 -0500 Subject: [PATCH] fix(striker-ui-api): make getAnvilData generic to identify return value --- striker-ui-api/src/lib/accessModule.ts | 4 +-- .../fence/getFenceTemplate.ts | 2 +- .../host/createHostConnection.ts | 2 +- .../host/getHostConnection.ts | 8 +++--- striker-ui-api/src/types/AnvilDataStruct.d.ts | 3 --- striker-ui-api/src/types/DatabaseHash.d.ts | 10 ------- .../src/types/GetAnvilDataFunction.d.ts | 26 +++++++++++++++++++ .../src/types/GetAnvilDataOptions.d.ts | 3 --- 8 files changed, 35 insertions(+), 23 deletions(-) delete mode 100644 striker-ui-api/src/types/AnvilDataStruct.d.ts delete mode 100644 striker-ui-api/src/types/DatabaseHash.d.ts create mode 100644 striker-ui-api/src/types/GetAnvilDataFunction.d.ts delete mode 100644 striker-ui-api/src/types/GetAnvilDataOptions.d.ts diff --git a/striker-ui-api/src/lib/accessModule.ts b/striker-ui-api/src/lib/accessModule.ts index 94bed8fb..f1b02268 100644 --- a/striker-ui-api/src/lib/accessModule.ts +++ b/striker-ui-api/src/lib/accessModule.ts @@ -134,10 +134,10 @@ const dbWrite = (query: string, options?: SpawnSyncOptions) => { return execAnvilAccessModule(['--query', query, '--mode', 'write'], options); }; -const getAnvilData = ( +const getAnvilData = ( dataStruct: AnvilDataStruct, { predata, ...spawnSyncOptions }: GetAnvilDataOptions = {}, -) => +): HashType => execAnvilAccessModule( [ '--predata', diff --git a/striker-ui-api/src/lib/request_handlers/fence/getFenceTemplate.ts b/striker-ui-api/src/lib/request_handlers/fence/getFenceTemplate.ts index e285cc1a..beec0a0e 100644 --- a/striker-ui-api/src/lib/request_handlers/fence/getFenceTemplate.ts +++ b/striker-ui-api/src/lib/request_handlers/fence/getFenceTemplate.ts @@ -6,7 +6,7 @@ export const getFenceTemplate: RequestHandler = (request, response) => { let rawFenceData; try { - ({ fence_data: rawFenceData } = getAnvilData( + ({ fence_data: rawFenceData } = getAnvilData<{ fence_data: unknown }>( { fence_data: true }, { predata: [['Striker->get_fence_data']] }, )); diff --git a/striker-ui-api/src/lib/request_handlers/host/createHostConnection.ts b/striker-ui-api/src/lib/request_handlers/host/createHostConnection.ts index c688dc9c..38a66989 100644 --- a/striker-ui-api/src/lib/request_handlers/host/createHostConnection.ts +++ b/striker-ui-api/src/lib/request_handlers/host/createHostConnection.ts @@ -154,7 +154,7 @@ export const createHostConnection: RequestHandler< database: { [localHostUUID]: { port: rawLocalDBPort }, }, - } = getAnvilData({ database: true }) as { database: DatabaseHash }; + } = getAnvilData<{ database: AnvilDataDatabaseHash }>({ database: true }); localDBPort = sanitize(rawLocalDBPort, 'number'); } catch (subError) { diff --git a/striker-ui-api/src/lib/request_handlers/host/getHostConnection.ts b/striker-ui-api/src/lib/request_handlers/host/getHostConnection.ts index 8671ac32..cf5e5453 100644 --- a/striker-ui-api/src/lib/request_handlers/host/getHostConnection.ts +++ b/striker-ui-api/src/lib/request_handlers/host/getHostConnection.ts @@ -7,7 +7,7 @@ import { stdout } from '../../shell'; const buildHostConnections = ( fromHostUUID: string, - databaseHash: DatabaseHash, + databaseHash: AnvilDataDatabaseHash, { defaultPort = 5432, defaultUser = 'admin', @@ -42,7 +42,7 @@ export const getHostConnection = buildGetRequestHandler( (request, buildQueryOptions) => { const { hostUUIDs: rawHostUUIDs } = request.query; - let rawDatabaseData: DatabaseHash; + let rawDatabaseData: AnvilDataDatabaseHash; const hostUUIDField = 'ip_add.ip_address_host_uuid'; const localHostUUID: string = getLocalHostUUID(); @@ -59,7 +59,9 @@ export const getHostConnection = buildGetRequestHandler( stdout(`condHostUUIDs=[${condHostUUIDs}]`); try { - ({ database: rawDatabaseData } = getAnvilData({ database: true })); + ({ database: rawDatabaseData } = getAnvilData<{ database: AnvilDataDatabaseHash }>( + { database: true }, + )); } catch (subError) { throw new Error(`Failed to get anvil data; CAUSE: ${subError}`); } diff --git a/striker-ui-api/src/types/AnvilDataStruct.d.ts b/striker-ui-api/src/types/AnvilDataStruct.d.ts deleted file mode 100644 index 984875f9..00000000 --- a/striker-ui-api/src/types/AnvilDataStruct.d.ts +++ /dev/null @@ -1,3 +0,0 @@ -interface AnvilDataStruct { - [key: string]: AnvilDataStruct | boolean; -} diff --git a/striker-ui-api/src/types/DatabaseHash.d.ts b/striker-ui-api/src/types/DatabaseHash.d.ts deleted file mode 100644 index 1acbab2d..00000000 --- a/striker-ui-api/src/types/DatabaseHash.d.ts +++ /dev/null @@ -1,10 +0,0 @@ -type DatabaseHash = { - [hostUUID: string]: { - host: string; - name: string; - password: string; - ping: string; - port: string; - user: string; - }; -}; diff --git a/striker-ui-api/src/types/GetAnvilDataFunction.d.ts b/striker-ui-api/src/types/GetAnvilDataFunction.d.ts new file mode 100644 index 00000000..191337b3 --- /dev/null +++ b/striker-ui-api/src/types/GetAnvilDataFunction.d.ts @@ -0,0 +1,26 @@ +interface AnvilDataStruct { + [key: string]: AnvilDataStruct | boolean; +} + +type AnvilDataDatabaseHash = { + [hostUUID: string]: { + host: string; + name: string; + password: string; + ping: string; + port: string; + user: string; + }; +}; + +type AnvilDataUPSHash = { + [upsName: string]: { + agent: string; + brand: string; + description: string; + }; +}; + +type GetAnvilDataOptions = import('child_process').SpawnSyncOptions & { + predata?: Array<[string, ...unknown[]]>; +}; diff --git a/striker-ui-api/src/types/GetAnvilDataOptions.d.ts b/striker-ui-api/src/types/GetAnvilDataOptions.d.ts deleted file mode 100644 index baf39d7a..00000000 --- a/striker-ui-api/src/types/GetAnvilDataOptions.d.ts +++ /dev/null @@ -1,3 +0,0 @@ -type GetAnvilDataOptions = import('child_process').SpawnSyncOptions & { - predata?: Array<[string, ...unknown[]]>; -};