From 3ca74f6f625b60b16bb6ac0dc7f4a21b9b9b6d86 Mon Sep 17 00:00:00 2001 From: Constance Chen Date: Mon, 5 Apr 2021 09:46:33 -0700 Subject: [PATCH] PR feedback: naming/comments --- .../components/api_logs/api_log/api_log_flyout.tsx | 6 +++--- .../app_search/components/api_logs/utils.test.ts | 8 ++++---- .../applications/app_search/components/api_logs/utils.ts | 6 +++--- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/x-pack/plugins/enterprise_search/public/applications/app_search/components/api_logs/api_log/api_log_flyout.tsx b/x-pack/plugins/enterprise_search/public/applications/app_search/components/api_logs/api_log/api_log_flyout.tsx index b3cc45801786..dd53e997da0f 100644 --- a/x-pack/plugins/enterprise_search/public/applications/app_search/components/api_logs/api_log/api_log_flyout.tsx +++ b/x-pack/plugins/enterprise_search/public/applications/app_search/components/api_logs/api_log/api_log_flyout.tsx @@ -32,7 +32,7 @@ import { } from '@elastic/eui'; import { i18n } from '@kbn/i18n'; -import { getStatusColor, safeJsonParseAndStringify } from '../utils'; +import { getStatusColor, attemptToFormatJson } from '../utils'; import { ApiLogLogic } from './'; @@ -112,7 +112,7 @@ export const ApiLogFlyout: React.FC = () => { })} - {safeJsonParseAndStringify(apiLog.request_body)} + {attemptToFormatJson(apiLog.request_body)} @@ -122,7 +122,7 @@ export const ApiLogFlyout: React.FC = () => { })} - {safeJsonParseAndStringify(apiLog.response_body)} + {attemptToFormatJson(apiLog.response_body)} diff --git a/x-pack/plugins/enterprise_search/public/applications/app_search/components/api_logs/utils.test.ts b/x-pack/plugins/enterprise_search/public/applications/app_search/components/api_logs/utils.test.ts index a8966d6c3bb8..ac464e2af353 100644 --- a/x-pack/plugins/enterprise_search/public/applications/app_search/components/api_logs/utils.test.ts +++ b/x-pack/plugins/enterprise_search/public/applications/app_search/components/api_logs/utils.test.ts @@ -7,7 +7,7 @@ import dedent from 'dedent'; -import { getDateString, getStatusColor, safeJsonParseAndStringify } from './utils'; +import { getDateString, getStatusColor, attemptToFormatJson } from './utils'; describe('getDateString', () => { const mockDate = jest @@ -35,9 +35,9 @@ describe('getStatusColor', () => { }); }); -describe('safeJsonParseAndStringify', () => { +describe('attemptToFormatJson', () => { it('takes an unformatted JSON string and correctly newlines/indents it', () => { - expect(safeJsonParseAndStringify('{"hello":"world","lorem":{"ipsum":"dolor","sit":"amet"}}')) + expect(attemptToFormatJson('{"hello":"world","lorem":{"ipsum":"dolor","sit":"amet"}}')) .toEqual(dedent`{ "hello": "world", "lorem": { @@ -48,6 +48,6 @@ describe('safeJsonParseAndStringify', () => { }); it('returns the original content if it is not properly formatted JSON', () => { - expect(safeJsonParseAndStringify('{invalid json}')).toEqual('{invalid json}'); + expect(attemptToFormatJson('{invalid json}')).toEqual('{invalid json}'); }); }); diff --git a/x-pack/plugins/enterprise_search/public/applications/app_search/components/api_logs/utils.ts b/x-pack/plugins/enterprise_search/public/applications/app_search/components/api_logs/utils.ts index 1f78da8dd207..7e5f19686f13 100644 --- a/x-pack/plugins/enterprise_search/public/applications/app_search/components/api_logs/utils.ts +++ b/x-pack/plugins/enterprise_search/public/applications/app_search/components/api_logs/utils.ts @@ -20,12 +20,12 @@ export const getStatusColor = (status: number) => { return color; }; -export const attemptToFormatPossibleJson = (possibleJson: string) => { +export const attemptToFormatJson = (possibleJson: string) => { try { - // it is JSON, we can format it + // it is JSON, we can format it with newlines/indentation return JSON.stringify(JSON.parse(possibleJson), null, 2); } catch { - // it's not JSON, likely a string message, we return it rather than format it + // if it's not JSON, we return the original content return possibleJson; } };