Skip to content

Commit

Permalink
refactor: let httpRequest method use fetch options
Browse files Browse the repository at this point in the history
  • Loading branch information
jvandenaardweg committed Dec 6, 2022
1 parent 20d4f11 commit d66a6c8
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 15 deletions.
12 changes: 6 additions & 6 deletions src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
HomeWizardApiStatePutResponse,
HomeWizardApiStateResponse,
} from "@/api/types";
import { httpRequest, HttpRequestResponse } from "@/http-request";
import { httpRequest, HttpRequestResponse } from "@/utils/http-request";

interface Endpoints {
basic: string;
Expand Down Expand Up @@ -75,7 +75,7 @@ export class HomeWizardApi {
return this.throwApiError(method, response);
}

const data = response.data;
const data = await response.json();

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

const data = response.data;
const data = await response.json();

this.log.info(
this.loggerPrefix,
Expand Down Expand Up @@ -138,15 +138,15 @@ export class HomeWizardApi {
this.endpoints.state,
{
method,
data: params,
body: JSON.stringify(params),
}
);

if (!response.ok) {
return this.throwApiError(method, response);
}

const data = response.data;
const data = await response.json();

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

const data = response.data;
const data = await response.json();

this.log.debug(
this.loggerPrefix,
Expand Down
40 changes: 31 additions & 9 deletions src/http-request.ts → src/utils/http-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,29 @@ export interface HttpRequestResponse<T> {
readonly status: number;
readonly statusText: string | undefined;
readonly url: string;
readonly data: T;
readonly body: string;
json(): Promise<T>;
}

type HttpRequestOptions = Omit<
RequestOptions,
"protocol" | "host" | "path" | "port" | "hostname" | "localAddress" | "href"
> & {
data?: object;
body?: string;
};

/**
* A little type-safe `http` / `https` wrapper, similar to `fetch`. So we can replace it with a fetch implementation later on when needed.
*
* Why?
*
* There seems to be a bug in node-fetch on Node 18, which makes `response.json()` not work on our PUT requests.
*
* For now, this `httpRequest` method will do, so we don't have to import any dependencies.
*/
export const httpRequest = <T>(
url: string,
options: HttpRequestOptions
options?: HttpRequestOptions
): Promise<HttpRequestResponse<T>> =>
new Promise((resolve, reject) => {
let request = http.request;
Expand All @@ -38,7 +48,7 @@ export const httpRequest = <T>(
port,
headers: {
"Content-Type": "application/json",
...options.headers,
...options?.headers,
},
...options,
},
Expand All @@ -62,16 +72,28 @@ export const httpRequest = <T>(
);
}

const ok = !!(statusCode && statusCode < 400);
const ok = !!(statusCode >= 200 && statusCode < 300);

const data = JSON.parse(joinedBody) as T;
// fetch-like json method
const json = async (): Promise<T> => {
return new Promise((resolve, reject) => {
try {
const json = JSON.parse(joinedBody) as T;

resolve(json);
} catch (err) {
reject(err);
}
});
};

const response: HttpRequestResponse<T> = {
ok,
headers,
data,
json,
status: statusCode,
statusText,
body: joinedBody,
url,
};

Expand All @@ -82,8 +104,8 @@ export const httpRequest = <T>(

req.on("error", reject);

if (options.data) {
req.write(JSON.stringify(options.data));
if (options?.body) {
req.write(options.body);
}

req.end();
Expand Down

0 comments on commit d66a6c8

Please sign in to comment.