From 2f810abb7f2075bc3d37dd360161f7e32a4b2f15 Mon Sep 17 00:00:00 2001 From: muhammad-ahmed Date: Mon, 16 Dec 2024 14:22:21 +0800 Subject: [PATCH] ahmed/DAPI-841/chore--fix-custom-endpoint-specific-scenarios --- .../__tests__/RequestJsonBox.test.tsx | 1 + .../__test__/RequestResponseRenderer.test.tsx | 1 + .../Apiexplorer/RequestResponseRenderer/index.tsx | 8 ++++++-- .../Apiexplorer/__tests__/ApiExplorer.test.tsx | 1 + src/hooks/useWs/index.tsx | 14 +++++++++++--- 5 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/features/Apiexplorer/RequestJSONBox/__tests__/RequestJsonBox.test.tsx b/src/features/Apiexplorer/RequestJSONBox/__tests__/RequestJsonBox.test.tsx index c11aa6271..80660ff27 100644 --- a/src/features/Apiexplorer/RequestJSONBox/__tests__/RequestJsonBox.test.tsx +++ b/src/features/Apiexplorer/RequestJSONBox/__tests__/RequestJsonBox.test.tsx @@ -19,6 +19,7 @@ const fakeHookObject = { tick: 1, echo_req: { tick: 1 }, }, + disableApiNameOnRequest: jest.fn(), }; jest.mock('@site/src/hooks/useAuthContext'); diff --git a/src/features/Apiexplorer/RequestResponseRenderer/__test__/RequestResponseRenderer.test.tsx b/src/features/Apiexplorer/RequestResponseRenderer/__test__/RequestResponseRenderer.test.tsx index a4e103e67..887291b76 100644 --- a/src/features/Apiexplorer/RequestResponseRenderer/__test__/RequestResponseRenderer.test.tsx +++ b/src/features/Apiexplorer/RequestResponseRenderer/__test__/RequestResponseRenderer.test.tsx @@ -48,6 +48,7 @@ mockUseWS.mockImplementation(() => ({ ping: 'pong', req_id: 1, }, + disableApiNameOnRequest: jest.fn(), })); describe('RequestResponseRenderer', () => { diff --git a/src/features/Apiexplorer/RequestResponseRenderer/index.tsx b/src/features/Apiexplorer/RequestResponseRenderer/index.tsx index 9f0ca1b2a..116678cf6 100644 --- a/src/features/Apiexplorer/RequestResponseRenderer/index.tsx +++ b/src/features/Apiexplorer/RequestResponseRenderer/index.tsx @@ -1,4 +1,4 @@ -import React, { useState, useCallback } from 'react'; +import React, { useState, useCallback, useEffect } from 'react'; import { TSocketEndpointNames, TSocketRequestProps } from '@site/src/configs/websocket/types'; import useWS from '@site/src/hooks/useWs'; import useAuthContext from '@site/src/hooks/useAuthContext'; @@ -24,11 +24,15 @@ function RequestResponseRenderer({ const AUTH_ENABLED = 1; const { is_logged_in } = useAuthContext(); const { disableSendRequest } = useDisableSendRequest(); - const { full_response, is_loading, send, clear, error } = useWS(name); + const { full_response, is_loading, send, clear, error, disableApiNameOnRequest } = useWS(name); const [toggle_modal, setToggleModal] = useState(false); const [response_state, setResponseState] = useState(false); const [is_not_valid, setIsNotValid] = useState(false); + useEffect(() => { + disableApiNameOnRequest(); + }, []); + const parseRequestJSON = () => { let request_data: TSocketRequestProps extends never ? undefined : TSocketRequestProps; diff --git a/src/features/Apiexplorer/__tests__/ApiExplorer.test.tsx b/src/features/Apiexplorer/__tests__/ApiExplorer.test.tsx index dcbfa85df..f00a0d6df 100644 --- a/src/features/Apiexplorer/__tests__/ApiExplorer.test.tsx +++ b/src/features/Apiexplorer/__tests__/ApiExplorer.test.tsx @@ -58,6 +58,7 @@ mockuseWS.mockImplementation(() => ({ tick: 1, echo_req: { tick: 1 }, }, + disableApiNameOnRequest: jest.fn(), })); jest.mock('@site/src/hooks/useDynamicImportJSON'); diff --git a/src/hooks/useWs/index.tsx b/src/hooks/useWs/index.tsx index 1e0d54633..82d705317 100644 --- a/src/hooks/useWs/index.tsx +++ b/src/hooks/useWs/index.tsx @@ -11,6 +11,7 @@ const useWS = (name?: T) => { const [error, setError] = useState(); const [data, setData] = useState>(); const [full_response, setFullResponse] = useState>(); + const [isUseName, setIsUseName] = useState(true); const clear = useCallback(() => { setError(null); @@ -18,17 +19,24 @@ const useWS = (name?: T) => { setFullResponse(null); }, []); + // This function is used to disable the API name on request from playground. + const disableApiNameOnRequest = useCallback(() => { + setIsUseName(false); + }, []); + const send = useCallback( async (data?: Parameters>[0]) => { let payload = data; - if ((!data && name) || (name == 'api_token' || name == 'app_register')) { + const isAllowName = isUseName && (name == 'api_token' || name == 'app_register'); + + if ((!data && name) || isAllowName) { payload = { [name]: 1, ...payload }; } else { payload = { ...payload }; } setIsLoading(true); - + try { const response = await apiManager.augmentedSend(payload); const key = response['msg_type'] ?? name; @@ -43,7 +51,7 @@ const useWS = (name?: T) => { [name], ); - return { send, full_response, is_loading, error, data, clear }; + return { send, full_response, is_loading, error, data, clear, disableApiNameOnRequest }; }; export default useWS;