Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const fakeHookObject = {
tick: 1,
echo_req: { tick: 1 },
},
disableApiNameOnRequest: jest.fn(),
};

jest.mock('@site/src/hooks/useAuthContext');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ mockUseWS.mockImplementation(() => ({
ping: 'pong',
req_id: 1,
},
disableApiNameOnRequest: jest.fn(),
}));

describe('RequestResponseRenderer', () => {
Expand Down
8 changes: 6 additions & 2 deletions src/features/Apiexplorer/RequestResponseRenderer/index.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -24,11 +24,15 @@ function RequestResponseRenderer<T extends TSocketEndpointNames>({
const AUTH_ENABLED = 1;
const { is_logged_in } = useAuthContext();
const { disableSendRequest } = useDisableSendRequest();
const { full_response, is_loading, send, clear, error } = useWS<T>(name);
const { full_response, is_loading, send, clear, error, disableApiNameOnRequest } = useWS<T>(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<T> extends never ? undefined : TSocketRequestProps<T>;

Expand Down
1 change: 1 addition & 0 deletions src/features/Apiexplorer/__tests__/ApiExplorer.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ mockuseWS.mockImplementation(() => ({
tick: 1,
echo_req: { tick: 1 },
},
disableApiNameOnRequest: jest.fn(),
}));

jest.mock('@site/src/hooks/useDynamicImportJSON');
Expand Down
14 changes: 11 additions & 3 deletions src/hooks/useWs/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,32 @@ const useWS = <T extends TSocketEndpointNames>(name?: T) => {
const [error, setError] = useState<unknown>();
const [data, setData] = useState<TSocketResponseData<T>>();
const [full_response, setFullResponse] = useState<TSocketResponse<T>>();
const [isUseName, setIsUseName] = useState(true);

const clear = useCallback(() => {
setError(null);
setData(null);
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<typeof apiManager.augmentedSend<T>>[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;
Expand All @@ -43,7 +51,7 @@ const useWS = <T extends TSocketEndpointNames>(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;
Loading