add some log changes to http client class

This commit is contained in:
Mark Kim
2023-11-01 15:14:57 -04:00
parent 2c946bbf3e
commit 5a03e009e2
2 changed files with 23 additions and 7 deletions

View File

@@ -10,6 +10,7 @@ interface FetchConfig {
data?: unknown;
retries?: number;
origin?: string;
requestId?: string;
}
// internal request (insomniaFetch)
// should validate ssl certs on our server
@@ -41,13 +42,14 @@ const exponentialBackOff = async (url: string, init: RequestInit, retries = 0):
}
};
export async function insomniaFetch<T = void>({ method, path, data, sessionId, origin }: FetchConfig): Promise<T> {
export async function insomniaFetch<T = void>({ method, path, data, sessionId, origin, requestId }: FetchConfig): Promise<T> {
const config: RequestInit = {
method,
headers: {
'X-Insomnia-Client': getClientString(),
...(sessionId ? { 'X-Session-Id': sessionId } : {}),
...(data ? { 'Content-Type': 'application/json' } : {}),
...(requestId ? { 'insomnia-request-id': requestId } : {}),
},
...(data ? { body: JSON.stringify(data) } : {}),
};

View File

@@ -1,3 +1,4 @@
import { randomUUID } from 'crypto';
import type { LogFunctions } from 'electron-log';
import { insomniaFetch } from '../../insomniaFetch';
@@ -7,6 +8,13 @@ interface ErrorResponse {
error: string;
message: string;
}
/**
* This HttpClient class is a wrapper around insomniaFetch, created for the following purposes:
*
* - to enable logging specific to migration.
* - to enable authenticated request to Insomnia API
*/
export class HttpClient {
private _logger: LogFunctions;
private _identity: IdentityService;
@@ -17,16 +25,19 @@ export class HttpClient {
}
public get<T>(path: string): Promise<T | ErrorResponse> {
this._logger.info('[migration] making a network call:', path);
const requestId = randomUUID();
this._logger.info(`[migration][http][GET][${requestId}] making a http request:`, path);
return insomniaFetch<T>({
method: 'GET',
path,
requestId,
sessionId: this._identity.sessionId,
});
}
public post<T, D>(path: string, data: D): Promise<T | ErrorResponse> {
this._logger.info('[migration] making a network call:', path);
const requestId = randomUUID();
this._logger.info(`[migration][http][POST][${requestId}] making a http request:`, path);
return insomniaFetch<T>({
method: 'POST',
path,
@@ -36,7 +47,8 @@ export class HttpClient {
}
public delete<T>(path: string): Promise<T | ErrorResponse> {
this._logger.info('[migration] making a network call:', path);
const requestId = randomUUID();
this._logger.info(`[migration][http][DELETE][${requestId}] making a http request:`, path);
return insomniaFetch<T>({
method: 'DELETE',
path,
@@ -45,9 +57,10 @@ export class HttpClient {
}
public patch<T, D>(path: string, data: D): Promise<T | ErrorResponse> {
this._logger.info('[migration] making a network call:', path);
const requestId = randomUUID();
this._logger.info(`[migration][http][PATCH][${requestId}] making a http request:`, path);
return insomniaFetch<T>({
method: 'DELETE',
method: 'PATCH',
path,
data,
sessionId: this._identity.sessionId,
@@ -55,7 +68,8 @@ export class HttpClient {
}
public put<T, D>(path: string, data: D): Promise<T | ErrorResponse> {
this._logger.info('[migration] making a network call:', path);
const requestId = randomUUID();
this._logger.info(`[migration][http][PUT][${requestId}] making a http request:`, path);
return insomniaFetch<T>({
method: 'PUT',
path,