From f5f48adc42614d98125c02595f5d4b3aee4f1597 Mon Sep 17 00:00:00 2001 From: vladimir-cucu Date: Tue, 5 Mar 2024 09:52:33 +0200 Subject: [PATCH] fix: resolve comments --- api/client.ts | 24 ++++++++++++++++-------- api/helpers.ts | 6 ------ api/tests/test-client.ts | 2 -- api/utils.ts | 8 +++----- generator/interfaces.ts | 1 + 5 files changed, 20 insertions(+), 21 deletions(-) diff --git a/api/client.ts b/api/client.ts index 68046c9d9..d9c7c6b2c 100644 --- a/api/client.ts +++ b/api/client.ts @@ -20,7 +20,11 @@ import { Error as MacaroonError, MacaroonObject, } from "@canonical/macaroon-bakery/dist/macaroon"; -import type { Callback, JujuRequest } from "../generator/interfaces"; +import type { + Callback, + CloseCallback, + JujuRequest, +} from "../generator/interfaces"; import { ClassType, Facade, @@ -34,7 +38,7 @@ export const CLIENT_VERSION = "3.3.2"; export interface ConnectOptions { bakery?: Bakery | null; - closeCallback: Callback; + closeCallback: CloseCallback; debug?: boolean; facades?: (ClassType | GenericFacade)[]; onWSCreated?: (ws: WebSocket) => void; @@ -368,7 +372,7 @@ class Client { another callback. It is responsibility of the callback to call the provided callback if present. */ - logout(callback?: Callback) { + logout(callback?: (code: number, callback: CloseCallback) => void) { this._transport.close(callback); } @@ -414,10 +418,10 @@ export class Transport { _ws: WebSocket; _counter: number; _callbacks: { [k: number]: Callback }; - _closeCallback: Callback; + _closeCallback: CloseCallback; _debug: boolean; - constructor(ws: WebSocket, closeCallback: Callback, debug: boolean) { + constructor(ws: WebSocket, closeCallback: CloseCallback, debug: boolean) { this._ws = ws; this._counter = 0; this._callbacks = {}; @@ -433,7 +437,7 @@ export class Transport { if (this._debug) { console.debug("close:", evt.code, evt.reason); } - this._closeCallback(toError(evt.code.toString())); + this._closeCallback(evt.code); }; } @@ -447,7 +451,11 @@ export class Transport { @param resolve Function called when the request is successful. @param reject Function called when the request is not successful. */ - write(req: JujuRequest, resolve: Function, reject: (error: any) => void) { + write( + req: JujuRequest, + resolve: (value: any) => void, + reject: (error: Error) => void + ) { // Check that the connection is ready and sane. const state = this._ws.readyState; if (state !== 1) { @@ -477,7 +485,7 @@ export class Transport { callback receives the close code and optionally another callback. It is responsibility of the callback to call the provided callback if present. */ - close(callback?: Callback) { + close(callback?: (code: number, callback: CloseCallback) => void) { const closeCallback = this._closeCallback; this._closeCallback = (code) => { if (callback) { diff --git a/api/helpers.ts b/api/helpers.ts index a09687c52..b26162eec 100644 --- a/api/helpers.ts +++ b/api/helpers.ts @@ -272,9 +272,6 @@ function wrapClient(cls: any) { method which can be provided a callback receiving an error. */ cls.prototype.watch = function (callback: Callback): object | undefined { - if (!callback) { - callback = (_error: CallbackError, _result?: any) => {}; - } // Check that the AllWatcher facade is loaded, as we will use it. const allWatcher = this._info.getFacade("allWatcher"); if (!allWatcher) { @@ -341,9 +338,6 @@ function wrapController(cls: any) { method which can be provided a callback receiving an error. */ cls.prototype.watch = function (callback: Callback): object | undefined { - if (!callback) { - callback = (_error: CallbackError, _result?: any) => {}; - } // Check that the AllModelWatcher facade is loaded, as we will use it. const allModelWatcher: any = this._info.getFacade("allModelWatcher"); if (!allModelWatcher) { diff --git a/api/tests/test-client.ts b/api/tests/test-client.ts index 2bbcc5108..c6d0b94f6 100644 --- a/api/tests/test-client.ts +++ b/api/tests/test-client.ts @@ -162,7 +162,6 @@ describe("connect", () => { it("login redirection error failure via promise", (done) => { connect("wss://1.2.3.4", options).then((juju: Client) => { - // juju._admin.redirectInfo = jest.fn().mockImplementation(() => null); juju ?.login({}) .then(() => fail) @@ -206,7 +205,6 @@ describe("connect", () => { it("login generic redirection error failure via promise", (done) => { connect("wss://1.2.3.4", options).then((juju: Client) => { - // juju._admin.redirectInfo = jest.fn().mockImplementation(() => null); juju ?.login({}) .then(() => fail) diff --git a/api/utils.ts b/api/utils.ts index 7ce213979..a78a756c4 100644 --- a/api/utils.ts +++ b/api/utils.ts @@ -38,9 +38,9 @@ export function autoBind(obj: { [k: string]: any }): void { export function createAsyncHandler( callback: Callback | undefined, resolve: (value: T) => void, - reject: (value: CallbackError) => void, + reject: (error: CallbackError) => void, transform?: (value: T) => T -): { resolve: Function; reject: Function } { +): { resolve: (value: T) => void; reject: (error: CallbackError) => void } { return { resolve: (value: T) => { if (transform) { @@ -48,9 +48,7 @@ export function createAsyncHandler( } callback ? callback(null, value) : resolve(value!); }, - reject: (error: CallbackError) => { - callback ? callback(error) : reject(error); - }, + reject: callback ? callback : reject, }; } diff --git a/generator/interfaces.ts b/generator/interfaces.ts index 2d1016b74..c9cbd1f34 100644 --- a/generator/interfaces.ts +++ b/generator/interfaces.ts @@ -57,3 +57,4 @@ export interface JujuRequest { export type CallbackError = Error | null; export type Callback = (error: CallbackError, value?: T) => void; +export type CloseCallback = (code: number) => void;