Refactor IPC handlers to enforce consistent return types and enhance type definitions

- Update return types for IPC handlers in `character.ipc.ts`, `chapter.ipc.ts`, and `location.ipc.ts` for stricter type safety.
- Introduce new type definitions for better structure and clarity.
- Remove redundant console logs and unnecessary comments for cleaner code.
This commit is contained in:
natreex
2025-11-18 23:01:52 -05:00
parent b56f45d1ba
commit 71d13e2b12
4 changed files with 24 additions and 20 deletions

View File

@@ -2,6 +2,7 @@ import { ipcMain } from 'electron';
import { createHandler } from '../database/LocalSystem.js'; import { createHandler } from '../database/LocalSystem.js';
import Book from '../database/models/Book.js'; import Book from '../database/models/Book.js';
import type { BookProps, GuideLine, GuideLineAI, Act, Issue, WorldProps } from '../database/models/Book.js'; import type { BookProps, GuideLine, GuideLineAI, Act, Issue, WorldProps } from '../database/models/Book.js';
import type { ChapterProps } from '../database/models/Chapter.js';
interface UpdateBookBasicData { interface UpdateBookBasicData {
title: string; title: string;
@@ -34,7 +35,7 @@ interface StoryData {
interface UpdateStoryData { interface UpdateStoryData {
bookId: string; bookId: string;
acts: Act[]; acts: Act[];
mainChapters: any[]; // ChapterProps[] from your API mainChapters: ChapterProps[];
} }
interface CreateBookData { interface CreateBookData {

View File

@@ -147,8 +147,8 @@ ipcMain.handle('db:chapter:information:add', createHandler<AddChapterInformation
); );
// DELETE /chapter/resume/remove - Remove chapter information // DELETE /chapter/resume/remove - Remove chapter information
ipcMain.handle('db:chapter:information:remove', createHandler<string, any>( ipcMain.handle('db:chapter:information:remove', createHandler<string, boolean>(
function(userId: string, chapterInfoId: string, lang: 'fr' | 'en') { function(userId: string, chapterInfoId: string, lang: 'fr' | 'en'): boolean {
return Chapter.removeChapterInformation(userId, chapterInfoId, lang); return Chapter.removeChapterInformation(userId, chapterInfoId, lang);
} }
) )

View File

@@ -47,8 +47,8 @@ ipcMain.handle('db:character:attribute:add', createHandler<AddAttributeData, str
); );
// DELETE /character/attribute/delete - Delete character attribute // DELETE /character/attribute/delete - Delete character attribute
ipcMain.handle('db:character:attribute:delete', createHandler<string, any>( ipcMain.handle('db:character:attribute:delete', createHandler<string, boolean>(
function(userId: string, attributeId: string, lang: 'fr' | 'en') { function(userId: string, attributeId: string, lang: 'fr' | 'en'): boolean {
return Character.deleteAttribute(userId, attributeId, lang); return Character.deleteAttribute(userId, attributeId, lang);
} }
) )
@@ -60,6 +60,4 @@ ipcMain.handle('db:character:update', createHandler<CharacterPropsPost, boolean>
return Character.updateCharacter(userId, character, lang); return Character.updateCharacter(userId, character, lang);
} }
) )
); );
console.log('[IPC] Character handlers registered');

View File

@@ -3,6 +3,11 @@ import { createHandler } from '../database/LocalSystem.js';
import Location from '../database/models/Location.js'; import Location from '../database/models/Location.js';
import type { LocationProps } from '../database/models/Location.js'; import type { LocationProps } from '../database/models/Location.js';
interface UpdateLocationResponse {
valid: boolean;
message: string;
}
interface AddLocationSectionData { interface AddLocationSectionData {
locationName: string; locationName: string;
bookId: string; bookId: string;
@@ -39,48 +44,48 @@ ipcMain.handle('db:location:section:add', createHandler<AddLocationSectionData,
); );
// POST /location/element/add - Add location element // POST /location/element/add - Add location element
ipcMain.handle('db:location:element:add', createHandler<AddLocationElementData, any>( ipcMain.handle('db:location:element:add', createHandler<AddLocationElementData, string>(
function(userId: string, data: AddLocationElementData, lang: 'fr' | 'en') { function(userId: string, data: AddLocationElementData, lang: 'fr' | 'en'): string {
return Location.addLocationElement(userId, data.locationId, data.elementName, lang); return Location.addLocationElement(userId, data.locationId, data.elementName, lang);
} }
) )
); );
// POST /location/sub-element/add - Add location sub-element // POST /location/sub-element/add - Add location sub-element
ipcMain.handle('db:location:subelement:add', createHandler<AddLocationSubElementData, any>( ipcMain.handle('db:location:subelement:add', createHandler<AddLocationSubElementData, string>(
function(userId: string, data: AddLocationSubElementData, lang: 'fr' | 'en') { function(userId: string, data: AddLocationSubElementData, lang: 'fr' | 'en'): string {
return Location.addLocationSubElement(userId, data.elementId, data.subElementName, lang); return Location.addLocationSubElement(userId, data.elementId, data.subElementName, lang);
} }
) )
); );
// POST /location/update - Update location section // POST /location/update - Update location section
ipcMain.handle('db:location:update', createHandler<UpdateLocationData, any>( ipcMain.handle('db:location:update', createHandler<UpdateLocationData, UpdateLocationResponse>(
function(userId: string, data: UpdateLocationData, lang: 'fr' | 'en') { function(userId: string, data: UpdateLocationData, lang: 'fr' | 'en'): UpdateLocationResponse {
return Location.updateLocationSection(userId, data.locations, lang); return Location.updateLocationSection(userId, data.locations, lang);
} }
) )
); );
// DELETE /location/delete - Delete location section // DELETE /location/delete - Delete location section
ipcMain.handle('db:location:delete', createHandler<string, any>( ipcMain.handle('db:location:delete', createHandler<string, boolean>(
function(userId: string, locationId: string, lang: 'fr' | 'en') { function(userId: string, locationId: string, lang: 'fr' | 'en'): boolean {
return Location.deleteLocationSection(userId, locationId, lang); return Location.deleteLocationSection(userId, locationId, lang);
} }
) )
); );
// DELETE /location/element/delete - Delete location element // DELETE /location/element/delete - Delete location element
ipcMain.handle('db:location:element:delete', createHandler<string, any>( ipcMain.handle('db:location:element:delete', createHandler<string, boolean>(
function(userId: string, elementId: string, lang: 'fr' | 'en') { function(userId: string, elementId: string, lang: 'fr' | 'en'): boolean {
return Location.deleteLocationElement(userId, elementId, lang); return Location.deleteLocationElement(userId, elementId, lang);
} }
) )
); );
// DELETE /location/sub-element/delete - Delete location sub-element // DELETE /location/sub-element/delete - Delete location sub-element
ipcMain.handle('db:location:subelement:delete', createHandler<string, any>( ipcMain.handle('db:location:subelement:delete', createHandler<string, boolean>(
function(userId: string, subElementId: string, lang: 'fr' | 'en') { function(userId: string, subElementId: string, lang: 'fr' | 'en'): boolean {
return Location.deleteLocationSubElement(userId, subElementId, lang); return Location.deleteLocationSubElement(userId, subElementId, lang);
} }
) )