From 6b17607f9f6d51eb59d23bf1d6ba42d54e89bd05 Mon Sep 17 00:00:00 2001 From: "Valentin D. Pinkman" Date: Wed, 12 Jun 2024 17:21:59 +0200 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20(core):=20Method=20sendCom?= =?UTF-8?q?mand=20now=20returns=20a=20promise?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Instead of returning a new function, let's run it directly and return a promise --- .changeset/spotty-glasses-smell.md | 5 ++++ .../use-case/SendCommandUseCase.test.ts | 2 +- .../command/use-case/SendCommandUseCase.ts | 7 ++--- .../device-session/model/DeviceSession.ts | 26 +++++++++---------- 4 files changed, 21 insertions(+), 19 deletions(-) create mode 100644 .changeset/spotty-glasses-smell.md diff --git a/.changeset/spotty-glasses-smell.md b/.changeset/spotty-glasses-smell.md new file mode 100644 index 000000000..c632242f0 --- /dev/null +++ b/.changeset/spotty-glasses-smell.md @@ -0,0 +1,5 @@ +--- +"@ledgerhq/device-sdk-core": patch +--- + +Change sendCommand signature to directly return a Promise instead of a new function diff --git a/packages/core/src/api/command/use-case/SendCommandUseCase.test.ts b/packages/core/src/api/command/use-case/SendCommandUseCase.test.ts index 64b58466d..c9ea6fb16 100644 --- a/packages/core/src/api/command/use-case/SendCommandUseCase.test.ts +++ b/packages/core/src/api/command/use-case/SendCommandUseCase.test.ts @@ -36,7 +36,7 @@ describe("SendCommandUseCase", () => { jest .spyOn(deviceSession, "sendCommand") - .mockReturnValue(async () => Promise.resolve({ status: "success" })); + .mockResolvedValue({ status: "success" }); const response = await useCase.execute<{ status: string }>({ sessionId: fakeSessionId, diff --git a/packages/core/src/api/command/use-case/SendCommandUseCase.ts b/packages/core/src/api/command/use-case/SendCommandUseCase.ts index 7b64b80af..81e0012e6 100644 --- a/packages/core/src/api/command/use-case/SendCommandUseCase.ts +++ b/packages/core/src/api/command/use-case/SendCommandUseCase.ts @@ -54,11 +54,8 @@ export class SendCommandUseCase { return deviceSessionOrError.caseOf({ // Case device session found - Right: async (deviceSession) => { - const deviceModelId = deviceSession.connectedDevice.deviceModel.id; - const action = deviceSession.sendCommand(command); - return await action(deviceModelId); - }, + Right: async (deviceSession) => + await deviceSession.sendCommand(command), // Case device session not found Left: (error) => { this._logger.error("Error getting session", { diff --git a/packages/core/src/internal/device-session/model/DeviceSession.ts b/packages/core/src/internal/device-session/model/DeviceSession.ts index b22000bd6..5bb693241 100644 --- a/packages/core/src/internal/device-session/model/DeviceSession.ts +++ b/packages/core/src/internal/device-session/model/DeviceSession.ts @@ -4,7 +4,6 @@ import { v4 as uuidv4 } from "uuid"; import { Command } from "@api/command/Command"; import { CommandUtils } from "@api/command/utils/CommandUtils"; -import { DeviceModelId } from "@api/device/DeviceModel"; import { DeviceStatus } from "@api/device/DeviceStatus"; import { DeviceSessionState, @@ -97,18 +96,19 @@ export class DeviceSession { }); } - sendCommand(command: Command) { - return async (deviceModelId: DeviceModelId): Promise => { - const apdu = command.getApdu(); - const response = await this.sendApdu(apdu.getRawApdu()); - - return response.caseOf({ - Left: (err) => { - throw err; - }, - Right: (r) => command.parseResponse(r, deviceModelId), - }); - }; + async sendCommand( + command: Command, + ): Promise { + const apdu = command.getApdu(); + const response = await this.sendApdu(apdu.getRawApdu()); + + return response.caseOf({ + Left: (err) => { + throw err; + }, + Right: (r) => + command.parseResponse(r, this._connectedDevice.deviceModel.id), + }); } close() {