2020-05-30 02:39:18 +00:00
|
|
|
import React, { useState } from "react";
|
2019-10-25 05:35:20 +00:00
|
|
|
import { connect } from "react-redux";
|
|
|
|
|
import { withRouter, RouteComponentProps } from "react-router";
|
2019-11-25 05:07:27 +00:00
|
|
|
import { BaseText } from "components/designSystems/blueprint/TextComponent";
|
2019-10-25 05:35:20 +00:00
|
|
|
import styled from "styled-components";
|
2019-11-25 05:07:27 +00:00
|
|
|
import { AppState } from "reducers";
|
|
|
|
|
import { ActionResponse } from "api/ActionAPI";
|
|
|
|
|
import { formatBytes } from "utils/helpers";
|
2019-11-22 17:04:40 +00:00
|
|
|
import { APIEditorRouteParams } from "constants/routes";
|
2019-12-11 15:14:38 +00:00
|
|
|
import LoadingOverlayScreen from "components/editorComponents/LoadingOverlayScreen";
|
2020-07-01 10:01:07 +00:00
|
|
|
import ReadOnlyEditor from "components/editorComponents/ReadOnlyEditor";
|
2020-01-30 13:23:04 +00:00
|
|
|
import { getActionResponses } from "selectors/entitiesSelector";
|
2020-04-14 12:34:14 +00:00
|
|
|
import { Colors } from "constants/Colors";
|
2020-06-04 13:49:22 +00:00
|
|
|
import _ from "lodash";
|
2020-06-23 04:12:52 +00:00
|
|
|
import { RequestView } from "./RequestView";
|
2020-06-23 03:58:42 +00:00
|
|
|
import { useLocalStorage } from "utils/hooks/localstorage";
|
|
|
|
|
import {
|
|
|
|
|
CHECK_REQUEST_BODY,
|
|
|
|
|
DONT_SHOW_THIS_AGAIN,
|
|
|
|
|
SHOW_REQUEST,
|
|
|
|
|
} from "constants/messages";
|
2021-02-11 12:54:00 +00:00
|
|
|
import { TabComponent } from "components/ads/Tabs";
|
|
|
|
|
import Text, { Case, TextType } from "components/ads/Text";
|
|
|
|
|
import Icon from "components/ads/Icon";
|
|
|
|
|
import { Classes, Variant } from "components/ads/common";
|
|
|
|
|
import { EditorTheme } from "./CodeEditor/EditorConfig";
|
|
|
|
|
import Callout from "components/ads/Callout";
|
|
|
|
|
import Button from "components/ads/Button";
|
2019-10-25 05:35:20 +00:00
|
|
|
|
|
|
|
|
const ResponseWrapper = styled.div`
|
2019-10-29 12:02:58 +00:00
|
|
|
position: relative;
|
2020-02-07 02:32:52 +00:00
|
|
|
flex: 1;
|
2021-02-11 12:54:00 +00:00
|
|
|
height: 50%;
|
|
|
|
|
background-color: ${(props) => props.theme.colors.apiPane.responseBody.bg};
|
|
|
|
|
|
|
|
|
|
.react-tabs__tab-panel {
|
|
|
|
|
overflow: hidden;
|
|
|
|
|
}
|
2019-10-25 05:35:20 +00:00
|
|
|
`;
|
|
|
|
|
const ResponseMetaInfo = styled.div`
|
|
|
|
|
display: flex;
|
|
|
|
|
${BaseText} {
|
|
|
|
|
color: #768896;
|
2021-02-11 12:54:00 +00:00
|
|
|
margin-left: ${(props) => props.theme.spaces[9]}px;
|
2019-10-25 05:35:20 +00:00
|
|
|
}
|
|
|
|
|
`;
|
|
|
|
|
|
2020-12-08 09:24:36 +00:00
|
|
|
const ResponseMetaWrapper = styled.div`
|
|
|
|
|
align-items: center;
|
|
|
|
|
display: flex;
|
2021-02-11 12:54:00 +00:00
|
|
|
position: absolute;
|
|
|
|
|
right: ${(props) => props.theme.spaces[12]}px;
|
|
|
|
|
top: ${(props) => props.theme.spaces[4]}px;
|
2020-12-08 09:24:36 +00:00
|
|
|
`;
|
|
|
|
|
|
2019-10-29 12:02:58 +00:00
|
|
|
const StatusCodeText = styled(BaseText)<{ code: string }>`
|
2020-12-24 04:32:25 +00:00
|
|
|
color: ${(props) =>
|
2020-08-10 04:54:33 +00:00
|
|
|
props.code.match(/2\d\d/) ? props.theme.colors.primaryOld : Colors.RED};
|
2019-10-29 12:02:58 +00:00
|
|
|
`;
|
2019-10-25 05:35:20 +00:00
|
|
|
|
2020-06-25 10:04:57 +00:00
|
|
|
// const TableWrapper = styled.div`
|
|
|
|
|
// &&& {
|
|
|
|
|
// table {
|
|
|
|
|
// table-layout: fixed;
|
|
|
|
|
// width: 100%;
|
|
|
|
|
// td {
|
|
|
|
|
// font-size: 12px;
|
|
|
|
|
// width: 50%;
|
|
|
|
|
// white-space: nowrap;
|
|
|
|
|
// overflow: hidden;
|
|
|
|
|
// text-overflow: ellipsis;
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// `;
|
2019-10-25 05:35:20 +00:00
|
|
|
|
2019-10-29 12:02:58 +00:00
|
|
|
interface ReduxStateProps {
|
2020-01-30 13:23:04 +00:00
|
|
|
responses: Record<string, ActionResponse | undefined>;
|
2020-06-03 05:40:48 +00:00
|
|
|
isRunning: Record<string, boolean>;
|
2019-10-29 12:02:58 +00:00
|
|
|
}
|
|
|
|
|
|
2020-06-25 10:04:57 +00:00
|
|
|
// const ResponseHeadersView = (props: { data: Record<string, string[]> }) => {
|
|
|
|
|
// if (!props.data) return <div />;
|
|
|
|
|
// return (
|
|
|
|
|
// <TableWrapper>
|
|
|
|
|
// <table className="bp3-html-table bp3-html-table-striped bp3-html-table-condensed">
|
|
|
|
|
// <thead>
|
|
|
|
|
// <tr>
|
|
|
|
|
// <th>Key</th>
|
|
|
|
|
// <th>Value</th>
|
|
|
|
|
// </tr>
|
|
|
|
|
// </thead>
|
|
|
|
|
// <tbody>
|
|
|
|
|
// {Object.keys(props.data).map(k => (
|
|
|
|
|
// <tr key={k}>
|
|
|
|
|
// <td>{k}</td>
|
|
|
|
|
// <td>{props.data[k].join(", ")}</td>
|
|
|
|
|
// </tr>
|
|
|
|
|
// ))}
|
|
|
|
|
// </tbody>
|
|
|
|
|
// </table>
|
|
|
|
|
// </TableWrapper>
|
|
|
|
|
// );
|
|
|
|
|
// };
|
2019-10-25 05:35:20 +00:00
|
|
|
|
2021-02-11 12:54:00 +00:00
|
|
|
type Props = ReduxStateProps &
|
|
|
|
|
RouteComponentProps<APIEditorRouteParams> & { theme?: EditorTheme };
|
2019-11-22 17:04:40 +00:00
|
|
|
|
2021-02-04 09:38:25 +00:00
|
|
|
export const EMPTY_RESPONSE: ActionResponse = {
|
2019-11-22 17:04:40 +00:00
|
|
|
statusCode: "",
|
|
|
|
|
duration: "",
|
|
|
|
|
body: {},
|
|
|
|
|
headers: {},
|
2020-06-19 16:27:12 +00:00
|
|
|
request: {
|
|
|
|
|
headers: {},
|
|
|
|
|
body: {},
|
|
|
|
|
httpMethod: "",
|
|
|
|
|
url: "",
|
|
|
|
|
},
|
2019-11-22 17:04:40 +00:00
|
|
|
size: "",
|
|
|
|
|
};
|
2019-10-25 05:35:20 +00:00
|
|
|
|
2021-02-11 12:54:00 +00:00
|
|
|
const TabbedViewWrapper = styled.div<{ isCentered: boolean }>`
|
|
|
|
|
height: calc(100% - 30px);
|
|
|
|
|
|
|
|
|
|
&&& {
|
|
|
|
|
ul.react-tabs__tab-list {
|
|
|
|
|
padding: 0px ${(props) => props.theme.spaces[12]}px;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
${(props) =>
|
|
|
|
|
props.isCentered
|
|
|
|
|
? `
|
|
|
|
|
&&& {
|
|
|
|
|
.react-tabs__tab-panel {
|
|
|
|
|
display: flex;
|
|
|
|
|
align-items: center;
|
|
|
|
|
justify-content: center;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
`
|
|
|
|
|
: null}
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
const SectionDivider = styled.div`
|
|
|
|
|
height: 2px;
|
2020-06-23 03:58:42 +00:00
|
|
|
width: 100%;
|
2021-02-11 12:54:00 +00:00
|
|
|
background: ${(props) => props.theme.colors.apiPane.dividerBg};
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
const Flex = styled.div`
|
|
|
|
|
display: flex;
|
|
|
|
|
align-items: center;
|
|
|
|
|
margin-left: 20px;
|
|
|
|
|
|
|
|
|
|
span:first-child {
|
|
|
|
|
margin-right: ${(props) => props.theme.spaces[1] + 1}px;
|
2020-06-23 03:58:42 +00:00
|
|
|
}
|
2020-05-30 02:39:18 +00:00
|
|
|
`;
|
|
|
|
|
|
2021-02-11 12:54:00 +00:00
|
|
|
const NoResponseContainer = styled.div`
|
|
|
|
|
.${Classes.ICON} {
|
|
|
|
|
margin-right: 0px;
|
|
|
|
|
svg {
|
|
|
|
|
width: 150px;
|
|
|
|
|
height: 150px;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.${Classes.TEXT} {
|
|
|
|
|
margin-top: ${(props) => props.theme.spaces[9]}px;
|
|
|
|
|
}
|
2020-06-12 03:54:12 +00:00
|
|
|
`;
|
|
|
|
|
|
2021-02-11 12:54:00 +00:00
|
|
|
const FailedMessage = styled.div`
|
|
|
|
|
display: flex;
|
|
|
|
|
align-items: center;
|
|
|
|
|
justify-content: space-between;
|
|
|
|
|
width: 100%;
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
const ButtonContainer = styled.div`
|
|
|
|
|
display: flex;
|
|
|
|
|
align-items: center;
|
|
|
|
|
span {
|
|
|
|
|
color: ${Colors.Galliano};
|
|
|
|
|
cursor: pointer;
|
|
|
|
|
}
|
|
|
|
|
button {
|
|
|
|
|
margin-left: ${(props) => props.theme.spaces[9]}px;
|
2020-06-23 03:58:42 +00:00
|
|
|
}
|
|
|
|
|
`;
|
|
|
|
|
|
2019-10-25 05:35:20 +00:00
|
|
|
const ApiResponseView = (props: Props) => {
|
2019-11-22 17:04:40 +00:00
|
|
|
const {
|
|
|
|
|
match: {
|
|
|
|
|
params: { apiId },
|
|
|
|
|
},
|
|
|
|
|
responses,
|
|
|
|
|
} = props;
|
|
|
|
|
let response: ActionResponse = EMPTY_RESPONSE;
|
2019-12-11 15:14:38 +00:00
|
|
|
let isRunning = false;
|
2020-05-30 02:39:18 +00:00
|
|
|
let hasFailed = false;
|
2019-11-22 17:04:40 +00:00
|
|
|
if (apiId && apiId in responses) {
|
2020-01-30 13:23:04 +00:00
|
|
|
response = responses[apiId] || EMPTY_RESPONSE;
|
2020-06-03 05:40:48 +00:00
|
|
|
isRunning = props.isRunning[apiId];
|
2020-05-30 02:39:18 +00:00
|
|
|
hasFailed = response.statusCode ? response.statusCode[0] !== "2" : false;
|
2019-11-22 17:04:40 +00:00
|
|
|
}
|
2020-05-30 02:39:18 +00:00
|
|
|
|
2020-06-23 03:58:42 +00:00
|
|
|
const [requestDebugVisible, setRequestDebugVisible] = useLocalStorage(
|
|
|
|
|
"requestDebugVisible",
|
|
|
|
|
"true",
|
|
|
|
|
);
|
|
|
|
|
|
2020-05-30 02:39:18 +00:00
|
|
|
const [selectedIndex, setSelectedIndex] = useState(0);
|
2020-05-22 03:43:01 +00:00
|
|
|
const tabs = [
|
|
|
|
|
{
|
|
|
|
|
key: "body",
|
|
|
|
|
title: "Response Body",
|
|
|
|
|
panelComponent: (
|
2020-05-30 02:39:18 +00:00
|
|
|
<>
|
2021-02-11 12:54:00 +00:00
|
|
|
{hasFailed && !isRunning && requestDebugVisible && (
|
|
|
|
|
<Callout
|
|
|
|
|
variant={Variant.warning}
|
|
|
|
|
fill
|
|
|
|
|
label={
|
|
|
|
|
<FailedMessage>
|
|
|
|
|
<Text type={TextType.P2}>{CHECK_REQUEST_BODY}</Text>
|
|
|
|
|
<ButtonContainer>
|
|
|
|
|
<Text
|
|
|
|
|
type={TextType.H6}
|
|
|
|
|
case={Case.UPPERCASE}
|
|
|
|
|
onClick={() => {
|
|
|
|
|
setRequestDebugVisible(false);
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
{DONT_SHOW_THIS_AGAIN}
|
|
|
|
|
</Text>
|
|
|
|
|
<Button
|
|
|
|
|
tag="button"
|
|
|
|
|
text={SHOW_REQUEST}
|
|
|
|
|
variant={Variant.info}
|
|
|
|
|
onClick={() => {
|
|
|
|
|
setSelectedIndex(1);
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
</ButtonContainer>
|
|
|
|
|
</FailedMessage>
|
|
|
|
|
}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
{_.isEmpty(response.body) ? (
|
|
|
|
|
<NoResponseContainer>
|
|
|
|
|
<Icon name="no-response" />
|
|
|
|
|
<Text type={TextType.P1}>Hit Run to get a Response</Text>
|
|
|
|
|
</NoResponseContainer>
|
|
|
|
|
) : (
|
|
|
|
|
<ReadOnlyEditor
|
|
|
|
|
input={{
|
|
|
|
|
value: response.body
|
|
|
|
|
? JSON.stringify(response.body, null, 2)
|
|
|
|
|
: "",
|
|
|
|
|
}}
|
|
|
|
|
height={"100%"}
|
|
|
|
|
/>
|
2020-06-23 03:58:42 +00:00
|
|
|
)}
|
2020-05-30 02:39:18 +00:00
|
|
|
</>
|
2020-05-22 03:43:01 +00:00
|
|
|
),
|
|
|
|
|
},
|
|
|
|
|
{
|
2020-06-23 03:58:42 +00:00
|
|
|
key: "request",
|
|
|
|
|
title: "Request",
|
2020-05-22 03:43:01 +00:00
|
|
|
panelComponent: (
|
2020-06-23 03:58:42 +00:00
|
|
|
<RequestView
|
|
|
|
|
requestURL={response.request?.url || ""}
|
|
|
|
|
requestHeaders={response.request?.headers || {}}
|
|
|
|
|
requestMethod={response.request?.httpMethod || ""}
|
|
|
|
|
requestBody={
|
|
|
|
|
_.isObject(response.request?.body)
|
2020-06-19 17:33:02 +00:00
|
|
|
? JSON.stringify(response.request?.body, null, 2)
|
2020-06-23 03:58:42 +00:00
|
|
|
: response.request?.body || ""
|
|
|
|
|
}
|
2020-05-22 03:43:01 +00:00
|
|
|
/>
|
|
|
|
|
),
|
|
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
|
2019-10-25 05:35:20 +00:00
|
|
|
return (
|
|
|
|
|
<ResponseWrapper>
|
2021-02-11 12:54:00 +00:00
|
|
|
<SectionDivider />
|
2019-12-11 15:14:38 +00:00
|
|
|
{isRunning && (
|
2021-02-11 12:54:00 +00:00
|
|
|
<LoadingOverlayScreen theme={props.theme}>
|
|
|
|
|
Sending Request
|
|
|
|
|
</LoadingOverlayScreen>
|
2019-12-11 15:14:38 +00:00
|
|
|
)}
|
2021-02-11 12:54:00 +00:00
|
|
|
<TabbedViewWrapper
|
|
|
|
|
isCentered={_.isEmpty(response.body) && selectedIndex === 0}
|
|
|
|
|
>
|
2020-12-08 09:24:36 +00:00
|
|
|
{response.statusCode && (
|
|
|
|
|
<ResponseMetaWrapper>
|
|
|
|
|
{response.statusCode && (
|
2021-02-11 12:54:00 +00:00
|
|
|
<Flex>
|
|
|
|
|
<Text type={TextType.P3}>Status: </Text>
|
|
|
|
|
<StatusCodeText
|
|
|
|
|
accent="secondary"
|
|
|
|
|
code={response.statusCode.toString()}
|
|
|
|
|
>
|
|
|
|
|
{response.statusCode}
|
|
|
|
|
</StatusCodeText>
|
|
|
|
|
</Flex>
|
2020-12-08 09:24:36 +00:00
|
|
|
)}
|
|
|
|
|
<ResponseMetaInfo>
|
|
|
|
|
{response.duration && (
|
2021-02-11 12:54:00 +00:00
|
|
|
<Flex>
|
|
|
|
|
<Text type={TextType.P3}>Time: </Text>
|
|
|
|
|
<Text type={TextType.H5}>{response.duration} ms</Text>
|
|
|
|
|
</Flex>
|
2020-12-08 09:24:36 +00:00
|
|
|
)}
|
|
|
|
|
{response.size && (
|
2021-02-11 12:54:00 +00:00
|
|
|
<Flex>
|
|
|
|
|
<Text type={TextType.P3}>Size: </Text>
|
|
|
|
|
<Text type={TextType.H5}>
|
|
|
|
|
{formatBytes(parseInt(response.size))}
|
|
|
|
|
</Text>
|
|
|
|
|
</Flex>
|
2020-12-08 09:24:36 +00:00
|
|
|
)}
|
|
|
|
|
</ResponseMetaInfo>
|
|
|
|
|
</ResponseMetaWrapper>
|
|
|
|
|
)}
|
2021-02-11 12:54:00 +00:00
|
|
|
<TabComponent
|
2020-06-12 03:54:12 +00:00
|
|
|
tabs={tabs}
|
|
|
|
|
selectedIndex={selectedIndex}
|
2021-02-11 12:54:00 +00:00
|
|
|
onSelect={setSelectedIndex}
|
2020-06-12 03:54:12 +00:00
|
|
|
/>
|
|
|
|
|
</TabbedViewWrapper>
|
2019-10-25 05:35:20 +00:00
|
|
|
</ResponseWrapper>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
2020-05-22 03:43:01 +00:00
|
|
|
const mapStateToProps = (state: AppState): ReduxStateProps => {
|
|
|
|
|
return {
|
|
|
|
|
responses: getActionResponses(state),
|
2020-06-03 05:40:48 +00:00
|
|
|
isRunning: state.ui.apiPane.isRunning,
|
2020-05-22 03:43:01 +00:00
|
|
|
};
|
|
|
|
|
};
|
2019-10-25 05:35:20 +00:00
|
|
|
|
|
|
|
|
export default connect(mapStateToProps)(withRouter(ApiResponseView));
|