From 5a03e009e25e468b0410b38fed53e3cfe080aaad Mon Sep 17 00:00:00 2001 From: Mark Kim Date: Wed, 1 Nov 2023 15:14:57 -0400 Subject: [PATCH] add some log changes to http client class --- packages/insomnia/src/main/insomniaFetch.ts | 4 ++- .../src/main/migration/services/http.ts | 26 ++++++++++++++----- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/packages/insomnia/src/main/insomniaFetch.ts b/packages/insomnia/src/main/insomniaFetch.ts index d8b8ebd361..6062e047c0 100644 --- a/packages/insomnia/src/main/insomniaFetch.ts +++ b/packages/insomnia/src/main/insomniaFetch.ts @@ -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({ method, path, data, sessionId, origin }: FetchConfig): Promise { +export async function insomniaFetch({ method, path, data, sessionId, origin, requestId }: FetchConfig): Promise { 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) } : {}), }; diff --git a/packages/insomnia/src/main/migration/services/http.ts b/packages/insomnia/src/main/migration/services/http.ts index 9719fa5551..b8976e1087 100644 --- a/packages/insomnia/src/main/migration/services/http.ts +++ b/packages/insomnia/src/main/migration/services/http.ts @@ -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(path: string): Promise { - 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({ method: 'GET', path, + requestId, sessionId: this._identity.sessionId, }); } public post(path: string, data: D): Promise { - 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({ method: 'POST', path, @@ -36,7 +47,8 @@ export class HttpClient { } public delete(path: string): Promise { - 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({ method: 'DELETE', path, @@ -45,9 +57,10 @@ export class HttpClient { } public patch(path: string, data: D): Promise { - 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({ - method: 'DELETE', + method: 'PATCH', path, data, sessionId: this._identity.sessionId, @@ -55,7 +68,8 @@ export class HttpClient { } public put(path: string, data: D): Promise { - 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({ method: 'PUT', path,