refactor: use strict typing

This commit is contained in:
Carlos Valente
2025-03-21 19:44:16 +01:00
committed by Carlos Valente
parent 4ed38340e0
commit 178640bfc4
39 changed files with 339 additions and 246 deletions
+19 -6
View File
@@ -102,7 +102,7 @@ class SocketServer implements IAdapter {
ws.on('message', (data) => {
try {
// @ts-expect-error -- ??
// @ts-expect-error -- this works fine
const message = JSON.parse(data);
const { type, payload } = message;
@@ -120,7 +120,7 @@ class SocketServer implements IAdapter {
ws.send(
JSON.stringify({
type: 'client-name',
payload: this.clients.get(clientId).name,
payload: this.getOrCreateClient(clientId),
}),
);
return;
@@ -136,7 +136,7 @@ class SocketServer implements IAdapter {
if (type === 'set-client-type') {
if (payload && typeof payload == 'string') {
const previousData = this.clients.get(clientId);
const previousData = this.getOrCreateClient(clientId);
this.clients.set(clientId, { ...previousData, type: payload });
}
this.sendClientList();
@@ -145,7 +145,7 @@ class SocketServer implements IAdapter {
if (type === 'set-client-path') {
if (payload && typeof payload == 'string') {
const previousData = this.clients.get(clientId);
const previousData = this.getOrCreateClient(clientId);
previousData.path = payload;
this.clients.set(clientId, previousData);
@@ -166,13 +166,13 @@ class SocketServer implements IAdapter {
if (type === 'set-client-name') {
if (payload) {
const previousData = this.clients.get(clientId);
const previousData = this.getOrCreateClient(clientId);
logger.info(LogOrigin.Client, `Client ${previousData.name} renamed to ${payload}`);
this.clients.set(clientId, { ...previousData, name: payload });
ws.send(
JSON.stringify({
type: 'client-name',
payload: this.clients.get(clientId).name,
payload: this.getOrCreateClient(clientId).name,
}),
);
}
@@ -215,6 +215,19 @@ class SocketServer implements IAdapter {
};
}
private getOrCreateClient(clientId: string): Client {
if (!this.clients.has(clientId)) {
this.clients.set(clientId, {
type: 'unknown',
identify: false,
name: getRandomName(),
origin: '',
path: '',
});
}
return this.clients.get(clientId) as Client;
}
private sendClientList(): void {
const payload = Object.fromEntries(this.clients.entries());
this.sendAsJson({ type: 'client-list', payload });
+2 -2
View File
@@ -4,7 +4,7 @@
* @param {string} value - value to assign
* @returns {object | string | null} nested object or null if no object was created
*/
export const integrationPayloadFromPath = (path: string[], value?: unknown): object | string | null => {
export function integrationPayloadFromPath(path: string[], value?: unknown): object | string | null {
if (path.length === 1) {
const key = path[0];
return value === undefined ? key : { [key]: value };
@@ -16,4 +16,4 @@ export const integrationPayloadFromPath = (path: string[], value?: unknown): obj
const obj = shortenedPath.reduceRight((result, key) => ({ [key]: result }), parsedValue);
return typeof obj === 'object' ? obj : null;
};
}