Skip to content

Commit

Permalink
fix(doc): add missing docs
Browse files Browse the repository at this point in the history
  • Loading branch information
con-cis committed Dec 27, 2024
1 parent 45ddb54 commit acfd60f
Show file tree
Hide file tree
Showing 26 changed files with 425 additions and 158 deletions.
76 changes: 41 additions & 35 deletions src/classes/DataHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,6 @@ import { ConfigData } from '../types/ConfigData'
*/
export class DataHandler {
private static instance: DataHandler
public dataObject: ConfigData

/**
* Private constructor to prevent direct instantiation.
* Initializes the data object with an initial value.
*/
private constructor() {
this.dataObject = { status: undefined }
}

/**
* Retrieves the singleton instance of the DataObjectSingleton class.
Expand All @@ -31,38 +22,20 @@ export class DataHandler {
return DataHandler.instance
}

public dataObject: ConfigData

/**
* Converts the data object to its JSON representation.
* @returns A JSON string representing the data object.
* Private constructor to prevent direct instantiation.
* Initializes the data object with an initial value.
*/
public toJSON(): string {
return JSON.stringify({
extractedData: this.dataObject.extractedData,
metadata: this.dataObject?.metadata
})
private constructor() {
this.dataObject = { status: undefined }
}

/**
* Creates a data object based on processed results.
* If processedResult is an error, returns an error status data object.
* Otherwise, returns a success status data object with extractedData and metadata.
* @param processedResult - The processed result to create the data object from.
* @returns void.
* Adds Annotations to a Channel
* @returns API Response of operation.
*/
public setDataObject(
processedResult: Error | { extractedData: ExtractedData; metadata: MetaData }
): void {
if (processedResult instanceof Error) {
this.dataObject = { status: ApiResponses.ERROR_RESOLVING_CONFIG }
} else {
this.dataObject = {
extractedData: processedResult.extractedData,
metadata: processedResult.metadata,
status: ApiResponses.RESOLVED_SUCCESSFULLY
}
}
}

public addAnnotation(data: { channelId: string; annotation: string }): ApiResponses {
const escapedAnnotation = escapeString(data.annotation)
let channelFound = false
Expand All @@ -83,6 +56,7 @@ export class DataHandler {
return ApiResponses.ERROR_RESOLVING_ANNOTATION
}
}

/**
* Sets data to an empty array
* @returns The singleton instance of DataObjectSingleton.
Expand All @@ -98,4 +72,36 @@ export class DataHandler {
return ApiResponses.ERROR_RESOLVING_ANNOTATION
}
}

/**
* Creates a data object based on processed results.
* If processedResult is an error, returns an error status data object.
* Otherwise, returns a success status data object with extractedData and metadata.
* @param processedResult - The processed result to create the data object from.
* @returns void.
*/
public setDataObject(
processedResult: Error | { extractedData: ExtractedData; metadata: MetaData }
): void {
if (processedResult instanceof Error) {
this.dataObject = { status: ApiResponses.ERROR_RESOLVING_CONFIG }
} else {
this.dataObject = {
extractedData: processedResult.extractedData,
metadata: processedResult.metadata,
status: ApiResponses.RESOLVED_SUCCESSFULLY
}
}
}

/**
* Converts the data object to its JSON representation.
* @returns A JSON string representing the data object.
*/
public toJSON(): string {
return JSON.stringify({
extractedData: this.dataObject.extractedData,
metadata: this.dataObject?.metadata
})
}
}
29 changes: 29 additions & 0 deletions src/classes/IpcHandler.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,60 @@
/**
* IpcHandler class manages IPC (Inter-Process Communication) between the main and renderer processes.
* It handles file operations and data management through registered event handlers.
*/
import { ipcMain } from 'electron'
import { openFile, saveFile } from '../main/modules/fileHandling/FileService'
import { DataHandler } from './DataHandler'

export class IpcHandler {
/** Instance of DataHandler for managing application data */
private dataHandler: DataHandler

/**
* Initializes the IpcHandler by getting DataHandler instance and registering IPC handlers
*/
constructor() {
this.dataHandler = DataHandler.getInstance()
this.registerHandlers()
}

/**
* Registers all IPC event handlers for the application
* @private
*/
private registerHandlers(): void {
/**
* Handles opening file dialog and loading file
*/
ipcMain.handle('open-file-dialog', async () => {
await openFile()
})

/**
* Handles saving file dialog and saving file
* @returns Promise with save operation result
*/
ipcMain.handle('save-file-dialog', async () => {
return await saveFile()
})

/**
* Handles setting annotations for channels
* @param _event - IPC event object
* @param data - Object containing channelId and annotation string
* @returns Promise with annotation operation result
*/
ipcMain.handle(
'set-annotation',
async (_event, data: { channelId: string; annotation: string }) => {
return this.dataHandler.addAnnotation(data)
}
)

/**
* Handles resetting all application data
* @returns Promise with reset operation result
*/
ipcMain.handle('reset-data', async () => {
return this.dataHandler.resetData()
})
Expand Down
15 changes: 13 additions & 2 deletions src/enums/ApiResponses.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
/**
* Enum containing standard API response messages
* @enum {string}
*
* @property {string} OPERATION_CANCELLED - Response when operation is cancelled
* @property {string} RESOLVED_SUCCESSFULLY - Response for successful resolution
* @property {string} ERROR_RESOLVING_CONFIG - Response when config file resolution fails
* @property {string} NO_DATA_RESOLVING_DATA - Response when no data is available to resolve
* @property {string} ERROR_RESOLVING_ANNOTATION - Response when annotation resolution fails
* @property {string} ERROR_RESETTING_DATA - Response when data reset operation fails
*/
enum ApiResponses {
OPERATION_CANCELLED = 'Operation cancelled',
RESOLVED_SUCCESSFULLY = 'Resolved successfully',
RESOLVED_SUCCESSFULLY = 'Resolved successfully',
ERROR_RESOLVING_CONFIG = 'Error while resolving config file',
NO_DATA_RESOLVING_DATA = 'No data to resolve',
ERROR_RESOLVING_ANNOTATION = 'Error while resolving annotation',
ERROR_RESETTING_DATA = 'Error while resetting data'
}

export default ApiResponses
export default ApiResponses
8 changes: 7 additions & 1 deletion src/enums/ConnectorType.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
/**
* Enum representing the types of connectors in the system
* @enum {string}
* @property {string} Source - Represents a source connector that data flows from
* @property {string} Destination - Represents a destination connector that data flows to
*/
enum ConnectorType {
Source = 'source',
Destination = 'destination'
}

export default ConnectorType
export default ConnectorType
15 changes: 14 additions & 1 deletion src/enums/FilteredProperties.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
/**
* Enum containing properties that should be filtered/masked in logs and outputs
* @enum {string}
* @property {string} Password - Password field
* @property {string} Username - Username field
* @property {string} PreprocessingScript - Script run before processing
* @property {string} PostprocessingScript - Script run after processing
* @property {string} DeployScript - Script used for deployment
* @property {string} UndeployScript - Script used for undeployment
* @property {string} GlobalScripts - Global scripts
* @property {string} KeyPW - Key password
* @property {string} KeyStorePW - Keystore password
*/
enum FilteredProperties {
Password = 'password',
Username = 'username',
Expand All @@ -11,4 +24,4 @@ enum FilteredProperties {
KeyStorePW = 'keyStorePW'
}

export default FilteredProperties
export default FilteredProperties
16 changes: 14 additions & 2 deletions src/main/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
/**
* @fileoverview Main electron application file that handles window creation, app lifecycle, and security settings
* @module main
* @requires electron
* @requires path
* @requires @electron-toolkit/utils
* @requires ./utils/Common
* @requires ./modules/fileHandling/FileService
*/
import { app, shell, BrowserWindow, Menu, session } from 'electron'
import { resolve, join } from 'path'
import { electronApp, optimizer, is } from '@electron-toolkit/utils'
import { configureMenu } from './utils/Common'
import { openFile, saveFile } from './modules/fileHandling/FileService'

/**
* Creates and configures the main application window
* @function createWindow
* @returns {void}
*/
function createWindow(): void {
// Create the browser window.
const mainWindow = new BrowserWindow({
Expand Down Expand Up @@ -42,8 +56,6 @@ function createWindow(): void {
}

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.whenReady().then(() => {
// Set app user model id for windows
electronApp.setAppUserModelId('com.electron')
Expand Down
16 changes: 16 additions & 0 deletions src/main/modules/fileHandling/FileDialogService.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
/**
* File dialog utility functions for Electron application
*/

import { BrowserWindow, dialog } from 'electron'

/**
* Opens a file selection dialog that allows choosing XML or JSON files
* @returns Promise that resolves to dialog result containing:
* - canceled: boolean indicating if dialog was canceled
* - filePaths: array of selected file paths, empty if canceled
*/
export function openFileDialog(): Promise<
Electron.OpenDialogReturnValue | { canceled: boolean; filePaths: [] }
> {
Expand All @@ -15,6 +25,12 @@ export function openFileDialog(): Promise<
}
}

/**
* Opens a file save dialog that allows saving JSON files
* @returns Promise that resolves to dialog result containing:
* - canceled: boolean indicating if dialog was canceled
* - filePath: selected save path, empty if canceled
*/
export function saveFileDialog(): Promise<
Electron.SaveDialogReturnValue | { canceled: boolean; filePath: [] }
> {
Expand Down
22 changes: 22 additions & 0 deletions src/main/modules/fileHandling/FileService.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/**
* @fileoverview Handles file operations including reading, processing and saving JSON/XML files
*/

import fs from 'fs/promises'
import { processJsonFile } from '../jsonProcessing/JsonProcessingService'
import { processXmlFile } from '../xmlProcessing/XmlProcessingService'
Expand All @@ -14,6 +18,11 @@ new IpcHandler()

const dataHandler = DataHandler.getInstance()

/**
* Reads and processes a file from the given file path
* @param filePath - Path to the file to be processed
* @returns Object containing extracted data and metadata, or Error if processing fails
*/
async function readFileAndProcess(filePath: string): Promise<
| {
extractedData: ExtractedData
Expand All @@ -38,6 +47,11 @@ async function readFileAndProcess(filePath: string): Promise<
}
}

/**
* Determines the type of file based on content
* @param content - String content of the file
* @returns 'JSON', 'XML' or 'unknown format'
*/
function getFileType(content: string): string {
let format: string
if (isJSON(content)) {
Expand All @@ -50,6 +64,10 @@ function getFileType(content: string): string {
return format
}

/**
* Opens a file dialog and processes the selected file
* @returns ApiResponse indicating the operation result
*/
export async function openFile(): Promise<ApiResponses> {
const mainWindow = BrowserWindow.getFocusedWindow()

Expand All @@ -75,6 +93,10 @@ export async function openFile(): Promise<ApiResponses> {
}
}

/**
* Opens a save file dialog and saves the current data
* @returns ApiResponse indicating the operation result
*/
export async function saveFile(): Promise<ApiResponses> {
try {
const result = await saveFileDialog()
Expand Down
7 changes: 7 additions & 0 deletions src/main/utils/Common.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { MenuItemConstructorOptions } from 'electron'

/**
* Configures the application menu structure
* @param appName - The name of the application to display in the menu
* @param openFileCallback - Callback function triggered when "Open File" is selected
* @param saveFileCallback - Callback function triggered when "Save File" is selected
* @returns Array of menu items that define the application's menu structure
*/
export function configureMenu(
appName: string,
openFileCallback: () => Promise<string>,
Expand Down
5 changes: 3 additions & 2 deletions src/main/utils/StringEscape.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/**
* Escapes special characters in a string to prevent injection attacks.
* @param str - The input string to escape.
* @returns The escaped string.
* @function escapeString
* @param {string} str - The input string to escape.
* @returns {string} The escaped string.
*/
export function escapeString(str: string): string {
return str
Expand Down
12 changes: 11 additions & 1 deletion src/main/utils/Validator.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* Checks if a string is valid JSON
* @param {string} content - The string to check
* @returns {boolean} True if the string is valid JSON, false otherwise
*/
export function isJSON(content: string): boolean {
try {
JSON.parse(content)
Expand All @@ -7,7 +12,12 @@ export function isJSON(content: string): boolean {
}
}

/**
* Checks if a string contains XML by looking for opening tags
* @param {string} content - The string to check
* @returns {boolean} True if the string appears to be XML, false otherwise
*/
export function isXML(content: string): boolean {
// Check for a common XML opening tag
return /<\s*\w+/.test(content)
}
}
Loading

0 comments on commit acfd60f

Please sign in to comment.