Skip to content

Commit

Permalink
fixes to console commands
Browse files Browse the repository at this point in the history
  • Loading branch information
garredow committed Apr 22, 2024
1 parent cdfa2cc commit 8684b56
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nothing-special/kaiware-lib",
"version": "0.8.0",
"version": "0.9.0",
"type": "module",
"author": {
"name": "Garrett Downs",
Expand Down
11 changes: 7 additions & 4 deletions src/lib/connection.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { z } from 'zod';
import { MessageType } from '../enums';
import { Config, ConsoleCommandResPayload, MessageWithId, rawMessageSchema } from '../types';
import { makeSerializable, parseConsoleCommand, stringifyObject } from '../utils';
import { makeSerializable, parseConsoleCommand } from '../utils';

export class Connection {
private config: Config;
Expand Down Expand Up @@ -230,17 +229,21 @@ export class Connection {

let responseData;
if (typeof currentValue === 'object' && !Array.isArray(currentValue)) {
responseData = stringifyObject(currentValue ?? {});
console.log('responseData', responseData);
responseData = makeSerializable(currentValue ?? {});
} else if (Array.isArray(currentValue)) {
responseData = currentValue.map((val) => makeSerializable(val));
} else if (typeof currentValue === 'function') {
responseData = `${currentValue.name}()`;
} else {
responseData = currentValue;
}

response = {
result: responseData
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (err: any) {
this.cleanConsole.error(err);
response = { error: err?.message ?? 'Unknown error' };
}

Expand Down
5 changes: 3 additions & 2 deletions src/utils/makeSerializable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ export function makeSerializable(obj: object, spaces = 2): object {

const keys = getObjectKeys(obj);
keys.forEach((key) => {
newObj[key] = (obj as { [key: string]: unknown })[key];
const val = (obj as { [key: string]: unknown })[key];
newObj[key] = typeof val === 'function' ? `${val.name}()` : val;
});

return JSON.parse(JSON.stringify(newObj, null, spaces));
return JSON.parse(JSON.stringify(newObj, keys, spaces));
}

0 comments on commit 8684b56

Please sign in to comment.