Skip to content

Commit

Permalink
fix: response.json() not returning the correct data
Browse files Browse the repository at this point in the history
  • Loading branch information
jvandenaardweg committed Dec 5, 2022
1 parent 8c5d076 commit 91f4a87
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Logger } from "homebridge";
import fetch, { Response } from "node-fetch";
import {
HomeWizardApiBasicInformationResponse,
HomeWizardApiErrorResponse,
HomeWizardApiIdentifyResponse,
HomeWizardApiStatePutParams,
HomeWizardApiStatePutResponse,
Expand Down Expand Up @@ -35,13 +36,15 @@ export class HomeWizardApi {
};
}

throwApiError(method: string, response: Response): never {
async throwApiError(method: string, response: Response): Promise<never> {
const responseData = await response.json();

throw new Error(
`Api ${method.toUpperCase()} call at ${
response.url
} failed, with status ${
response.status
} and response data ${JSON.stringify(response)}`
} and response data ${JSON.stringify(responseData)}`
);
}

Expand Down Expand Up @@ -70,7 +73,7 @@ export class HomeWizardApi {
}

const data =
response.json() as unknown as HomeWizardApiBasicInformationResponse;
(await response.json()) as unknown as HomeWizardApiBasicInformationResponse;

this.log.debug(
this.loggerPrefix,
Expand Down Expand Up @@ -100,7 +103,8 @@ export class HomeWizardApi {
return this.throwApiError(method, response);
}

const data = response.json() as unknown as HomeWizardApiStateResponse;
const data =
(await response.json()) as unknown as HomeWizardApiStateResponse;

this.log.info(
this.loggerPrefix,
Expand Down Expand Up @@ -135,7 +139,8 @@ export class HomeWizardApi {
return this.throwApiError(method, response);
}

const data = response.json() as unknown as HomeWizardApiStatePutResponse;
const data =
(await response.json()) as unknown as HomeWizardApiStatePutResponse;

this.log.debug(
this.loggerPrefix,
Expand Down Expand Up @@ -181,7 +186,8 @@ export class HomeWizardApi {
return this.throwApiError(method, response);
}

const data = response.json() as unknown as HomeWizardApiIdentifyResponse;
const data =
(await response.json()) as unknown as HomeWizardApiIdentifyResponse;

this.log.debug(
this.loggerPrefix,
Expand Down

0 comments on commit 91f4a87

Please sign in to comment.